Skip to content

Commit

Permalink
[Feat]: provide compatibility with eslint v9
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Apr 7, 2024
1 parent e4ecbcf commit cbe8e69
Show file tree
Hide file tree
Showing 39 changed files with 158 additions and 129 deletions.
11 changes: 6 additions & 5 deletions lib/rules/destructuring-assignment.js
Expand Up @@ -101,7 +101,7 @@ module.exports = {
function handleStatelessComponent(node) {
const params = evalParams(node.params);

const SFCComponent = components.get(context.getScope(node).block);
const SFCComponent = components.get((context.sourceCode || context).getScope(node).block);
if (!SFCComponent) {
return;
}
Expand All @@ -119,7 +119,7 @@ module.exports = {
}

function handleStatelessComponentExit(node) {
const SFCComponent = components.get(context.getScope(node).block);
const SFCComponent = components.get((context.sourceCode || context).getScope(node).block);
if (SFCComponent) {
sfcParams.pop();
}
Expand Down Expand Up @@ -191,7 +191,7 @@ module.exports = {
'FunctionExpression:exit': handleStatelessComponentExit,

MemberExpression(node) {
let scope = context.getScope(node);
let scope = (context.sourceCode || context).getScope(node);
let SFCComponent = components.get(scope.block);
while (!SFCComponent && scope.upper && scope.upper !== scope) {
SFCComponent = components.get(scope.upper.block);
Expand All @@ -209,7 +209,7 @@ module.exports = {

VariableDeclarator(node) {
const classComponent = utils.getParentComponent(node);
const SFCComponent = components.get(context.getScope(node).block);
const SFCComponent = components.get((context.sourceCode || context).getScope(node).block);

const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern');
// let {foo} = props;
Expand Down Expand Up @@ -247,7 +247,8 @@ module.exports = {
&& destructureInSignature === 'always'
&& node.init.name === 'props'
) {
const scopeSetProps = context.getScope().set.get('props');
const scope = (context.sourceCode || context).getScope(node);
const scopeSetProps = scope.set.get('props');
const propsRefs = scopeSetProps && scopeSetProps.references;
if (!propsRefs) {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/forbid-prop-types.js
Expand Up @@ -162,7 +162,7 @@ module.exports = {
checkProperties(node.properties);
break;
case 'Identifier': {
const propTypesObject = variableUtil.findVariableByName(context, node.name);
const propTypesObject = variableUtil.findVariableByName(context, node);
if (propTypesObject && propTypesObject.properties) {
checkProperties(propTypesObject.properties);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/jsx-fragments.js
Expand Up @@ -103,8 +103,8 @@ module.exports = {
};
}

function refersToReactFragment(name) {
const variableInit = variableUtil.findVariableByName(context, name);
function refersToReactFragment(node, name) {
const variableInit = variableUtil.findVariableByName(context, node, name);
if (!variableInit) {
return false;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ module.exports = {
const openingEl = node.openingElement;
const elName = elementType(openingEl);

if (fragmentNames.has(elName) || refersToReactFragment(elName)) {
if (fragmentNames.has(elName) || refersToReactFragment(node, elName)) {
if (reportOnReactVersion(node)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-max-depth.js
Expand Up @@ -149,7 +149,7 @@ module.exports = {
return;
}

const variables = variableUtil.variablesInScope(context);
const variables = variableUtil.variablesInScope(context, node);
const element = findJSXElementOrFragment(variables, node.expression.name, []);

if (element) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-constructed-context-values.js
Expand Up @@ -179,7 +179,7 @@ module.exports = {
}

const valueExpression = valueNode.expression;
const invocationScope = context.getScope();
const invocationScope = (context.sourceCode || context).getScope(node);

// Check if the value prop is a construction
const constructInfo = isConstruction(valueExpression, invocationScope);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-undef.js
Expand Up @@ -49,7 +49,7 @@ module.exports = {
* @returns {void}
*/
function checkIdentifierInJSX(node) {
let scope = context.getScope();
let scope = (context.sourceCode || context).getScope(node);
const sourceCode = context.getSourceCode();
const sourceType = sourceCode.ast.sourceType;
const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';
Expand Down
9 changes: 5 additions & 4 deletions lib/rules/jsx-sort-default-props.js
Expand Up @@ -87,11 +87,12 @@ module.exports = {

/**
* Find a variable by name in the current scope.
* @param {string} name Name of the variable to look for.
* @param {ASTNode} node The node to check.
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
*/
function findVariableByName(name) {
const variable = variableUtil.variablesInScope(context).find((item) => item.name === name);
function findVariableByName(node) {
const name = node.name;
const variable = variableUtil.variablesInScope(context, node).find((item) => item.name === name);

if (!variable || !variable.defs[0] || !variable.defs[0].node) {
return null;
Expand Down Expand Up @@ -147,7 +148,7 @@ module.exports = {
if (node.type === 'ObjectExpression') {
checkSorted(node.properties);
} else if (node.type === 'Identifier') {
const propTypesObject = findVariableByName(node.name);
const propTypesObject = findVariableByName(node);
if (propTypesObject && propTypesObject.properties) {
checkSorted(propTypesObject.properties);
}
Expand Down
16 changes: 9 additions & 7 deletions lib/rules/no-access-state-in-setstate.js
Expand Up @@ -47,8 +47,9 @@ module.exports = {
return current.arguments[0] === node;
}

function isClassComponent() {
return !!(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context));
function isClassComponent(node) {
return !!(componentUtil.getParentES6Component(context, node)
|| componentUtil.getParentES5Component(context, node));
}

// The methods array contains all methods or functions that are using this.state
Expand All @@ -58,7 +59,7 @@ module.exports = {
const vars = [];
return {
CallExpression(node) {
if (!isClassComponent()) {
if (!isClassComponent(node)) {
return;
}
// Appends all the methods that are calling another
Expand Down Expand Up @@ -103,7 +104,7 @@ module.exports = {
if (
node.property.name === 'state'
&& node.object.type === 'ThisExpression'
&& isClassComponent()
&& isClassComponent(node)
) {
let current = node;
while (current.type !== 'Program') {
Expand Down Expand Up @@ -134,7 +135,7 @@ module.exports = {
if (current.type === 'VariableDeclarator') {
vars.push({
node,
scope: context.getScope(),
scope: (context.sourceCode || context).getScope(node),
variableName: current.id.name,
});
break;
Expand All @@ -155,10 +156,11 @@ module.exports = {
current.parent.value === current
|| current.parent.object === current
) {
const scope = (context.sourceCode || context).getScope(node);
while (current.type !== 'Program') {
if (isFirstArgumentInSetStateCall(current, node)) {
vars
.filter((v) => v.scope === context.getScope() && v.variableName === node.name)
.filter((v) => v.scope === scope && v.variableName === node.name)
.forEach((v) => {
report(context, messages.useCallback, 'useCallback', {
node: v.node,
Expand All @@ -176,7 +178,7 @@ module.exports = {
if (property && property.key && property.key.name === 'state' && isDerivedFromThis) {
vars.push({
node: property.key,
scope: context.getScope(),
scope: (context.sourceCode || context).getScope(node),
variableName: property.key.name,
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-array-index-key.js
Expand Up @@ -28,7 +28,7 @@ function isCreateCloneElement(node, context) {
}

if (node.type === 'Identifier') {
const variable = variableUtil.findVariableByName(context, node.name);
const variable = variableUtil.findVariableByName(context, node);
if (variable && variable.type === 'ImportSpecifier') {
return variable.parent.source.value === 'react';
}
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/no-danger-with-children.js
Expand Up @@ -31,8 +31,8 @@ module.exports = {
schema: [], // no options
},
create(context) {
function findSpreadVariable(name) {
return variableUtil.variablesInScope(context).find((item) => item.name === name);
function findSpreadVariable(name, node) {
return variableUtil.variablesInScope(context, node).find((item) => item.name === name);
}
/**
* Takes a ObjectExpression and returns the value of the prop if it has it
Expand All @@ -50,7 +50,7 @@ module.exports = {
return prop.key.name === propName;
}
if (prop.type === 'ExperimentalSpreadProperty' || prop.type === 'SpreadElement') {
const variable = findSpreadVariable(prop.argument.name);
const variable = findSpreadVariable(prop.argument.name, node);
if (variable && variable.defs.length && variable.defs[0].node.init) {
if (seenProps.indexOf(prop.argument.name) > -1) {
return false;
Expand All @@ -73,7 +73,7 @@ module.exports = {
const attributes = node.openingElement.attributes;
return attributes.find((attribute) => {
if (attribute.type === 'JSXSpreadAttribute') {
const variable = findSpreadVariable(attribute.argument.name);
const variable = findSpreadVariable(attribute.argument.name, node);
if (variable && variable.defs.length && variable.defs[0].node.init) {
return findObjectProp(variable.defs[0].node.init, propName, []);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ module.exports = {
let props = node.arguments[1];

if (props.type === 'Identifier') {
const variable = variableUtil.variablesInScope(context).find((item) => item.name === props.name);
const variable = variableUtil.variablesInScope(context, node).find((item) => item.name === props.name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
props = variable.defs[0].node.init;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-direct-mutation-state.js
Expand Up @@ -97,7 +97,7 @@ module.exports = {
},

AssignmentExpression(node) {
const component = components.get(utils.getParentComponent());
const component = components.get(utils.getParentComponent(node));
if (shouldIgnoreComponent(component) || !node.left || !node.left.object) {
return;
}
Expand All @@ -113,7 +113,7 @@ module.exports = {
},

UpdateExpression(node) {
const component = components.get(utils.getParentComponent());
const component = components.get(utils.getParentComponent(node));
if (shouldIgnoreComponent(component) || node.argument.type !== 'MemberExpression') {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-set-state.js
Expand Up @@ -67,7 +67,7 @@ module.exports = {
) {
return;
}
const component = components.get(utils.getParentComponent());
const component = components.get(utils.getParentComponent(node));
const setStateUsages = (component && component.setStateUsages) || [];
setStateUsages.push(callee);
components.set(node, {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-string-refs.js
Expand Up @@ -49,7 +49,7 @@ module.exports = {
*/
function isRefsUsage(node) {
return !!(
(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context))
(componentUtil.getParentES6Component(context, node) || componentUtil.getParentES5Component(context, node))
&& node.object.type === 'ThisExpression'
&& node.property.name === 'refs'
);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-this-in-sfc.js
Expand Up @@ -33,7 +33,7 @@ module.exports = {
create: Components.detect((context, components, utils) => ({
MemberExpression(node) {
if (node.object.type === 'ThisExpression') {
const component = components.get(utils.getParentStatelessComponent());
const component = components.get(utils.getParentStatelessComponent(node));
if (!component || (component.node && component.node.parent && component.node.parent.type === 'Property')) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-unstable-nested-components.js
Expand Up @@ -305,7 +305,7 @@ module.exports = {
* @returns {Boolean} True if node is inside class component's render block, false if not
*/
function isInsideRenderMethod(node) {
const parentComponent = utils.getParentComponent();
const parentComponent = utils.getParentComponent(node);

if (!parentComponent || parentComponent.type !== 'ClassDeclaration') {
return false;
Expand Down Expand Up @@ -333,8 +333,8 @@ module.exports = {
* @returns {Boolean} True if given node a function component declared inside class component, false if not
*/
function isFunctionComponentInsideClassComponent(node) {
const parentComponent = utils.getParentComponent();
const parentStatelessComponent = utils.getParentStatelessComponent();
const parentComponent = utils.getParentComponent(node);
const parentStatelessComponent = utils.getParentStatelessComponent(node);

return (
parentComponent
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-unused-state.js
Expand Up @@ -107,7 +107,7 @@ module.exports = {
'componentDidUpdate',
];

let scope = context.getScope();
let scope = (context.sourceCode || context).getScope(node);
while (scope) {
const parent = scope.block && scope.block.parent;
if (
Expand Down Expand Up @@ -368,7 +368,8 @@ module.exports = {
return;
}

const childScope = context.getScope().childScopes.find((x) => x.block === node.value);
const nodeScope = (context.sourceCode || context).getScope(node);
const childScope = nodeScope.childScopes.find((x) => x.block === node.value);
if (!childScope) {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/prefer-exact-props.js
Expand Up @@ -147,8 +147,7 @@ module.exports = {
} else if (isNonExactPropWrapperFunction(right)) {
reportPropTypesError(node);
} else if (right.type === 'Identifier') {
const identifier = right.name;
const propsDefinition = variableUtil.findVariableByName(context, identifier);
const propsDefinition = variableUtil.findVariableByName(context, right);
if (isNonEmptyObjectExpression(propsDefinition)) {
reportPropTypesError(node);
} else if (isNonExactPropWrapperFunction(propsDefinition)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-stateless-function.js
Expand Up @@ -341,7 +341,7 @@ module.exports = {
// Mark `render` that do not return some JSX
ReturnStatement(node) {
let blockNode;
let scope = context.getScope();
let scope = (context.sourceCode || context).getScope(node);
while (scope) {
blockNode = scope.block && scope.block.parent;
if (blockNode && (blockNode.type === 'MethodDefinition' || blockNode.type === 'Property')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/react-in-jsx-scope.js
Expand Up @@ -36,7 +36,7 @@ module.exports = {
const pragma = pragmaUtil.getFromContext(context);

function checkIfReactIsInScope(node) {
const variables = variableUtil.variablesInScope(context);
const variables = variableUtil.variablesInScope(context, node);
if (variableUtil.findVariable(variables, pragma)) {
return;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/rules/require-optimization.js
Expand Up @@ -153,11 +153,12 @@ module.exports = {

/**
* Checks if we are declaring function in class
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if we are declaring function in class, false if not.
*/
function isFunctionInClass() {
function isFunctionInClass(node) {
let blockNode;
let scope = context.getScope();
let scope = (context.sourceCode || context).getScope(node);
while (scope) {
blockNode = scope.block;
if (blockNode && blockNode.type === 'ClassDeclaration') {
Expand All @@ -172,7 +173,7 @@ module.exports = {
return {
ArrowFunctionExpression(node) {
// Skip if the function is declared in the class
if (isFunctionInClass()) {
if (isFunctionInClass(node)) {
return;
}
// Stateless Functional Components cannot be optimized (yet)
Expand All @@ -192,7 +193,7 @@ module.exports = {

FunctionDeclaration(node) {
// Skip if the function is declared in the class
if (isFunctionInClass()) {
if (isFunctionInClass(node)) {
return;
}
// Stateless Functional Components cannot be optimized (yet)
Expand All @@ -201,7 +202,7 @@ module.exports = {

FunctionExpression(node) {
// Skip if the function is declared in the class
if (isFunctionInClass()) {
if (isFunctionInClass(node)) {
return;
}
// Stateless Functional Components cannot be optimized (yet)
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-render-return.js
Expand Up @@ -60,7 +60,7 @@ module.exports = {

return {
ReturnStatement(node) {
const ancestors = context.getAncestors(node).reverse();
const ancestors = (context.sourceCode || context).getAncestors(node).reverse();
let depth = 0;
ancestors.forEach((ancestor) => {
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
Expand Down

0 comments on commit cbe8e69

Please sign in to comment.