Skip to content

Commit 5ee3915

Browse files
authored
fix: yoda invalid autofix at statement start (#21424)
- Add a semicolon before a flipped comparison that starts an expression statement with ( [ + - a template or a regex when the previous line would otherwise continue into it. - Wrap the flipped comparison in parentheses when it would start a statement or an arrow concise body with {, function, async function or class. Fixes #21421
1 parent 0880666 commit 5ee3915

2 files changed

Lines changed: 430 additions & 5 deletions

File tree

‎lib/rules/yoda.js‎

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,66 @@ function getNormalizedLiteral(node) {
104104
return null;
105105
}
106106

107+
/**
108+
* Checks whether a token starts a function or class expression,
109+
* which would be parsed as a declaration at the start of a statement.
110+
* @param {SourceCode} sourceCode The source code object.
111+
* @param {Token} token The first token of the expression.
112+
* @param {ASTNode} node The node that contains the expression.
113+
* @returns {boolean} Whether the token starts a function or class expression.
114+
*/
115+
function startsFunctionOrClass(sourceCode, token, node) {
116+
if (
117+
token.type === "Keyword" &&
118+
(token.value === "function" || token.value === "class")
119+
) {
120+
return true;
121+
}
122+
123+
const nextToken = sourceCode.getTokenAfter(token);
124+
125+
return (
126+
(token.type === "Identifier" || token.type === "Keyword") &&
127+
token.value === "async" &&
128+
Boolean(nextToken) &&
129+
nextToken.range[1] <= node.range[1] &&
130+
nextToken.type === "Keyword" &&
131+
nextToken.value === "function"
132+
);
133+
}
134+
135+
/**
136+
* Checks whether a token at the start of a line would continue the expression on the previous line.
137+
* @param {Token} token The token to check.
138+
* @returns {boolean} Whether the token can continue the previous expression.
139+
*/
140+
function canContinueExpression(token) {
141+
return (
142+
token.type === "Template" ||
143+
token.type === "RegularExpression" ||
144+
(token.type === "Punctuator" &&
145+
["(", "[", "+", "-"].includes(token.value))
146+
);
147+
}
148+
149+
/**
150+
* Checks whether a token would continue an expression that ends with a postfix `++` or `--`.
151+
* A postfix `++` or `--` ends the statement before `(` or `[`, but `+`, `-` and `/` continue it.
152+
* @param {Token} token The first token of the new expression.
153+
* @param {Token|null} tokenBefore The token before the new expression.
154+
* @returns {boolean} Whether the token would continue the expression.
155+
*/
156+
function continuesPostfixUpdate(token, tokenBefore) {
157+
return (
158+
Boolean(tokenBefore) &&
159+
tokenBefore.type === "Punctuator" &&
160+
(tokenBefore.value === "++" || tokenBefore.value === "--") &&
161+
(token.type === "RegularExpression" ||
162+
(token.type === "Punctuator" &&
163+
(token.value === "+" || token.value === "-")))
164+
);
165+
}
166+
107167
//------------------------------------------------------------------------------
108168
// Rule Definition
109169
//------------------------------------------------------------------------------
@@ -313,15 +373,44 @@ module.exports = {
313373
suffix = " ";
314374
}
315375

316-
return (
317-
prefix +
376+
let flippedText =
318377
rightText +
319378
textBeforeOperator +
320379
OPERATOR_FLIP_MAP[operatorToken.value] +
321380
textAfterOperator +
322-
leftText +
323-
suffix
324-
);
381+
leftText;
382+
const isStartOfExpressionStatement =
383+
astUtils.isStartOfExpressionStatement(node);
384+
385+
/*
386+
* `{` cannot start an expression statement or the concise body of an arrow function,
387+
* and `function` or `class` cannot start an expression statement or follow `export default`.
388+
*/
389+
const needsParens =
390+
((isStartOfExpressionStatement ||
391+
(tokenBefore &&
392+
astUtils.isKeywordToken(tokenBefore) &&
393+
tokenBefore.value === "default")) &&
394+
startsFunctionOrClass(sourceCode, firstRightToken, node)) ||
395+
((isStartOfExpressionStatement ||
396+
(tokenBefore && astUtils.isArrowToken(tokenBefore))) &&
397+
astUtils.isOpeningBraceToken(firstRightToken));
398+
399+
if (needsParens) {
400+
flippedText = `(${flippedText})`;
401+
}
402+
403+
if (
404+
!prefix &&
405+
isStartOfExpressionStatement &&
406+
(needsParens || canContinueExpression(firstRightToken)) &&
407+
(astUtils.needsPrecedingSemicolon(sourceCode, node) ||
408+
continuesPostfixUpdate(firstRightToken, tokenBefore))
409+
) {
410+
prefix = ";";
411+
}
412+
413+
return prefix + flippedText + suffix;
325414
}
326415

327416
//--------------------------------------------------------------------------

0 commit comments

Comments
 (0)