diff --git a/src/rules/noShadowedVariableRule.ts b/src/rules/noShadowedVariableRule.ts index d2363e9ac25..2617217c82e 100644 --- a/src/rules/noShadowedVariableRule.ts +++ b/src/rules/noShadowedVariableRule.ts @@ -64,7 +64,8 @@ export class Rule extends Lint.Rules.AbstractRule { optionsDescription: Lint.Utils.dedent` You can optionally pass an object to disable checking for certain kinds of declarations. Possible keys are \`"class"\`, \`"enum"\`, \`"function"\`, \`"import"\`, \`"interface"\`, \`"namespace"\`, \`"typeAlias"\` - and \`"typeParameter"\`. Just set the value to \`false\` for the check you want to disable. + and \`"typeParameter"\`. You can also pass \`"underscore\`" to ignore variable names that begin with \`_\`. + Just set the value to \`false\` for the check you want to disable. All checks default to \`true\`, i.e. are enabled by default. Note that you cannot disable variables and parameters. @@ -101,6 +102,7 @@ export class Rule extends Lint.Rules.AbstractRule { typeAlias: { type: "boolean" }, typeParameter: { type: "boolean" }, temporalDeadZone: { type: "boolean" }, + underscore: { type: "boolean" }, }, }, optionExamples: [ @@ -115,6 +117,7 @@ export class Rule extends Lint.Rules.AbstractRule { namespace: true, typeAlias: false, typeParameter: false, + underscore: false, }, ], ], @@ -147,7 +150,8 @@ type Kind = | "namespace" | "typeParameter" | "typeAlias" - | "temporalDeadZone"; + | "temporalDeadZone" + | "underscore"; type Options = Record; function parseOptions(option: Partial | undefined): Options { @@ -161,6 +165,7 @@ function parseOptions(option: Partial | undefined): Options { temporalDeadZone: true, typeAlias: true, typeParameter: true, + underscore: true, ...option, }; } @@ -402,7 +407,8 @@ class NoShadowedVariableWalker extends Lint.AbstractWalker { declarationsInScope.some( declaration => !declaration.tdz || declaration.identifier.pos < identifier.pos, - )) + )) && + (this.options.underscore || !identifier.getText().startsWith("_")) ) { this.addFailureAtNode(identifier, Rule.FAILURE_STRING_FACTORY(name)); } else if (parent !== undefined) { diff --git a/test/rules/no-shadowed-variable/default/test.ts.lint b/test/rules/no-shadowed-variable/default/test.ts.lint index 00ef37cefd0..b7d372cd6d8 100644 --- a/test/rules/no-shadowed-variable/default/test.ts.lint +++ b/test/rules/no-shadowed-variable/default/test.ts.lint @@ -2,6 +2,7 @@ import { Foo } from './foo'; import * as Bar from '.bar'; import Baz from './baz'; import Bas = require('./bas'); +import _ = require('lodash'); { const Foo = 'bar'; ~~~ [err % ('Foo')] @@ -11,6 +12,8 @@ import Bas = require('./bas'); ~~~ [err % ('Baz')] const Bas = Baz; ~~~ [err % ('Bas')] + const _ = _; + ~ [err % ('_')] } function letTesting() { var a = 1; diff --git a/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint b/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint new file mode 100644 index 00000000000..4e1fa88763d --- /dev/null +++ b/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint @@ -0,0 +1,10 @@ +import _ = require('lodash'); + +function foo(_) { + console.log(_); +} + +function foo(_1, _2) { + // Allow shadowing of multiple unused parameters + [].map((_1, _2) => undefined); +} diff --git a/test/rules/no-shadowed-variable/ignore-underscore/tslint.json b/test/rules/no-shadowed-variable/ignore-underscore/tslint.json new file mode 100644 index 00000000000..ed40897ba47 --- /dev/null +++ b/test/rules/no-shadowed-variable/ignore-underscore/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-shadowed-variable": [true, {"underscore": false}] + } + }