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: indent for async arrow functions (fixes #13497) #13544

Merged
merged 10 commits into from Aug 14, 2020
13 changes: 13 additions & 0 deletions lib/rules/indent.js
Expand Up @@ -1094,6 +1094,19 @@ 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);
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

if (astUtils.isOpeningParenToken(openingParen)) {
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
106 changes: 106 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -5724,6 +5724,77 @@ ruleTester.run("indent", rule, {
`,
options: [4, { MemberExpression: 2 }],
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
const foo = async (arg1,
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
arg2) =>
{
return arg1 + arg2;
}
`,
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
options: [2, { FunctionDeclaration: { parameters: "first" }, FunctionExpression: { parameters: "first" } }]
},
{
code: unIndent`
const a = async b => {}
`,
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 (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 +11544,41 @@ ruleTester.run("indent", rule, {
[6, 4, 0, "Punctuator"],
[7, 4, 0, "Punctuator"]
])
},
{
code: unIndent`
const foo = async (arg1,
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
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"]
])
}
]
});