Skip to content

Commit

Permalink
Fix: add parens when in operator (fixes eslint#11849)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Apr 25, 2020
1 parent fdfa364 commit 870ce88
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/rules/arrow-body-style.js
Expand Up @@ -99,6 +99,24 @@ module.exports = {
return sourceCode.getTokenAfter(node);
}

/**
* check if node has `in` operator or not
* @param {Node} node The argument node which needs to be checked
* @returns {boolean} `true` if there is an `in` operator else `false`.
*/
function isIn(node) {
if (
node.type === "BinaryExpression" ||
node.type === "LogicalExpression"
) {
if (node.operator === "in") {
return true;
}
return isIn(node.left) || isIn(node.right);
}
return false;
}

/**
* Determines whether a arrow function body needs braces
* @param {ASTNode} node The arrow function node.
Expand Down Expand Up @@ -157,6 +175,8 @@ module.exports = {
sourceCode.commentsExistBetween(openingBrace, firstValueToken) ||
sourceCode.commentsExistBetween(lastValueToken, closingBrace);

const isInOp = isIn(blockBody[0].argument);

/*
* Remove tokens around the return value.
* If comments don't exist, remove extra spaces as well.
Expand All @@ -178,7 +198,7 @@ module.exports = {
* If the first token of the reutrn value is `{` or the return value is a sequence expression,
* enclose the return value by parentheses to avoid syntax error.
*/
if (astUtils.isOpeningBraceToken(firstValueToken) || blockBody[0].argument.type === "SequenceExpression") {
if (astUtils.isOpeningBraceToken(firstValueToken) || blockBody[0].argument.type === "SequenceExpression" || isInOp) {
fixes.push(
fixer.insertTextBefore(firstValueToken, "("),
fixer.insertTextAfter(lastValueToken, ")")
Expand Down
44 changes: 44 additions & 0 deletions tests/lib/rules/arrow-body-style.js
Expand Up @@ -45,6 +45,50 @@ ruleTester.run("arrow-body-style", rule, {
{ code: "var foo = () => { return { bar: 0 }; };", options: ["as-needed", { requireReturnForObjectLiteral: true }] }
],
invalid: [
{
code: "for (let a = (b, c, d) => { return vb && c in d; }; ;);",
output: "for (let a = (b, c, d) => (vb && c in d); ;);",
errors: [
{
line: 1,
column: 27,
messageId: "unexpectedSingleBlock"
}
]
},
{
code: "for (let a = (b, c, d) => { return v in b && c in d; }; ;);",
output: "for (let a = (b, c, d) => (v in b && c in d); ;);",
errors: [
{
line: 1,
column: 27,
messageId: "unexpectedSingleBlock"
}
]
},
{
code: "for (let a = (b, c, d) => { return vb in dd ; }; ;);",
output: "for (let a = (b, c, d) => (vb in dd ); ;);",
errors: [
{
line: 1,
column: 27,
messageId: "unexpectedSingleBlock"
}
]
},
{
code: "for (let a = (b, c, d) => { return vb in c in dd ; }; ;);",
output: "for (let a = (b, c, d) => (vb in c in dd ); ;);",
errors: [
{
line: 1,
column: 27,
messageId: "unexpectedSingleBlock"
}
]
},
{
code: "var foo = () => 0",
output: "var foo = () => {return 0}",
Expand Down

0 comments on commit 870ce88

Please sign in to comment.