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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 9 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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
}

0 commit comments

Comments
 (0)