Skip to content

Commit a466a77

Browse files
authored
Merge pull request jsx-eslint#1909 from alexandernanberg/fix/class-property-destructure-assignment
Ignore class properties in destructuring-assignment
2 parents 9dbb834 + 4382aa5 commit a466a77

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

docs/rules/destructuring-assignment.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,24 @@ const Foo = class extends React.PureComponent {
8585
return <div>{this.state.title}</div>;
8686
}
8787
};
88+
```
89+
90+
## Rule Options
91+
92+
```js
93+
...
94+
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean> }]
95+
...
96+
```
97+
98+
### `ignoreClassFields`
99+
100+
When `true` the rule will ignore class field declarations.
101+
102+
The following patterns are then considered okay and do not cause warnings:
103+
104+
```jsx
105+
class Foo extends React.PureComponent {
106+
bar = this.props.bar
107+
}
108+
```

lib/rules/destructuring-assignment.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ module.exports = {
2222
'always',
2323
'never'
2424
]
25+
}, {
26+
type: 'object',
27+
properties: {
28+
ignoreClassFields: {
29+
type: 'boolean'
30+
}
31+
},
32+
additionalProperties: false
2533
}]
2634
},
2735

2836
create: Components.detect((context, components, utils) => {
2937
const configuration = context.options[0] || DEFAULT_OPTION;
30-
38+
const ignoreClassFields = context.options[1] && context.options[1].ignoreClassFields === true || false;
3139

3240
/**
3341
* Checks if a prop is being assigned a value props.bar = 'bar'
@@ -82,7 +90,10 @@ module.exports = {
8290
!isAssignmentToProp(node)
8391
);
8492

85-
if (isPropUsed && configuration === 'always') {
93+
if (
94+
isPropUsed && configuration === 'always' &&
95+
!(ignoreClassFields && node.parent.type === 'ClassProperty')
96+
) {
8697
context.report({
8798
node: node,
8899
message: `Must use destructuring ${node.object.property.name} assignment`
@@ -128,7 +139,10 @@ module.exports = {
128139
});
129140
}
130141

131-
if (classComponent && destructuringClass && configuration === 'never') {
142+
if (
143+
classComponent && destructuringClass && configuration === 'never' &&
144+
!(ignoreClassFields && node.parent.type === 'ClassProperty')
145+
) {
132146
context.report({
133147
node: node,
134148
message: `Must never use destructuring ${node.init.property.name} assignment`

tests/lib/rules/destructuring-assignment.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ ruleTester.run('destructuring-assignment', rule, {
165165
}
166166
}
167167
`
168+
}, {
169+
code: `
170+
class Foo extends React.Component {
171+
bar = this.props.bar
172+
}
173+
`,
174+
options: ['always', {ignoreClassFields: true}],
175+
parser: 'babel-eslint'
168176
}],
169177

170178
invalid: [{

0 commit comments

Comments
 (0)