Skip to content

fix(eslint-plugin): [no-unnecessary-template-expression] do not render escaped strings in autofixes #8688

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 14 commits into from
Jul 20, 2024

Conversation

auvred
Copy link
Member

@auvred auvred commented Mar 16, 2024

PR Checklist

Overview

Addresses many issues with escaping of ` and ${. To better understand what's going on here, I'd suggest reading the comments in the code.

NOTE: I changed the base branch for this PR to v8 since this PR uses multipass fixes - #8883

@typescript-eslint
Copy link
Contributor

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.

Copy link

netlify bot commented Mar 16, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 4633002
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6697798b46c47200089f70ea
😎 Deploy Preview https://deploy-preview-8688--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 98 (🟢 up 4 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Mar 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.91%. Comparing base (6bd4211) to head (4633002).
Report is 17 commits behind head on v8.

Additional details and impacted files
@@            Coverage Diff             @@
##               v8    #8688      +/-   ##
==========================================
- Coverage   87.91%   87.91%   -0.01%     
==========================================
  Files         397      397              
  Lines       13558    13577      +19     
  Branches     3939     3947       +8     
==========================================
+ Hits        11920    11936      +16     
- Misses       1324     1327       +3     
  Partials      314      314              
Flag Coverage Δ
unittest 87.91% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...in/src/rules/no-unnecessary-template-expression.ts 100.00% <100.00%> (ø)

... and 12 files with indirect coverage changes

@kirkwaiblinger
Copy link
Member

kirkwaiblinger commented Mar 17, 2024

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.

Copy link
Member Author

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!

Copy link
Member

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 :)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah +1, I think ideally it wouldn't change the syntax, but that's a small implementation detail and not worth doing a bunch of work for. If folks care they should lint for it separately.

@auvred auvred requested a review from kirkwaiblinger March 22, 2024 14:13
if (isLiteral(expression)) {
const escapedValue =
typeof expression.value === 'string'
? expression.raw.slice(1, -1).replace(/([`$])/g, '\\$1')
Copy link
Member

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?

Copy link
Member Author

@auvred auvred Mar 22, 2024

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?

Copy link
Member

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 ⇒

  1. Removing enclosing quotation characters
  2. just add escapes to characters that need escape in template string, but didn't as a string literal (` and $)

cooked string ⇒

  1. 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 😄

kirkwaiblinger
kirkwaiblinger previously approved these changes Mar 22, 2024
@auvred
Copy link
Member Author

auvred commented Mar 30, 2024

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.

@auvred
Copy link
Member Author

auvred commented May 26, 2024

#8554 resolved! I will revisit and mark this PR as ready for review in a little while! Marking it as awaiting response for myself

@auvred auvred added awaiting response Issues waiting for a reply from the OP or another party and removed blocked by another PR PRs which are ready to go but waiting on another PR labels May 26, 2024
@kirkwaiblinger
Copy link
Member

FYI @auvred - This will a semantic merge conflict with #8821, which has now been merged. When this is ready to go, you'll want to duplicate the fix to the no-unnecessary-template-expression copy of the rule

@auvred auvred changed the base branch from main to v8 June 27, 2024 14:51
@auvred auvred marked this pull request as ready for review June 27, 2024 15:15
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 27, 2024
@bradzacher bradzacher added the bug Something isn't working label Jul 14, 2024
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

I wonder - do you think it'd be worth having a 2nd pass test here which does like

cases.invalid.forEach(
	testCase =>
		expect(eval(testCase.code)).toEqual(eval(testCase.output))
)

to really validate that we don't regress the value transform?
probably not worth the effort but definitely an interesting thought...

Comment on lines 178 to 179
code: '`${0b1010} ${0b1111}`;',
output: '`10 15`;',
Copy link
Member

Choose a reason for hiding this comment

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

part of me wonders if we should not handle these weird number representation cases and just leave them as is.

eg I could see more value in writing code with a bunch of variations on the binary string to ensure completeness vs having the converted int val.

for example

`${0b00} ${0b01} ${0b10} ${0b11}` 

encodes information better (i.e. that all binary cases are represented) rather than

`0 1 2 3`

Obviously this is a simplified case but one could imagine some complex binary sequence represented (eg power of two or something).
Same reason that it's not uncommon for people to define flag enums as

