Skip to content

Commit 7f655f8

Browse files
102ljharb
authored andcommitted
Revert "[Fix] destructuring-assignment: Handle destructuring of useContext in SFC"
- [Tests] `destructuring-assignment`: Add more modern context cases This reverts commit 523db20 / #2797 Fixes #3536.
1 parent 693860f commit 7f655f8

File tree

3 files changed

+30
-71
lines changed

3 files changed

+30
-71
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2020
* [`display-name`], [`prop-types`]: when checking for a capitalized name, ignore underscores entirely ([#3560][] @ljharb)
2121
* [`no-unused-state`]: avoid crashing on a class field function with destructured state ([#3568][] @ljharb)
2222
* [`no-unused-prop-types`]: allow using spread with object expression in jsx ([#3570][] @akulsr0)
23+
* Revert "[`destructuring-assignment`]: Handle destructuring of useContext in SFC" ([#3583][] [#2797][] @102)
2324

2425
### Changed
2526
* [Docs] [`jsx-newline`], [`no-unsafe`], [`static-property-placement`]: Fix code syntax highlighting ([#3563][] @nbsp1221)
2627
* [readme] resore configuration URL ([#3582][] @gokaygurcan)
2728

29+
[#3583]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3583
2830
[#3582]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3582
2931
[#3570]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3570
3032
[#3568]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3568

lib/rules/destructuring-assignment.js

+3-40
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const Components = require('../util/Components');
88
const docsUrl = require('../util/docsUrl');
99
const isAssignmentLHS = require('../util/ast').isAssignmentLHS;
1010
const report = require('../util/report');
11-
const testReactVersion = require('../util/version').testReactVersion;
1211

1312
const DEFAULT_OPTION = 'always';
1413

@@ -95,8 +94,6 @@ module.exports = {
9594
const destructureInSignature = (context.options[1] && context.options[1].destructureInSignature) || 'ignore';
9695
const sfcParams = createSFCParams();
9796

98-
// set to save renamed var of useContext
99-
const contextSet = new Set();
10097
/**
10198
* @param {ASTNode} node We expect either an ArrowFunctionExpression,
10299
* FunctionDeclaration, or FunctionExpression
@@ -131,7 +128,7 @@ module.exports = {
131128
function handleSFCUsage(node) {
132129
const propsName = sfcParams.propsName();
133130
const contextName = sfcParams.contextName();
134-
// props.aProp
131+
// props.aProp || context.aProp
135132
const isPropUsed = (
136133
(propsName && node.object.name === propsName)
137134
|| (contextName && node.object.name === contextName)
@@ -145,21 +142,6 @@ module.exports = {
145142
},
146143
});
147144
}
148-
149-
// const foo = useContext(aContext);
150-
// foo.aProp
151-
const isContextUsed = contextSet.has(node.object.name) && !isAssignmentLHS(node);
152-
const optional = node.optional
153-
// the below is for the old typescript-eslint parser
154-
|| context.getSourceCode().getText(node).slice(node.object.range[1] - node.range[0], node.object.range[1] - node.range[0] + 1) === '?';
155-
if (isContextUsed && configuration === 'always' && !optional) {
156-
report(context, messages.useDestructAssignment, 'useDestructAssignment', {
157-
node,
158-
data: {
159-
type: node.object.name,
160-
},
161-
});
162-
}
163145
}
164146

165147
function isInClassProperty(node) {
@@ -194,9 +176,8 @@ module.exports = {
194176
}
195177
}
196178

197-
const hasHooks = testReactVersion(context, '>= 16.9');
198-
199179
return {
180+
200181
FunctionDeclaration: handleStatelessComponent,
201182

202183
ArrowFunctionExpression: handleStatelessComponent,
@@ -231,31 +212,13 @@ module.exports = {
231212
const SFCComponent = components.get(context.getScope(node).block);
232213

233214
const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern');
234-
const identifier = (node.init && node.id && node.id.type === 'Identifier');
235215
// let {foo} = props;
236-
const destructuringSFC = destructuring && node.init.name === 'props';
237-
// let {foo} = useContext(aContext);
238-
const destructuringUseContext = hasHooks && destructuring && node.init.callee && node.init.callee.name === 'useContext';
239-
// let foo = useContext(aContext);
240-
const assignUseContext = hasHooks && identifier && node.init.callee && node.init.callee.name === 'useContext';
216+
const destructuringSFC = destructuring && (node.init.name === 'props' || node.init.name === 'context');
241217
// let {foo} = this.props;
242218
const destructuringClass = destructuring && node.init.object && node.init.object.type === 'ThisExpression' && (
243219
node.init.property.name === 'props' || node.init.property.name === 'context' || node.init.property.name === 'state'
244220
);
245221

246-
if (SFCComponent && assignUseContext) {
247-
contextSet.add(node.id.name);
248-
}
249-
250-
if (SFCComponent && destructuringUseContext && configuration === 'never') {
251-
report(context, messages.noDestructAssignment, 'noDestructAssignment', {
252-
node,
253-
data: {
254-
type: node.init.callee.name,
255-
},
256-
});
257-
}
258-
259222
if (SFCComponent && destructuringSFC && configuration === 'never') {
260223
report(context, messages.noDestructAssignment, 'noDestructAssignment', {
261224
node,

tests/lib/rules/destructuring-assignment.js

+25-31
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,18 @@ ruleTester.run('destructuring-assignment', rule, {
374374
code: `
375375
import { useContext } from 'react';
376376
377+
const MyComponent = (props) => {
378+
const {foo} = useContext(aContext);
379+
return <div>{foo}</div>
380+
};
381+
`,
382+
options: ['always'],
383+
settings: { react: { version: '16.9.0' } },
384+
},
385+
{
386+
code: `
387+
import { useContext } from 'react';
388+
377389
const MyComponent = (props) => {
378390
const foo = useContext(aContext);
379391
return <div>{foo.test}</div>
@@ -382,6 +394,18 @@ ruleTester.run('destructuring-assignment', rule, {
382394
options: ['never'],
383395
settings: { react: { version: '16.9.0' } },
384396
},
397+
{
398+
code: `
399+
import { useContext } from 'react';
400+
401+
const MyComponent = (props) => {
402+
const foo = useContext(aContext);
403+
return <div>{foo.test}</div>
404+
};
405+
`,
406+
options: ['always'],
407+
settings: { react: { version: '16.9.0' } },
408+
},
385409
{
386410
code: `
387411
const MyComponent = (props) => {
@@ -860,36 +884,6 @@ ruleTester.run('destructuring-assignment', rule, {
860884
`,
861885
features: ['ts', 'no-babel'],
862886
},
863-
] : [],
864-
{
865-
code: `
866-
import { useContext } from 'react';
867-
868-
const MyComponent = (props) => {
869-
const foo = useContext(aContext);
870-
return <div>{foo.test}</div>
871-
};
872-
`,
873-
options: ['always'],
874-
settings: { react: { version: '16.9.0' } },
875-
errors: [
876-
{ message: 'Must use destructuring foo assignment' },
877-
],
878-
},
879-
{
880-
code: `
881-
import { useContext } from 'react';
882-
883-
const MyComponent = (props) => {
884-
const {foo} = useContext(aContext);
885-
return <div>{foo}</div>
886-
};
887-
`,
888-
options: ['never'],
889-
settings: { react: { version: '16.9.0' } },
890-
errors: [
891-
{ message: 'Must never use destructuring useContext assignment' },
892-
],
893-
}
887+
] : []
894888
)),
895889
});

0 commit comments

Comments
 (0)