Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove unnecessary use of SourceCode#getAncestors in rules #17075

Merged
merged 1 commit into from Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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