enum Flag {
  A = 1 << 0,
  B = 1 << 1,
  C = 1 << 2,
  D = 1 << 3,
  E = 1 << 4,
}

// rather than

enum Flag {
  A = 1,
  B = 2,
  C = 4,
  D = 8,
  E = 16,
}

probably just put a pin in it and leave it as a potential future exercise

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting point, yea, I can imagine someone would want their binary/hex numbers to remain untouched. But this PR already has a lot of changes beyond the original issue. So I'd say we should file a follow-up issue or wait for someone to report it.

probably just put a pin in it and leave it as a potential future exercise

👍 +1 here

@bradzacher bradzacher added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jul 14, 2024
@auvred
Copy link
Member Author

auvred commented Jul 14, 2024

I wonder - do you think it'd be worth having a 2nd pass test here which does like

cases.invalid.forEach(
	testCase =>
		expect(eval(testCase.code)).toEqual(eval(testCase.output))
)

to really validate that we don't regress the value transform? probably not worth the effort but definitely an interesting thought...

Wow, I was waiting for someone to mention this! That's exactly what I did locally, and it helped catch a lot of errors, but I didn't see this being common practice in typescript-eslint, so I decided not to commit it! I'll be sure to add those tests!

@auvred auvred changed the title fix(eslint-plugin): [no-useless-template-literal] do not render escaped strings in autofixes fix(eslint-plugin): [no-unnecessary-template-expression] do not render escaped strings in autofixes Jul 14, 2024
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

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

What a complicated problem this turned out to be!

I've spent some amount time in the playground testing out confusing test cases...

  1. They all passed
  2. When I looked closer, you already had an equivalent for each of them in your added test cases. Great job on test thoroughness!

My only complaint is that the big fixableExpressions.forEach() callback has gotten huge (~100 lines - in fairness, many of which are comments) and really complicated (order-dependent control flow, context.report() side effects, local functions, and several different behaviors based on node type).

Now, to be sure, a fair amount of that complexity may be irreducible, since... this is just a complex problem. So, I'm ok with merging as is (especially in light of your excellent test coverage 🙂). But if you think that there is room for some simplification, I would certainly welcome it as a reader. It does look like at a minimum the regex evenNumOfBackslashesRegExp and local function endsWithUnescapedDollarSign could be taken out of the callback, since don't use any variables or state from the closure.

So, leaving it to your discretion if you'd like to click the green merge button or make any further tweaks.

Most importantly, though, once again great job in tackling this really complex bug!

@kirkwaiblinger
Copy link
Member

Oh, FYI - please, before merging, double check whether this will cause heinous merge conflicts with #8673 or not. If so, maybe wait for that to go through, since it's targeted to main rather than v8.

@kirkwaiblinger
Copy link
Member

Actually - there is one oddity stemming from the fact that the fixes are mutually dependent, though, I don't think I'd consider it a bug. If you're using the editor, you can apply fixes separately, rather than all at once... So, given

`${'$'}${'{'}`
   ~~~   ~~~

if we fix the first error only in the editor, we'll get

`\$${'{'}`
     ~~~

even though

`$${'{'}`
    ~~~

would have been fine.

Note that

`${'$'}${''}`
   ~~~   ~~

fixes the first error to

`$${''}`
    ~~

I think that's an acceptable quirk that will most likely go completely unnoticed by users. Just noting it here to leave a bradcrumb unless some really obscure bug somehow arises in relation to this.

@auvred
Copy link
Member Author

auvred commented Jul 17, 2024

My only complaint is that the big fixableExpressions.forEach() callback has gotten huge (~100 lines - in fairness, many of which are comments) and really complicated (order-dependent control flow, context.report() side effects, local functions, and several different behaviors based on node type).

Yes, I can imagine how difficult this is to read for someone who hasn't struggled with this code for a decent amount of time, but I don't see any way to simplify this code :(


Oh, FYI - please, before merging, double check whether this will cause heinous merge conflicts with #8673 or not. If so, maybe wait for that to go through, since it's targeted to main rather than v8.

Fortunately, #8673 changes a part of the rule that wasn't touched in this PR, so I think there should be no problem with the merge!

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

:shipit:

@JoshuaKGoldberg JoshuaKGoldberg merged commit def1da1 into typescript-eslint:v8 Jul 20, 2024
61 of 63 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-template-expression] Inconsistent handling of escapes and quotes in autofixes.
5 participants