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
fix(eslint-plugin): [no-useless-template-literal] do not render escaped strings in autofixes #8688
base: main
Are you sure you want to change the base?
Conversation
…ed strings in autofixes
Thanks for the PR, @auvred! 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. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #8688 +/- ##
==========================================
- Coverage 87.36% 87.35% -0.02%
==========================================
Files 255 254 -1
Lines 12498 12488 -10
Branches 3923 3920 -3
==========================================
- Hits 10919 10909 -10
- Misses 1304 1307 +3
+ Partials 275 272 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
|
packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Outdated
Show resolved
Hide resolved
good stuff here! In addition to the comment, one thing though that I want to flag is that in the issue it was also intended that `${'a'}`;
// should autofix to
`a`;
// rather than
'a'; though it's possible that that flew under the radar on the issue itself as well when it was marked accepting PRs. I also note that #8669 somewhat conflicts with that change (in that it would be made redundant). Could be worth flagging there, and/or dropping the quotes -> backticks change from #8677 entirely and mentioning that explicitly on the issue. Not strongly opinionated to which decision is made but just think we should document it. |
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.
In addition to the comment, one thing though that I want to flag is that in the issue it was also intended that
`${'a'}`; // should autofix to `a`; // rather than 'a';
@kirkwaiblinger, Ohh, I missed that in the original issue!
I'm 50/50 on this!
though it's possible that that flew under the radar on the issue itself as well when it was marked accepting PRs.
Maybe..
Fixing to string literal vs template literal sounds a bit stylistic to me, I think it would be great to hear more opinions on this!
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.
Yeah, agreed. I will say, at the time, I didn't realize it was just removing the wrapping template syntax, as opposed to overriding whatever the user had typed with one particular quote style. So my motivation for that change has softened a lot as well. It still feels a little surprising to me, but it does have a very justifiable internal logic as-is.
So unless someone comes in and has a strong opinion, I'd say, just leave it as-is :)
if (isLiteral(expression)) { | ||
const escapedValue = | ||
typeof expression.value === 'string' | ||
? expression.raw.slice(1, -1).replace(/([`$])/g, '\\$1') |
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.
(nit, optional) - why are the escaping patterns different for each branch? Maybe worth a 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.
Yeah, it does seem a bit confusing
Does e07e14607+ae589b436
clear it up?
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.
oof, i think i get it, but i still struggle tbh 😆 It's just really tricky to me to figure out which things are in escaped-land and which things are non-escaped in the code comment there (it's even hard for me to type out the special characters in this comment 😆 ). Is the idea
raw string ⇒
- Removing enclosing quotation characters
- just add escapes to characters that need escape in template string, but didn't as a string literal (` and $)
cooked string ⇒
- escape everything that needs escapes (`, $, and \)
I really think a few English words would be helpful in the code comments (to go along with the examples), since my brain is not big enough to make meaning of all the special characters at a glance 😄
packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Outdated
Show resolved
Hide resolved
Thanks for the reviews! I tried to cover all the possible edge cases (although I've likely missed something). Currently this PR is blocked by #8883, because some test cases, like this one below, require multiple fixer passes: ` ${'$'}${''}{} `; So I'm going to convert this PR do a draft until #8554 issue is resolved. |
PR Checklist
Overview
If expression is a string literal, then get the raw value by removing quotes (
.slice(1, -1)
). Otherwise doString(expression.value)
- whatgetStaticStringValue
internally does.