-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [no-unnecessary-template-expression] handle template literal type #10612
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
Conversation
Thanks for the PR, @yeonjuan! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
View your CI Pipeline Execution ↗ for commit 14beb1a.
☁️ Nx Cloud last updated this comment at |
e4f963b
to
849fa9a
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10612 +/- ##
==========================================
+ Coverage 87.15% 87.23% +0.08%
==========================================
Files 448 450 +2
Lines 15576 15677 +101
Branches 4551 4589 +38
==========================================
+ Hits 13575 13676 +101
Misses 1645 1645
Partials 356 356
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done on managing to reuse the existing code even though the AST is slightly different! And especially kudos for making it a readable git diff! (PSA to other readers - you need to have the "ignore whitespace" feature enabled in order to read this PR).
Requesting some changes but I think the overall approach is good!
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
@@ -976,6 +976,9 @@ ruleTester.run('no-unnecessary-template-expression', rule, { | |||
valid: [ | |||
"const string = 'a';", | |||
'const string = `a`;', | |||
'const string = `NaN: ${/* comment */ NaN}`;', | |||
'const string = `undefined: ${/* comment */ undefined}`;', | |||
'const string = `Infinity: ${Infinity /* comment */}`;', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixes #10684
@kirkwaiblinger Thank you for your review. I've fixed it! 👍 de4d428 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great! Couple questions, no real blockers.
@@ -51,6 +57,15 @@ const wrappedIntersection = `${intersectionWithString}`; | |||
|
|||
const ab1 = `ab`; | |||
const ab2 = `ab`; | |||
type AB = `AB`; | |||
|
|||
// Transforming enum members into string unions using template literals is allowed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, what's the reasoning for this? I'm not familiar with this usage, so I'm curious for a reference on it, and not seeing one on the issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirkwaiblinger
I should have written an explanation for this, but I missed it. This is an easy way to change an enum to a union type, and the union type is not the same as the enum.
enum Enum {
A = 'A',
B = 'B',
C = 'C'
}
type Union = `${Enum}`;
function foo(a: Enum) {}
foo("A"); // ts error: Argument of type '"A"' is not assignable to parameter of type 'EnumA'.
foo(Enum.A);
function bar(a: Union) {}
bar("A");
typescript-eslint is also using this approach.
| `${TSESTree.AST_NODE_TYPES}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok 👍 Seems plausible to me! I'll defer to you and other reviewers on this, thanks for the explanation!
return isStringLike(type); | ||
} | ||
|
||
function isEnumType(type: ts.Type): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like enums have a type flag ts.TypeFlags.EnumLike
set. Can we just use that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kirkwaiblinger
This function is for checking if it is an enum declaration symbol (not member).
enum Enum { A = 'A' }
type A = `${Enum}`;
That doesn't seem work because both enum declaration and enum members have the EnumLike
flag.
Perhaps it would be better to rename the function to hasEnumDeclarationSymbol
, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. Then, I think it's good as is! If anything, a comment with example like
/**
* Checks for whole enum types, i.e. `MyEnum`, and not their values, i.e. `MyEnum.A`
*/
might be more helpful. But fine either way!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I've added the comment. 14beb1a
packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!!
@typescript-eslint/triage-team Looking for a second review on this since it's tricky stuff!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f1e9a5d
into
typescript-eslint:main
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.21.0 | 8.22.0 | | npm | @typescript-eslint/parser | 8.21.0 | 8.22.0 | ## [v8.22.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8220-2025-01-27) ##### 🩹 Fixes - **eslint-plugin:** \[no-unnecessary-template-expression] handle template literal type ([#10612](typescript-eslint/typescript-eslint#10612)) - **eslint-plugin:** \[prefer-readonly] autofixer doesn't add type to property that is mutated in the constructor ([#10552](typescript-eslint/typescript-eslint#10552)) - **eslint-plugin:** \[no-extraneous-class] handle accessor keyword ([#10678](typescript-eslint/typescript-eslint#10678)) - **eslint-plugin:** \[no-shadow] don't report unnecessarily on valid ways of using module augmentation ([#10616](typescript-eslint/typescript-eslint#10616)) - **eslint-plugin:** \[no-duplicate-type-constituents] handle nested types ([#10638](typescript-eslint/typescript-eslint#10638)) - **eslint-plugin:** \[prefer-nullish-coalescing] doesn't report on ternary but on equivalent || ([#10517](typescript-eslint/typescript-eslint#10517)) ##### ❤️ Thank You - mdm317 - Olivier Zalmanski [@OlivierZal](https://github.com/OlivierZal) - Ronen Amiel - YeonJuan [@yeonjuan](https://github.com/yeonjuan) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.21.0 | 8.22.0 | | npm | @typescript-eslint/parser | 8.21.0 | 8.22.0 | ## [v8.22.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8220-2025-01-27) ##### 🩹 Fixes - **eslint-plugin:** \[no-unnecessary-template-expression] handle template literal type ([#10612](typescript-eslint/typescript-eslint#10612)) - **eslint-plugin:** \[prefer-readonly] autofixer doesn't add type to property that is mutated in the constructor ([#10552](typescript-eslint/typescript-eslint#10552)) - **eslint-plugin:** \[no-extraneous-class] handle accessor keyword ([#10678](typescript-eslint/typescript-eslint#10678)) - **eslint-plugin:** \[no-shadow] don't report unnecessarily on valid ways of using module augmentation ([#10616](typescript-eslint/typescript-eslint#10616)) - **eslint-plugin:** \[no-duplicate-type-constituents] handle nested types ([#10638](typescript-eslint/typescript-eslint#10638)) - **eslint-plugin:** \[prefer-nullish-coalescing] doesn't report on ternary but on equivalent || ([#10517](typescript-eslint/typescript-eslint#10517)) ##### ❤️ Thank You - mdm317 - Olivier Zalmanski [@OlivierZal](https://github.com/OlivierZal) - Ronen Amiel - YeonJuan [@yeonjuan](https://github.com/yeonjuan) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
PR Checklist
Overview
This pr addresses #9971.
I didn't apply all the tests written for
TemplateLiteral
s toTemplateLiteralType
s because it shares most of its logic withTemplateLiteral
s.This includes tests for basic behavior similar to
TemplateLiteral
and some behavior that must be handled separately forTemplateLiteralType
.