Blame view
node_modules/eslint/lib/rules/no-redeclare.js
3.06 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/** * @fileoverview Rule to flag when the same variable is declared more then once. * @author Ilya Volodin */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { meta: { docs: { description: "disallow variable redeclaration", category: "Best Practices", recommended: true }, schema: [ { type: "object", properties: { builtinGlobals: { type: "boolean" } }, additionalProperties: false } ] }, create(context) { const options = { builtinGlobals: Boolean(context.options[0] && context.options[0].builtinGlobals) }; /** * Find variables in a given scope and flag redeclared ones. * @param {Scope} scope - An escope scope object. * @returns {void} * @private */ function findVariablesInScope(scope) { scope.variables.forEach(variable => { const hasBuiltin = options.builtinGlobals && "writeable" in variable; const count = (hasBuiltin ? 1 : 0) + variable.identifiers.length; if (count >= 2) { variable.identifiers.sort((a, b) => a.range[1] - b.range[1]); for (let i = (hasBuiltin ? 0 : 1), l = variable.identifiers.length; i < l; i++) { context.report({ node: variable.identifiers[i], message: "'{{a}}' is already defined.", data: { a: variable.name } }); } } }); } /** * Find variables in the current scope. * @param {ASTNode} node - The Program node. * @returns {void} * @private */ function checkForGlobal(node) { const scope = context.getScope(), parserOptions = context.parserOptions, ecmaFeatures = parserOptions.ecmaFeatures || {}; // Nodejs env or modules has a special scope. if (ecmaFeatures.globalReturn || node.sourceType === "module") { findVariablesInScope(scope.childScopes[0]); } else { findVariablesInScope(scope); } } /** * Find variables in the current scope. * @returns {void} * @private */ function checkForBlock() { findVariablesInScope(context.getScope()); } if (context.parserOptions.ecmaVersion >= 6) { return { Program: checkForGlobal, BlockStatement: checkForBlock, SwitchStatement: checkForBlock }; } return { Program: checkForGlobal, FunctionDeclaration: checkForBlock, FunctionExpression: checkForBlock, ArrowFunctionExpression: checkForBlock }; } }; |