Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 abouthow customized files appear on GitHub.

View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp