Update internal ESLint rule (#18766)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-27 22:19:28 +08:00
1 parent 3b93efe979
commit 8ff2c345eb
3 files changed
+26 -4

No files matched your search

@@ -2,7 +2,11 @@ import path from "node:path";
const parentPropertyCheckSelector = [
"FunctionDeclaration",
'[id.name="needsParens"]',
":matches(",
["needsParens", "needsParentheses"]
.map((name) => `[id.name="${name}"]`)
.join(","),
")",
" ",
"BinaryExpression",
":matches(",
@@ -85,6 +89,22 @@ export default {
context.report({
node,
messageId: MESSAGE_ID_KEY_CHECK_FIRST,
fix(fixer) {
const operatorToken = sourceCode.getTokenAfter(
node.left,
(token) =>
token.type === "Punctuator" && token.value === node.operator,
);
const left = sourceCode.text.slice(
node.range[0],
operatorToken.range[0],
);
const right = sourceCode.text.slice(
operatorToken.range[1],
node.range[1],
);
return fixer.replaceText(node, `${right} ${node.operator} ${left}`);
},
});
},
};
@@ -37,7 +37,9 @@ test("better-parent-property-check-in-parentheses-check", {
valid: ["function needsParens() {return parent.test === node;}"],
invalid: [
{
code: 'return parent.type === "MemberExpression" && key === "object";',
code: 'return (( (( parent.type === "MemberExpression" )) && ((key === "object")) ));',
output:
'return (( ((key === "object")) && (( parent.type === "MemberExpression" )) ));',
errors: [{ message: "`key` comparison should be on left side." }],
},
{