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: fix indent errors on multiline destructure #8756

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 1 addition & 2 deletions lib/rules/indent.js
Expand Up @@ -658,7 +658,6 @@ module.exports = {
while (astUtils.isOpeningParenToken(token) && token !== startToken) {
token = sourceCode.getTokenBefore(token);
}

return sourceCode.getTokenAfter(token);
}

Expand All @@ -675,7 +674,6 @@ module.exports = {
if (offset === "first" && elements.length && !elements[0]) {
return;
}

elements.forEach((element, index) => {
if (offset === "off") {
offsets.ignoreToken(getFirstToken(element));
Expand Down Expand Up @@ -1242,6 +1240,7 @@ module.exports = {
offsets.ignoreToken(equalOperator);
offsets.ignoreToken(tokenAfterOperator);
offsets.matchIndentOf(equalOperator, tokenAfterOperator);
Copy link
Member

Choose a reason for hiding this comment

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

I think the root cause of the problem is that the = token's indentation is not set correctly in the VariableDeclarator listener. By design, offsets.ignoreToken is called on the token, but this is a no-op if the token is not the first of its line. So the = token ends up with an offset of 1 from the start of the declarator, and then the value on the right of the equals sign is matched against the indentation of the equals sign. As a result, the rule indents value on the right of the equals sign by 1 from the start of the declarator, when it should be indented by 0.

I think this can be fixed by explicitly specifying the indentation of the = token relative to the declarator. Applying the following diff to the original version of the file seems to make everything work correctly and fixes the bug:

diff --git a/lib/rules/indent.js b/lib/rules/indent.js
index 5a8feee8..4b4355b3 100644
--- a/lib/rules/indent.js
+++ b/lib/rules/indent.js
@@ -1242,6 +1242,7 @@ module.exports = {
                     offsets.ignoreToken(equalOperator);
                     offsets.ignoreToken(tokenAfterOperator);
                     offsets.matchIndentOf(equalOperator, tokenAfterOperator);
+                    offsets.matchIndentOf(sourceCode.getFirstToken(node), equalOperator);
                 }
             },
 

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for the explanation. That makes sense

offsets.matchIndentOf(sourceCode.getFirstToken(node), equalOperator);
}
},

Expand Down
50 changes: 50 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -2026,6 +2026,38 @@ ruleTester.run("indent", rule, {
`,
options: [2]
},
{
code: unIndent`
const {
a
}
=
{
a: 1
}
`,
options: [2]
},
{
code: unIndent`
const {
a
} = {
a: 1
}
`,
options: [2]
},
{
code: unIndent`
const [
a
] = [
1
]
`,
options: [2]
},
{

// https://github.com/eslint/eslint/issues/7233
Expand Down Expand Up @@ -6834,6 +6866,24 @@ ruleTester.run("indent", rule, {
options: [2],
errors: expectedErrors([[2, 2, 0, "Identifier"], [4, 2, 4, "Identifier"], [5, 2, 6, "Identifier"], [6, 0, 2, "Punctuator"]])
},
{
code: unIndent`
const {
a
} = {
a: 1
}
`,
output: unIndent`
const {
a
} = {
a: 1
}
`,
options: [2],
errors: expectedErrors([[4, 2, 4, "Identifier"], [5, 0, 2, "Punctuator"]])
},
{
code: unIndent`
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add tests to make sure these are valid?

const
    {
        a
    } = {
        a: 1
    };
const
    foo = {
        bar: 1
    }

var foo = [
Expand Down