Skip to content

Commit 8e1a94b

Browse files
MatiPl01ljharb
andcommitted
[Refactor] create getScope util; context.getScope is deprecated
Co-authored-by: Mateusz Łopaciński <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent d6e9059 commit 8e1a94b

37 files changed

+218
-158
lines changed

lib/rules/destructuring-assignment.js

+6-5
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

+1-1
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

+3-3
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

+1-1
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

+2-1
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

+1-1
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

+1-1
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

+6-3
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

+15-7
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

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

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ 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(node, name) {
35+
return variableUtil.variablesInScope(context, node)
36+
.find((item) => item.name === name);
3637
}
3738
/**
3839
* Takes a ObjectExpression and returns the value of the prop if it has it
@@ -50,7 +51,7 @@ module.exports = {
5051
return prop.key.name === propName;
5152
}
5253
if (prop.type === 'ExperimentalSpreadProperty' || prop.type === 'SpreadElement') {
53-
const variable = findSpreadVariable(prop.argument.name);
54+
const variable = findSpreadVariable(node, prop.argument.name);
5455
if (variable && variable.defs.length && variable.defs[0].node.init) {
5556
if (seenProps.indexOf(prop.argument.name) > -1) {
5657
return false;
@@ -73,7 +74,7 @@ module.exports = {
7374
const attributes = node.openingElement.attributes;
7475
return attributes.find((attribute) => {
7576
if (attribute.type === 'JSXSpreadAttribute') {
76-
const variable = findSpreadVariable(attribute.argument.name);
77+
const variable = findSpreadVariable(node, attribute.argument.name);
7778
if (variable && variable.defs.length && variable.defs[0].node.init) {
7879
return findObjectProp(variable.defs[0].node.init, propName, []);
7980
}
@@ -127,7 +128,8 @@ module.exports = {
127128
let props = node.arguments[1];
128129

129130
if (props.type === 'Identifier') {
130-
const variable = variableUtil.variablesInScope(context).find((item) => item.name === props.name);
131+
const variable = variableUtil.variablesInScope(context, node)
132+
.find((item) => item.name === props.name);
131133
if (variable && variable.defs.length && variable.defs[0].node.init) {
132134
props = variable.defs[0].node.init;
133135
}

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

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

100100
AssignmentExpression(node) {
101-
const component = components.get(utils.getParentComponent());
101+
const component = components.get(utils.getParentComponent(node));
102102
if (shouldIgnoreComponent(component) || !node.left || !node.left.object) {
103103
return;
104104
}
@@ -114,7 +114,7 @@ module.exports = {
114114
},
115115

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

lib/rules/no-set-state.js

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

lib/rules/no-string-refs.js

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

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

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

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

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

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

340340
return (
341341
parentComponent

lib/rules/no-unused-state.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const docsUrl = require('../util/docsUrl');
1313
const ast = require('../util/ast');
1414
const componentUtil = require('../util/componentUtil');
1515
const report = require('../util/report');
16+
const getScope = require('../util/eslint').getScope;
1617

1718
// Descend through all wrapping TypeCastExpressions and return the expression
1819
// that was cast.
@@ -107,7 +108,7 @@ module.exports = {
107108
'componentDidUpdate',
108109
];
109110

110-
let scope = context.getScope();
111+
let scope = getScope(context, node);
111112
while (scope) {
112113
const parent = scope.block && scope.block.parent;
113114
if (
@@ -368,7 +369,7 @@ module.exports = {
368369
return;
369370
}
370371

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

lib/rules/prefer-exact-props.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module.exports = {
149149
reportPropTypesError(node);
150150
} else if (right.type === 'Identifier') {
151151
const identifier = right.name;
152-
const propsDefinition = variableUtil.findVariableByName(context, identifier);
152+
const propsDefinition = variableUtil.findVariableByName(context, node, identifier);
153153
if (isNonEmptyObjectExpression(propsDefinition)) {
154154
reportPropTypesError(node);
155155
} else if (isNonExactPropWrapperFunction(propsDefinition)) {

lib/rules/prefer-stateless-function.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const docsUrl = require('../util/docsUrl');
1717
const report = require('../util/report');
1818
const eslintUtil = require('../util/eslint');
1919

20+
const getScope = eslintUtil.getScope;
2021
const getText = eslintUtil.getText;
2122

2223
// ------------------------------------------------------------------------------
@@ -345,7 +346,7 @@ module.exports = {
345346
// Mark `render` that do not return some JSX
346347
ReturnStatement(node) {
347348
let blockNode;
348-
let scope = context.getScope();
349+
let scope = getScope(context, node);
349350
while (scope) {
350351
blockNode = scope.block && scope.block.parent;
351352
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
@@ -37,7 +37,7 @@ module.exports = {
3737
const pragma = pragmaUtil.getFromContext(context);
3838

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

0 commit comments

Comments
 (0)