From 254990e87914457ca25ea2d7ee012964e56fc9e5 Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 14 Aug 2020 08:02:20 +0530 Subject: [PATCH] Fix: indent for async arrow functions (fixes #13497) (#13544) * 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 --- lib/rules/indent.js | 7 +- tests/lib/rules/indent.js | 134 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 22b633845b5..1c0dccc5c98 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -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); }, diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index b253be22067..ad35ad09c70 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -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" } }] } ], @@ -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"] + ]) } ] });