Fix build indenting.

Former-commit-id: 87a7793799ee3ed28fe67cb894aa2876be103c50
This commit is contained in:
John-David Dalton
2013-02-22 02:00:42 -08:00
parent 860339b90e
commit 4136f1b377
2 changed files with 396 additions and 361 deletions

View File

@@ -275,7 +275,8 @@
function addChainMethods(source) { function addChainMethods(source) {
// add `_.chain` // add `_.chain`
source = source.replace(matchFunction(source, 'tap'), function(match) { source = source.replace(matchFunction(source, 'tap'), function(match) {
return [ var indent = getIndent(match);
return indent + [
'', '',
'/**', '/**',
' * Creates a `lodash` object that wraps the given `value`.', ' * Creates a `lodash` object that wraps the given `value`.',
@@ -306,12 +307,13 @@
'}', '}',
'', '',
match match
].join('\n'); ].join('\n' + indent);
}); });
// add `wrapperChain` // add `wrapperChain`
source = source.replace(matchFunction(source, 'wrapperToString'), function(match) { source = source.replace(matchFunction(source, 'wrapperToString'), function(match) {
return [ var indent = getIndent(match);
return indent + [
'', '',
'/**', '/**',
' * Enables method chaining on the wrapper object.', ' * Enables method chaining on the wrapper object.',
@@ -334,7 +336,7 @@
'}', '}',
'', '',
match match
].join('\n'); ].join('\n' + indent);
}); });
// add `lodash.chain` assignment // add `lodash.chain` assignment
@@ -354,7 +356,7 @@
// move `mixin(lodash)` to after the method assignments // move `mixin(lodash)` to after the method assignments
source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(lodash\).+/, ''); source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(lodash\).+/, '');
source = source.replace(getMethodAssignments(source), function(match) { source = source.replace(getMethodAssignments(source), function(match) {
var indent = /^ *(?=lodash)/m.exec(match)[0]; var indent = /^ *(?=lodash\.)/m.exec(match)[0];
return match + [ return match + [
'', '',
'', '',
@@ -365,8 +367,8 @@
// add `__chain__` checks to `_.mixin` // add `__chain__` checks to `_.mixin`
source = source.replace(matchFunction(source, 'mixin'), function(match) { source = source.replace(matchFunction(source, 'mixin'), function(match) {
return match.replace(/^( *)return new lodash.+/m, function() { return match.replace(/^ *return new lodash.+/m, function() {
var indent = arguments[1]; var indent = getIndent(match);
return indent + [ return indent + [
'', '',
'var result = func.apply(lodash, args);', 'var result = func.apply(lodash, args);',
@@ -380,8 +382,8 @@
}); });
// replace wrapper `Array` method assignments // replace wrapper `Array` method assignments
source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function() { source = source.replace(/^(?: *\/\/.*\n)*( *)each\(\['[\s\S]+?\n\1}$/m, function(match) {
var indent = arguments[1]; var indent = getIndent(match);
return indent + [ return indent + [
'// add `Array` mutator functions to the wrapper', '// add `Array` mutator functions to the wrapper',
"each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {", "each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {",
@@ -705,20 +707,35 @@
* *
* @private * @private
* @param {Function} func The function to process. * @param {Function} func The function to process.
* @param {String} indent The function indent.
* @returns {String} Returns the formatted source. * @returns {String} Returns the formatted source.
*/ */
function getFunctionSource(func) { function getFunctionSource(func, indent) {
var source = func.source || (func + ''); var source = func.source || (func + '');
if (indent == null) {
indent = ' ';
}
// format leading whitespace // format leading whitespace
return source.replace(/\n(?:.*)/g, function(match, index) { return source.replace(/\n(?:.*)/g, function(match, index) {
match = match.slice(1); match = match.slice(1);
return ( return (
match == '}' && source.indexOf('}', index + 2) < 0 ? '\n ' : '\n ' '\n' + indent +
(match == '}' && source.indexOf('}', index + 2) < 0 ? '' : ' ')
) + match; ) + 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`. * Gets the `_.isArguments` fallback from `source`.
* *
@@ -907,7 +924,9 @@
if ( snippet) { if ( snippet) {
// remove data object property assignment // remove data object property assignment
var modified = snippet.replace(RegExp("^ *'" + varName + "': *" + varName + '.+\\n', 'm'), ''); 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 // clip at the `factory` assignment
snippet = modified.match(/Function\([\s\S]+$/)[0]; snippet = modified.match(/Function\([\s\S]+$/)[0];
@@ -917,7 +936,9 @@
.replace(/, *',/, "',") .replace(/, *',/, "',")
.replace(/,\s*\)/, ')') .replace(/,\s*\)/, ')')
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
} }
return source; return source;
} }
@@ -960,7 +981,9 @@
}, snippet); }, snippet);
// replace with the modified snippet // replace with the modified snippet
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
return removeFromCreateIterator(source, funcName); return removeFromCreateIterator(source, funcName);
} }
@@ -1265,7 +1288,9 @@
// clip snippet after the JSDoc comment block // clip snippet after the JSDoc comment block
match = match.replace(/^\s*(?:\/\/.*|\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)\n/, ''); match = match.replace(/^\s*(?:\/\/.*|\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)\n/, '');
source = source.replace(match, function() { source = source.replace(match, function() {
return funcValue.trimRight() + '\n'; return funcValue
.replace(RegExp('^' + getIndent(funcValue), 'gm'), getIndent(match))
.trimRight() + '\n';
}); });
} }
return source; return source;
@@ -2198,11 +2223,11 @@
// inline all functions defined with `createIterator` // inline all functions defined with `createIterator`
_.functions(lodash).forEach(function(methodName) { _.functions(lodash).forEach(function(methodName) {
// strip leading underscores to match pseudo private functions // 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)) { if (reFunc.test(source)) {
// extract, format, and inject the compiled function's source code // extract, format, and inject the compiled function's source code
source = source.replace(reFunc, function(match, captured) { 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) { if (!exposeIsPlainObject) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.isPlainObject *= *.+\n/m, ''); 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` // remove `thisArg` from unexposed `forIn` and `forOwn`
@@ -2273,8 +2300,9 @@
} }
if (!(isMobile || isUnderscore)) { if (!(isMobile || isUnderscore)) {
// inline `iteratorTemplate` template // inline `iteratorTemplate` template
source = source.replace(getIteratorTemplate(source), function() { source = source.replace(getIteratorTemplate(source), function(match) {
var snippet = getFunctionSource(lodash._iteratorTemplate); var indent = getIndent(match),
snippet = getFunctionSource(lodash._iteratorTemplate, indent);
// prepend data object references to property names to avoid having to // prepend data object references to property names to avoid having to
// use a with-statement // use a with-statement
@@ -2303,7 +2331,7 @@
// remove comments, including sourceURLs // remove comments, including sourceURLs
snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, ''); snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, '');
return ' var iteratorTemplate = ' + snippet + ';\n'; return indent + 'var iteratorTemplate = ' + snippet + ';\n';
}); });
} }
} }

View File

@@ -296,7 +296,9 @@
}); });
// replace with modified snippet // replace with modified snippet
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
}); });
}()); }());
@@ -331,7 +333,10 @@
if (isCreateIterator) { if (isCreateIterator) {
// clip before the `factory` call to avoid minifying its arguments // 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]+$/, ''); snippet = modified = modified.replace(/return factory\([\s\S]+$/, '');
} }
// minify `createIterator` option property names // minify `createIterator` option property names
@@ -358,7 +363,9 @@
}); });
// replace with modified snippet // replace with modified snippet
source = source.replace(snippet, modified); source = source.replace(snippet, function() {
return modified;
});
}); });
return source; return source;