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): [consistent-type-imports] incorrect handling of computed property type signatures #2990

Merged
merged 1 commit into from Feb 1, 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
106 changes: 78 additions & 28 deletions packages/eslint-plugin/src/rules/consistent-type-imports.ts
Expand Up @@ -164,20 +164,49 @@ export default util.createRule<Options, MessageIds>({
}
}
if (ref.isValueReference) {
// `type T = typeof foo` will create a value reference because "foo" must be a value type
// however this value reference is safe to use with type-only imports
let parent = ref.identifier.parent;
let parent: TSESTree.Node | undefined =
ref.identifier.parent;
let child: TSESTree.Node = ref.identifier;
while (parent) {
if (parent.type === AST_NODE_TYPES.TSTypeQuery) {
return true;
switch (parent.type) {
// CASE 1:
// `type T = typeof foo` will create a value reference because "foo" must be a value type
// however this value reference is safe to use with type-only imports
case AST_NODE_TYPES.TSTypeQuery:
return true;

case AST_NODE_TYPES.TSQualifiedName:
// TSTypeQuery must have a TSESTree.EntityName as its child, so we can filter here and break early
if (parent.left !== child) {
return false;
}
child = parent;
parent = parent.parent;
continue;
// END CASE 1

//////////////

// CASE 2:
// `type T = { [foo]: string }` will create a value reference because "foo" must be a value type
// however this value reference is safe to use with type-only imports.
// Also this is represented as a non-type AST - hence it uses MemberExpression
case AST_NODE_TYPES.TSPropertySignature:
return parent.key === child;

case AST_NODE_TYPES.MemberExpression:
if (parent.object !== child) {
return false;
}
child = parent;
parent = parent.parent;
continue;
// END CASE 2

default:
return false;
}
// TSTypeQuery must have a TSESTree.EntityName as its child, so we can filter here and break early
if (parent.type !== AST_NODE_TYPES.TSQualifiedName) {
break;
}
parent = parent.parent;
}
return false;
}

return ref.isTypeReference;
Expand Down Expand Up @@ -229,25 +258,46 @@ export default util.createRule<Options, MessageIds>({
? report.valueSpecifiers
: report.typeSpecifiers
).map(specifier => `"${specifier.local.name}"`);

const message = ((): {
messageId: MessageIds;
data: Record<string, unknown>;
} => {
if (importNames.length === 1) {
const typeImports = importNames[0];
if (isTypeImport) {
return {
messageId: 'aImportInDecoMeta',
data: { typeImports },
};
} else {
return {
messageId: 'aImportIsOnlyTypes',
data: { typeImports },
};
}
} else {
const typeImports = [
importNames.slice(0, -1).join(', '),
importNames.slice(-1)[0],
].join(' and ');
if (isTypeImport) {
return {
messageId: 'someImportsInDecoMeta',
data: { typeImports },
};
} else {
return {
messageId: 'someImportsAreOnlyTypes',
data: { typeImports },
};
}
}
})();

context.report({
node: report.node,
messageId:
importNames.length === 1
? isTypeImport
? 'aImportInDecoMeta'
: 'aImportIsOnlyTypes'
: isTypeImport
? 'someImportsInDecoMeta'
: 'someImportsAreOnlyTypes',
data: {
typeImports:
importNames.length === 1
? importNames[0]
: [
importNames.slice(0, -1).join(', '),
importNames.slice(-1)[0],
].join(' and '),
},
...message,
*fix(fixer) {
if (isTypeImport) {
yield* fixToValueImportInDecoMeta(
Expand Down
Expand Up @@ -341,6 +341,14 @@ ruleTester.run('consistent-type-imports', rule, {
`,
parserOptions: withMetaParserOptions,
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2989
`
import type * as constants from './constants';

export type Y = {
[constants.X]: ReadonlyArray<string>;
};
`,
],
invalid: [
{
Expand Down