10000 fix(eslint-plugin): [consistent-type-imports] dont report on types us… · danvk/typescript-eslint@7139e45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7139e45

Browse files
auvreddanvk
authored andcommitted
fix(eslint-plugin): [consistent-type-imports] dont report on types used in export assignment expressions (typescript-eslint#8332)
1 parent 86f4a6a commit 7139e45

File tree

6 files changed

+115
-12
lines changed

6 files changed

+115
-12
lines changed

packages/eslint-plugin/src/rules/consistent-type-imports.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ export default createRule<Options, MessageIds>({
183183
* keep origin import kind when export
184184
* export { Type }
185185
* export default Type;
186+
* export = Type;
186187
*/
187188
if (
188189
ref.identifier.parent.type ===
189190
AST_NODE_TYPES.ExportSpecifier ||
190191
ref.identifier.parent.type ===
191-
AST_NODE_TYPES.ExportDefaultDeclaration
192+
AST_NODE_TYPES.ExportDefaultDeclaration ||
193+
ref.identifier.parent.type ===
194+
AST_NODE_TYPES.TSExportAssignment
192195
) {
193196
if (ref.isValueReference && ref.isTypeReference) {
194197
return node.importKind === 'type';

packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,22 @@ export type Y = {
566566
[constants.X]: ReadonlyArray<string>;
567567
};
568568
`,
569+
`
570+
import A from 'foo';
571+
export = A;
572+
`,
573+
`
574+
import type A from 'foo';
575+
export = A;
576+
`,
577+
`
578+
import type A from 'foo';
579+
export = {} as A;
580+
`,
581+
`
582+
import { type A } from 'foo';
583+
export = {} as A;
584+
`,
569585
],
570586
invalid: [
571587
{
@@ -2230,5 +2246,41 @@ let baz: D;
22302246
},
22312247
],
22322248
},
2249+
{
2250+
code: `
2251+
import A from 'foo';
2252+
export = {} as A;
2253+
`,
2254+
output: `
2255+
import type A from 'foo';
2256+
export = {} as A;
2257+
`,
2258+
options: [{ prefer: 'type-imports', fixStyle: 'inline-type-imports' }],
2259+
errors: [
2260+
{
2261+
messageId: 'typeOverValue',
2262+
line: 2,
2263+
column: 1,
2264+
},
2265+
],
2266+
},
2267+
{
2268+
code: `
2269+
import { A } from 'foo';
2270+
export = {} as A;
2271+
`,
2272+
output: `
2273+
import { type A } from 'foo';
2274+
export = {} as A;
2275+
`,
2276+
options: [{ prefer: 'type-imports', fixStyle: 'inline-type-imports' }],
2277+
errors: [
2278+
{
2279+
messageId: 'typeOverValue',
2280+
line: 2,
2281+
column: 1,
2282+
},
2283+
],
2284+
},
22332285
],
22342286
});

packages/scope-manager/src/referencer/ExportVisitor.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { Visitor } from './Visitor';
77
type ExportNode =
88
| TSESTree.ExportAllDeclaration
99
| TSESTree.ExportDefaultDeclaration
10-
| TSESTree.ExportNamedDeclaration
11-
| TSESTree.TSExportAssignment;
10+
| TSESTree.ExportNamedDeclaration;
1211

1312
class ExportVisitor extends Visitor {
1413
readonly #referencer: Referencer;
@@ -26,10 +25,7 @@ class ExportVisitor extends Visitor {
2625
}
2726

2827
protected Identifier(node: TSESTree.Identifier): void {
29-
if (
30-
this.#exportNode.type !== AST_NODE_TYPES.TSExportAssignment &&
31-
this.#exportNode.exportKind === 'type'
32-
) {
28+
if (this.#exportNode.exportKind === 'type') {
3329
// export type { T };
3430
// type exports can only reference types
3531
this.#referencer.currentScope().referenceType(node);
@@ -53,10 +49,6 @@ class ExportVisitor extends Visitor {
5349
}
5450
}
5551

56-
protected TSExportAssignment(node: TSESTree.TSExportAssignment): void {
57-
this.visit(node.expression);
58-
}
59-
6052
protected ExportNamedDeclaration(
6153
node: TSESTree.ExportNamedDeclaration,
6254
): void {

packages/scope-manager/src/referencer/Referencer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,11 @@ class Referencer extends Visitor {
444444
}
445445

446446
protected TSExportAssignment(node: TSESTree.TSExportAssignment): void {
447-
ExportVisitor.visit(this, node);
447+
if (node.expression.type === AST_NODE_TYPES.Identifier) {
448+
this.currentScope().referenceDualValueType(node.expression);
449+
} else {
450+
this.visit(node.expression);
451+
}
448452
}
449453

450454
protected ExportNamedDeclaration(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type T = 1;
2+
3+
export = {} as T;

packages/scope-manager/tests/fixtures/export/equals4-type.ts.shot

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0