From eac4943ba2e4edb3dbfea0470e5d4b15a4926c40 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Tue, 11 Apr 2023 21:24:28 +0200 Subject: [PATCH] refactor: remove unnecessary use of `SourceCode#getAncestors` in rules (#17075) --- lib/rules/no-lone-blocks.js | 2 +- lib/rules/no-lonely-if.js | 5 ++--- lib/rules/no-unused-expressions.js | 12 +++++------- lib/rules/valid-typeof.js | 2 +- lib/rules/wrap-regex.js | 5 ++--- lib/rules/yoda.js | 2 +- 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/rules/no-lone-blocks.js b/lib/rules/no-lone-blocks.js index 416bbd499c0..c31439b6ae2 100644 --- a/lib/rules/no-lone-blocks.js +++ b/lib/rules/no-lone-blocks.js @@ -76,7 +76,7 @@ module.exports = { return; } - const block = sourceCode.getAncestors(node).pop(); + const block = node.parent; if (loneBlocks[loneBlocks.length - 1] === block) { loneBlocks.pop(); diff --git a/lib/rules/no-lonely-if.js b/lib/rules/no-lonely-if.js index 4abaa6753ba..63a7859825f 100644 --- a/lib/rules/no-lonely-if.js +++ b/lib/rules/no-lonely-if.js @@ -32,9 +32,8 @@ module.exports = { return { IfStatement(node) { - const ancestors = sourceCode.getAncestors(node), - parent = ancestors.pop(), - grandparent = ancestors.pop(); + const parent = node.parent, + grandparent = parent.parent; if (parent && parent.type === "BlockStatement" && parent.body.length === 1 && grandparent && diff --git a/lib/rules/no-unused-expressions.js b/lib/rules/no-unused-expressions.js index 156d8b6f606..0ec4c008b28 100644 --- a/lib/rules/no-unused-expressions.js +++ b/lib/rules/no-unused-expressions.js @@ -70,8 +70,7 @@ module.exports = { allowShortCircuit = config.allowShortCircuit || false, allowTernary = config.allowTernary || false, allowTaggedTemplates = config.allowTaggedTemplates || false, - enforceForJSX = config.enforceForJSX || false, - sourceCode = context.getSourceCode(); + enforceForJSX = config.enforceForJSX || false; /** * Has AST suggesting a directive. @@ -110,12 +109,11 @@ module.exports = { /** * Detect if a Node is a directive. * @param {ASTNode} node any node - * @param {ASTNode[]} ancestors the given node's ancestors * @returns {boolean} whether the given node is considered a directive in its current position */ - function isDirective(node, ancestors) { - const parent = ancestors[ancestors.length - 1], - grandparent = ancestors[ancestors.length - 2]; + function isDirective(node) { + const parent = node.parent, + grandparent = parent.parent; /** * https://tc39.es/ecma262/#directive-prologue @@ -181,7 +179,7 @@ module.exports = { return { ExpressionStatement(node) { - if (Checker.isDisallowed(node.expression) && !isDirective(node, sourceCode.getAncestors(node))) { + if (Checker.isDisallowed(node.expression) && !isDirective(node)) { context.report({ node, messageId: "unusedExpression" }); } } diff --git a/lib/rules/valid-typeof.js b/lib/rules/valid-typeof.js index 3562e653c08..5946b01da83 100644 --- a/lib/rules/valid-typeof.js +++ b/lib/rules/valid-typeof.js @@ -83,7 +83,7 @@ module.exports = { UnaryExpression(node) { if (isTypeofExpression(node)) { - const parent = sourceCode.getAncestors(node).pop(); + const { parent } = node; if (parent.type === "BinaryExpression" && OPERATORS.has(parent.operator)) { const sibling = parent.left === node ? parent.right : parent.left; diff --git a/lib/rules/wrap-regex.js b/lib/rules/wrap-regex.js index 10c388e8f07..6342e5a5087 100644 --- a/lib/rules/wrap-regex.js +++ b/lib/rules/wrap-regex.js @@ -40,10 +40,9 @@ module.exports = { if (nodeType === "RegularExpression") { const beforeToken = sourceCode.getTokenBefore(node); const afterToken = sourceCode.getTokenAfter(node); - const ancestors = sourceCode.getAncestors(node); - const grandparent = ancestors[ancestors.length - 1]; + const { parent } = node; - if (grandparent.type === "MemberExpression" && grandparent.object === node && + if (parent.type === "MemberExpression" && parent.object === node && !(beforeToken && beforeToken.value === "(" && afterToken && afterToken.value === ")")) { context.report({ node, diff --git a/lib/rules/yoda.js b/lib/rules/yoda.js index f3acece47a7..87c10490064 100644 --- a/lib/rules/yoda.js +++ b/lib/rules/yoda.js @@ -343,7 +343,7 @@ module.exports = { ) && !(!isEqualityOperator(node.operator) && onlyEquality) && isComparisonOperator(node.operator) && - !(exceptRange && isRangeTest(sourceCode.getAncestors(node).pop())) + !(exceptRange && isRangeTest(node.parent)) ) { context.report({ node,