mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-15 05:07:49 +00:00
Fix build indenting.
Former-commit-id: 87a7793799ee3ed28fe67cb894aa2876be103c50
This commit is contained in:
152
build.js
152
build.js
@@ -275,9 +275,10 @@
|
||||
function addChainMethods(source) {
|
||||
// add `_.chain`
|
||||
source = source.replace(matchFunction(source, 'tap'), function(match) {
|
||||
return [
|
||||
var indent = getIndent(match);
|
||||
return indent + [
|
||||
'',
|
||||
' /**',
|
||||
'/**',
|
||||
' * Creates a `lodash` object that wraps the given `value`.',
|
||||
' *',
|
||||
' * @static',
|
||||
@@ -299,21 +300,22 @@
|
||||
' * .first();',
|
||||
" * // => 'moe is 40'",
|
||||
' */',
|
||||
' function chain(value) {',
|
||||
'function chain(value) {',
|
||||
' value = new lodash(value);',
|
||||
' value.__chain__ = true;',
|
||||
' return value;',
|
||||
' }',
|
||||
'}',
|
||||
'',
|
||||
match
|
||||
].join('\n');
|
||||
].join('\n' + indent);
|
||||
});
|
||||
|
||||
// add `wrapperChain`
|
||||
source = source.replace(matchFunction(source, 'wrapperToString'), function(match) {
|
||||
return [
|
||||
var indent = getIndent(match);
|
||||
return indent + [
|
||||
'',
|
||||
' /**',
|
||||
'/**',
|
||||
' * Enables method chaining on the wrapper object.',
|
||||
' *',
|
||||
' * @name chain',
|
||||
@@ -328,13 +330,13 @@
|
||||
' * .value()',
|
||||
' * // => 6`',
|
||||
' */',
|
||||
' function wrapperChain() {',
|
||||
'function wrapperChain() {',
|
||||
' this.__chain__ = true;',
|
||||
' return this;',
|
||||
' }',
|
||||
'}',
|
||||
'',
|
||||
match
|
||||
].join('\n');
|
||||
].join('\n' + indent);
|
||||
});
|
||||
|
||||
// add `lodash.chain` assignment
|
||||
@@ -354,7 +356,7 @@
|
||||
// move `mixin(lodash)` to after the method assignments
|
||||
source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(lodash\).+/, '');
|
||||
source = source.replace(getMethodAssignments(source), function(match) {
|
||||
var indent = /^ *(?=lodash)/m.exec(match)[0];
|
||||
var indent = /^ *(?=lodash\.)/m.exec(match)[0];
|
||||
return match + [
|
||||
'',
|
||||
'',
|
||||
@@ -365,8 +367,8 @@
|
||||
|
||||
// add `__chain__` checks to `_.mixin`
|
||||
source = source.replace(matchFunction(source, 'mixin'), function(match) {
|
||||
return match.replace(/^( *)return new lodash.+/m, function() {
|
||||
var indent = arguments[1];
|
||||
return match.replace(/^ *return new lodash.+/m, function() {
|
||||
var indent = getIndent(match);
|
||||
return indent + [
|
||||
'',
|
||||
'var result = func.apply(lodash, args);',
|
||||
@@ -380,8 +382,8 @@
|
||||
});
|
||||
|
||||
// replace wrapper `Array` method assignments
|
||||
source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function() {
|
||||
var indent = arguments[1];
|
||||
source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function(match) {
|
||||
var indent = getIndent(match);
|
||||
return indent + [
|
||||
'// add `Array` mutator functions to the wrapper',
|
||||
"each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {",
|
||||
@@ -705,20 +707,35 @@
|
||||
*
|
||||
* @private
|
||||
* @param {Function} func The function to process.
|
||||
* @param {String} indent The function indent.
|
||||
* @returns {String} Returns the formatted source.
|
||||
*/
|
||||
function getFunctionSource(func) {
|
||||
function getFunctionSource(func, indent) {
|
||||
var source = func.source || (func + '');
|
||||
|
||||
if (indent == null) {
|
||||
indent = ' ';
|
||||
}
|
||||
// format leading whitespace
|
||||
return source.replace(/\n(?:.*)/g, function(match, index) {
|
||||
match = match.slice(1);
|
||||
return (
|
||||
match == '}' && source.indexOf('}', index + 2) < 0 ? '\n ' : '\n '
|
||||
'\n' + indent +
|
||||
(match == '}' && source.indexOf('}', index + 2) < 0 ? '' : ' ')
|
||||
) + match;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the indent of the given function.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} func The function to process.
|
||||
* @returns {String} Returns the indent.
|
||||
*/
|
||||
function getIndent(func) {
|
||||
return /^ *(?=\S)/m.exec(func.source || func)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `_.isArguments` fallback from `source`.
|
||||
*
|
||||
@@ -907,7 +924,9 @@
|
||||
if ( snippet) {
|
||||
// remove data object property assignment
|
||||
var modified = snippet.replace(RegExp("^ *'" + varName + "': *" + varName + '.+\\n', 'm'), '');
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
|
||||
// clip at the `factory` assignment
|
||||
snippet = modified.match(/Function\([\s\S]+$/)[0];
|
||||
@@ -917,7 +936,9 @@
|
||||
.replace(/, *',/, "',")
|
||||
.replace(/,\s*\)/, ')')
|
||||
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
}
|
||||
return source;
|
||||
}
|
||||
@@ -960,7 +981,9 @@
|
||||
}, snippet);
|
||||
|
||||
// replace with the modified snippet
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
|
||||
return removeFromCreateIterator(source, funcName);
|
||||
}
|
||||
@@ -1265,7 +1288,9 @@
|
||||
// clip snippet after the JSDoc comment block
|
||||
match = match.replace(/^\s*(?:\/\/.*|\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)\n/, '');
|
||||
source = source.replace(match, function() {
|
||||
return funcValue.trimRight() + '\n';
|
||||
return funcValue
|
||||
.replace(RegExp('^' + getIndent(funcValue), 'gm'), getIndent(match))
|
||||
.trimRight() + '\n';
|
||||
});
|
||||
}
|
||||
return source;
|
||||
@@ -1670,7 +1695,7 @@
|
||||
|
||||
// replace `_.assign`
|
||||
source = replaceFunction(source, 'assign', [
|
||||
' function assign(object) {',
|
||||
'function assign(object) {',
|
||||
' if (!object) {',
|
||||
' return object;',
|
||||
' }',
|
||||
@@ -1683,23 +1708,23 @@
|
||||
' }',
|
||||
' }',
|
||||
' return object;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.clone`
|
||||
if (useUnderscoreClone) {
|
||||
source = replaceFunction(source, 'clone', [
|
||||
' function clone(value) {',
|
||||
'function clone(value) {',
|
||||
' return isObject(value)',
|
||||
' ? (isArray(value) ? slice(value) : assign({}, value))',
|
||||
' : value',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
}
|
||||
|
||||
// replace `_.contains`
|
||||
source = replaceFunction(source, 'contains', [
|
||||
' function contains(collection, target) {',
|
||||
'function contains(collection, target) {',
|
||||
' var length = collection ? collection.length : 0,',
|
||||
' result = false;',
|
||||
" if (typeof length == 'number') {",
|
||||
@@ -1710,12 +1735,12 @@
|
||||
' });',
|
||||
' }',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.defaults`
|
||||
source = replaceFunction(source, 'defaults', [
|
||||
' function defaults(object) {',
|
||||
'function defaults(object) {',
|
||||
' if (!object) {',
|
||||
' return object;',
|
||||
' }',
|
||||
@@ -1730,12 +1755,12 @@
|
||||
' }',
|
||||
' }',
|
||||
' return object;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.difference`
|
||||
source = replaceFunction(source, 'difference', [
|
||||
' function difference(array) {',
|
||||
'function difference(array) {',
|
||||
' var index = -1,',
|
||||
' length = array.length,',
|
||||
' flattened = concat.apply(arrayRef, arguments),',
|
||||
@@ -1748,12 +1773,12 @@
|
||||
' }',
|
||||
' }',
|
||||
' return result',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.intersection`
|
||||
source = replaceFunction(source, 'intersection', [
|
||||
' function intersection(array) {',
|
||||
'function intersection(array) {',
|
||||
' var args = arguments,',
|
||||
' argsLength = args.length,',
|
||||
' index = -1,',
|
||||
@@ -1774,12 +1799,12 @@
|
||||
' }',
|
||||
' }',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.isEmpty`
|
||||
source = replaceFunction(source, 'isEmpty', [
|
||||
' function isEmpty(value) {',
|
||||
'function isEmpty(value) {',
|
||||
' if (!value) {',
|
||||
' return true;',
|
||||
' }',
|
||||
@@ -1792,12 +1817,12 @@
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.isEqual`
|
||||
source = replaceFunction(source, 'isEqual', [
|
||||
' function isEqual(a, b, stackA, stackB) {',
|
||||
'function isEqual(a, b, stackA, stackB) {',
|
||||
' if (a === b) {',
|
||||
' return a !== 0 || (1 / a == 1 / b);',
|
||||
' }',
|
||||
@@ -1893,12 +1918,12 @@
|
||||
' });',
|
||||
' }',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.omit`
|
||||
source = replaceFunction(source, 'omit', [
|
||||
' function omit(object) {',
|
||||
'function omit(object) {',
|
||||
' var props = concat.apply(arrayRef, arguments),',
|
||||
' result = {};',
|
||||
'',
|
||||
@@ -1908,12 +1933,12 @@
|
||||
' }',
|
||||
' });',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.pick`
|
||||
source = replaceFunction(source, 'pick', [
|
||||
' function pick(object) {',
|
||||
'function pick(object) {',
|
||||
' var index = 0,',
|
||||
' props = concat.apply(arrayRef, arguments),',
|
||||
' length = props.length,',
|
||||
@@ -1926,20 +1951,20 @@
|
||||
' }',
|
||||
' }',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.result`
|
||||
source = replaceFunction(source, 'result', [
|
||||
' function result(object, property) {',
|
||||
'function result(object, property) {',
|
||||
' var value = object ? object[property] : null;',
|
||||
' return isFunction(value) ? object[property]() : value;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.template`
|
||||
source = replaceFunction(source, 'template', [
|
||||
' function template(text, data, options) {',
|
||||
'function template(text, data, options) {',
|
||||
" text || (text = '');",
|
||||
' options = defaults({}, options, lodash.templateSettings);',
|
||||
'',
|
||||
@@ -1990,12 +2015,12 @@
|
||||
' }',
|
||||
' result.source = source;',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.uniq`
|
||||
source = replaceFunction(source, 'uniq', [
|
||||
' function uniq(array, isSorted, callback, thisArg) {',
|
||||
'function uniq(array, isSorted, callback, thisArg) {',
|
||||
' var index = -1,',
|
||||
' length = array ? array.length : 0,',
|
||||
' result = [],',
|
||||
@@ -2025,29 +2050,29 @@
|
||||
' }',
|
||||
' }',
|
||||
' return result;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.uniqueId`
|
||||
source = replaceFunction(source, 'uniqueId', [
|
||||
' function uniqueId(prefix) {',
|
||||
'function uniqueId(prefix) {',
|
||||
" var id = ++idCounter + '';",
|
||||
' return prefix ? prefix + id : id;',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.where`
|
||||
source = replaceFunction(source, 'where', [
|
||||
' function where(collection, properties, first) {',
|
||||
'function where(collection, properties, first) {',
|
||||
' return (first && isEmpty(properties))',
|
||||
' ? null',
|
||||
' : (first ? find : filter)(collection, properties);',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// replace `_.without`
|
||||
source = replaceFunction(source, 'without', [
|
||||
' function without(array) {',
|
||||
'function without(array) {',
|
||||
' var index = -1,',
|
||||
' length = array.length,',
|
||||
' result = [];',
|
||||
@@ -2059,16 +2084,16 @@
|
||||
' }',
|
||||
' }',
|
||||
' return result',
|
||||
' }'
|
||||
'}'
|
||||
].join('\n'));
|
||||
|
||||
// add `_.findWhere`
|
||||
source = source.replace(matchFunction(source, 'find'), function (match) {
|
||||
return match + [
|
||||
'',
|
||||
' function findWhere(object, properties) {',
|
||||
'function findWhere(object, properties) {',
|
||||
' return where(object, properties, true);',
|
||||
' }',
|
||||
'}',
|
||||
''
|
||||
].join('\n')
|
||||
});
|
||||
@@ -2198,11 +2223,11 @@
|
||||
// inline all functions defined with `createIterator`
|
||||
_.functions(lodash).forEach(function(methodName) {
|
||||
// strip leading underscores to match pseudo private functions
|
||||
var reFunc = RegExp('(\\bvar ' + methodName.replace(/^_/, '') + ' *= *)createIterator\\(((?:{|[a-zA-Z])[\\s\\S]+?)\\);\\n');
|
||||
var reFunc = RegExp('(^ *var ' + methodName.replace(/^_/, '') + ' *= *)createIterator\\(((?:{|[a-zA-Z])[\\s\\S]+?)\\);\\n');
|
||||
if (reFunc.test(source)) {
|
||||
// extract, format, and inject the compiled function's source code
|
||||
source = source.replace(reFunc, function(match, captured) {
|
||||
return captured + getFunctionSource(lodash[methodName]) + ';\n';
|
||||
return captured + getFunctionSource(lodash[methodName], getIndent(captured)) + ';\n';
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -2225,7 +2250,9 @@
|
||||
if (!exposeIsPlainObject) {
|
||||
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.isPlainObject *= *.+\n/m, '');
|
||||
}
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
}());
|
||||
|
||||
// remove `thisArg` from unexposed `forIn` and `forOwn`
|
||||
@@ -2273,8 +2300,9 @@
|
||||
}
|
||||
if (!(isMobile || isUnderscore)) {
|
||||
// inline `iteratorTemplate` template
|
||||
source = source.replace(getIteratorTemplate(source), function() {
|
||||
var snippet = getFunctionSource(lodash._iteratorTemplate);
|
||||
source = source.replace(getIteratorTemplate(source), function(match) {
|
||||
var indent = getIndent(match),
|
||||
snippet = getFunctionSource(lodash._iteratorTemplate, indent);
|
||||
|
||||
// prepend data object references to property names to avoid having to
|
||||
// use a with-statement
|
||||
@@ -2303,7 +2331,7 @@
|
||||
// remove comments, including sourceURLs
|
||||
snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, '');
|
||||
|
||||
return ' var iteratorTemplate = ' + snippet + ';\n';
|
||||
return indent + 'var iteratorTemplate = ' + snippet + ';\n';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +296,9 @@
|
||||
});
|
||||
|
||||
// replace with modified snippet
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
});
|
||||
}());
|
||||
|
||||
@@ -331,7 +333,10 @@
|
||||
|
||||
if (isCreateIterator) {
|
||||
// clip before the `factory` call to avoid minifying its arguments
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
|
||||
snippet = modified = modified.replace(/return factory\([\s\S]+$/, '');
|
||||
}
|
||||
// minify `createIterator` option property names
|
||||
@@ -358,7 +363,9 @@
|
||||
});
|
||||
|
||||
// replace with modified snippet
|
||||
source = source.replace(snippet, modified);
|
||||
source = source.replace(snippet, function() {
|
||||
return modified;
|
||||
});
|
||||
});
|
||||
|
||||
return source;
|
||||
|
||||
Reference in New Issue
Block a user