Skip to content
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): [consistent-type-assertions] prevent syntax errors on arrow functions #8826

Conversation

kirkwaiblinger
Copy link
Member

PR Checklist

Overview

Upon looking into this bug, I learned that there are a whole host of circumstances where the <T>(expr) => (expr) as T fix failed to put necessary parens around expr (since it was coded to never put any parens around expr). A generic fix has been added, and test cases from lots of precedence categories.

Also, had to change the precedence pertaining to the => operator to address the specific issue in the bug, bringing the => precedence into agreement with its ranking on MDN. Feels suspicious, but didn't seem to break any test cases.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @kirkwaiblinger!

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 Apr 3, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit d294e00
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/660cd749b6e7cc0008d3c5a2
😎 Deploy Preview https://deploy-preview-8826--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: 97 (🟢 up 6 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (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.

@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): [consistent-type-assertion] prevent syntax errors on arrow functions. fix(eslint-plugin): [consistent-type-assertion] prevent syntax errors on arrow functions Apr 3, 2024
const x = new (Generic<string> as Foo)();
const x = new (Generic<string> as Foo)('string');
const x = new ((Generic<string>) as Foo)();
const x = new ((Generic<string>) as Foo)('string');
Copy link
Member Author

Choose a reason for hiding this comment

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

From playing around with the AST viewer, I think this test case was simply incorrect (though tbh this syntax is bonkers and I struggle to follow it so I'm not totally certain.)

For reference, the inputs are

const x = new (<Foo>Generic<string>)();
const x = new (<Foo>Generic<string>)('string');

(located above in the ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE string.)

@kirkwaiblinger kirkwaiblinger marked this pull request as ready for review April 3, 2024 04:28
Copy link

codecov bot commented Apr 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.42%. Comparing base (a14ba9d) to head (d294e00).
Report is 65 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8826      +/-   ##
==========================================
- Coverage   88.02%   87.42%   -0.61%     
==========================================
  Files         405      255     -150     
  Lines       14089    12513    -1576     
  Branches     4125     3927     -198     
==========================================
- Hits        12402    10939    -1463     
+ Misses       1382     1297      -85     
+ Partials      305      277      -28     
Flag Coverage Δ
unittest 87.42% <100.00%> (-0.61%) ⬇️

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

Files Coverage Δ
...int-plugin/src/rules/consistent-type-assertions.ts 92.53% <100.00%> (+0.22%) ⬆️
...es/eslint-plugin/src/util/getOperatorPrecedence.ts 50.00% <ø> (+8.92%) ⬆️

... and 150 files with indirect coverage changes

],
},
{
code: 'const f = <any>function () {};',
Copy link
Member Author

Choose a reason for hiding this comment

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

note that non-arrow functions behave differently from arrow functions

],
},
{
// prettier wants to remove the parens around the yield expression,
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is a prettier bug but tbh I'm not 100% certain. Filed prettier/prettier#16192; we'll see what they say.

Copy link
Member Author

Choose a reason for hiding this comment

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

was able to get this bug fixed. This will play nicely with #6128

Copy link
Member

Choose a reason for hiding this comment

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

Nice!! Love to see an upstream bug discovered as part of our testing 😄

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

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

Great improvements 👍 Do you know why we have so many precedence levels defined for what MDN defines as "precedence 2"?

@kirkwaiblinger
Copy link
Member Author

kirkwaiblinger commented Apr 10, 2024

Great improvements 👍

❤️

Do you know why we have so many precedence levels defined for what MDN defines as "precedence 2"?

I personally do not know for sure. But looking at our precedences, eslint's precedences, and prettier's precedences, none of them seems to match 1:1 with the table on MDN. And, for TS syntax, part of that is explainable by there being additional operators not present in raw JS.... If I had to really speculate, though, I'd guess most of it comes down to the fact that we use the precedence switch for printing not for parsing, and

  1. It doesn't look like we're tooooo careful about right/left associativity checks in the printing code
  2. We don't always want to print code with the minimum of technically required parens (eg mixing && and ||), so this may have crept into precedence values themselves rather than special-casing in the printing logic

so what we see may just be the organic result of getting test cases to pass.

To get more concrete info, we'd need to file a new issue to investigate, I think.

@Josh-Cena
Copy link
Member

Yeah, we should probably put more thought into this. I personally wrote that MDN table and towards the bottom things definitely get much murkier.

@kirkwaiblinger kirkwaiblinger added the bug Something isn't working label Apr 27, 2024
@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): [consistent-type-assertion] prevent syntax errors on arrow functions fix(eslint-plugin): [consistent-type-assertions] prevent syntax errors on arrow functions Apr 30, 2024
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.

🔥

@JoshuaKGoldberg JoshuaKGoldberg merged commit 342b873 into typescript-eslint:main May 26, 2024
64 of 65 checks passed
@kirkwaiblinger kirkwaiblinger deleted the consistent-type-assertions-fix-fix branch May 26, 2024 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [consistent-type-assertions] broken autofix for arrow functions
3 participants