From 9155df09a5cf47a3bbb59d50d94aec2b5c6c08d4 Mon Sep 17 00:00:00 2001 From: tao <2471314@gmail.com> Date: Tue, 5 Aug 2025 20:35:56 +0800 Subject: [PATCH] fix type shadowing --- .../src/rules/consistent-type-exports.ts | 23 +++++++--------- .../rules/consistent-type-exports.test.ts | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-type-exports.ts b/packages/eslint-plugin/src/rules/consistent-type-exports.ts index 324a622255d7..f70d59618514 100644 --- a/packages/eslint-plugin/src/rules/consistent-type-exports.ts +++ b/packages/eslint-plugin/src/rules/consistent-type-exports.ts @@ -1,7 +1,6 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils'; -import * as tsutils from 'ts-api-utils'; import * as ts from 'typescript'; import { @@ -91,22 +90,18 @@ export default createRule({ function isSymbolTypeBased( symbol: ts.Symbol | undefined, ): boolean | undefined { - if (!symbol) { - return undefined; + while (symbol && symbol.flags & ts.SymbolFlags.Alias) { + symbol = checker.getAliasedSymbol(symbol); + if ( + symbol.getDeclarations()?.find(ts.isTypeOnlyImportOrExportDeclaration) + ) { + return true; + } } - - const aliasedSymbol = tsutils.isSymbolFlagSet( - symbol, - ts.SymbolFlags.Alias, - ) - ? checker.getAliasedSymbol(symbol) - : symbol; - - if (checker.isUnknownSymbol(aliasedSymbol)) { + if (!symbol || checker.isUnknownSymbol(symbol)) { return undefined; } - - return !(aliasedSymbol.flags & ts.SymbolFlags.Value); + return !(symbol.flags & ts.SymbolFlags.Value); } return { diff --git a/packages/eslint-plugin/tests/rules/consistent-type-exports.test.ts b/packages/eslint-plugin/tests/rules/consistent-type-exports.test.ts index 8508cd75ae77..b513411d676d 100644 --- a/packages/eslint-plugin/tests/rules/consistent-type-exports.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-type-exports.test.ts @@ -64,6 +64,11 @@ export { NonTypeNS }; "export type * as foo from './consistent-type-exports/type-only-exports';", "export type * as foo from './consistent-type-exports/type-only-reexport';", "export * as foo from './consistent-type-exports/value-reexport';", + ` +import * as Foo from './consistent-type-exports'; +type Foo = 1; +export { Foo } + `, ], invalid: [ { @@ -483,5 +488,26 @@ export { export type * as foo from './consistent-type-exports/type-only-reexport'; `, }, + { + code: ` + import type * as Foo from './consistent-type-exports'; + type Foo = 1; + export { Foo }; + `, + errors: [ + { + column: 9, + endColumn: 24, + endLine: 4, + line: 4, + messageId: 'typeOverValue', + }, + ], + output: ` + import type * as Foo from './consistent-type-exports'; + type Foo = 1; + export type { Foo }; + `, + }, ], });