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

Implement 4201: add option to ignore shadowed underscore #4546

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/rules/noShadowedVariableRule.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import _ = require('lodash');

function foo(_) {
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-shadowed-variable": [true, {"underscore": false}]
}
}