Skip to content

Commit cf4eaa1

Browse files
committed
Omit test cases for eslint < 4 and rename destructAtParameter to destructureInSignature
1 parent 9df0684 commit cf4eaa1

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

docs/rules/destructuring-assignment.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const Foo = class extends React.PureComponent {
9191

9292
```js
9393
...
94-
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean>, "destructAtParameter": "always" | "ignore" }]
94+
"react/destructuring-assignment": [<enabled>, "always", { "ignoreClassFields": <boolean>, "destructureInSignature": "always" | "ignore" }]
9595
...
9696
```
9797

@@ -105,11 +105,11 @@ class Foo extends React.PureComponent {
105105
}
106106
```
107107

108-
### `destructAtParameter` (default: "ignore")
108+
### `destructureInSignature` (default: "ignore")
109109

110110
This option can be one of `always` or `ignore`. When configured with `always`, the rule will require props destructuring happens in the function signature.
111111

112-
Examples of **incorrect** code for `destructAtParameter: 'always'` :
112+
Examples of **incorrect** code for `destructureInSignature: 'always'` :
113113

114114
```jsx
115115
function Foo(props) {
@@ -118,7 +118,7 @@ function Foo(props) {
118118
}
119119
```
120120

121-
Examples of **correct** code for `destructAtParameter: 'always'` :
121+
Examples of **correct** code for `destructureInSignature: 'always'` :
122122

123123
```jsx
124124
function Foo({a}) {

lib/rules/destructuring-assignment.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const messages = {
5050
noDestructContextInSFCArg: 'Must never use destructuring context assignment in SFC argument',
5151
noDestructAssignment: 'Must never use destructuring {{type}} assignment',
5252
useDestructAssignment: 'Must use destructuring {{type}} assignment',
53-
destructAtParameter: 'Must destructure props in the function signature.',
53+
destructureInSignature: 'Must destructure props in the function signature.',
5454
};
5555

5656
module.exports = {
@@ -76,7 +76,7 @@ module.exports = {
7676
ignoreClassFields: {
7777
type: 'boolean',
7878
},
79-
destructAtParameter: {
79+
destructureInSignature: {
8080
type: 'string',
8181
enum: [
8282
'always',
@@ -91,7 +91,7 @@ module.exports = {
9191
create: Components.detect((context, components, utils) => {
9292
const configuration = context.options[0] || DEFAULT_OPTION;
9393
const ignoreClassFields = (context.options[1] && (context.options[1].ignoreClassFields === true)) || false;
94-
const destructAtParameter = (context.options[1] && context.options[1].destructAtParameter) || 'ignore';
94+
const destructureInSignature = (context.options[1] && context.options[1].destructureInSignature) || 'ignore';
9595
const sfcParams = createSFCParams();
9696

9797
/**
@@ -244,9 +244,9 @@ module.exports = {
244244
SFCComponent
245245
&& destructuringSFC
246246
&& configuration === 'always'
247-
&& destructAtParameter === 'always'
247+
&& destructureInSignature === 'always'
248248
&& node.init.name === 'props'
249-
) {
249+
) {
250250
const scopeSetProps = context.getScope().set.get('props');
251251
const propsRefs = scopeSetProps && scopeSetProps.references;
252252
if (!propsRefs) {
@@ -256,7 +256,7 @@ module.exports = {
256256
if (propsRefs.length > 1) {
257257
return;
258258
}
259-
report(context, messages.destructAtParameter, 'destructAtParameter', {
259+
report(context, messages.destructureInSignature, 'destructureInSignature', {
260260
node,
261261
fix(fixer) {
262262
const param = SFCComponent.node.params[0];

tests/lib/rules/destructuring-assignment.js

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
'use strict';
66

77
const RuleTester = require('eslint').RuleTester;
8+
const semver = require('semver');
9+
const eslintPkg = require('eslint/package.json');
810
const rule = require('../../../lib/rules/destructuring-assignment');
911

1012
const parsers = require('../../helpers/parsers');
@@ -345,7 +347,7 @@ ruleTester.run('destructuring-assignment', rule, {
345347
return <Goo {...props}>{a}</Goo>;
346348
}
347349
`,
348-
options: ['always', { destructAtParameter: 'always' }],
350+
options: ['always', { destructureInSignature: 'always' }],
349351
},
350352
{
351353
code: `
@@ -354,11 +356,11 @@ ruleTester.run('destructuring-assignment', rule, {
354356
return <Goo f={() => props}>{a}</Goo>;
355357
}
356358
`,
357-
options: ['always', { destructAtParameter: 'always' }],
359+
options: ['always', { destructureInSignature: 'always' }],
358360
},
359361
]),
360362

361-
invalid: parsers.all([
363+
invalid: parsers.all([].concat(
362364
{
363365
code: `
364366
const MyComponent = (props) => {
@@ -738,48 +740,51 @@ ruleTester.run('destructuring-assignment', rule, {
738740
},
739741
],
740742
},
741-
{
742-
code: `
743-
function Foo(props) {
744-
const {a} = props;
745-
return <p>{a}</p>;
746-
}
747-
`,
748-
options: ['always', { destructAtParameter: 'always' }],
749-
errors: [
750-
{
751-
messageId: 'destructAtParameter',
752-
line: 3,
753-
},
754-
],
755-
output: `
756-
function Foo({a}) {
757-
758-
return <p>{a}</p>;
759-
}
760-
`,
761-
},
762-
{
763-
code: `
764-
function Foo(props: FooProps) {
765-
const {a} = props;
766-
return <p>{a}</p>;
767-
}
768-
`,
769-
options: ['always', { destructAtParameter: 'always' }],
770-
errors: [
771-
{
772-
messageId: 'destructAtParameter',
773-
line: 3,
774-
},
775-
],
776-
output: `
777-
function Foo({a}: FooProps) {
778-
779-
return <p>{a}</p>;
780-
}
781-
`,
782-
features: ['ts', 'no-babel'],
783-
},
784-
]),
743+
// Ignore for ESLint < 4 because ESLint < 4 does not support array fixer.
744+
semver.satisfies(eslintPkg.version, '>= 4') ? [
745+
{
746+
code: `
747+
function Foo(props) {
748+
const {a} = props;
749+
return <p>{a}</p>;
750+
}
751+
`,
752+
options: ['always', { destructureInSignature: 'always' }],
753+
errors: [
754+
{
755+
messageId: 'destructureInSignature',
756+
line: 3,
757+
},
758+
],
759+
output: `
760+
function Foo({a}) {
761+
762+
return <p>{a}</p>;
763+
}
764+
`,
765+
},
766+
{
767+
code: `
768+
function Foo(props: FooProps) {
769+
const {a} = props;
770+
return <p>{a}</p>;
771+
}
772+
`,
773+
options: ['always', { destructureInSignature: 'always' }],
774+
errors: [
775+
{
776+
messageId: 'destructureInSignature',
777+
line: 3,
778+
},
779+
],
780+
output: `
781+
function Foo({a}: FooProps) {
782+
783+
return <p>{a}</p>;
784+
}
785+
`,
786+
features: ['ts', 'no-babel'],
787+
},
788+
] : []
789+
)),
785790
});

0 commit comments

Comments
 (0)