From 39e475353955a530b07f8c113d3c2ee13564dd7d Mon Sep 17 00:00:00 2001 From: Anix Date: Sat, 16 May 2020 12:05:02 +0000 Subject: [PATCH] Fix: no reportin for comments inside (fixes #12995) --- lib/rules/arrow-parens.js | 8 +++++ tests/lib/rules/arrow-parens.js | 58 ++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/rules/arrow-parens.js b/lib/rules/arrow-parens.js index dc3c3825791..a3f5bfc6666 100644 --- a/lib/rules/arrow-parens.js +++ b/lib/rules/arrow-parens.js @@ -105,6 +105,14 @@ module.exports = { ], `${shouldAddSpaceForAsync ? " " : ""}${paramToken.value}`); } + if ( + node.params.length === 1 && + (sourceCode.getCommentsBefore(node.params[0]).length > 0 || + sourceCode.getCommentsAfter(node.params[0]).length > 0) + ) { + return; + } + // "as-needed", { "requireForBlockBody": true }: x => x if ( requireForBlockBody && diff --git a/tests/lib/rules/arrow-parens.js b/tests/lib/rules/arrow-parens.js index 3e796c1ec3c..51cd22776d6 100644 --- a/tests/lib/rules/arrow-parens.js +++ b/tests/lib/rules/arrow-parens.js @@ -29,6 +29,12 @@ const valid = [ "(a) => {\n}", "a.then((foo) => {});", "a.then((foo) => { if (true) {}; });", + "const f = (/* */a) => a + a;", + "const f = (a/** */) => a + a;", + "const f = (a//\n) => a + a;", + "const f = (//\na) => a + a;", + "const f = (/*\n */a//\n) => a + a;", + "const f = (/** @type {number} */a/**hello*/) => a + a;", { code: "a.then(async (foo) => { if (true) {}; });", parserOptions: { ecmaVersion: 8 } }, // "always" (explicit) @@ -68,7 +74,31 @@ const valid = [ { code: "async a => ({})", options: ["as-needed", { requireForBlockBody: true }], parserOptions: { ecmaVersion: 8 } }, { code: "async a => a", options: ["as-needed", { requireForBlockBody: true }], parserOptions: { ecmaVersion: 8 } }, { code: "(a: T) => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("identifer-type") }, - { code: "(a): T => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("return-type") } + { code: "(a): T => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("return-type") }, + { + code: "const f = (/** @type {number} */a/**hello*/) => a + a;", + options: ["as-needed"] + }, + { + code: "const f = (/* */a) => a + a;", + options: ["as-needed"] + }, + { + code: "const f = (a/** */) => a + a;", + options: ["as-needed"] + }, + { + code: "const f = (a//\n) => a + a;", + options: ["as-needed"] + }, + { + code: "const f = (//\na) => a + a;", + options: ["as-needed"] + }, + { + code: "const f = (/*\n */a//\n) => a + a;", + options: ["as-needed"] + } ]; const type = "ArrowFunctionExpression"; @@ -271,6 +301,32 @@ const invalid = [ messageId: "unexpectedParensInline", type }] + }, + { + code: "const f = /** @type {number} */(a)/**hello*/ => a + a;", + options: ["as-needed"], + output: "const f = /** @type {number} */a/**hello*/ => a + a;", + errors: [{ + line: 1, + column: 33, + type, + messageId: "unexpectedParens", + endLine: 1, + endColumn: 34 + }] + }, + { + code: "const f = //\n(a) => a + a;", + output: "const f = //\na => a + a;", + options: ["as-needed"], + errors: [{ + line: 2, + column: 2, + type, + messageId: "unexpectedParens", + endLine: 2, + endColumn: 3 + }] } ];