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): [prefer-nullish-coalescing] ensure ternary fix does not remove parens #9380

Merged
merged 3 commits into from
Jun 24, 2024

Conversation

jakebailey
Copy link
Collaborator

@jakebailey jakebailey commented Jun 17, 2024

PR Checklist

Overview

Parens aren't in the AST, and this fixer's text manipulation did not handle that.

I took the time to move a helper out of another lint rule into a common place. I thought I would modify other lint rules to use this code, but not every lint rule has to do this simple manipulation it seems.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @jakebailey!

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 Jun 17, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 6af7a2c
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6670b17193ec98000844dbb4
😎 Deploy Preview https://deploy-preview-9380--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: 100 (🟢 up 2 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

nx-cloud bot commented Jun 17, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 6af7a2c. 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


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

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.

The implementation looks great to me! Requesting changes on testing and an import path. Thanks! ✨

isParenthesized,
nullThrows,
NullThrowsReasons,
} from '.';
Copy link
Member

Choose a reason for hiding this comment

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

We generally try not to import from '.', as that generates a cycle:

Suggested change
} from '.';
} from '../util';

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can do that, though that's still a cycle; those are the same imports, they both point to index.ts.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, maybe we need to flatten these out...

@@ -333,6 +333,26 @@ x ?? 'foo';
},
],
},
{
code: code.replace(/y/g, '(z = y)'),
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] The changed logic includes beforeCount and afterCount, but there's only one here. Could you add some tests?

  • ((z = y))
  • ( /* */ ( z = y ) /* */ )
  • Maybe, a multiline comment or two?
  • ...whatever you think is also a good idea to test

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I'm afraid it doesn't handle any of those cases. the getTextWithParentheses function I moved isn't fancy enough to handle that.

Is there a helper that already exists to do this that I'm not aware of? Reprinting a node like this sure seems like a common case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe getWrappingFixer? I had looked at that but couldn't reverse engineer how to use it in this application.

Copy link
Member

Choose a reason for hiding this comment

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

That would be the one, yeah. Does its use in strict-boolean-expressions help?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tried it out, and it adds parens in places it shouldn't, and doesn't preserve multiple parens. So, I don't think I have a good idea...

Copy link
Member

Choose a reason for hiding this comment

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

Hmm. Well, I guess you're on your own then. Blurgh.

If there's anything that's an existing bug and would be a lot more work to tackle, I think it's fine as a followup issue. I'll just mark this as ✅ for now.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jun 18, 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 f29150f into typescript-eslint:main Jun 24, 2024
63 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [prefer-nullish-coalescing] quick fix for ternary removes required parens
2 participants