Skip to content

Commit

Permalink
Fix: indent for async arrow functions (fixes #13497) (#13544)
Browse files Browse the repository at this point in the history
* Fix: indent for async arrow functions (fixes #13497)

* Update: addde open paren check and tests

* Chore: changed variable name to nextToken

* Chore: added tests with comments

* Chore: added one more test case

* Chore: simplified logic

* Chore: tests indentation

* Chore: fixed indentation of the test cases

* Chore: formatting for the test cases

* Chore: using descriptive skip options instead of number
  • Loading branch information
anikethsaha committed Aug 14, 2020
1 parent 28ca339 commit 254990e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/rules/indent.js
Expand Up @@ -1084,16 +1084,17 @@ module.exports = {
},

ArrowFunctionExpression(node) {
const firstToken = sourceCode.getFirstToken(node);
const maybeOpeningParen = sourceCode.getFirstToken(node, { skip: node.async ? 1 : 0 });

if (astUtils.isOpeningParenToken(firstToken)) {
const openingParen = firstToken;
if (astUtils.isOpeningParenToken(maybeOpeningParen)) {
const openingParen = maybeOpeningParen;
const closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken);

parameterParens.add(openingParen);
parameterParens.add(closingParen);
addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
}

addBlocklessNodeIndent(node.body);
},

Expand Down
134 changes: 134 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -5724,6 +5724,105 @@ ruleTester.run("indent", rule, {
`,
options: [4, { MemberExpression: 2 }],
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
const foo = async (arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
},
{
code: unIndent`
const foo = async /* some comments */(arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
},
{
code: unIndent`
const a = async
b => {}
`,
options: [2]
},
{
code: unIndent`
const foo = (arg1,
arg2) => async (arr1,
arr2) =>
{
return arg1 + arg2;
}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
},
{
code: unIndent`
const foo = async (arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2]
},
{
code: unIndent`
const foo = async /*comments*/(arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2]
},
{
code: unIndent`
const foo = async (arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2, { FunctionDeclaration: { parameters: 4 }, FunctionExpression: { parameters: 4 } }]
},
{
code: unIndent`
const foo = (arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2, { FunctionDeclaration: { parameters: 4 }, FunctionExpression: { parameters: 4 } }]
},
{
code: unIndent`
async function fn(ar1,
ar2){}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
},
{
code: unIndent`
async function /* some comments */ fn(ar1,
ar2){}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
},
{
code: unIndent`
async /* some comments */ function fn(ar1,
ar2){}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
}
],

Expand Down Expand Up @@ -11473,6 +11572,41 @@ ruleTester.run("indent", rule, {
[6, 4, 0, "Punctuator"],
[7, 4, 0, "Punctuator"]
])
},
{
code: unIndent`
const foo = async (arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
output: unIndent`
const foo = async (arg1,
arg2) =>
{
return arg1 + arg2;
}
`,
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }],
parserOptions: { ecmaVersion: 2020 },
errors: expectedErrors([
[2, 19, 20, "Identifier"]
])
},
{
code: unIndent`
const a = async
b => {}
`,
output: unIndent`
const a = async
b => {}
`,
options: [2],
errors: expectedErrors([
[2, 0, 1, "Identifier"]
])
}
]
});

0 comments on commit 254990e

Please sign in to comment.