Skip to content

Commit eac4943

Browse files
authored
refactor: remove unnecessary use of SourceCode#getAncestors in rules (#17075)
1 parent 6d8bffd commit eac4943

File tree

6 files changed

+12
-16
lines changed

6 files changed

+12
-16
lines changed

lib/rules/no-lone-blocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module.exports = {
7676
return;
7777
}
7878

79-
const block = sourceCode.getAncestors(node).pop();
79+
const block = node.parent;
8080

8181
if (loneBlocks[loneBlocks.length - 1] === block) {
8282
loneBlocks.pop();

lib/rules/no-lonely-if.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ module.exports = {
3232

3333
return {
3434
IfStatement(node) {
35-
const ancestors = sourceCode.getAncestors(node),
36-
parent = ancestors.pop(),
37-
grandparent = ancestors.pop();
35+
const parent = node.parent,
36+
grandparent = parent.parent;
3837

3938
if (parent && parent.type === "BlockStatement" &&
4039
parent.body.length === 1 && grandparent &&

lib/rules/no-unused-expressions.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ module.exports = {
7070
allowShortCircuit = config.allowShortCircuit || false,
7171
allowTernary = config.allowTernary || false,
7272
allowTaggedTemplates = config.allowTaggedTemplates || false,
73-
enforceForJSX = config.enforceForJSX || false,
74-
sourceCode = context.getSourceCode();
73+
enforceForJSX = config.enforceForJSX || false;
7574

7675
/**
7776
* Has AST suggesting a directive.
@@ -110,12 +109,11 @@ module.exports = {
110109
/**
111110
* Detect if a Node is a directive.
112111
* @param {ASTNode} node any node
113-
* @param {ASTNode[]} ancestors the given node's ancestors
114112
* @returns {boolean} whether the given node is considered a directive in its current position
115113
*/
116-
function isDirective(node, ancestors) {
117-
const parent = ancestors[ancestors.length - 1],
118-
grandparent = ancestors[ancestors.length - 2];
114+
function isDirective(node) {
115+
const parent = node.parent,
116+
grandparent = parent.parent;
119117

120118
/**
121119
* https://tc39.es/ecma262/#directive-prologue
@@ -181,7 +179,7 @@ module.exports = {
181179

182180
return {
183181
ExpressionStatement(node) {
184-
if (Checker.isDisallowed(node.expression) && !isDirective(node, sourceCode.getAncestors(node))) {
182+
if (Checker.isDisallowed(node.expression) && !isDirective(node)) {
185183
context.report({ node, messageId: "unusedExpression" });
186184
}
187185
}

lib/rules/valid-typeof.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module.exports = {
8383

8484
UnaryExpression(node) {
8585
if (isTypeofExpression(node)) {
86-
const parent = sourceCode.getAncestors(node).pop();
86+
const { parent } = node;
8787

8888
if (parent.type === "BinaryExpression" && OPERATORS.has(parent.operator)) {
8989
const sibling = parent.left === node ? parent.right : parent.left;

lib/rules/wrap-regex.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ module.exports = {
4040
if (nodeType === "RegularExpression") {
4141
const beforeToken = sourceCode.getTokenBefore(node);
4242
const afterToken = sourceCode.getTokenAfter(node);
43-
const ancestors = sourceCode.getAncestors(node);
44-
const grandparent = ancestors[ancestors.length - 1];
43+
const { parent } = node;
4544

46-
if (grandparent.type === "MemberExpression" && grandparent.object === node &&
45+
if (parent.type === "MemberExpression" && parent.object === node &&
4746
!(beforeToken && beforeToken.value === "(" && afterToken && afterToken.value === ")")) {
4847
context.report({
4948
node,

lib/rules/yoda.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ module.exports = {
343343
) &&
344344
!(!isEqualityOperator(node.operator) && onlyEquality) &&
345345
isComparisonOperator(node.operator) &&
346-
!(exceptRange && isRangeTest(sourceCode.getAncestors(node).pop()))
346+
!(exceptRange && isRangeTest(node.parent))
347347
) {
348348
context.report({
349349
node,

0 commit comments

Comments
 (0)