Skip to content

Commit e8c06fc

Browse files
committed
[Feat]: provide compatibility with eslint v9
1 parent e4ecbcf commit e8c06fc

39 files changed

+156
-127
lines changed

lib/rules/destructuring-assignment.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = {
101101
function handleStatelessComponent(node) {
102102
const params = evalParams(node.params);
103103

104-
const SFCComponent = components.get(context.getScope(node).block);
104+
const SFCComponent = components.get((context.sourceCode || context).getScope(node).block);
105105
if (!SFCComponent) {
106106
return;
107107
}
@@ -119,7 +119,7 @@ module.exports = {
119119
}
120120

121121
function handleStatelessComponentExit(node) {
122-
const SFCComponent = components.get(context.getScope(node).block);
122+
const SFCComponent = components.get((context.sourceCode || context).getScope(node).block);
123123
if (SFCComponent) {
124124
sfcParams.pop();
125125
}
@@ -191,7 +191,7 @@ module.exports = {
191191
'FunctionExpression:exit': handleStatelessComponentExit,
192192

193193
MemberExpression(node) {
194-
let scope = context.getScope(node);
194+
let scope = (context.sourceCode || context).getScope(node);
195195
let SFCComponent = components.get(scope.block);
196196
while (!SFCComponent && scope.upper && scope.upper !== scope) {
197197
SFCComponent = components.get(scope.upper.block);
@@ -209,7 +209,7 @@ module.exports = {
209209

210210
VariableDeclarator(node) {
211211
const classComponent = utils.getParentComponent(node);
212-
const SFCComponent = components.get(context.getScope(node).block);
212+
const SFCComponent = components.get((context.sourceCode || context).getScope(node).block);
213213

214214
const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern');
215215
// let {foo} = props;
@@ -247,7 +247,8 @@ module.exports = {
247247
&& destructureInSignature === 'always'
248248
&& node.init.name === 'props'
249249
) {
250-
const scopeSetProps = context.getScope().set.get('props');
250+
const scope = (context.sourceCode || context).getScope(node);
251+
const scopeSetProps = scope.set.get('props');
251252
const propsRefs = scopeSetProps && scopeSetProps.references;
252253
if (!propsRefs) {
253254
return;

lib/rules/forbid-prop-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module.exports = {
162162
checkProperties(node.properties);
163163
break;
164164
case 'Identifier': {
165-
const propTypesObject = variableUtil.findVariableByName(context, node.name);
165+
const propTypesObject = variableUtil.findVariableByName(context, node);
166166
if (propTypesObject && propTypesObject.properties) {
167167
checkProperties(propTypesObject.properties);
168168
}

lib/rules/jsx-fragments.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ module.exports = {
103103
};
104104
}
105105

106-
function refersToReactFragment(name) {
107-
const variableInit = variableUtil.findVariableByName(context, name);
106+
function refersToReactFragment(node, name) {
107+
const variableInit = variableUtil.findVariableByName(context, node, name);
108108
if (!variableInit) {
109109
return false;
110110
}
@@ -185,7 +185,7 @@ module.exports = {
185185
const openingEl = node.openingElement;
186186
const elName = elementType(openingEl);
187187

188-
if (fragmentNames.has(elName) || refersToReactFragment(elName)) {
188+
if (fragmentNames.has(elName) || refersToReactFragment(node, elName)) {
189189
if (reportOnReactVersion(node)) {
190190
return;
191191
}

lib/rules/jsx-max-depth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module.exports = {
149149
return;
150150
}
151151

152-
const variables = variableUtil.variablesInScope(context);
152+
const variables = variableUtil.variablesInScope(context, node);
153153
const element = findJSXElementOrFragment(variables, node.expression.name, []);
154154

155155
if (element) {

lib/rules/jsx-no-constructed-context-values.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ module.exports = {
179179
}
180180

181181
const valueExpression = valueNode.expression;
182-
const invocationScope = context.getScope();
182+
const invocationScope = (context.sourceCode || context).getScope(node);
183183

184184
// Check if the value prop is a construction
185185
const constructInfo = isConstruction(valueExpression, invocationScope);

lib/rules/jsx-no-undef.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = {
4949
* @returns {void}
5050
*/
5151
function checkIdentifierInJSX(node) {
52-
let scope = context.getScope();
52+
let scope = (context.sourceCode || context).getScope(node);
5353
const sourceCode = context.getSourceCode();
5454
const sourceType = sourceCode.ast.sourceType;
5555
const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';

lib/rules/jsx-sort-default-props.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ module.exports = {
8787

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

9697
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
9798
return null;
@@ -147,7 +148,7 @@ module.exports = {
147148
if (node.type === 'ObjectExpression') {
148149
checkSorted(node.properties);
149150
} else if (node.type === 'Identifier') {
150-
const propTypesObject = findVariableByName(node.name);
151+
const propTypesObject = findVariableByName(node);
151152
if (propTypesObject && propTypesObject.properties) {
152153
checkSorted(propTypesObject.properties);
153154
}

lib/rules/no-access-state-in-setstate.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ module.exports = {
4747
return current.arguments[0] === node;
4848
}
4949

50-
function isClassComponent() {
51-
return !!(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context));
50+
function isClassComponent(node) {
51+
return !!(componentUtil.getParentES6Component(context, node)
52+
|| componentUtil.getParentES5Component(context, node));
5253
}
5354

5455
// The methods array contains all methods or functions that are using this.state
@@ -58,7 +59,7 @@ module.exports = {
5859
const vars = [];
5960
return {
6061
CallExpression(node) {
61-
if (!isClassComponent()) {
62+
if (!isClassComponent(node)) {
6263
return;
6364
}
6465
// Appends all the methods that are calling another
@@ -103,7 +104,7 @@ module.exports = {
103104
if (
104105
node.property.name === 'state'
105106
&& node.object.type === 'ThisExpression'
106-
&& isClassComponent()
107+
&& isClassComponent(node)
107108
) {
108109
let current = node;
109110
while (current.type !== 'Program') {
@@ -134,7 +135,7 @@ module.exports = {
134135
if (current.type === 'VariableDeclarator') {
135136
vars.push({
136137
node,
137-
scope: context.getScope(),
138+
scope: (context.sourceCode || context).getScope(node),
138139
variableName: current.id.name,
139140
});
140141
break;
@@ -155,10 +156,11 @@ module.exports = {
155156
current.parent.value === current
156157
|| current.parent.object === current
157158
) {
159+
const scope = (context.sourceCode || context).getScope(node);
158160
while (current.type !== 'Program') {
159161
if (isFirstArgumentInSetStateCall(current, node)) {
160162
vars
161-
.filter((v) => v.scope === context.getScope() && v.variableName === node.name)
163+
.filter((v) => v.scope === scope && v.variableName === node.name)
162164
.forEach((v) => {
163165
report(context, messages.useCallback, 'useCallback', {
164166
node: v.node,
@@ -176,7 +178,7 @@ module.exports = {
176178
if (property && property.key && property.key.name === 'state' && isDerivedFromThis) {
177179
vars.push({
178180
node: property.key,
179-
scope: context.getScope(),
181+
scope: (context.sourceCode || context).getScope(node),
180182
variableName: property.key.name,
181183
});
182184
}

lib/rules/no-array-index-key.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function isCreateCloneElement(node, context) {
2828
}
2929

3030
if (node.type === 'Identifier') {
31-
const variable = variableUtil.findVariableByName(context, node.name);
31+
const variable = variableUtil.findVariableByName(context, node);
3232
if (variable && variable.type === 'ImportSpecifier') {
3333
return variable.parent.source.value === 'react';
3434
}

lib/rules/no-danger-with-children.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ module.exports = {
3131
schema: [], // no options
3232
},
3333
create(context) {
34-
function findSpreadVariable(name) {
35-
return variableUtil.variablesInScope(context).find((item) => item.name === name);
34+
function findSpreadVariable(name, node) {
35+
return variableUtil.variablesInScope(context, node).find((item) => item.name === name);
3636
}
3737
/**
3838
* Takes a ObjectExpression and returns the value of the prop if it has it
@@ -50,7 +50,7 @@ module.exports = {
5050
return prop.key.name === propName;
5151
}
5252
if (prop.type === 'ExperimentalSpreadProperty' || prop.type === 'SpreadElement') {
53-
const variable = findSpreadVariable(prop.argument.name);
53+
const variable = findSpreadVariable(prop.argument.name, node);
5454
if (variable && variable.defs.length && variable.defs[0].node.init) {
5555
if (seenProps.indexOf(prop.argument.name) > -1) {
5656
return false;
@@ -73,7 +73,7 @@ module.exports = {
7373
const attributes = node.openingElement.attributes;
7474
return attributes.find((attribute) => {
7575
if (attribute.type === 'JSXSpreadAttribute') {
76-
const variable = findSpreadVariable(attribute.argument.name);
76+
const variable = findSpreadVariable(attribute.argument.name, node);
7777
if (variable && variable.defs.length && variable.defs[0].node.init) {
7878
return findObjectProp(variable.defs[0].node.init, propName, []);
7979
}
@@ -127,7 +127,7 @@ module.exports = {
127127
let props = node.arguments[1];
128128

129129
if (props.type === 'Identifier') {
130-
const variable = variableUtil.variablesInScope(context).find((item) => item.name === props.name);
130+
const variable = variableUtil.variablesInScope(context, node).find((item) => item.name === props.name);
131131
if (variable && variable.defs.length && variable.defs[0].node.init) {
132132
props = variable.defs[0].node.init;
133133
}

lib/rules/no-direct-mutation-state.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports = {
9797
},
9898

9999
AssignmentExpression(node) {
100-
const component = components.get(utils.getParentComponent());
100+
const component = components.get(utils.getParentComponent(node));
101101
if (shouldIgnoreComponent(component) || !node.left || !node.left.object) {
102102
return;
103103
}
@@ -113,7 +113,7 @@ module.exports = {
113113
},
114114

115115
UpdateExpression(node) {
116-
const component = components.get(utils.getParentComponent());
116+
const component = components.get(utils.getParentComponent(node));
117117
if (shouldIgnoreComponent(component) || node.argument.type !== 'MemberExpression') {
118118
return;
119119
}

lib/rules/no-set-state.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = {
6767
) {
6868
return;
6969
}
70-
const component = components.get(utils.getParentComponent());
70+
const component = components.get(utils.getParentComponent(node));
7171
const setStateUsages = (component && component.setStateUsages) || [];
7272
setStateUsages.push(callee);
7373
components.set(node, {

lib/rules/no-string-refs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = {
4949
*/
5050
function isRefsUsage(node) {
5151
return !!(
52-
(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context))
52+
(componentUtil.getParentES6Component(context, node) || componentUtil.getParentES5Component(context, node))
5353
&& node.object.type === 'ThisExpression'
5454
&& node.property.name === 'refs'
5555
);

lib/rules/no-this-in-sfc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = {
3333
create: Components.detect((context, components, utils) => ({
3434
MemberExpression(node) {
3535
if (node.object.type === 'ThisExpression') {
36-
const component = components.get(utils.getParentStatelessComponent());
36+
const component = components.get(utils.getParentStatelessComponent(node));
3737
if (!component || (component.node && component.node.parent && component.node.parent.type === 'Property')) {
3838
return;
3939
}

lib/rules/no-unstable-nested-components.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ module.exports = {
305305
* @returns {Boolean} True if node is inside class component's render block, false if not
306306
*/
307307
function isInsideRenderMethod(node) {
308-
const parentComponent = utils.getParentComponent();
308+
const parentComponent = utils.getParentComponent(node);
309309

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

339339
return (
340340
parentComponent

lib/rules/no-unused-state.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module.exports = {
107107
'componentDidUpdate',
108108
];
109109

110-
let scope = context.getScope();
110+
let scope = (context.sourceCode || context).getScope(node);
111111
while (scope) {
112112
const parent = scope.block && scope.block.parent;
113113
if (
@@ -368,7 +368,8 @@ module.exports = {
368368
return;
369369
}
370370

371-
const childScope = context.getScope().childScopes.find((x) => x.block === node.value);
371+
const nodeScope = (context.sourceCode || context).getScope(node);
372+
const childScope = nodeScope.childScopes.find((x) => x.block === node.value);
372373
if (!childScope) {
373374
return;
374375
}

lib/rules/prefer-exact-props.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ module.exports = {
147147
} else if (isNonExactPropWrapperFunction(right)) {
148148
reportPropTypesError(node);
149149
} else if (right.type === 'Identifier') {
150-
const identifier = right.name;
151-
const propsDefinition = variableUtil.findVariableByName(context, identifier);
150+
const propsDefinition = variableUtil.findVariableByName(context, right);
152151
if (isNonEmptyObjectExpression(propsDefinition)) {
153152
reportPropTypesError(node);
154153
} else if (isNonExactPropWrapperFunction(propsDefinition)) {

lib/rules/prefer-stateless-function.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ module.exports = {
341341
// Mark `render` that do not return some JSX
342342
ReturnStatement(node) {
343343
let blockNode;
344-
let scope = context.getScope();
344+
let scope = (context.sourceCode || context).getScope(node);
345345
while (scope) {
346346
blockNode = scope.block && scope.block.parent;
347347
if (blockNode && (blockNode.type === 'MethodDefinition' || blockNode.type === 'Property')) {

lib/rules/react-in-jsx-scope.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
const pragma = pragmaUtil.getFromContext(context);
3737

3838
function checkIfReactIsInScope(node) {
39-
const variables = variableUtil.variablesInScope(context);
39+
const variables = variableUtil.variablesInScope(context, node);
4040
if (variableUtil.findVariable(variables, pragma)) {
4141
return;
4242
}

lib/rules/require-optimization.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ module.exports = {
153153

154154
/**
155155
* Checks if we are declaring function in class
156+
* @param {ASTNode} node The AST node being checked.
156157
* @returns {Boolean} True if we are declaring function in class, false if not.
157158
*/
158-
function isFunctionInClass() {
159+
function isFunctionInClass(node) {
159160
let blockNode;
160-
let scope = context.getScope();
161+
let scope = (context.sourceCode || context).getScope(node);
161162
while (scope) {
162163
blockNode = scope.block;
163164
if (blockNode && blockNode.type === 'ClassDeclaration') {
@@ -172,7 +173,7 @@ module.exports = {
172173
return {
173174
ArrowFunctionExpression(node) {
174175
// Skip if the function is declared in the class
175-
if (isFunctionInClass()) {
176+
if (isFunctionInClass(node)) {
176177
return;
177178
}
178179
// Stateless Functional Components cannot be optimized (yet)
@@ -192,7 +193,7 @@ module.exports = {
192193

193194
FunctionDeclaration(node) {
194195
// Skip if the function is declared in the class
195-
if (isFunctionInClass()) {
196+
if (isFunctionInClass(node)) {
196197
return;
197198
}
198199
// Stateless Functional Components cannot be optimized (yet)
@@ -201,7 +202,7 @@ module.exports = {
201202

202203
FunctionExpression(node) {
203204
// Skip if the function is declared in the class
204-
if (isFunctionInClass()) {
205+
if (isFunctionInClass(node)) {
205206
return;
206207
}
207208
// Stateless Functional Components cannot be optimized (yet)

lib/rules/require-render-return.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060

6161
return {
6262
ReturnStatement(node) {
63-
const ancestors = context.getAncestors(node).reverse();
63+
const ancestors = (context.sourceCode || context).getAncestors(node).reverse();
6464
let depth = 0;
6565
ancestors.forEach((ancestor) => {
6666
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {

0 commit comments

Comments
 (0)