Skip to content

fix(eslint-plugin): [no-unnecessary-template-expression] ignore enum and enum members #10782

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 1 commit into from
Feb 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ enum ABC {
C = 'C',
}
type ABCUnion = `${ABC}`;
type A = `${ABC.A}`;

// Interpolating type parameters is allowed.
type TextUtil<T extends string> = `${T}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TSESLint } from '@typescript-eslint/utils';

import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import {
Expand Down Expand Up @@ -76,16 +77,13 @@ export default createRule<[], MessageId>({
return isStringLike(type);
}

/**
* Checks for whole enum types, i.e. `MyEnum`, and not their values, i.e. `MyEnum.A`
*/
function isEnumType(type: ts.Type): boolean {
const symbol = type.getSymbol();

return !!(
symbol?.valueDeclaration &&
ts.isEnumDeclaration(symbol.valueDeclaration)
);
function isEnumMemberType(type: ts.Type): boolean {
return tsutils.typeParts(type).some(t => {
const symbol = t.getSymbol();
return !!(
symbol?.valueDeclaration && ts.isEnumMember(symbol.valueDeclaration)
);
});
}

const isLiteral = isNodeOfType(TSESTree.AST_NODE_TYPES.Literal);
Expand Down Expand Up @@ -460,7 +458,7 @@ export default createRule<[], MessageId>({
constraintType &&
!isTypeParameter &&
isUnderlyingTypeString(constraintType) &&
!isEnumType(constraintType)
!isEnumMemberType(constraintType)
) {
reportSingleInterpolation(node);
return;
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,60 @@ enum Foo {
A = 1,
B = 2,
}
type Bar = \`\${Foo.A}\`;
`,
`
enum Enum1 {
A = 'A1',
B = 'B1',
}

enum Enum2 {
A = 'A2',
B = 'B2',
}

type Union = \`\${Enum1 | Enum2}\`;
`,
`
enum Enum1 {
A = 'A1',
B = 'B1',
}

enum Enum2 {
A = 'A2',
B = 'B2',
}

type Union = \`\${Enum1.A | Enum2.B}\`;
`,
`
enum Enum1 {
A = 'A1',
B = 'B1',
}

enum Enum2 {
A = 'A2',
B = 'B2',
}
type Enums = Enum1 | Enum2;
type Union = \`\${Enums}\`;
`,
`
enum Enum {
A = 'A',
B = 'A',
}

type Intersection = \`\${Enum1.A & string}\`;
`,
`
enum Foo {
A = 'A',
B = 'B',
}
type Bar = \`\${Foo.A}\`;
`,
`
Expand Down Expand Up @@ -1412,30 +1466,5 @@ type Bar = Foo;
],
output: "type FooBar = 'foo' | 'bar';",
},
{
code: `
enum Foo {
A = 'A',
B = 'B',
}
type Bar = \`\${Foo.A}\`;
`,
errors: [
{
column: 13,
endColumn: 21,
endLine: 6,
line: 6,
messageId: 'noUnnecessaryTemplateExpression',
},
],
output: `
enum Foo {
A = 'A',
B = 'B',
}
type Bar = Foo.A;
`,
},
],
});
Loading