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

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 14 commits intotypescript-eslint:v8fromauvred:fix/8677
Jul 20, 2024

Conversation

auvred
Copy link
Member

@auvredauvred commentedMar 16, 2024
edited
Loading

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 tov8 since this PR uses multipass fixes -#8883

kirkwaiblinger reacted with heart emoji
@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 onhttps://opencollective.com/typescript-eslint.

@netlifyNetlify
Copy link

netlifybot commentedMar 16, 2024
edited
Loading

Deploy Preview fortypescript-eslint ready!

NameLink
🔨 Latest commit4633002
🔍 Latest deploy loghttps://app.netlify.com/sites/typescript-eslint/deploys/6697798b46c47200089f70ea
😎 Deploy Previewhttps://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 yourNetlify site configuration.

@nx-cloudNx Cloud
Copy link

nx-cloudbot commentedMar 16, 2024
edited
Loading

☁️ Nx Cloud Report

CI is running/has finished running commands for commit4633002. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


🟥 Failed Commands
nx run-many --target=lint --parallel --exclude eslint-plugin
✅ Successfully ran 30 targets

Sent with 💌 fromNxCloud.

@codecovCodecov
Copy link

codecovbot commentedMar 16, 2024
edited
Loading

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
FlagCoverage Δ
unittest87.91% <100.00%> (-0.01%)⬇️

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

FilesCoverage Δ
...in/src/rules/no-unnecessary-template-expression.ts100.00% <100.00%> (ø)

... and12 files with indirect coverage changes

@kirkwaiblinger
Copy link
Member

kirkwaiblinger commentedMar 17, 2024
edited
Loading

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
MemberAuthor

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!

kirkwaiblinger reacted with thumbs up emoji

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

auvred reacted with heart emoji

Choose a reason for hiding this comment

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

Yeah +1, I thinkideally 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.

if (isLiteral(expression)) {
const escapedValue =
typeof expression.value === 'string'
? expression.raw.slice(1, -1).replace(/([`$])/g, '\\$1')

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
MemberAuthor

@auvredauvredMar 22, 2024
edited
Loading

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

Doese07e14607+ae589b436 clear it up?

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 changesMar 22, 2024
@auvred
Copy link
MemberAuthor

auvred commentedMar 30, 2024
edited
Loading

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.

kirkwaiblinger reacted with thumbs up emoji

@auvred
Copy link
MemberAuthor

auvred commentedMay 26, 2024
edited
Loading

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

JoshuaKGoldberg reacted with rocket emoji

@auvredauvred added awaiting responseIssues waiting for a reply from the OP or another party and removed blocked by another PRPRs which are ready to go but waiting on another PR labelsMay 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 theno-unnecessary-template-expression copy of the rule

auvred reacted with heart emoji

@auvredauvred changed the base branch frommain tov8June 27, 2024 14:51
@auvredauvred marked this pull request as ready for reviewJune 27, 2024 15:15
@github-actionsgithub-actionsbot removed the awaiting responseIssues waiting for a reply from the OP or another party labelJun 27, 2024
@bradzacherbradzacher added the bugSomething isn't working labelJul 14, 2024
Copy link
Member

@bradzacherbradzacher 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...

auvred reacted with rocket emoji
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 thanenum 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
MemberAuthor

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

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

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!

kirkwaiblinger reacted with laugh emoji

@auvredauvred changed the titlefix(eslint-plugin): [no-useless-template-literal] do not render escaped strings in autofixesfix(eslint-plugin): [no-unnecessary-template-expression] do not render escaped strings in autofixesJul 14, 2024
Copy link
Member

@kirkwaiblingerkirkwaiblinger left a comment
edited
Loading

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 bigfixableExpressions.forEach() callback has gotten huge (~100 lines - in fairness, many of which are comments) andreally 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 regexevenNumOfBackslashesRegExp and local functionendsWithUnescapedDollarSign 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!

auvred reacted with thumbs up emoji
@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 oneoddity 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 fixthe 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 somereally obscure bug somehow arises in relation to this.

auvred reacted with thumbs up emoji

@auvred
Copy link
MemberAuthor

My only complaint is that the bigfixableExpressions.forEach() callback has gotten huge (~100 lines - in fairness, many of which are comments) andreally 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!

kirkwaiblinger and JoshuaKGoldberg reacted with thumbs up emoji

Copy link
Member

@JoshuaKGoldbergJoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

:shipit:

@JoshuaKGoldbergJoshuaKGoldberg merged commitdef1da1 intotypescript-eslint:v8Jul 20, 2024
61 of 63 checks passed
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsJul 28, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@JoshuaKGoldbergJoshuaKGoldbergJoshuaKGoldberg approved these changes

@bradzacherbradzacherbradzacher approved these changes

@kirkwaiblingerkirkwaiblingerkirkwaiblinger approved these changes

@Josh-CenaJosh-CenaAwaiting requested review from Josh-Cena

Assignees
No one assigned
Labels
1 approval>=1 team member has approved this PR; we're now leaving it open for more reviews before we mergebugSomething isn't working
Projects
None yet
Milestone
No milestone
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
@auvred@kirkwaiblinger@JoshuaKGoldberg@bradzacher@Josh-Cena

[8]ページ先頭

©2009-2025 Movatter.jp