Skip to content

Commit

Permalink
[New] destructuring-assignment: add ignoreUseContext option
Browse files Browse the repository at this point in the history
  • Loading branch information
102 committed Jun 1, 2023
1 parent 747fad0 commit 0266151
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
17 changes: 16 additions & 1 deletion docs/rules/destructuring-assignment.md
Expand Up @@ -96,7 +96,7 @@ const Foo = class extends React.PureComponent {

```js
...
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean>, "destructureInSignature": "always" | "ignore" }]
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean>, "destructureInSignature": "always" | "ignore", "ignoreUseContext": <boolean> }]
...
```

Expand Down Expand Up @@ -139,3 +139,18 @@ function Foo(props) {
return <Goo a={a}/>
}
```

### `ignoreUseContext`

When configured with `true`, the rule will ignore values returned from `useContext`.

Examples of **correct** code for this rule:

```jsx
import { useContext } from 'react';

function Foo() {
const foo = useContext(fooContext);
return <>{foo.bar}</>
}
```
6 changes: 5 additions & 1 deletion lib/rules/destructuring-assignment.js
Expand Up @@ -84,6 +84,9 @@ module.exports = {
'ignore',
],
},
ignoreUseContext: {
type: 'boolean',
},
},
additionalProperties: false,
}],
Expand All @@ -92,6 +95,7 @@ module.exports = {
create: Components.detect((context, components, utils) => {
const configuration = context.options[0] || DEFAULT_OPTION;
const ignoreClassFields = (context.options[1] && (context.options[1].ignoreClassFields === true)) || false;
const ignoreUseContext = (context.options[1] && (context.options[1].ignoreUseContext === true)) || false;
const destructureInSignature = (context.options[1] && context.options[1].destructureInSignature) || 'ignore';
const sfcParams = createSFCParams();

Expand Down Expand Up @@ -247,7 +251,7 @@ module.exports = {
contextSet.add(node.id.name);
}

if (SFCComponent && destructuringUseContext && configuration === 'never') {
if (SFCComponent && destructuringUseContext && !ignoreUseContext && configuration === 'never') {
report(context, messages.noDestructAssignment, 'noDestructAssignment', {
node,
data: {
Expand Down
28 changes: 26 additions & 2 deletions tests/lib/rules/destructuring-assignment.js
Expand Up @@ -833,7 +833,7 @@ ruleTester.run('destructuring-assignment', rule, {
],
output: `
function Foo({a}) {
return <p>{a}</p>;
}
`,
Expand All @@ -854,7 +854,7 @@ ruleTester.run('destructuring-assignment', rule, {
],
output: `
function Foo({a}: FooProps) {
return <p>{a}</p>;
}
`,
Expand Down Expand Up @@ -890,6 +890,30 @@ ruleTester.run('destructuring-assignment', rule, {
errors: [
{ message: 'Must never use destructuring useContext assignment' },
],
},
{
code: `
import { useContext } from 'react';
const MyComponent = (props) => {
const foo = useContext(aContext);
return <div>{foo.test}</div>
};
`,
options: ['always', { ignoreUseContext: true }],
settings: { react: { version: '16.9.0' } },
},
{
code: `
import { useContext } from 'react';
const MyComponent = (props) => {
const {foo} = useContext(aContext);
return <div>{foo}</div>
};
`,
options: ['never', { ignoreUseContext: true }],
settings: { react: { version: '16.9.0' } },
}
)),
});

0 comments on commit 0266151

Please sign in to comment.