From 0cbb58677d79deb896c28f6573aec6a45728ae5d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 23 May 2021 07:30:10 -0400 Subject: [PATCH] fix(eslint-plugin): [no-type-alias] consider type imports as alias types --- .../eslint-plugin/src/rules/no-type-alias.ts | 1 + .../tests/rules/no-type-alias.test.ts | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/packages/eslint-plugin/src/rules/no-type-alias.ts b/packages/eslint-plugin/src/rules/no-type-alias.ts index 9b80efdd9d6..3e93a995e64 100644 --- a/packages/eslint-plugin/src/rules/no-type-alias.ts +++ b/packages/eslint-plugin/src/rules/no-type-alias.ts @@ -122,6 +122,7 @@ export default util.createRule({ ]; const aliasTypes = new Set([ AST_NODE_TYPES.TSArrayType, + AST_NODE_TYPES.TSImportType, AST_NODE_TYPES.TSTypeReference, AST_NODE_TYPES.TSLiteralType, AST_NODE_TYPES.TSTypeQuery, diff --git a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts index b48acae7e17..ab4e3dd4919 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -394,6 +394,10 @@ export type ClassValue = code: 'type Foo = typeof bar;', options: [{ allowAliases: 'always' }], }, + { + code: "type Foo = typeof import('foo');", + options: [{ allowAliases: 'always' }], + }, { code: ` const WithAKey = { AKey: true }; @@ -405,6 +409,10 @@ type KeyNames = keyof typeof SCALARS; code: 'type Foo = typeof bar | typeof baz;', options: [{ allowAliases: 'in-unions' }], }, + { + code: "type Foo = typeof bar | typeof import('foo');", + options: [{ allowAliases: 'in-unions' }], + }, { code: 'type Foo = keyof [string];', options: [{ allowTupleTypes: 'always' }], @@ -505,6 +513,20 @@ type KeyNames = keyof typeof SCALARS; }, ], }, + { + code: "type Foo = typeof import('foo');", + options: [{ allowAliases: 'never' }], + errors: [ + { + messageId: 'noTypeAlias', + data: { + alias: 'aliases', + }, + line: 1, + column: 12, + }, + ], + }, { code: "type Foo = 'a' | 'b';", errors: [ @@ -528,6 +550,29 @@ type KeyNames = keyof typeof SCALARS; }, ], }, + { + code: "type Foo = 'a' | typeof import('foo');", + errors: [ + { + messageId: 'noCompositionAlias', + data: { + typeName: 'Aliases', + compositionType: 'union', + }, + line: 1, + column: 12, + }, + { + messageId: 'noCompositionAlias', + data: { + typeName: 'Aliases', + compositionType: 'union', + }, + line: 1, + column: 18, + }, + ], + }, { code: "type Foo = 'a' | 'b';", options: [{ allowLiterals: 'in-unions' }],