Skip to content

Commit 8e1a94b

Browse files
MatiPl01ljharb
andcommitted
[Refactor] create getScope util; context.getScope is deprecated
Co-authored-by: Mateusz Łopaciński <lop.mateusz.2001@gmail.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
1 parent d6e9059 commit 8e1a94b

37 files changed

+218
-158
lines changed

lib/rules/destructuring-assignment.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const eslintUtil = require('../util/eslint');
1010
const isAssignmentLHS = require('../util/ast').isAssignmentLHS;
1111
const report = require('../util/report');
1212

13+
const getScope = eslintUtil.getScope;
1314
const getText = eslintUtil.getText;
1415

1516
const DEFAULT_OPTION = 'always';
@@ -105,7 +106,7 @@ module.exports = {
105106
function handleStatelessComponent(node) {
106107
const params = evalParams(node.params);
107108

108-
const SFCComponent = components.get(context.getScope(node).block);
109+
const SFCComponent = components.get(getScope(context, node).block);
109110
if (!SFCComponent) {
110111
return;
111112
}
@@ -123,7 +124,7 @@ module.exports = {
123124
}
124125

125126
function handleStatelessComponentExit(node) {
126-
const SFCComponent = components.get(context.getScope(node).block);
127+
const SFCComponent = components.get(getScope(context, node).block);
127128
if (SFCComponent) {
128129
sfcParams.pop();
129130
}
@@ -195,7 +196,7 @@ module.exports = {
195196
'FunctionExpression:exit': handleStatelessComponentExit,
196197

197198
MemberExpression(node) {
198-
let scope = context.getScope(node);
199+
let scope = getScope(context, node);
199200
let SFCComponent = components.get(scope.block);
200201
while (!SFCComponent && scope.upper && scope.upper !== scope) {
201202
SFCComponent = components.get(scope.upper.block);
@@ -213,7 +214,7 @@ module.exports = {
213214

214215
VariableDeclarator(node) {
215216
const classComponent = utils.getParentComponent(node);
216-
const SFCComponent = components.get(context.getScope(node).block);
217+
const SFCComponent = components.get(getScope(context, node).block);
217218

218219
const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern');
219220
// let {foo} = props;
@@ -251,7 +252,7 @@ module.exports = {
251252
&& destructureInSignature === 'always'
252253
&& node.init.name === 'props'
253254
) {
254-
const scopeSetProps = context.getScope().set.get('props');
255+
const scopeSetProps = getScope(context, node).set.get('props');
255256
const propsRefs = scopeSetProps && scopeSetProps.references;
256257
if (!propsRefs) {
257258
return;

lib/rules/forbid-prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module.exports = {
163163
checkProperties(node.properties);
164164
break;
165165
case 'Identifier': {
166-
const propTypesObject = variableUtil.findVariableByName(context, node.name);
166+
const propTypesObject = variableUtil.findVariableByName(context, node, node.name);
167167
if (propTypesObject && propTypesObject.properties) {
168168
checkProperties(propTypesObject.properties);
169169
}

lib/rules/jsx-fragments.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ module.exports = {
102102
};
103103
}
104104

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

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

lib/rules/jsx-max-depth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ module.exports = {
150150
return;
151151
}
152152

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

156156
if (element) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const Components = require('../util/Components');
1010
const docsUrl = require('../util/docsUrl');
11+
const getScope = require('../util/eslint').getScope;
1112
const report = require('../util/report');
1213

1314
// ------------------------------------------------------------------------------
@@ -180,7 +181,7 @@ module.exports = {
180181
}
181182

182183
const valueExpression = valueNode.expression;
183-
const invocationScope = context.getScope();
184+
const invocationScope = getScope(context, node);
184185

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

lib/rules/jsx-no-leaked-render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ module.exports = {
161161
if (isCoerceValidLeftSide || getIsCoerceValidNestedLogicalExpression(leftSide)) {
162162
return;
163163
}
164-
const variables = variableUtil.variablesInScope(context);
164+
const variables = variableUtil.variablesInScope(context, node);
165165
const leftSideVar = variableUtil.getVariable(variables, leftSide.name);
166166
if (leftSideVar) {
167167
const leftSideValue = leftSideVar.defs

lib/rules/jsx-no-undef.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151
* @returns {void}
5252
*/
5353
function checkIdentifierInJSX(node) {
54-
let scope = context.getScope();
54+
let scope = eslintUtil.getScope(context, node);
5555
const sourceCode = eslintUtil.getSourceCode(context);
5656
const sourceType = sourceCode.ast.sourceType;
5757
const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ module.exports = {
9191

9292
/**
9393
* Find a variable by name in the current scope.
94+
* @param {ASTNode} node The node to look for.
9495
* @param {string} name Name of the variable to look for.
9596
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
9697
*/
97-
function findVariableByName(name) {
98-
const variable = variableUtil.variablesInScope(context).find((item) => item.name === name);
98+
function findVariableByName(node, name) {
99+
const variable = variableUtil
100+
.variablesInScope(context, node)
101+
.find((item) => item.name === name);
99102

100103
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
101104
return null;
@@ -151,7 +154,7 @@ module.exports = {
151154
if (node.type === 'ObjectExpression') {
152155
checkSorted(node.properties);
153156
} else if (node.type === 'Identifier') {
154-
const propTypesObject = findVariableByName(node.name);
157+
const propTypesObject = findVariableByName(node, node.name);
155158
if (propTypesObject && propTypesObject.properties) {
156159
checkSorted(propTypesObject.properties);
157160
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const docsUrl = require('../util/docsUrl');
99
const componentUtil = require('../util/componentUtil');
1010
const report = require('../util/report');
11+
const getScope = require('../util/eslint').getScope;
1112

1213
// ------------------------------------------------------------------------------
1314
// Rule Definition
@@ -47,8 +48,15 @@ module.exports = {
4748
return current.arguments[0] === node;
4849
}
4950

50-
function isClassComponent() {
51-
return !!(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context));
51+
/**
52+
* @param {ASTNode} node
53+
* @returns {boolean}
54+
*/
55+
function isClassComponent(node) {
56+
return !!(
57+
componentUtil.getParentES6Component(context, node)
58+
|| componentUtil.getParentES5Component(context, node)
59+
);
5260
}
5361

5462
// The methods array contains all methods or functions that are using this.state
@@ -58,7 +66,7 @@ module.exports = {
5866
const vars = [];
5967
return {
6068
CallExpression(node) {
61-
if (!isClassComponent()) {
69+
if (!isClassComponent(node)) {
6270
return;
6371
}
6472
// Appends all the methods that are calling another
@@ -103,7 +111,7 @@ module.exports = {
103111
if (
104112
node.property.name === 'state'
105113
&& node.object.type === 'ThisExpression'
106-
&& isClassComponent()
114+
&& isClassComponent(node)
107115
) {
108116
let current = node;
109117
while (current.type !== 'Program') {
@@ -134,7 +142,7 @@ module.exports = {
134142
if (current.type === 'VariableDeclarator') {
135143
vars.push({
136144
node,
137-
scope: context.getScope(),
145+
scope: getScope(context, node),
138146
variableName: current.id.name,
139147
});
140148
break;
@@ -158,7 +166,7 @@ module.exports = {
158166
while (current.type !== 'Program') {
159167
if (isFirstArgumentInSetStateCall(current, node)) {
160168
vars
161-
.filter((v) => v.scope === context.getScope() && v.variableName === node.name)
169+
.filter((v) => v.scope === getScope(context, node) && v.variableName === node.name)
162170
.forEach((v) => {
163171
report(context, messages.useCallback, 'useCallback', {
164172
node: v.node,
@@ -176,7 +184,7 @@ module.exports = {
176184
if (property && property.key && property.key.name === 'state' && isDerivedFromThis) {
177185
vars.push({
178186
node: property.key,
179-
scope: context.getScope(),
187+
scope: getScope(context, node),
180188
variableName: property.key.name,
181189
});
182190
}

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, node.name);
3232
if (variable && variable.type === 'ImportSpecifier') {
3333
return variable.parent.source.value === 'react';
3434
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy