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

feat: fix indent rule for else-if #17318

Merged
merged 2 commits into from Jul 3, 2023
Merged

feat: fix indent rule for else-if #17318

merged 2 commits into from Jul 3, 2023

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Fixes #17316

This bug fix can generate more warnings from the indent rule in existing code, so marked as feat.

What changes did you make? (Give an overview)

Removed a condition that causes indentation not to be applied to if in else-if positions.

Is there anything you'd like reviewers to focus on?

Please check if this change could cause any unwanted behavior. All existing tests are passing after this change, so I'm not sure which cases this condition was targeting. The condition was added in cc53481. I tried removing the same condition in that version, and some tests from that version are indeed failing, but those tests are still here and are now passing without the condition. I've added a few test cases that target this part of the code and the behavior seems fine to me.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint indent Relates to the `indent` rule labels Jun 28, 2023
@mdjermanovic mdjermanovic requested a review from a team as a code owner June 28, 2023 14:05
Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

I love the new test cases, would appreciate if we can add a few more invalid cases matching the valid ones for completeness.

Comment on lines +13537 to +13543
code: unIndent`
if (foo)
\tif (bar) doSomething();
\telse doSomething();
else
if (bar) doSomething();
else doSomething();
Copy link
Member

Choose a reason for hiding this comment

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

Compared to all the great valid test cases, this single invalid case seems a bit... insufficient? 😅

Would you mind copying a few of them here (and make them invalid of course)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, I'll add more invalid test cases to verify that this works well.

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've added more invalid tests in 9142eb2, please take a look.

@@ -1250,7 +1250,7 @@ module.exports = {

IfStatement(node) {
addBlocklessNodeIndent(node.consequent);
if (node.alternate && node.alternate.type !== "IfStatement") {
Copy link
Member

@BYK BYK Jun 29, 2023

Choose a reason for hiding this comment

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

I was able to trace this logic all the way back to the big rewrite of this rule so I'm guessing it was carried over from broken logic earlier or was an inherent bug in the initial implementation.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. I'm kind of shocked indent works at all given the complexity. Leaving open to see if we want to add more test cases.

Copy link
Member

@fasttime fasttime left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Leaving open while a patch release is pending.

@netlify
Copy link

netlify bot commented Jul 1, 2023

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 9142eb2
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/64a0566a6178560008493097
😎 Deploy Preview https://deploy-preview-17318--docs-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 configuration.

Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

Awesome, thanks!

@mdjermanovic mdjermanovic merged commit d34abe5 into main Jul 3, 2023
22 checks passed
@mdjermanovic mdjermanovic deleted the issue17316 branch July 3, 2023 21:20
@darksabrefr
Copy link

This fix has broken things, see : #17316 (comment)

@djibarian
Copy link

djibarian commented Oct 9, 2023

Yep, indent is broken for else lines. Using 8.44.0 for the time being as 8.45.0 reports plenty of indent errors in my case.

@Rec0iL99
Copy link
Member

Feel free to open an issue reporting this. Thanks.

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Dec 31, 2023
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Dec 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly feature This change adds a new feature to ESLint indent Relates to the `indent` rule rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: wrong indent in else-if
7 participants