Skip to content

Commit

Permalink
Fix: indent for async arrow functions (fixes eslint#13497)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Aug 2, 2020
1 parent e8f5289 commit 4e462f5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/rules/indent.js
Expand Up @@ -1094,6 +1094,17 @@ module.exports = {
parameterParens.add(closingParen);
addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
}

if (firstToken.type === "Identifier" && firstToken.value === "async") {
const openingParen = sourceCode.getTokenAfter(firstToken);
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
79 changes: 79 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -5724,6 +5724,64 @@ 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 = (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 (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" } }]
}
],

Expand Down Expand Up @@ -11473,6 +11531,27 @@ 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"]
])
}
]
});

0 comments on commit 4e462f5

Please sign in to comment.