Skip to content

Commit

Permalink
refactor: remove unnecessary use of SourceCode#getAncestors in rules (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Apr 11, 2023
1 parent 6d8bffd commit eac4943
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-lone-blocks.js
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/no-lonely-if.js
Expand Up @@ -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 &&
Expand Down
12 changes: 5 additions & 7 deletions lib/rules/no-unused-expressions.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" });
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/valid-typeof.js
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions lib/rules/wrap-regex.js
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/yoda.js
Expand Up @@ -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,
Expand Down

0 comments on commit eac4943

Please sign in to comment.