Skip to content

Commit

Permalink
feat: add new allowParensAfterComment option to the `no-extra-paren…
Browse files Browse the repository at this point in the history
…s` rule
  • Loading branch information
snitin315 committed Nov 19, 2022
1 parent b8769f1 commit c0de43c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/no-extra-parens.js
Expand Up @@ -52,7 +52,8 @@ module.exports = {
enforceForArrowConditionals: { type: "boolean" },
enforceForSequenceExpressions: { type: "boolean" },
enforceForNewInMemberExpressions: { type: "boolean" },
enforceForFunctionPrototypeMethods: { type: "boolean" }
enforceForFunctionPrototypeMethods: { type: "boolean" },
allowParensAfterComment: { type: "boolean" }
},
additionalProperties: false
}
Expand Down Expand Up @@ -86,6 +87,7 @@ module.exports = {
context.options[1].enforceForNewInMemberExpressions === false;
const IGNORE_FUNCTION_PROTOTYPE_METHODS = ALL_NODES && context.options[1] &&
context.options[1].enforceForFunctionPrototypeMethods === false;
const ALLOW_PARENS_AFTER_COMMENT = ALL_NODES && context.options[1] && context.options[1].allowParensAfterComment === true;

const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
Expand Down Expand Up @@ -402,6 +404,10 @@ module.exports = {
if (isIIFE(node) && !isParenthesised(node.callee)) {
return;
}

if (ALLOW_PARENS_AFTER_COMMENT && isParenthesised(node) && sourceCode.getCommentsBefore(leftParenToken).length > 0) {
return;
}
}

/**
Expand Down
94 changes: 94 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -735,6 +735,43 @@ ruleTester.run("no-extra-parens", rule, {
code: "var foo = (function(){}?.call())",
options: ["all", { enforceForFunctionPrototypeMethods: false }],
parserOptions: { ecmaVersion: 2020 }
},

// "allowParensAfterComment" option
{
code: "const span = /**@type {HTMLSpanElement}*/(event.currentTarget);",
options: ["all", { allowParensAfterComment: true }],
parserOptions: { ecmaVersion: 2020 }
},
{
code: "if (/** @type {Compiler | MultiCompiler} */(options).hooks) console.log('good');",
options: ["all", { allowParensAfterComment: true }],
parserOptions: { ecmaVersion: 2020 }
},
{
code: `
validate(/** @type {Schema} */ (schema), options, {
name: "Dev Server",
baseDataPath: "options",
});
`,
options: ["all", { allowParensAfterComment: true }],
parserOptions: { ecmaVersion: 2020 }
},
{
code: `
if (condition) {
/** @type {ServerOptions} */
(options.server.options).requestCert = false;
}
`,
options: ["all", { allowParensAfterComment: true }],
parserOptions: { ecmaVersion: 2020 }
},
{
code: "const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));",
options: ["all", { allowParensAfterComment: true }],
parserOptions: { ecmaVersion: 2020 }
}
],

Expand Down Expand Up @@ -3187,6 +3224,63 @@ ruleTester.run("no-extra-parens", rule, {
errors: [{ messageId: "unexpected" }]
},

// "allowParensAfterComment" option
{
code: "const span = /**@type {HTMLSpanElement}*/(event.currentTarget);",
output: "const span = /**@type {HTMLSpanElement}*/event.currentTarget;",
options: ["all", { allowParensAfterComment: false }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpected" }]
},
{
code: "if (/** @type {Compiler | MultiCompiler} */(options).hooks) console.log('good');",
output: "if (/** @type {Compiler | MultiCompiler} */options.hooks) console.log('good');",
options: ["all", { allowParensAfterComment: false }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpected" }]
},
{
code: `
validate(/** @type {Schema} */ (schema), options, {
name: "Dev Server",
baseDataPath: "options",
});
`,
output: `
validate(/** @type {Schema} */ schema, options, {
name: "Dev Server",
baseDataPath: "options",
});
`,
options: ["all", { allowParensAfterComment: false }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpected" }]
},
{
code: `
if (condition) {
/** @type {ServerOptions} */
(options.server.options).requestCert = false;
}
`,
output: `
if (condition) {
/** @type {ServerOptions} */
options.server.options.requestCert = false;
}
`,
options: ["all", { allowParensAfterComment: false }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpected" }]
},
{
code: "const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));",
output: "const net = ipaddr.parseCIDR(/** @type {string} */ cidr);",
options: ["all", { allowParensAfterComment: false }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpected" }]
},

// Optional chaining
{
code: "var v = (obj?.aaa)?.aaa",
Expand Down

0 comments on commit c0de43c

Please sign in to comment.