Skip to content
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
20 changes: 11 additions & 9 deletions packages/eslint-plugin/src/rules/consistent-type-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ export default createRule<Options, MessageIds>({
function isSymbolTypeBased(
symbol: ts.Symbol | undefined,
): boolean | undefined {
while (symbol && symbol.flags & ts.SymbolFlags.Alias) {
symbol = checker.getAliasedSymbol(symbol);
if (
symbol.getDeclarations()?.find(ts.isTypeOnlyImportOrExportDeclaration)
) {
return true;
}
}
if (!symbol || checker.isUnknownSymbol(symbol)) {
return undefined;
}
return !(symbol.flags & ts.SymbolFlags.Value);
if (
symbol.getDeclarations()?.some(ts.isTypeOnlyImportOrExportDeclaration)
) {
return true;
}
if (symbol.flags & ts.SymbolFlags.Value) {
return false;
}
return symbol.flags & ts.SymbolFlags.Alias
? isSymbolTypeBased(checker.getImmediateAliasedSymbol(symbol))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace getAliasedSymbol with getImmediateAliasedSymbol.
Because the latter can handle this case as well:

// a.ts
export const NAME = 1;
export type NAME = 1;
// b.ts
import { type NAME as Foo } from './a';
export { Foo };

: true;
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export const value1 = 2;
export const value2 = 2;

export class Class1 {}

export type NAME = 'name';
export const NAME = 'name';
24 changes: 24 additions & 0 deletions packages/eslint-plugin/tests/rules/consistent-type-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ import * as Foo from './consistent-type-exports';
type Foo = 1;
export { Foo }
`,
`
import { Type1 } from './consistent-type-exports';
const Type1 = 1;
export { Type1 };
`,
],
invalid: [
{
Expand Down Expand Up @@ -502,5 +507,24 @@ export {
export type { Foo };
`,
},
{
code: `
import { type NAME as Foo } from './consistent-type-exports';
export { Foo };
`,
errors: [
{
column: 9,
endColumn: 24,
endLine: 3,
line: 3,
messageId: 'typeOverValue',
},
],
output: `
import { type NAME as Foo } from './consistent-type-exports';
export type { Foo };
`,
},
],
});