diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
commit | bbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch) | |
tree | c58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/tslint/lib/rules/noShadowedVariableRule.js | |
parent | 003fb34971cf63466184351b4db5f7c67df4f444 (diff) |
update packages
Diffstat (limited to 'node_modules/tslint/lib/rules/noShadowedVariableRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/noShadowedVariableRule.js | 84 |
1 files changed, 60 insertions, 24 deletions
diff --git a/node_modules/tslint/lib/rules/noShadowedVariableRule.js b/node_modules/tslint/lib/rules/noShadowedVariableRule.js index c2db583c6..7ada497cc 100644 --- a/node_modules/tslint/lib/rules/noShadowedVariableRule.js +++ b/node_modules/tslint/lib/rules/noShadowedVariableRule.js @@ -36,8 +36,8 @@ var Rule = /** @class */ (function (_super) { Rule.metadata = { ruleName: "no-shadowed-variable", description: "Disallows shadowing variable declarations.", - rationale: "Shadowing a variable masks access to it and obscures to what value an identifier actually refers.", - optionsDescription: (_a = ["\n You can optionally pass an object to disable checking for certain kinds of declarations.\n Possible keys are `\"class\"`, `\"enum\"`, `\"function\"`, `\"import\"`, `\"interface\"`, `\"namespace\"`, `\"typeAlias\"`\n and `\"typeParameter\"`. Just set the value to `false` for the check you want to disable.\n All checks default to `true`, i.e. are enabled by default.\n Note that you cannot disable variables and parameters.\n "], _a.raw = ["\n You can optionally pass an object to disable checking for certain kinds of declarations.\n Possible keys are \\`\"class\"\\`, \\`\"enum\"\\`, \\`\"function\"\\`, \\`\"import\"\\`, \\`\"interface\"\\`, \\`\"namespace\"\\`, \\`\"typeAlias\"\\`\n and \\`\"typeParameter\"\\`. Just set the value to \\`false\\` for the check you want to disable.\n All checks default to \\`true\\`, i.e. are enabled by default.\n Note that you cannot disable variables and parameters.\n "], Lint.Utils.dedent(_a)), + rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Shadowing a variable masks access to it and obscures to what value an identifier actually refers.\n For example, in the following code, it can be confusing why the filter is likely never true:\n\n ```\n const findNeighborsWithin = (instance: MyClass, instances: MyClass[]): MyClass[] => {\n return instances.filter((instance) => instance.neighbors.includes(instance));\n };\n ```\n "], ["\n Shadowing a variable masks access to it and obscures to what value an identifier actually refers.\n For example, in the following code, it can be confusing why the filter is likely never true:\n\n \\`\\`\\`\n const findNeighborsWithin = (instance: MyClass, instances: MyClass[]): MyClass[] => {\n return instances.filter((instance) => instance.neighbors.includes(instance));\n };\n \\`\\`\\`\n "]))), + optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n You can optionally pass an object to disable checking for certain kinds of declarations.\n Possible keys are `\"class\"`, `\"enum\"`, `\"function\"`, `\"import\"`, `\"interface\"`, `\"namespace\"`, `\"typeAlias\"`\n and `\"typeParameter\"`. Just set the value to `false` for the check you want to disable.\n All checks default to `true`, i.e. are enabled by default.\n Note that you cannot disable variables and parameters.\n\n The option `\"temporalDeadZone\"` defaults to `true` which shows errors when shadowing block scoped declarations in their\n temporal dead zone. When set to `false` parameters, classes, enums and variables declared\n with `let` or `const` are not considered shadowed if the shadowing occurs within their\n [temporal dead zone](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified).\n\n The following example shows how the `\"temporalDeadZone\"` option changes the linting result:\n\n ```ts\n function fn(value) {\n if (value) {\n const tmp = value; // no error on this line if \"temporalDeadZone\" is false\n return tmp;\n }\n let tmp = undefined;\n if (!value) {\n const tmp = value; // this line always contains an error\n return tmp;\n }\n }\n ```\n "], ["\n You can optionally pass an object to disable checking for certain kinds of declarations.\n Possible keys are \\`\"class\"\\`, \\`\"enum\"\\`, \\`\"function\"\\`, \\`\"import\"\\`, \\`\"interface\"\\`, \\`\"namespace\"\\`, \\`\"typeAlias\"\\`\n and \\`\"typeParameter\"\\`. Just set the value to \\`false\\` for the check you want to disable.\n All checks default to \\`true\\`, i.e. are enabled by default.\n Note that you cannot disable variables and parameters.\n\n The option \\`\"temporalDeadZone\"\\` defaults to \\`true\\` which shows errors when shadowing block scoped declarations in their\n temporal dead zone. When set to \\`false\\` parameters, classes, enums and variables declared\n with \\`let\\` or \\`const\\` are not considered shadowed if the shadowing occurs within their\n [temporal dead zone](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified).\n\n The following example shows how the \\`\"temporalDeadZone\"\\` option changes the linting result:\n\n \\`\\`\\`ts\n function fn(value) {\n if (value) {\n const tmp = value; // no error on this line if \"temporalDeadZone\" is false\n return tmp;\n }\n let tmp = undefined;\n if (!value) {\n const tmp = value; // this line always contains an error\n return tmp;\n }\n }\n \\`\\`\\`\n "]))), options: { type: "object", properties: { @@ -49,6 +49,7 @@ var Rule = /** @class */ (function (_super) { namespace: { type: "boolean" }, typeAlias: { type: "boolean" }, typeParameter: { type: "boolean" }, + temporalDeadZone: { type: "boolean" }, }, }, optionExamples: [ @@ -62,7 +63,7 @@ var Rule = /** @class */ (function (_super) { }(Lint.Rules.AbstractRule)); exports.Rule = Rule; function parseOptions(option) { - return tslib_1.__assign({ class: true, enum: true, function: true, import: true, interface: true, namespace: true, typeAlias: true, typeParameter: true }, option); + return tslib_1.__assign({ class: true, enum: true, function: true, import: true, interface: true, namespace: true, temporalDeadZone: true, typeAlias: true, typeParameter: true }, option); } var Scope = /** @class */ (function () { function Scope(functionScope) { @@ -72,16 +73,21 @@ var Scope = /** @class */ (function () { // if no functionScope is provided we are in the process of creating a new function scope, which for consistency links to itself this.functionScope = functionScope !== undefined ? functionScope : this; } - Scope.prototype.addVariable = function (identifier, blockScoped) { + Scope.prototype.addVariable = function (identifier, blockScoped, tdz) { if (blockScoped === void 0) { blockScoped = true; } + if (tdz === void 0) { tdz = false; } // block scoped variables go to the block scope, function scoped variables to the containing function scope var scope = blockScoped ? this : this.functionScope; var list = scope.variables.get(identifier.text); + var variableInfo = { + identifier: identifier, + tdz: tdz, + }; if (list === undefined) { - scope.variables.set(identifier.text, [identifier]); + scope.variables.set(identifier.text, [variableInfo]); } else { - list.push(identifier); + list.push(variableInfo); } }; return Scope; @@ -89,10 +95,15 @@ var Scope = /** @class */ (function () { var NoShadowedVariableWalker = /** @class */ (function (_super) { tslib_1.__extends(NoShadowedVariableWalker, _super); function NoShadowedVariableWalker() { - return _super !== null && _super.apply(this, arguments) || this; + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.scope = new Scope(); + return _this; } NoShadowedVariableWalker.prototype.walk = function (sourceFile) { var _this = this; + if (sourceFile.isDeclarationFile) { + return; + } this.scope = new Scope(); var cb = function (node) { var parentScope = _this.scope; @@ -153,7 +164,7 @@ var NoShadowedVariableWalker = /** @class */ (function (_super) { break; case ts.SyntaxKind.ClassDeclaration: if (_this.options.class && node.name !== undefined) { - parentScope.addVariable(node.name); + parentScope.addVariable(node.name, true, true); } // falls through case ts.SyntaxKind.ClassExpression: @@ -168,7 +179,7 @@ var NoShadowedVariableWalker = /** @class */ (function (_super) { break; case ts.SyntaxKind.EnumDeclaration: if (_this.options.enum) { - parentScope.addVariable(node.name); + parentScope.addVariable(node.name, true, true); } break; case ts.SyntaxKind.InterfaceDeclaration: @@ -180,15 +191,21 @@ var NoShadowedVariableWalker = /** @class */ (function (_super) { if (node.parent.kind !== ts.SyntaxKind.IndexSignature && !tsutils_1.isThisParameter(node) && tsutils_1.isFunctionWithBody(node.parent)) { - _this.handleBindingName(node.name, false); + _this.handleBindingName(node.name, false, true); } break; case ts.SyntaxKind.ModuleDeclaration: if (_this.options.namespace && node.parent.kind !== ts.SyntaxKind.ModuleDeclaration && - node.name.kind === ts.SyntaxKind.Identifier) { + node.name.kind === ts.SyntaxKind.Identifier && + !tsutils_1.isNodeFlagSet(node, ts.NodeFlags.GlobalAugmentation)) { parentScope.addVariable(node.name, false); } + if (tsutils_1.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)) { + _this.onScopeEnd(parentScope); + _this.scope = parentScope; + return; // don't check any ambient declaration blocks + } break; case ts.SyntaxKind.ImportClause: if (_this.options.import && node.name !== undefined) { @@ -238,15 +255,16 @@ var NoShadowedVariableWalker = /** @class */ (function (_super) { this.handleBindingName(variable.name, blockScoped); } }; - NoShadowedVariableWalker.prototype.handleBindingName = function (node, blockScoped) { + NoShadowedVariableWalker.prototype.handleBindingName = function (node, blockScoped, tdz) { + if (tdz === void 0) { tdz = blockScoped; } if (node.kind === ts.SyntaxKind.Identifier) { - this.scope.addVariable(node, blockScoped); + this.scope.addVariable(node, blockScoped, tdz); } else { for (var _i = 0, _a = node.elements; _i < _a.length; _i++) { var element = _a[_i]; if (element.kind !== ts.SyntaxKind.OmittedExpression) { - this.handleBindingName(element.name, blockScoped); + this.handleBindingName(element.name, blockScoped, tdz); } } } @@ -255,14 +273,21 @@ var NoShadowedVariableWalker = /** @class */ (function (_super) { var _this = this; var _a = this.scope, variables = _a.variables, variablesSeen = _a.variablesSeen; variablesSeen.forEach(function (identifiers, name) { - if (variables.has(name)) { - for (var _i = 0, identifiers_1 = identifiers; _i < identifiers_1.length; _i++) { - var identifier = identifiers_1[_i]; + var declarationsInScope = variables.get(name); + var _loop_1 = function (identifier) { + if (declarationsInScope !== undefined && + (_this.options.temporalDeadZone || + // check if any of the declaration either has no temporal dead zone or is declared before the identifier + declarationsInScope.some(function (declaration) { return !declaration.tdz || declaration.identifier.pos < identifier.pos; }))) { _this.addFailureAtNode(identifier, Rule.FAILURE_STRING_FACTORY(name)); } - } - else if (parent !== undefined) { - addToList(parent.variablesSeen, name, identifiers); + else if (parent !== undefined) { + addOneToList(parent.variablesSeen, name, identifier); + } + }; + for (var _i = 0, identifiers_1 = identifiers; _i < identifiers_1.length; _i++) { + var identifier = identifiers_1[_i]; + _loop_1(identifier); } }); if (parent !== undefined) { @@ -273,13 +298,24 @@ var NoShadowedVariableWalker = /** @class */ (function (_super) { }; return NoShadowedVariableWalker; }(Lint.AbstractWalker)); -function addToList(map, name, identifiers) { +function addToList(map, name, variables) { + var list = map.get(name); + if (list === undefined) { + list = []; + map.set(name, list); + } + for (var _i = 0, variables_1 = variables; _i < variables_1.length; _i++) { + var variable = variables_1[_i]; + list.push(variable.identifier); + } +} +function addOneToList(map, name, identifier) { var list = map.get(name); if (list === undefined) { - map.set(name, identifiers); + map.set(name, [identifier]); } else { - list.push.apply(list, identifiers); + list.push(identifier); } } -var _a; +var templateObject_1, templateObject_2; |