Skip to content

Commit 77b1383

Browse files
committed
refactor: [destructuring-assignment] a single config option
1 parent 1e7dc52 commit 77b1383

File tree

3 files changed

+31
-48
lines changed

3 files changed

+31
-48
lines changed

docs/rules/destructuring-assignment.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Enforce consostent usage of destructuring assignment of props, state and context (react/destructuring-assignment)
22

3-
Rule can be set to either of `always`, `never`, `ignore` for `class` and `SFC` components.
3+
Rule can be set to either of `always` or `never`;
44
```js
5-
"react/destructuring-assignment": [<enabled>, { "SFC": "always", "class": "always"}]
5+
"react/destructuring-assignment": [<enabled>, 'always']
66
```
77

88
## Rule Details
@@ -47,7 +47,7 @@ const Foo = class extends React.PureComponent {
4747
};
4848
```
4949

50-
If rule option is set to `never`, the following patterns are considered warning:
50+
If rule is set to `never`, the following patterns are considered warning:
5151

5252
```js
5353
const MyComponent = ({id}) => {

lib/rules/destructuring-assignment.js

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
const Components = require('../util/Components');
77

8-
const DEFAULT_OPTIONS = {
9-
SFC: 'always',
10-
class: 'always'
11-
};
8+
const DEFAULT_OPTION = 'always';
129

1310
module.exports = {
1411
meta: {
@@ -18,30 +15,16 @@ module.exports = {
1815
recommended: false
1916
},
2017
schema: [{
21-
definitions: {
22-
value: {
23-
enum: [
24-
'always',
25-
'never',
26-
'ignore'
27-
]
28-
}
29-
},
30-
type: 'object',
31-
properties: {
32-
SFC: {$ref: '#/definitions/value'},
33-
class: {$ref: '#/definitions/value'}
34-
},
35-
additionalProperties: false
18+
type: 'string',
19+
enum: [
20+
'always',
21+
'never'
22+
]
3623
}]
3724
},
3825

3926
create: Components.detect((context, components, utils) => {
40-
const configuration = context.options[0] || {};
41-
const options = {
42-
SFC: configuration.SFC || DEFAULT_OPTIONS.SFC,
43-
class: configuration.class || DEFAULT_OPTIONS.class
44-
};
27+
const configuration = context.options[0] || DEFAULT_OPTION;
4528

4629

4730
/**
@@ -65,12 +48,12 @@ module.exports = {
6548
const destructuringProps = node.params && node.params[0] && node.params[0].type === 'ObjectPattern';
6649
const destructuringContext = node.params && node.params[1] && node.params[1].type === 'ObjectPattern';
6750

68-
if (destructuringProps && components.get(node) && options.SFC === 'never') {
51+
if (destructuringProps && components.get(node) && configuration === 'never') {
6952
context.report({
7053
node: node,
7154
message: 'Must never use destructuring props assignment in SFC argument'
7255
});
73-
} else if (destructuringContext && components.get(node) && options.SFC === 'never') {
56+
} else if (destructuringContext && components.get(node) && configuration === 'never') {
7457
context.report({
7558
node: node,
7659
message: 'Must never use destructuring context assignment in SFC argument'
@@ -81,7 +64,7 @@ module.exports = {
8164
function handleSFCUsage(node) {
8265
// props.aProp || context.aProp
8366
const isPropUsed = (node.object.name === 'props' || node.object.name === 'context') && !isAssignmentToProp(node);
84-
if (isPropUsed && options.SFC === 'always') {
67+
if (isPropUsed && configuration === 'always') {
8568
context.report({
8669
node: node,
8770
message: `Must use destructuring ${node.object.name} assignment`
@@ -96,7 +79,7 @@ module.exports = {
9679
node.object.type === 'MemberExpression'
9780
);
9881

99-
if (isPropUsed && options.class === 'always') {
82+
if (isPropUsed && configuration === 'always') {
10083
context.report({
10184
node: node,
10285
message: `Must use destructuring ${node.object.property.name} assignment`
@@ -135,14 +118,14 @@ module.exports = {
135118
node.init.property.name === 'props' || node.init.property.name === 'context' || node.init.property.name === 'state'
136119
);
137120

138-
if (SFCComponent && destructuringSFC && options.SFC === 'never') {
121+
if (SFCComponent && destructuringSFC && configuration === 'never') {
139122
context.report({
140123
node: node,
141124
message: `Must never use destructuring ${node.init.name} assignment`
142125
});
143126
}
144127

145-
if (classComponent && destructuringClass && options.class === 'never') {
128+
if (classComponent && destructuringClass && configuration === 'never') {
146129
context.report({
147130
node: node,
148131
message: `Must never use destructuring ${node.init.property.name} assignment`

tests/lib/rules/destructuring-assignment.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ruleTester.run('destructuring-assignment', rule, {
2626
return <div>{foo}</div>;
2727
}
2828
};`,
29-
options: [{SFC: 'always', class: 'always'}],
29+
options: ['always'],
3030
parser: 'babel-eslint'
3131
}, {
3232
code: `const MyComponent = ({ id, className }) => (
@@ -42,7 +42,7 @@ ruleTester.run('destructuring-assignment', rule, {
4242
code: `const MyComponent = ({ id, className }) => (
4343
<div id={id} className={className} />
4444
);`,
45-
options: [{SFC: 'always', class: 'always'}]
45+
options: ['always']
4646
}, {
4747
code: `const MyComponent = (props) => {
4848
const { id, className } = props;
@@ -53,7 +53,7 @@ ruleTester.run('destructuring-assignment', rule, {
5353
const { id, className } = props;
5454
return <div id={id} className={className} />
5555
};`,
56-
options: [{SFC: 'always', class: 'always'}]
56+
options: ['always']
5757
}, {
5858
code: `const MyComponent = (props) => (
5959
<div id={id} props={props} />
@@ -62,7 +62,7 @@ ruleTester.run('destructuring-assignment', rule, {
6262
code: `const MyComponent = (props) => (
6363
<div id={id} props={props} />
6464
);`,
65-
options: [{SFC: 'always', class: 'always'}]
65+
options: ['always']
6666
}, {
6767
code: `const MyComponent = (props, { color }) => (
6868
<div id={id} props={props} color={color} />
@@ -71,22 +71,22 @@ ruleTester.run('destructuring-assignment', rule, {
7171
code: `const MyComponent = (props, { color }) => (
7272
<div id={id} props={props} color={color} />
7373
);`,
74-
options: [{SFC: 'always', class: 'always'}]
74+
options: ['always']
7575
}, {
7676
code: `const Foo = class extends React.PureComponent {
7777
render() {
7878
return <div>{this.props.foo}</div>;
7979
}
8080
};`,
81-
options: [{class: 'ignore'}]
81+
options: ['never']
8282
}, {
8383
code: `class Foo extends React.Component {
8484
doStuff() {}
8585
render() {
8686
return <div>{this.props.foo}</div>;
8787
}
8888
}`,
89-
options: [{class: 'ignore'}]
89+
options: ['never']
9090
}, {
9191
code: `const Foo = class extends React.PureComponent {
9292
render() {
@@ -101,7 +101,7 @@ ruleTester.run('destructuring-assignment', rule, {
101101
return <div>{foo}</div>;
102102
}
103103
};`,
104-
options: [{SFC: 'always', class: 'always'}],
104+
options: ['always'],
105105
parser: 'babel-eslint'
106106
}, {
107107
code: `const Foo = class extends React.PureComponent {
@@ -117,14 +117,14 @@ ruleTester.run('destructuring-assignment', rule, {
117117
return <div>{foo}</div>;
118118
}
119119
};`,
120-
options: [{SFC: 'always', class: 'always'}],
120+
options: ['always'],
121121
parser: 'babel-eslint'
122122
}, {
123123
code: `const MyComponent = (props) => {
124124
const { h, i } = hi;
125125
return <div id={props.id} className={props.className} />
126126
};`,
127-
options: [{SFC: 'never'}],
127+
options: ['never'],
128128
parser: 'babel-eslint'
129129
}],
130130
invalid: [{
@@ -138,15 +138,15 @@ ruleTester.run('destructuring-assignment', rule, {
138138
code: `const MyComponent = ({ id, className }) => (
139139
<div id={id} className={className} />
140140
);`,
141-
options: [{SFC: 'never'}],
141+
options: ['never'],
142142
errors: [
143143
{message: 'Must never use destructuring props assignment in SFC argument'}
144144
]
145145
}, {
146146
code: `const MyComponent = (props, { color }) => (
147147
<div id={props.id} className={props.className} />
148148
);`,
149-
options: [{SFC: 'never'}],
149+
options: ['never'],
150150
errors: [
151151
{message: 'Must never use destructuring context assignment in SFC argument'}
152152
]
@@ -213,7 +213,7 @@ ruleTester.run('destructuring-assignment', rule, {
213213
return <div>{foo}</div>;
214214
}
215215
};`,
216-
options: [{SFC: 'always', class: 'never'}],
216+
options: ['never'],
217217
parser: 'babel-eslint',
218218
errors: [
219219
{message: 'Must never use destructuring props assignment'}
@@ -223,7 +223,7 @@ ruleTester.run('destructuring-assignment', rule, {
223223
const { id, className } = props;
224224
return <div id={id} className={className} />
225225
};`,
226-
options: [{SFC: 'never'}],
226+
options: ['never'],
227227
parser: 'babel-eslint',
228228
errors: [
229229
{message: 'Must never use destructuring props assignment'}
@@ -235,7 +235,7 @@ ruleTester.run('destructuring-assignment', rule, {
235235
return <div>{foo}</div>;
236236
}
237237
};`,
238-
options: [{SFC: 'always', class: 'never'}],
238+
options: ['never'],
239239
parser: 'babel-eslint',
240240
errors: [
241241
{message: 'Must never use destructuring state assignment'}

0 commit comments

Comments
 (0)