Skip to content

Commit 0266151

Browse files
committed
[New] destructuring-assignment: add ignoreUseContext option
1 parent 747fad0 commit 0266151

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

docs/rules/destructuring-assignment.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const Foo = class extends React.PureComponent {
9696

9797
```js
9898
...
99-
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean>, "destructureInSignature": "always" | "ignore" }]
99+
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean>, "destructureInSignature": "always" | "ignore", "ignoreUseContext": <boolean> }]
100100
...
101101
```
102102

@@ -139,3 +139,18 @@ function Foo(props) {
139139
return <Goo a={a}/>
140140
}
141141
```
142+
143+
### `ignoreUseContext`
144+
145+
When configured with `true`, the rule will ignore values returned from `useContext`.
146+
147+
Examples of **correct** code for this rule:
148+
149+
```jsx
150+
import { useContext } from 'react';
151+
152+
function Foo() {
153+
const foo = useContext(fooContext);
154+
return <>{foo.bar}</>
155+
}
156+
```

lib/rules/destructuring-assignment.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ module.exports = {
8484
'ignore',
8585
],
8686
},
87+
ignoreUseContext: {
88+
type: 'boolean',
89+
},
8790
},
8891
additionalProperties: false,
8992
}],
@@ -92,6 +95,7 @@ module.exports = {
9295
create: Components.detect((context, components, utils) => {
9396
const configuration = context.options[0] || DEFAULT_OPTION;
9497
const ignoreClassFields = (context.options[1] && (context.options[1].ignoreClassFields === true)) || false;
98+
const ignoreUseContext = (context.options[1] && (context.options[1].ignoreUseContext === true)) || false;
9599
const destructureInSignature = (context.options[1] && context.options[1].destructureInSignature) || 'ignore';
96100
const sfcParams = createSFCParams();
97101

@@ -247,7 +251,7 @@ module.exports = {
247251
contextSet.add(node.id.name);
248252
}
249253

250-
if (SFCComponent && destructuringUseContext && configuration === 'never') {
254+
if (SFCComponent && destructuringUseContext && !ignoreUseContext && configuration === 'never') {
251255
report(context, messages.noDestructAssignment, 'noDestructAssignment', {
252256
node,
253257
data: {

tests/lib/rules/destructuring-assignment.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ ruleTester.run('destructuring-assignment', rule, {
833833
],
834834
output: `
835835
function Foo({a}) {
836-
836+
837837
return <p>{a}</p>;
838838
}
839839
`,
@@ -854,7 +854,7 @@ ruleTester.run('destructuring-assignment', rule, {
854854
],
855855
output: `
856856
function Foo({a}: FooProps) {
857-
857+
858858
return <p>{a}</p>;
859859
}
860860
`,
@@ -890,6 +890,30 @@ ruleTester.run('destructuring-assignment', rule, {
890890
errors: [
891891
{ message: 'Must never use destructuring useContext assignment' },
892892
],
893+
},
894+
{
895+
code: `
896+
import { useContext } from 'react';
897+
898+
const MyComponent = (props) => {
899+
const foo = useContext(aContext);
900+
return <div>{foo.test}</div>
901+
};
902+
`,
903+
options: ['always', { ignoreUseContext: true }],
904+
settings: { react: { version: '16.9.0' } },
905+
},
906+
{
907+
code: `
908+
import { useContext } from 'react';
909+
910+
const MyComponent = (props) => {
911+
const {foo} = useContext(aContext);
912+
return <div>{foo}</div>
913+
};
914+
`,
915+
options: ['never', { ignoreUseContext: true }],
916+
settings: { react: { version: '16.9.0' } },
893917
}
894918
)),
895919
});

0 commit comments

Comments
 (0)