Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions rules/prefer-single-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,10 +62,7 @@ const cases = [
optional: false,
}),
},
].map(problematicalCase => ({
...problematicalCase,
test: callExpression => problematicalCase.test(callExpression) && isExpressionStatement(callExpression),
}));
];

function create(context) {
const {
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down
Loading