Skip to content

Commit 2e3f1ef

Browse files
committed
Revert "[Fix] destructuring-assignment: Handle destructuring of useContext in SFC"
This reverts commit 523db20.
1 parent 116fd92 commit 2e3f1ef

File tree

2 files changed

+4
-71
lines changed

2 files changed

+4
-71
lines changed

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

+1-31
Original file line numberDiff line numberDiff line change
@@ -860,36 +860,6 @@ ruleTester.run('destructuring-assignment', rule, {
860860
`,
861861
features: ['ts', 'no-babel'],
862862
},
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-
}
863+
] : []
894864
)),
895865
});

0 commit comments

Comments
 (0)