Skip to content

Commit

Permalink
fix(eslint-plugin): [no-shadow] ignore type-only imports properly (#3868
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rafaelss95 committed Sep 21, 2021
1 parent 8aa87a1 commit dda9cee
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
25 changes: 21 additions & 4 deletions packages/eslint-plugin/src/rules/no-shadow.ts
@@ -1,9 +1,14 @@
import {
TSESTree,
TSESLint,
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { ScopeType } from '@typescript-eslint/scope-manager';
import {
Definition,
DefinitionType,
ImportBindingDefinition,
ScopeType,
} from '@typescript-eslint/scope-manager';
import * as util from '../util';

type MessageIds = 'noShadow';
Expand Down Expand Up @@ -85,6 +90,15 @@ export default util.createRule<Options, MessageIds>({
return variable.defs[0].type === 'Parameter' && variable.name === 'this';
}

function isTypeImport(
definition: Definition,
): definition is ImportBindingDefinition {
return (
definition.type === DefinitionType.ImportBinding &&
definition.parent.importKind === 'type'
);
}

function isTypeValueShadow(
variable: TSESLint.Scope.Variable,
shadowed: TSESLint.Scope.Variable,
Expand All @@ -98,8 +112,11 @@ export default util.createRule<Options, MessageIds>({
return false;
}

const [firstDefinition] = shadowed.defs;
const isShadowedValue =
'isValueVariable' in shadowed ? shadowed.isValueVariable : true;
!('isValueVariable' in shadowed) ||
!firstDefinition ||
(!isTypeImport(firstDefinition) && shadowed.isValueVariable);
return variable.isValueVariable !== isShadowedValue;
}

Expand Down
46 changes: 45 additions & 1 deletion packages/eslint-plugin/tests/rules/no-shadow.test.ts
@@ -1,6 +1,6 @@
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import rule from '../../src/rules/no-shadow';
import { RuleTester } from '../RuleTester';
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';

const ruleTester = new RuleTester({
parserOptions: {
Expand Down Expand Up @@ -172,6 +172,18 @@ export class Wrapper<Wrapped> {
}
}
`,
{
// https://github.com/typescript-eslint/typescript-eslint/issues/3862
code: `
import type { foo } from './foo';
type bar = number;
// 'foo' is already declared in the upper scope
// 'bar' is fine
function doThing(foo: number, bar: number) {}
`,
options: [{ ignoreTypeValueShadow: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -1398,5 +1410,37 @@ function foo(cb) {
},
],
},
{
code: `
import type { foo } from './foo';
function doThing(foo: number, bar: number) {}
`,
options: [{ ignoreTypeValueShadow: false }],
errors: [
{
messageId: 'noShadow',
data: { name: 'foo' },
type: AST_NODE_TYPES.Identifier,
line: 3,
column: 18,
},
],
},
{
code: `
import { foo } from './foo';
function doThing(foo: number, bar: number) {}
`,
options: [{ ignoreTypeValueShadow: true }],
errors: [
{
messageId: 'noShadow',
data: { name: 'foo' },
type: AST_NODE_TYPES.Identifier,
line: 3,
column: 18,
},
],
},
],
});

0 comments on commit dda9cee

Please sign in to comment.