Skip to content

fix(eslint-plugin): [consistent-type-imports] dont report on types used in export assignment expressions #8332

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

Merged
merged 2 commits into from
Feb 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/consistent-type-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,15 @@ export default createRule<Options, MessageIds>({
* keep origin import kind when export
* export { Type }
* export default Type;
* export = Type;
*/
if (
ref.identifier.parent.type ===
AST_NODE_TYPES.ExportSpecifier ||
ref.identifier.parent.type ===
AST_NODE_TYPES.ExportDefaultDeclaration
AST_NODE_TYPES.ExportDefaultDeclaration ||
ref.identifier.parent.type ===
AST_NODE_TYPES.TSExportAssignment
) {
if (ref.isValueReference && ref.isTypeReference) {
return node.importKind === 'type';
Expand Down
52 changes: 52 additions & 0 deletions packages/eslint-plugin/tests/rules/consistent-type-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,22 @@ export type Y = {
[constants.X]: ReadonlyArray<string>;
};
`,
`
import A from 'foo';
export = A;
`,
`
import type A from 'foo';
export = A;
`,
`
import type A from 'foo';
export = {} as A;
`,
`
import { type A } from 'foo';
export = {} as A;
`,
],
invalid: [
{
Expand Down Expand Up @@ -2230,5 +2246,41 @@ let baz: D;
},
],
},
{
code: `
import A from 'foo';
export = {} as A;
`,
output: `
import type A from 'foo';
export = {} as A;
`,
options: [{ prefer: 'type-imports', fixStyle: 'inline-type-imports' }],
errors: [
{
messageId: 'typeOverValue',
line: 2,
column: 1,
},
],
},
{
code: `
import { A } from 'foo';
export = {} as A;
`,
output: `
import { type A } from 'foo';
export = {} as A;
`,
options: [{ prefer: 'type-imports', fixStyle: 'inline-type-imports' }],
errors: [
{
messageId: 'typeOverValue',
line: 2,
column: 1,
},
],
},
],
});
12 changes: 2 additions & 10 deletions packages/scope-manager/src/referencer/ExportVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { Visitor } from './Visitor';
type ExportNode =
| TSESTree.ExportAllDeclaration
| TSESTree.ExportDefaultDeclaration
| TSESTree.ExportNamedDeclaration
| TSESTree.TSExportAssignment;
| TSESTree.ExportNamedDeclaration;

class ExportVisitor extends Visitor {
readonly #referencer: Referencer;
Expand All @@ -26,10 +25,7 @@ class ExportVisitor extends Visitor {
}

protected Identifier(node: TSESTree.Identifier): void {
if (
this.#exportNode.type !== AST_NODE_TYPES.TSExportAssignment &&
this.#exportNode.exportKind === 'type'
) {
if (this.#exportNode.exportKind === 'type') {
// export type { T };
// type exports can only reference types
this.#referencer.currentScope().referenceType(node);
Expand All @@ -53,10 +49,6 @@ class ExportVisitor extends Visitor {
}
}

protected TSExportAssignment(node: TSESTree.TSExportAssignment): void {
this.visit(node.expression);
}

protected ExportNamedDeclaration(
node: TSESTree.ExportNamedDeclaration,
): void {
Expand Down
6 changes: 5 additions & 1 deletion packages/scope-manager/src/referencer/Referencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ class Referencer extends Visitor {
}

protected TSExportAssignment(node: TSESTree.TSExportAssignment): void {
ExportVisitor.visit(this, node);
if (node.expression.type === AST_NODE_TYPES.Identifier) {
this.currentScope().referenceDualValueType(node.expression);
} else {
this.visit(node.expression);
}
}

protected ExportNamedDeclaration(
Expand Down
3 changes: 3 additions & 0 deletions packages/scope-manager/tests/fixtures/export/equals4-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type T = 1;

export = {} as T;
49 changes: 49 additions & 0 deletions packages/scope-manager/tests/fixtures/export/equals4-type.ts.shot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.