From c235bc4dd68b1ff888d23108f46c1bb4b382221b Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Sat, 11 Sep 2021 21:03:39 -0300 Subject: [PATCH] fix(eslint-plugin): [no-shadow] ignore type-only imports properly --- packages/eslint-plugin/src/rules/no-shadow.ts | 10 ++++++---- .../eslint-plugin/tests/rules/no-shadow.test.ts | 14 +++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index 6eb2ef38f7c8..4c15d9e0f78a 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -1,9 +1,9 @@ import { - TSESTree, - TSESLint, AST_NODE_TYPES, + TSESLint, + TSESTree, } from '@typescript-eslint/experimental-utils'; -import { ScopeType } from '@typescript-eslint/scope-manager'; +import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager'; import * as util from '../util'; type MessageIds = 'noShadow'; @@ -99,7 +99,9 @@ export default util.createRule({ } const isShadowedValue = - 'isValueVariable' in shadowed ? shadowed.isValueVariable : true; + !('isValueVariable' in shadowed) || + (shadowed.defs[0]?.type !== DefinitionType.ImportBinding && + shadowed.isValueVariable); return variable.isValueVariable !== isShadowedValue; } diff --git a/packages/eslint-plugin/tests/rules/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow.test.ts index f48adfc1b57c..0166a7ada705 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/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: { @@ -172,6 +172,18 @@ export class Wrapper { } } `, + { + // 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: [ {