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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/indent.js
Expand Up @@ -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.

if (node.alternate) {
addBlocklessNodeIndent(node.alternate);
}
},
Expand Down
176 changes: 175 additions & 1 deletion tests/lib/rules/indent.js
Expand Up @@ -6361,7 +6361,156 @@ ruleTester.run("indent", rule, {
;[1, 2, 3].forEach(x=>console.log(x))
`,
options: [4]
}
},

// https://github.com/eslint/eslint/issues/17316
{
code: unIndent`
if (foo)
\tif (bar) doSomething();
\telse doSomething();
else
\tif (bar) doSomething();
\telse doSomething();
`,
options: ["tab"]
},
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (bar) doSomething();
else doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (bar)
doSomething();
else doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (bar) doSomething();
else
doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (bar)
doSomething();
else
doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (bar) doSomething();
else doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (bar)
doSomething();
else doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (bar) doSomething();
else
doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (bar)
doSomething();
else
doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (foo)
if (bar) doSomething();
else doSomething();
else
if (bar) doSomething();
else doSomething();

`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (foo)
if (bar) doSomething();
else
if (bar) doSomething();
else doSomething();
else doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (foo) doSomething();
else doSomething();
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (foo) {
doSomething();
}
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else if (foo)
{
doSomething();
}
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (foo) {
doSomething();
}
`,
unIndent`
if (foo)
if (bar) doSomething();
else doSomething();
else
if (foo)
{
doSomething();
}
`
],

invalid: [
Expand Down Expand Up @@ -13381,6 +13530,31 @@ ruleTester.run("indent", rule, {
`,
options: [4],
errors: expectedErrors([4, 0, 4, "Punctuator"])
},

// https://github.com/eslint/eslint/issues/17316
{
code: unIndent`
if (foo)
\tif (bar) doSomething();
\telse doSomething();
else
if (bar) doSomething();
else doSomething();
Comment on lines +13537 to +13543
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.

`,
output: unIndent`
if (foo)
\tif (bar) doSomething();
\telse doSomething();
else
\tif (bar) doSomething();
\telse doSomething();
`,
options: ["tab"],
errors: expectedErrors("tab", [
[5, 1, 0, "Keyword"],
[6, 1, 0, "Keyword"]
])
}
]
});