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: indent false positive with multi-line await expression. #8837
Conversation
Thanks for the pull request, @aladdin-add! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
@aladdin-add, thanks for your PR! By analyzing the history of the files in this pull request, we identified @not-an-aardvark, @vitorbal and @gyandeeps to be potential reviewers. |
Thanks for the pull request, @aladdin-add! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
…tructuring assignment
Thanks for the pull request, @aladdin-add! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
Thanks for the pull request, @aladdin-add! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
lib/rules/indent.js
Outdated
offsets.matchIndentOf(sourceCode.getTokenBefore(openingParen), openingParen); | ||
|
||
// https://github.com/eslint/eslint/issues/8815 | ||
const tokenBefore = sourceCode.getTokenBefore(openingParen, node.parent.type === "AwaitExpression" ? astUtils.isKeywordToken : void 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see tokenBefore
is not a good name, someone help me~~ 🆘
Hi @aladdin-add, sorry about the delay in reviewing this. I'm planning to test it out tonight or tomorrow and give some feedback then. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I left some suggestions.
tests/lib/rules/indent.js
Outdated
} | ||
` | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for these cases too?
{
code: unIndent`
function* test() {
const {
foo,
bar,
} = yield doSomethingAsync(
1,
2,
3,
);
}
`
},
{
code: unIndent`
({
a: b
} = +foo(
bar
));
`
},
{
code: unIndent`
const {
foo,
bar,
} = typeof foo(
1,
2,
3,
);
`
},
{
code: unIndent`
const {
foo,
bar,
} = +(
foo
);
`
},
lib/rules/indent.js
Outdated
offsets.matchIndentOf(sourceCode.getTokenBefore(openingParen), openingParen); | ||
|
||
// https://github.com/eslint/eslint/issues/8815 | ||
const tokenBefore = sourceCode.getTokenBefore(openingParen, node.parent.type === "AwaitExpression" ? astUtils.isKeywordToken : void 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this fix is quite correct. I think the problem is actually with the logic for AssignmentExpression
and VariableDeclaration
nodes, not with CallExpression
nodes -- the from the report in #8815 is just a special case that happens to include AwaitExpression
and a CallExpression
.
For example, the following code is also an error due to this bug, but it's not fixed:
const {
foo
} = typeof foo(
1
);
Instead of modifying the logic for CallExpression
s, I think the following fix solves the problem:
diff --git a/lib/rules/indent.js b/lib/rules/indent.js
index 6babe29c..fafd1292 100644
--- a/lib/rules/indent.js
+++ b/lib/rules/indent.js
@@ -1030,7 +1030,7 @@ module.exports = {
const nodeTokens = getTokensAndComments(node);
const tokensFromOperator = nodeTokens.slice(lodash.sortedIndexBy(nodeTokens, operator, token => token.range[0]));
- offsets.setDesiredOffsets(tokensFromOperator, sourceCode.getFirstToken(node.left), 1);
+ offsets.setDesiredOffsets(tokensFromOperator, sourceCode.getLastToken(node.left), 1);
offsets.ignoreToken(tokensFromOperator[0]);
offsets.ignoreToken(tokensFromOperator[1]);
},
@@ -1325,12 +1325,12 @@ module.exports = {
VariableDeclarator(node) {
if (node.init) {
const equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken);
- const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator);
+ const tokensAfterOperator = sourceCode.getTokensAfter(equalOperator, token => token.range[1] <= node.range[1]);
offsets.ignoreToken(equalOperator);
- offsets.ignoreToken(tokenAfterOperator);
- offsets.matchIndentOf(equalOperator, tokenAfterOperator);
- offsets.matchIndentOf(sourceCode.getFirstToken(node), equalOperator);
+ offsets.ignoreToken(tokensAfterOperator[0]);
+ offsets.setDesiredOffsets(tokensAfterOperator, equalOperator, 1);
+ offsets.matchIndentOf(sourceCode.getLastToken(node.id), equalOperator);
}
},
There are two parts to the fix:
-
With
AssignmentExpression
nodes, the equals sign should be offset from the last token of theAssignmentExpression
, not the first token. This prevents token-collapsing bugs like this one. -
With
VariableDeclarator
nodes, all tokens should be indented from the=
token, not just the first token after the=
token. The problem before was that in a case like this:var { } = await foo( )
Only the
await
token's indentation was getting set to be aligned with the}
, but thefoo
token's indentation was not getting aligned properly, so thefoo()
expression was offset from}
when it should have been aligned instead.
Thanks for the pull request, @aladdin-add! I took a look to make sure it's ready for merging and found some changes are needed:
Can you please update the pull request to address these? (More information can be found in our pull request guide.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Thanks for contributing! |
…tructuring assignment
What is the purpose of this pull request? (put an "X" next to item)
[x] Bug fix (template)
What changes did you make? (Give an overview)
fixes #8815
Is there anything you'd like reviewers to focus on?