Navigation Menu

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): [sort-type-constituents, sort-type-union-intersection-members] handle some required parentheses cases in the fixer #6118

Merged

Conversation

cherryblossom000
Copy link
Contributor

@cherryblossom000 cherryblossom000 commented Nov 28, 2022

PR Checklist

Overview

requiresParentheses now returns true for union types starting with | and intersection types starting with &.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @cherryblossom000!

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.

@nx-cloud
Copy link

nx-cloud bot commented Nov 28, 2022

☁️ Nx Cloud Report

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

📂 See all runs for this branch


✅ Successfully ran 47 targets

Sent with 💌 from NxCloud.

@netlify
Copy link

netlify bot commented Nov 28, 2022

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 2cf2ca9
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/638541d3e7f6e00008238f48
😎 Deploy Preview https://deploy-preview-6118--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@codecov
Copy link

codecov bot commented Nov 28, 2022

Codecov Report

Merging #6118 (d7f2588) into main (267da4e) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head d7f2588 differs from pull request most recent head 2cf2ca9. Consider uploading reports for the commit 2cf2ca9 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6118      +/-   ##
==========================================
- Coverage   91.27%   91.26%   -0.01%     
==========================================
  Files         366      366              
  Lines       12417    12416       -1     
  Branches     3631     3632       +1     
==========================================
- Hits        11333    11332       -1     
  Misses        774      774              
  Partials      310      310              
Flag Coverage Δ
unittest 91.26% <100.00%> (-0.01%) ⬇️

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

Impacted Files Coverage Δ
.../eslint-plugin/src/rules/sort-type-constituents.ts 90.38% <100.00%> (-0.36%) ⬇️
.../src/rules/sort-type-union-intersection-members.ts 90.38% <100.00%> (-0.36%) ⬇️
packages/eslint-plugin/src/util/misc.ts 97.01% <100.00%> (+0.13%) ⬆️

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.

Code looks great, thanks! Just requesting changes on deduplicating it a bit. ✨

@@ -183,7 +191,7 @@ export default util.createRule<Options, MessageIds>({
function checkSorting(
node: TSESTree.TSIntersectionType | TSESTree.TSUnionType,
): void {
const sourceOrder = node.types.map(type => {
const sourceOrder = node.types.map((type): Constituent => {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: any particular reason to add the return type explicitly, here and in the other file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to make it clear what the Constituent was and where it came from. I ended up getting rid of Constituent when I extracted requiresParentheses into a utility function.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Nov 28, 2022
@cherryblossom000
Copy link
Contributor Author

The force push is because I rebased with main due to a merge conflict.

@cherryblossom000
Copy link
Contributor Author

cherryblossom000 commented Nov 28, 2022

I also realised there’s another bug with this rule:

type T = (B | C) & A

gets fixed to

type T = C | A & B

This is because (B | C) & A gets incorrectly fixed to A & B | C, which is then fixed to C | A & B.

@cherryblossom000 cherryblossom000 changed the title fix(eslint-plugin): [sort-type-constituents] correctly fix constituents with leading | or & fix(eslint-plugin): [sort-type-constituents] add missing parentheses in fix Nov 28, 2022
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Nov 30, 2022
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 if we want to leverage TS's utilities here for completeness?
https://github.com/microsoft/TypeScript/blob/1b75edcec60415dfa77be2e04aeafce0efa2222e/src/compiler/factory/parenthesizerRules.ts

We probably can't consume them directly (cos I don't think the util existed in old versions of TS) - but we could probably fork it? Maybe something to do in a separate PR?

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.

Thanks for the fix for both rules!

flextape

I'm going to land this as is so we can get the fix out ASAP.
If we want to strengthen the logic further we can follow up in a separate PR.

@bradzacher bradzacher changed the title fix(eslint-plugin): [sort-type-constituents] add missing parentheses in fix fix(eslint-plugin): [sort-type-constituents, sort-type-union-intersection-members] handle some required parentheses cases in the fixer Dec 2, 2022
@bradzacher bradzacher merged commit 5d49d5d into typescript-eslint:main Dec 2, 2022
@cherryblossom000 cherryblossom000 deleted the sort-constituents-fix-fix branch December 2, 2022 03:49
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants