From da78fa11632a2908db4ac494012a16f5d5a88a64 Mon Sep 17 00:00:00 2001 From: Michal Dziekonski Date: Tue, 6 Oct 2020 15:03:03 +0200 Subject: [PATCH] Update: support async arrow fn in function-paren-newline (fixes #13728) (#13729) --- lib/rules/function-paren-newline.js | 2 +- tests/lib/rules/function-paren-newline.js | 369 ++++++++++++++++++++++ 2 files changed, 370 insertions(+), 1 deletion(-) diff --git a/lib/rules/function-paren-newline.js b/lib/rules/function-paren-newline.js index 894c8e331a7..9d8d67ba141 100644 --- a/lib/rules/function-paren-newline.js +++ b/lib/rules/function-paren-newline.js @@ -218,7 +218,7 @@ module.exports = { } case "ArrowFunctionExpression": { - const firstToken = sourceCode.getFirstToken(node); + const firstToken = sourceCode.getFirstToken(node, { skip: (node.async ? 1 : 0) }); if (!astUtils.isOpeningParenToken(firstToken)) { diff --git a/tests/lib/rules/function-paren-newline.js b/tests/lib/rules/function-paren-newline.js index f918a38ba83..0c9feb7a789 100644 --- a/tests/lib/rules/function-paren-newline.js +++ b/tests/lib/rules/function-paren-newline.js @@ -87,6 +87,23 @@ ruleTester.run("function-paren-newline", rule, { code: "function baz(foo, bar) {}", options: ["multiline"] }, + { + code: "async (foo, bar) => {};", + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo, + bar + ) => {}; + `, + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async foo => {};", + parserOptions: { ecmaVersion: 2017 } + }, { code: "import(source)", parserOptions: { ecmaVersion: 2020 } @@ -249,6 +266,40 @@ ruleTester.run("function-paren-newline", rule, { code: "new (Foo)", options: ["multiline-arguments"] }, + { + code: "async (foo, bar) => {};", + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async (foo) => {};", + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo + ) => {}; + `, + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo, + bar + ) => {}; + `, + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async foo => {};", + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 } + }, { code: "import(source)", options: ["multiline-arguments"], @@ -329,6 +380,30 @@ ruleTester.run("function-paren-newline", rule, { `, options: ["always"] }, + { + code: ` + async ( + foo + ) => {}; + `, + options: ["always"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo, + bar + ) => {}; + `, + options: ["always"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async foo => {};", + options: ["always"], + parserOptions: { ecmaVersion: 2017 } + }, { code: "import(\n source\n)", options: ["always"], @@ -360,6 +435,16 @@ ruleTester.run("function-paren-newline", rule, { code: "function baz() {}", options: ["never"] }, + { + code: "async (foo, bar) => {};", + options: ["never"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async foo => {};", + options: ["never"], + parserOptions: { ecmaVersion: 2017 } + }, { code: "import(source)", options: ["never"], @@ -391,6 +476,27 @@ ruleTester.run("function-paren-newline", rule, { code: "baz(foo, bar);", options: [{ minItems: 3 }] }, + { + code: "async (foo, bar) => {};", + options: [{ minItems: 3 }], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo, + bar, + baz + ) => {}; + `, + options: [{ minItems: 3 }], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async foo => {};", + options: [{ minItems: 3 }], + parserOptions: { ecmaVersion: 2017 } + }, { code: "import(source)", options: [{ minItems: 3 }], @@ -431,6 +537,43 @@ ruleTester.run("function-paren-newline", rule, { `, options: ["consistent"] }, + { + code: "async (foo, bar) => {};", + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: "async foo => {};", + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async (foo, + bar) => {}; + `, + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo, bar + ) => {}; + `, + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 } + }, + { + code: ` + async ( + foo, + bar + ) => {}; + `, + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 } + }, { code: "import(source)", options: ["consistent"], @@ -564,6 +707,55 @@ ruleTester.run("function-paren-newline", rule, { output: null, errors: [RIGHT_UNEXPECTED_ERROR] }, + { + code: ` + async ( + foo, bar + ) => {}; + `, + output: ` + async (foo, bar) => {}; + `, + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_UNEXPECTED_ERROR, RIGHT_UNEXPECTED_ERROR] + }, + { + code: ` + async (foo, bar + ) => {}; + `, + output: ` + async (foo, bar) => {}; + `, + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_UNEXPECTED_ERROR] + }, + { + code: ` + async (foo, + bar) => {}; + `, + output: ` + async (\nfoo, + bar\n) => {}; + `, + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_MISSING_ERROR, RIGHT_MISSING_ERROR] + }, + { + code: ` + async ( + foo, + bar) => {}; + `, + output: ` + async ( + foo, + bar\n) => {}; + `, + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_MISSING_ERROR] + }, { code: "import(\n source\n)", output: "import(source)", @@ -757,6 +949,46 @@ ruleTester.run("function-paren-newline", rule, { options: ["multiline-arguments"], errors: [RIGHT_UNEXPECTED_ERROR] }, + { + code: ` + async (foo, bar + ) => {}; + `, + output: ` + async (foo, bar) => {}; + `, + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_UNEXPECTED_ERROR] + }, + { + code: ` + async (foo, + bar) => {}; + `, + output: ` + async (\nfoo, + bar\n) => {}; + `, + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_MISSING_ERROR, RIGHT_MISSING_ERROR] + }, + { + code: ` + async ( + foo, + bar) => {}; + `, + output: ` + async ( + foo, + bar\n) => {}; + `, + options: ["multiline-arguments"], + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_MISSING_ERROR] + }, { code: "import(source\n)", output: "import(source)", @@ -849,6 +1081,45 @@ ruleTester.run("function-paren-newline", rule, { options: ["always"], errors: [LEFT_MISSING_ERROR, RIGHT_MISSING_ERROR] }, + { + code: ` + async (foo, bar) => {}; + `, + output: ` + async (\nfoo, bar\n) => {}; + `, + options: ["always"], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_MISSING_ERROR, RIGHT_MISSING_ERROR] + }, + { + code: ` + async (foo, + bar) => {}; + `, + output: ` + async (\nfoo, + bar\n) => {}; + `, + options: ["always"], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_MISSING_ERROR, RIGHT_MISSING_ERROR] + }, + { + code: ` + async ( + foo, + bar) => {}; + `, + output: ` + async ( + foo, + bar\n) => {}; + `, + options: ["always"], + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_MISSING_ERROR] + }, { code: "import(source)", output: "import(\nsource\n)", @@ -965,6 +1236,35 @@ ruleTester.run("function-paren-newline", rule, { options: ["never"], errors: [LEFT_UNEXPECTED_ERROR, RIGHT_UNEXPECTED_ERROR] }, + { + code: ` + async ( + foo, + bar + ) => {}; + `, + output: ` + async (foo, + bar) => {}; + `, + options: ["never"], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_UNEXPECTED_ERROR, RIGHT_UNEXPECTED_ERROR] + }, + { + code: ` + async ( + foo, + bar) => {}; + `, + output: ` + async (foo, + bar) => {}; + `, + options: ["never"], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_UNEXPECTED_ERROR] + }, { code: "import(\n source\n)", output: "import(source)", @@ -1012,6 +1312,46 @@ ruleTester.run("function-paren-newline", rule, { options: [{ minItems: 3 }], errors: [LEFT_UNEXPECTED_ERROR, RIGHT_UNEXPECTED_ERROR] }, + { + code: ` + async ( + foo, + bar + ) => {}; + `, + output: ` + async (foo, + bar) => {}; + `, + options: [{ minItems: 3 }], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_UNEXPECTED_ERROR, RIGHT_UNEXPECTED_ERROR] + }, + { + code: ` + async ( + foo, + bar) => {}; + `, + output: ` + async (foo, + bar) => {}; + `, + options: [{ minItems: 3 }], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_UNEXPECTED_ERROR] + }, + { + code: ` + async (foo, bar, baz) => {}; + `, + output: ` + async (\nfoo, bar, baz\n) => {}; + `, + options: [{ minItems: 3 }], + parserOptions: { ecmaVersion: 2017 }, + errors: [LEFT_MISSING_ERROR, RIGHT_MISSING_ERROR] + }, { code: "import(\n source\n)", output: "import(source)", @@ -1055,6 +1395,35 @@ ruleTester.run("function-paren-newline", rule, { options: ["consistent"], errors: [RIGHT_UNEXPECTED_ERROR] }, + { + code: ` + async ( + foo, + bar) => {}; + `, + output: ` + async ( + foo, + bar\n) => {}; + `, + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_MISSING_ERROR] + }, + { + code: ` + async (foo, + bar + ) => {}; + `, + output: ` + async (foo, + bar) => {}; + `, + options: ["consistent"], + parserOptions: { ecmaVersion: 2017 }, + errors: [RIGHT_UNEXPECTED_ERROR] + }, { code: "import(source\n)", output: "import(source)",