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: arrow-parens no reporting for comments inside (fixes #12995) #13312

Merged
merged 8 commits into from May 22, 2020
38 changes: 38 additions & 0 deletions lib/rules/arrow-parens.js
Expand Up @@ -105,6 +105,44 @@ module.exports = {
], `${shouldAddSpaceForAsync ? " " : ""}${paramToken.value}`);
}

/**
* Checks whether there are comments inside the params or not.
* @param {ASTNode} paramNode node to check
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
* @returns {boolean} `true` if there are comments inside of braces, else `false`
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
*/
function isCommentsInParens(paramNode) {
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

if (paramNode.params.length !== 1) {
return false;
}
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

if (!astUtils.isOpeningParenToken(firstTokenOfParam)) {
return false;
}

if (sourceCode.getCommentsBefore(paramNode.params[0]).length > 0) {
return true;
}

if (sourceCode.getCommentsAfter(paramNode.params[0]).length > 0) {
return true;
}

const commaToken = sourceCode.getTokenAfter(paramNode.params[0]);

if (commaToken.type === "Punctuator" && commaToken.value === ",") {
if (sourceCode.getCommentsAfter(commaToken).length > 0) {
return true;
}
}

return false;
}
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

if (isCommentsInParens(node)) {
return;
}

// "as-needed", { "requireForBlockBody": true }: x => x
if (
requireForBlockBody &&
Expand Down
113 changes: 112 additions & 1 deletion tests/lib/rules/arrow-parens.js
Expand Up @@ -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)
Expand Down Expand Up @@ -68,7 +74,61 @@ 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"]
},
{
code: "var foo = (a,/**/) => b;",
parserOptions: { ecmaVersion: 2017 },
options: ["as-needed"]
},
{
code: "var foo = (a , /**/) => b;",
parserOptions: { ecmaVersion: 2017 },
options: ["as-needed"]
},
{
code: "var foo = (a\n,\n/**/) => b;",
parserOptions: { ecmaVersion: 2017 },
options: ["as-needed"]
},
{
code: "var foo = (a,//\n) => b;",
parserOptions: { ecmaVersion: 2017 },
options: ["as-needed"]
},
{
code: "const i = (a/**/,) => a + a;",
parserOptions: { ecmaVersion: 2017 },
options: ["as-needed"]
},
{
code: "const i = (a \n /**/,) => a + a;",
parserOptions: { ecmaVersion: 2017 },
options: ["as-needed"]
}
];

const type = "ArrowFunctionExpression";
Expand Down Expand Up @@ -271,7 +331,58 @@ 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
}]
},
{
code: "var foo = /**/ a => b;",
output: "var foo = /**/ (a) => b;",
errors: [{
line: 1,
column: 16,
type: "ArrowFunctionExpression",
messageId: "expectedParens",
endLine: 1,
endColumn: 17
}]
},
{
code: "var bar = a /**/ => b;",
output: "var bar = (a) /**/ => b;",
errors: [{
line: 1,
column: 11,
type: "ArrowFunctionExpression",
messageId: "expectedParens",
endLine: 1,
endColumn: 12
}]
}

];

ruleTester.run("arrow-parens", rule, {
Expand Down