Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-shadow] ignore type-only imports properly #3868

Merged
merged 2 commits into from Sep 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
},
],
},
],
});