Skip to content

Commit

Permalink
[Refactor] create getScope util; context.getScope is deprecated
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Łopaciński <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
  • Loading branch information
MatiPl01 and ljharb committed Apr 28, 2024
1 parent d6e9059 commit 8e1a94b
Show file tree
Hide file tree
Showing 37 changed files with 218 additions and 158 deletions.
11 changes: 6 additions & 5 deletions lib/rules/destructuring-assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const eslintUtil = require('../util/eslint');
const isAssignmentLHS = require('../util/ast').isAssignmentLHS;
const report = require('../util/report');

const getScope = eslintUtil.getScope;
const getText = eslintUtil.getText;

const DEFAULT_OPTION = 'always';
Expand Down Expand Up @@ -105,7 +106,7 @@ module.exports = {
function handleStatelessComponent(node) {
const params = evalParams(node.params);

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

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

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

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

const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern');
// let {foo} = props;
Expand Down Expand Up @@ -251,7 +252,7 @@ module.exports = {
&& destructureInSignature === 'always'
&& node.init.name === 'props'
) {
const scopeSetProps = context.getScope().set.get('props');
const scopeSetProps = getScope(context, node).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
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ module.exports = {
checkProperties(node.properties);
break;
case 'Identifier': {
const propTypesObject = variableUtil.findVariableByName(context, node.name);
const propTypesObject = variableUtil.findVariableByName(context, node, node.name);
if (propTypesObject && propTypesObject.properties) {
checkProperties(propTypesObject.properties);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/jsx-fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,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 @@ -184,7 +184,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
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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
3 changes: 2 additions & 1 deletion lib/rules/jsx-no-constructed-context-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const getScope = require('../util/eslint').getScope;
const report = require('../util/report');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -180,7 +181,7 @@ module.exports = {
}

const valueExpression = valueNode.expression;
const invocationScope = context.getScope();
const invocationScope = getScope(context, 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-leaked-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module.exports = {
if (isCoerceValidLeftSide || getIsCoerceValidNestedLogicalExpression(leftSide)) {
return;
}
const variables = variableUtil.variablesInScope(context);
const variables = variableUtil.variablesInScope(context, node);
const leftSideVar = variableUtil.getVariable(variables, leftSide.name);
if (leftSideVar) {
const leftSideValue = leftSideVar.defs
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-undef.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
* @returns {void}
*/
function checkIdentifierInJSX(node) {
let scope = context.getScope();
let scope = eslintUtil.getScope(context, node);
const sourceCode = eslintUtil.getSourceCode(context);
const sourceType = sourceCode.ast.sourceType;
const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';
Expand Down
9 changes: 6 additions & 3 deletions lib/rules/jsx-sort-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ module.exports = {

/**
* Find a variable by name in the current scope.
* @param {ASTNode} node The node to look for.
* @param {string} name Name of the variable to look for.
* @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, 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 @@ -151,7 +154,7 @@ module.exports = {
if (node.type === 'ObjectExpression') {
checkSorted(node.properties);
} else if (node.type === 'Identifier') {
const propTypesObject = findVariableByName(node.name);
const propTypesObject = findVariableByName(node, node.name);
if (propTypesObject && propTypesObject.properties) {
checkSorted(propTypesObject.properties);
}
Expand Down
22 changes: 15 additions & 7 deletions lib/rules/no-access-state-in-setstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const docsUrl = require('../util/docsUrl');
const componentUtil = require('../util/componentUtil');
const report = require('../util/report');
const getScope = require('../util/eslint').getScope;

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -47,8 +48,15 @@ module.exports = {
return current.arguments[0] === node;
}

function isClassComponent() {
return !!(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context));
/**
* @param {ASTNode} node
* @returns {boolean}
*/
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 +66,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 +111,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 +142,7 @@ module.exports = {
if (current.type === 'VariableDeclarator') {
vars.push({
node,
scope: context.getScope(),
scope: getScope(context, node),
variableName: current.id.name,
});
break;
Expand All @@ -158,7 +166,7 @@ module.exports = {
while (current.type !== 'Program') {
if (isFirstArgumentInSetStateCall(current, node)) {
vars
.filter((v) => v.scope === context.getScope() && v.variableName === node.name)
.filter((v) => v.scope === getScope(context, node) && v.variableName === node.name)
.forEach((v) => {
report(context, messages.useCallback, 'useCallback', {
node: v.node,
Expand All @@ -176,7 +184,7 @@ module.exports = {
if (property && property.key && property.key.name === 'state' && isDerivedFromThis) {
vars.push({
node: property.key,
scope: context.getScope(),
scope: getScope(context, node),
variableName: property.key.name,
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-array-index-key.js
Original file line number Diff line number Diff line change
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, node.name);
if (variable && variable.type === 'ImportSpecifier') {
return variable.parent.source.value === 'react';
}
Expand Down
12 changes: 7 additions & 5 deletions lib/rules/no-danger-with-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ module.exports = {
schema: [], // no options
},
create(context) {
function findSpreadVariable(name) {
return variableUtil.variablesInScope(context).find((item) => item.name === name);
function findSpreadVariable(node, name) {
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 +51,7 @@ module.exports = {
return prop.key.name === propName;
}
if (prop.type === 'ExperimentalSpreadProperty' || prop.type === 'SpreadElement') {
const variable = findSpreadVariable(prop.argument.name);
const variable = findSpreadVariable(node, prop.argument.name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
if (seenProps.indexOf(prop.argument.name) > -1) {
return false;
Expand All @@ -73,7 +74,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(node, attribute.argument.name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
return findObjectProp(variable.defs[0].node.init, propName, []);
}
Expand Down Expand Up @@ -127,7 +128,8 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,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 @@ -114,7 +114,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
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,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 @@ -334,8 +334,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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const docsUrl = require('../util/docsUrl');
const ast = require('../util/ast');
const componentUtil = require('../util/componentUtil');
const report = require('../util/report');
const getScope = require('../util/eslint').getScope;

// Descend through all wrapping TypeCastExpressions and return the expression
// that was cast.
Expand Down Expand Up @@ -107,7 +108,7 @@ module.exports = {
'componentDidUpdate',
];

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

const childScope = context.getScope().childScopes.find((x) => x.block === node.value);
const childScope = getScope(context, node).childScopes.find((x) => x.block === node.value);
if (!childScope) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-exact-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module.exports = {
reportPropTypesError(node);
} else if (right.type === 'Identifier') {
const identifier = right.name;
const propsDefinition = variableUtil.findVariableByName(context, identifier);
const propsDefinition = variableUtil.findVariableByName(context, node, identifier);
if (isNonEmptyObjectExpression(propsDefinition)) {
reportPropTypesError(node);
} else if (isNonExactPropWrapperFunction(propsDefinition)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
const eslintUtil = require('../util/eslint');

const getScope = eslintUtil.getScope;
const getText = eslintUtil.getText;

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -345,7 +346,7 @@ module.exports = {
// Mark `render` that do not return some JSX
ReturnStatement(node) {
let blockNode;
let scope = context.getScope();
let scope = getScope(context, 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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
Loading

0 comments on commit 8e1a94b

Please sign in to comment.