Skip to content

Commit

Permalink
[Refactor] create getAncestors util, since context.getAncestors i…
Browse files Browse the repository at this point in the history
…s deprecated

Co-authored-by: Mateusz Łopaciński <lop.mateusz.2001@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
  • Loading branch information
MatiPl01 and ljharb committed Apr 28, 2024
1 parent 26765f9 commit 6be3329
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const propName = require('jsx-ast-utils/propName');
const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');
const report = require('../util/report');
const getAncestors = require('../util/eslint').getAncestors;

// -----------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -123,7 +124,7 @@ module.exports = {
}

function getBlockStatementAncestors(node) {
return context.getAncestors(node).filter(
return getAncestors(context, node).filter(
(ancestor) => ancestor.type === 'BlockStatement'
).reverse();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-is-mounted.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'use strict';

const docsUrl = require('../util/docsUrl');
const getAncestors = require('../util/eslint').getAncestors;
const report = require('../util/report');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -40,7 +41,7 @@ module.exports = {
if (callee.object.type !== 'ThisExpression' || callee.property.name !== 'isMounted') {
return;
}
const ancestors = context.getAncestors(callee);
const ancestors = getAncestors(context, node);
for (let i = 0, j = ancestors.length; i < j; i++) {
if (ancestors[i].type === 'Property' || ancestors[i].type === 'MethodDefinition') {
report(context, messages.noIsMounted, 'noIsMounted', {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/require-render-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const astUtil = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const getAncestors = require('../util/eslint').getAncestors;

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -61,7 +62,7 @@ module.exports = {

return {
ReturnStatement(node) {
const ancestors = context.getAncestors(node).reverse();
const ancestors = getAncestors(context, node).reverse();
let depth = 0;
ancestors.forEach((ancestor) => {
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/util/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ function getSourceCode(context) {
return context.getSourceCode ? context.getSourceCode() : context.sourceCode;
}

function getAncestors(context, node) {
const sourceCode = getSourceCode(context);
return sourceCode.getAncestors ? sourceCode.getAncestors(node) : context.getAncestors();
}

module.exports = {
getAncestors,
getSourceCode,
};
3 changes: 2 additions & 1 deletion lib/util/makeNoMethodSetStateRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const findLast = require('array.prototype.findlast');

const docsUrl = require('./docsUrl');
const report = require('./report');
const getAncestors = require('./eslint').getAncestors;
const testReactVersion = require('./version').testReactVersion;

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -93,7 +94,7 @@ module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafe
) {
return;
}
const ancestors = context.getAncestors(callee);
const ancestors = getAncestors(context, node);
let depth = 0;
findLast(ancestors, (ancestor) => {
// ancestors.some((ancestor) => {
Expand Down

0 comments on commit 6be3329

Please sign in to comment.