Make split regexes in matchVar and matchFunction into multiples, correct typo in getDependants, and remove a lot of edge case build code.

Former-commit-id: 65917a2a0acd7c3502e04e583834f3f10efd15f7
This commit is contained in:
John-David Dalton
2013-07-09 09:00:53 -07:00
parent fdae297fca
commit b2f4f96ef9

183
build.js Executable file → Normal file
View File

@@ -103,7 +103,7 @@
'defer': ['bind'], 'defer': ['bind'],
'delay': [], 'delay': [],
'difference': ['cacheIndexOf', 'createCache', 'getIndexOf', 'releaseObject'], 'difference': ['cacheIndexOf', 'createCache', 'getIndexOf', 'releaseObject'],
'escape': ['escapeHtmlChar'], 'escape': ['escapeHtmlChar', 'keys'],
'every': ['basicEach', 'createCallback', 'isArray'], 'every': ['basicEach', 'createCallback', 'isArray'],
'filter': ['basicEach', 'createCallback', 'isArray'], 'filter': ['basicEach', 'createCallback', 'isArray'],
'find': ['basicEach', 'createCallback', 'isArray'], 'find': ['basicEach', 'createCallback', 'isArray'],
@@ -177,11 +177,11 @@
'times': ['createCallback'], 'times': ['createCallback'],
'toArray': ['isString', 'slice', 'values'], 'toArray': ['isString', 'slice', 'values'],
'transform': ['createCallback', 'createObject', 'forOwn', 'isArray'], 'transform': ['createCallback', 'createObject', 'forOwn', 'isArray'],
'unescape': ['unescapeHtmlChar'], 'unescape': ['keys', 'unescapeHtmlChar'],
'union': ['basicFlatten', 'basicUniq'], 'union': ['basicFlatten', 'basicUniq'],
'uniq': ['basicUniq', 'overloadWrapper'], 'uniq': ['basicUniq', 'overloadWrapper'],
'uniqueId': [], 'uniqueId': [],
'value': ['basicEach', 'forOwn', 'isArray', 'lodash', 'wrapperValueOf', 'lodashWrapper'], 'value': ['basicEach', 'forOwn', 'isArray', 'lodash', 'mixin', 'wrapperValueOf', 'lodashWrapper'],
'values': ['keys'], 'values': ['keys'],
'where': ['filter'], 'where': ['filter'],
'without': ['difference'], 'without': ['difference'],
@@ -218,7 +218,7 @@
'shimIsPlainObject': ['forIn', 'isArguments', 'isFunction', 'isNode'], 'shimIsPlainObject': ['forIn', 'isArguments', 'isFunction', 'isNode'],
'shimKeys': ['createIterator'], 'shimKeys': ['createIterator'],
'slice': [], 'slice': [],
'unescapeHtmlChar': [], 'unescapeHtmlChar': ['invert'],
'wrapperToString': [], 'wrapperToString': [],
'wrapperValueOf': [], 'wrapperValueOf': [],
@@ -1190,7 +1190,7 @@
})) { })) {
stack.push(otherName); stack.push(otherName);
result.push(otherName); result.push(otherName);
if (isShallow) { if (!isShallow) {
result.push.apply(result, getDependants(otherName, isShallow, stack)); result.push.apply(result, getDependants(otherName, isShallow, stack));
} }
} }
@@ -1415,35 +1415,25 @@
* @returns {String} Returns the matched function snippet. * @returns {String} Returns the matched function snippet.
*/ */
function matchFunction(source, funcName) { function matchFunction(source, funcName) {
var result = source.match(RegExp( var result = _.reduce([
multilineComment +
// match variable declarations with `createIterator`, `overloadWrapper`, and `template` // match variable declarations with `createIterator`, `overloadWrapper`, and `template`
'( *)var ' + funcName + ' *=.*?(?:createIterator|overloadWrapper|template)\\((?:.+|[\\s\\S]+?\\n\\1}?)\\);\\n' '( *)var ' + funcName + ' *=.*?(?:createIterator|overloadWrapper|template)\\((?:.+|[\\s\\S]+?\\n\\1}?)\\);\\n',
));
result || (result = source.match(RegExp(
multilineComment +
// begin non-capturing group
'( *)(?:' +
// match a function declaration // match a function declaration
'function ' + funcName + '\\b[\\s\\S]+?\\n\\1}|' + '( *)function ' + funcName + '\\b[\\s\\S]+?\\n\\1}\\n',
// match a variable declaration with function expression // match a variable declaration with function expression
'var ' + funcName + ' *=.*?function\\(.+?\{\\n[\\s\\S]+?\\n\\1}(?:\\(\\)\\))?;' + '( *)var ' + funcName + ' *=.*?function\\(.+?\{\\n[\\s\\S]+?\\n\\1}(?:\\(\\)\\))?;\\n',
// end non-capturing group
')\\n'
)));
result || (result = source.match(RegExp(
multilineComment +
// match simple variable declarations // match simple variable declarations
'( *)var ' + funcName + ' *=.+?;\\n' '( *)var ' + funcName + ' *=.+?;\\n'
))); ], function(result, reSource) {
return result || ((result = source.match(RegExp(
multilineComment +
reSource
))) && result[0]) || '';
}, null);
if (/@type +Function\b/.test(result)) { if (/@type +Function\b/.test(result) ||
return result[0]; /(?:function(?:\s+\w+)?\b|createIterator|overloadWrapper|template)\(/.test(result)) {
} return result;
if (/(?:function(?:\s+\w+)?\b|createIterator|overloadWrapper|template)\(/.test(result)) {
return result[0];
} }
return ''; return '';
} }
@@ -1480,17 +1470,29 @@
*/ */
function matchVar(source, varName, isShallow) { function matchVar(source, varName, isShallow) {
var indentA = isShallow ? ' {2}' : ' {2,4}', var indentA = isShallow ? ' {2}' : ' {2,4}',
indentB = isShallow ? ' {6}' : ' {6,8}'; indentB = isShallow ? ' {6}' : ' {6,8}',
reSources = [];
var result = source.match(RegExp( if (varName != 'freeGlobal' && _.contains(complexVars, varName)) {
(varName != 'freeGlobal' && _.contains(complexVars, varName)) // match complex variable assignments
? '^' + indentA + 'var ' + varName + ' *=[\\s\\S]+?(?:\\(function[\\s\\S]+?\\([^)]*\\)\\);\\n(?=\\n)|[;}]\\n(?=\\n(?!\\s*\\(func)))' reSources.push(
: '^(' + indentA + ')var ' + varName + ' *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n|' + indentA + 'var ' + varName + ' *=[\\s\\S]+?(?:\\(function[\\s\\S]+?\\([^)]*\\)\\);\\n(?=\\n)|[;}]\\n(?=\\n(?!\\s*\\(func)))'
'^' + indentA + 'var ' + varName + ' *=.+?,\\n(?= *\\w+ *=)|' + );
'^' + indentB + varName + ' *=.+?[,;]\\n' } else {
, 'm')); reSources.push(
// match a varaible at the start of a declaration list
return result ? result[0] : ''; indentA + 'var ' + varName + ' *=.+?,\\n(?= *\\w+ *=)',
// match a variable declaration in a declaration list
indentB + varName + ' *=.+?[,;]\\n',
// match a variable that is not part of a declaration list
'(' + indentA + ')var ' + varName + ' *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n'
);
}
return _.reduce(reSources, function(result, reSource) {
return result || ((result = source.match(RegExp(
'^' + reSource
, 'm'))) && result[0]) || '';
}, null);
} }
/** /**
@@ -2157,21 +2159,34 @@
, 'm'), '$1 = null;') , 'm'), '$1 = null;')
} }
source = removeFunction(source, varName); _.some([
function() {
// match a variable declaration that's not part of a declaration list return removeFunction(source, varName);
source = source.replace(RegExp( },
multilineComment + function() {
'( *)var ' + varName + ' *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n' // remove a varaible at the start of a declaration list
), ''); return source.replace(RegExp('(var +)' + varName + ' *=.+?,\\n *'), '$1');
},
// match a variable declaration in a declaration list function() {
source = source.replace(RegExp( // remove a variable declaration in a declaration list
'( *(?:var +)?\\w+ *=.+?),\\n *' + varName + ' *=.+?([,;])(?=\\n)' return source.replace(RegExp(
), '$1$2'); '( *(?:var +)?\\w+ *=.+?),\\n *' + varName + ' *=.+?([,;])(?=\\n)'
), '$1$2');
// remove a varaible at the start of a declaration list },
source = source.replace(RegExp('(var +)' + varName + ' *=.+?,\\n *'), '$1'); function() {
// remove a variable that is not part of a declaration list
return source.replace(RegExp(
multilineComment +
'( *)var ' + varName + ' *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n'
), '');
}
], function(func) {
var result = func();
if (result !== source) {
source = result;
return true;
}
});
return source; return source;
} }
@@ -3617,22 +3632,13 @@
if (!isLodash('support')) { if (!isLodash('support')) {
source = source.replace(/\blodash\.support *= */, ''); source = source.replace(/\blodash\.support *= */, '');
} }
// replace `htmlEscapes` entries with hex entities
// add an `/` entry to `htmlEscapes`, `reEscapedHtml`, and `reUnescapedHtml`
if (!isLodash('escape')) { if (!isLodash('escape')) {
source = source.replace(matchVar(source, 'htmlEscapes'), function(match) { source = source.replace(matchVar(source, 'htmlEscapes'), function(match) {
return match return match
.replace('#39', '#x27') .replace('#39', '#x27')
.replace(/(\n *)}/, ",$1 '/': '/'$1}"); .replace(/(\n *)}/, ",$1 '/': '/'$1}");
}); });
source = source.replace(matchVar(source, 'reEscapedHtml'), function(match) {
return match.replace(/\/.*\//, "/&(?:amp|lt|gt|quot|#x27|#x2F);/");
});
source = source.replace(matchVar(source, 'reUnescapedHtml'), function(match) {
return match.replace(/\/.*\//, '/[&<>"\'\\/]/');
});
} }
// replace `basicFlatten` and `basicUniq` with `flatten` and `uniq` in `_.union` // replace `basicFlatten` and `basicUniq` with `flatten` and `uniq` in `_.union`
@@ -3923,41 +3929,8 @@
// modify/remove references to removed functions/variables // modify/remove references to removed functions/variables
if (!isTemplate) { if (!isTemplate) {
if (isExcluded('invert')) { if (isExcluded('lodashWrapper')) {
source = replaceVar(source, 'htmlUnescapes', JSON.stringify(_.invert(JSON.parse(
matchVar(source, 'htmlEscapes')
.replace(/([^"])'(?!")/g, '$1"')
.replace(/'"'/, '"\\""')
.match(/\{[\s\S]+?}/)[0]
))));
}
if (isExcluded('mixin')) {
// if possible, inline the `_.mixin` call to ensure proper chaining behavior
source = source.replace(/((?:\s*\/\/.*)\n)( *)mixin\(lodash\).*/m, function(match, prelude, indent) {
if (isExcluded('forOwn')) {
return '';
}
return prelude + indent + [
'forOwn(lodash, function(func, methodName) {',
' lodash[methodName] = func;',
'',
' lodash.prototype[methodName] = function() {',
' var value = this.__wrapped__,',
' args = [value];',
'',
' push.apply(args, arguments);',
' var result = func.apply(lodash, args);',
" return (value && typeof value == 'object' && value == result)",
' ? this',
' : new lodashWrapper(result);',
' };',
'});'
].join('\n' + indent);
});
}
if (isExcluded('value')) {
source = removeLodashWrapper(source); source = removeLodashWrapper(source);
source = removeSpliceObjectsFix(source);
// simplify the `lodash` function // simplify the `lodash` function
source = replaceFunction(source, 'lodash', [ source = replaceFunction(source, 'lodash', [
@@ -3965,15 +3938,13 @@
' // no operation performed', ' // no operation performed',
'}' '}'
].join('\n')); ].join('\n'));
}
// remove `lodash.prototype` method assignments from `_.mixin` if (isExcluded('mixin')) {
source = replaceFunction(source, 'mixin', [ // remove `mixin` call
'function mixin(object) {', source = source.replace(/(?:\s*\/\/.*)*\n( *)mixin\(.+?\).+/, '');
' forEach(functions(object), function(methodName) {', }
' lodash[methodName] = object[methodName];', if (isExcluded('value')) {
' });', source = removeSpliceObjectsFix(source);
'}'
].join('\n'));
// remove all `lodash.prototype` additions // remove all `lodash.prototype` additions
source = source source = source