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 1 commit
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
13 changes: 9 additions & 4 deletions src/rules/noShadowedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
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 +101,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 +116,7 @@ export class Rule extends Lint.Rules.AbstractRule {
namespace: true,
typeAlias: false,
typeParameter: false,
underscore: false,
},
],
],
Expand Down Expand Up @@ -147,7 +149,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 +164,7 @@ function parseOptions(option: Partial<Options> | undefined): Options {
temporalDeadZone: true,
typeAlias: true,
typeParameter: true,
underscore: true,
...option,
};
}
Expand Down Expand Up @@ -402,7 +406,8 @@ class NoShadowedVariableWalker extends Lint.AbstractWalker<Options> {
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) {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _ = require('lodash');

function foo(_) {
rrogowski marked this conversation as resolved.
Show resolved Hide resolved
console.log(_);
}
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}]
}
}