Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Implement 4201: add option to ignore shadowed underscore (#4546)
Browse files Browse the repository at this point in the history
* add option to ignore shadowed underscore

* allow shadowing of multiple unused parameters
  • Loading branch information
rrogowski authored and Josh Goldberg committed Mar 7, 2019
1 parent 8dc82fc commit 521e5a3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/rules/noShadowedVariableRule.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -101,6 +102,7 @@ export class Rule extends Lint.Rules.AbstractRule {
typeAlias: { type: "boolean" },
typeParameter: { type: "boolean" },
temporalDeadZone: { type: "boolean" },
underscore: { type: "boolean" },
},
},
optionExamples: [
Expand All @@ -115,6 +117,7 @@ export class Rule extends Lint.Rules.AbstractRule {
namespace: true,
typeAlias: false,
typeParameter: false,
underscore: false,
},
],
],
Expand Down Expand Up @@ -147,7 +150,8 @@ type Kind =
| "namespace"
| "typeParameter"
| "typeAlias"
| "temporalDeadZone";
| "temporalDeadZone"
| "underscore";
type Options = Record<Kind, boolean>;

function parseOptions(option: Partial<Options> | undefined): Options {
Expand All @@ -161,6 +165,7 @@ function parseOptions(option: Partial<Options> | undefined): Options {
temporalDeadZone: true,
typeAlias: true,
typeParameter: true,
underscore: true,
...option,
};
}
Expand Down Expand Up @@ -402,7 +407,8 @@ class NoShadowedVariableWalker extends Lint.AbstractWalker<Options> {
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) {
Expand Down
3 changes: 3 additions & 0 deletions test/rules/no-shadowed-variable/default/test.ts.lint
Expand Up @@ -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')]
Expand All @@ -11,6 +12,8 @@ import Bas = require('./bas');
~~~ [err % ('Baz')]
const Bas = Baz;
~~~ [err % ('Bas')]
const _ = _;
~ [err % ('_')]
}
function letTesting() {
var a = 1;
Expand Down
10 changes: 10 additions & 0 deletions 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);
}
5 changes: 5 additions & 0 deletions test/rules/no-shadowed-variable/ignore-underscore/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"no-shadowed-variable": [true, {"underscore": false}]
}
}

0 comments on commit 521e5a3

Please sign in to comment.