diff --git a/rules/prefer-single-call.js b/rules/prefer-single-call.js index 3e2e1f4a2f..a5773ac059 100644 --- a/rules/prefer-single-call.js +++ b/rules/prefer-single-call.js @@ -12,9 +12,6 @@ const messages = { [SUGGESTION]: 'Merge with previous one.', }; -const isExpressionStatement = node => - node?.parent.type === 'ExpressionStatement' - && node.parent.expression === node; const isClassList = node => isMemberExpression(node, { property: 'classList', optional: false, @@ -65,10 +62,7 @@ const cases = [ optional: false, }), }, -].map(problematicalCase => ({ - ...problematicalCase, - test: callExpression => problematicalCase.test(callExpression) && isExpressionStatement(callExpression), -})); +]; function create(context) { const { @@ -82,7 +76,7 @@ function create(context) { return { * CallExpression(secondCall) { for (const {description, test, ignore = []} of cases) { - if (!test(secondCall)) { + if (!test(secondCall) || secondCall.parent.type !== 'ExpressionStatement') { continue; } @@ -91,7 +85,12 @@ function create(context) { continue; } - const firstCall = getPreviousNode(secondCall.parent, sourceCode)?.expression; + const previousNode = getPreviousNode(secondCall.parent, sourceCode); + if (previousNode?.type !== 'ExpressionStatement') { + continue; + } + + const firstCall = previousNode.expression; if (!test(firstCall) || !isSameReference(firstCall.callee, secondCall.callee)) { continue; }