mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Cleanup getVars, matchFunction, and matchVar.
Former-commit-id: b06d258373912d75bdfbbb31707fa17c160f75fe
This commit is contained in:
71
build.js
71
build.js
@@ -1372,17 +1372,27 @@
|
|||||||
*/
|
*/
|
||||||
function getVars(source, isShallow) {
|
function getVars(source, isShallow) {
|
||||||
var indentA = isShallow ? ' {2}' : ' {2,4}',
|
var indentA = isShallow ? ' {2}' : ' {2,4}',
|
||||||
indentB = isShallow ? ' {6}' : ' {6,8}',
|
indentB = isShallow ? ' {6}' : ' {6,8}';
|
||||||
result = [];
|
|
||||||
|
|
||||||
source.replace(RegExp(
|
var result = _.reduce([
|
||||||
'^(' + indentA + ')var (\\w+) *(?:|= *(?:.+?(?:|&&\\n[^;]+)|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n|' +
|
// match a varaible at the start of a declaration list
|
||||||
'^' + indentA + 'var (\\w+) *=.+?,\\n(?= *\\w+ *=)|' +
|
indentA + 'var (\\w+) *=.+?,\\n(?= *\\w+ *=)',
|
||||||
'^' + indentB + '(\\w+) *=.+?[,;]\\n'
|
// match a variable declaration in a declaration list
|
||||||
,'gm'), function(match, indent, varA, varB, varC) {
|
indentB + '(\\w+) *=.+?[,;]\\n',
|
||||||
result.push(varA || varB || varC);
|
// match a variable that is not part of a declaration list
|
||||||
});
|
'(' + indentA + ')var (\\w+) *(?:|= *(?:.+?(?:&&\\n[^;]+)?|(?:\\w+\\(|[{[(]\\n)[\\s\\S]+?\\n\\1[^\\n ]+?));\\n'
|
||||||
|
], function(result, reSource) {
|
||||||
|
source = source.replace(RegExp('^' + reSource, 'gm'), function(match, indent, varName) {
|
||||||
|
if (typeof varName == 'number') {
|
||||||
|
varName = indent;
|
||||||
|
}
|
||||||
|
result.push(varName);
|
||||||
|
return '';
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// remove duplicates and function names
|
||||||
return _.difference(_.uniq(result), allFuncs).sort();
|
return _.difference(_.uniq(result), allFuncs).sort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1422,13 +1432,13 @@
|
|||||||
'( *)function ' + funcName + '\\b[\\s\\S]+?\\n\\1}\\n',
|
'( *)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}(?:\\(\\)\\))?;\\n',
|
'( *)var ' + funcName + ' *=.*?function\\(.+?\{\\n[\\s\\S]+?\\n\\1}(?:\\(\\)\\))?;\\n',
|
||||||
// match simple variable declarations
|
// match a simple variable declaration
|
||||||
'( *)var ' + funcName + ' *=.+?;\\n'
|
'( *)var ' + funcName + ' *=.+?;\\n'
|
||||||
], function(result, reSource) {
|
], function(result, reSource) {
|
||||||
return result || ((result = source.match(RegExp(
|
return result || (result = source.match(RegExp(
|
||||||
multilineComment +
|
multilineComment +
|
||||||
reSource
|
reSource
|
||||||
))) && result[0]) || '';
|
))) && result[0];
|
||||||
}, null);
|
}, null);
|
||||||
|
|
||||||
if (/@type +Function\b/.test(result) ||
|
if (/@type +Function\b/.test(result) ||
|
||||||
@@ -1470,29 +1480,30 @@
|
|||||||
*/
|
*/
|
||||||
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 reSources = [
|
||||||
|
// match a varaible at the start of a declaration list
|
||||||
|
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'
|
||||||
|
];
|
||||||
|
|
||||||
|
// match complex variable assignments
|
||||||
if (varName != 'freeGlobal' && _.contains(complexVars, varName)) {
|
if (varName != 'freeGlobal' && _.contains(complexVars, varName)) {
|
||||||
// match complex variable assignments
|
reSources = [
|
||||||
reSources.push(
|
indentA + 'var ' + varName + ' *=[\\s\\S]+?' +
|
||||||
indentA + 'var ' + varName + ' *=[\\s\\S]+?(?:\\(function[\\s\\S]+?\\([^)]*\\)\\);\\n(?=\\n)|[;}]\\n(?=\\n(?!\\s*\\(func)))'
|
'(?:\\(function[\\s\\S]+?\\([^)]*\\)\\);\\n(?=\\n)|' +
|
||||||
);
|
'[;}]\\n(?=\\n(?!\\s*\\(func)))'
|
||||||
} else {
|
];
|
||||||
reSources.push(
|
|
||||||
// match a varaible at the start of a declaration list
|
|
||||||
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 _.reduce(reSources, function(result, reSource) {
|
||||||
return result || ((result = source.match(RegExp(
|
return result || (result = source.match(RegExp(
|
||||||
'^' + reSource
|
'^' + reSource
|
||||||
, 'm'))) && result[0]) || '';
|
, 'm'))) && result[0];
|
||||||
}, null);
|
}, null) || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user