Skip to content

Commit

Permalink
fixup! fix(eslint-plugin): [no-shadow] ignore type-only imports properly
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 committed Sep 21, 2021
1 parent f3922fb commit cb1634a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -3,7 +3,12 @@ import {
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DefinitionType, 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,10 +112,11 @@ export default util.createRule<Options, MessageIds>({
return false;
}

const [firstDefinition] = shadowed.defs;
const isShadowedValue =
!('isValueVariable' in shadowed) ||
(shadowed.defs[0]?.type !== DefinitionType.ImportBinding &&
shadowed.isValueVariable);
!firstDefinition ||
(!isTypeImport(firstDefinition) && shadowed.isValueVariable);
return variable.isValueVariable !== isShadowedValue;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow.test.ts
Expand Up @@ -1426,5 +1426,21 @@ function doThing(foo: number, bar: number) {}
},
],
},
{
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 cb1634a

Please sign in to comment.