From 934a571be7c54f8c0e4c9c4ff5c968e6bc25cd1e Mon Sep 17 00:00:00 2001 From: rrogowski Date: Tue, 26 Feb 2019 10:44:05 -0500 Subject: [PATCH 1/2] add option to ignore shadowed underscore --- src/rules/noShadowedVariableRule.ts | 13 +++++++++---- .../rules/no-shadowed-variable/default/test.ts.lint | 3 +++ .../ignore-underscore/test.ts.lint | 5 +++++ .../ignore-underscore/tslint.json | 5 +++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint create mode 100644 test/rules/no-shadowed-variable/ignore-underscore/tslint.json diff --git a/src/rules/noShadowedVariableRule.ts b/src/rules/noShadowedVariableRule.ts index d2363e9ac25..c5356b0c75d 100644 --- a/src/rules/noShadowedVariableRule.ts +++ b/src/rules/noShadowedVariableRule.ts @@ -63,8 +63,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. + Possible keys are \`"class"\`, \`"enum"\`, \`"function"\`, \`"import"\`, \`"interface"\`, \`"namespace"\`, \`"typeAlias"\`, + \`"typeParameter"\` and \`"underscore"\`. 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 +101,7 @@ export class Rule extends Lint.Rules.AbstractRule { typeAlias: { type: "boolean" }, typeParameter: { type: "boolean" }, temporalDeadZone: { type: "boolean" }, + underscore: { type: "boolean" }, }, }, optionExamples: [ @@ -115,6 +116,7 @@ export class Rule extends Lint.Rules.AbstractRule { namespace: true, typeAlias: false, typeParameter: false, + underscore: false, }, ], ], @@ -147,7 +149,8 @@ type Kind = | "namespace" | "typeParameter" | "typeAlias" - | "temporalDeadZone"; + | "temporalDeadZone" + | "underscore"; type Options = Record; function parseOptions(option: Partial | undefined): Options { @@ -161,6 +164,7 @@ function parseOptions(option: Partial | undefined): Options { temporalDeadZone: true, typeAlias: true, typeParameter: true, + underscore: true, ...option, }; } @@ -402,7 +406,8 @@ class NoShadowedVariableWalker extends Lint.AbstractWalker { declarationsInScope.some( declaration => !declaration.tdz || declaration.identifier.pos < identifier.pos, - )) + )) && + (this.options.underscore || identifier.getText() !== "_") ) { 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..d544ee37300 --- /dev/null +++ b/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint @@ -0,0 +1,5 @@ +import _ = require('lodash'); + +function foo(_) { + console.log(_); +} 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}] + } + } From 1713f9d44cc9e2cfa264fa800dc5975a62199b4a Mon Sep 17 00:00:00 2001 From: rrogowski Date: Tue, 5 Mar 2019 14:15:20 -0500 Subject: [PATCH 2/2] allow shadowing of multiple unused parameters --- src/rules/noShadowedVariableRule.ts | 7 ++++--- .../no-shadowed-variable/ignore-underscore/test.ts.lint | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/rules/noShadowedVariableRule.ts b/src/rules/noShadowedVariableRule.ts index c5356b0c75d..2617217c82e 100644 --- a/src/rules/noShadowedVariableRule.ts +++ b/src/rules/noShadowedVariableRule.ts @@ -63,8 +63,9 @@ 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"\`, - \`"typeParameter"\` and \`"underscore"\`. Just set the value to \`false\` for the check you want to disable. + Possible keys are \`"class"\`, \`"enum"\`, \`"function"\`, \`"import"\`, \`"interface"\`, \`"namespace"\`, \`"typeAlias"\` + 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. @@ -407,7 +408,7 @@ class NoShadowedVariableWalker extends Lint.AbstractWalker { declaration => !declaration.tdz || declaration.identifier.pos < identifier.pos, )) && - (this.options.underscore || identifier.getText() !== "_") + (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/ignore-underscore/test.ts.lint b/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint index d544ee37300..4e1fa88763d 100644 --- a/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint +++ b/test/rules/no-shadowed-variable/ignore-underscore/test.ts.lint @@ -3,3 +3,8 @@ import _ = require('lodash'); function foo(_) { console.log(_); } + +function foo(_1, _2) { + // Allow shadowing of multiple unused parameters + [].map((_1, _2) => undefined); +}