Skip to content

Commit

Permalink
Omit test cases for eslint < 4 and rename destructAtParameter to dest…
Browse files Browse the repository at this point in the history
…ructureInSignature
  • Loading branch information
golopot committed Mar 16, 2022
1 parent 94cbd79 commit ef8c784
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 57 deletions.
8 changes: 4 additions & 4 deletions docs/rules/destructuring-assignment.md
Expand Up @@ -91,7 +91,7 @@ const Foo = class extends React.PureComponent {

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

Expand All @@ -105,11 +105,11 @@ class Foo extends React.PureComponent {
}
```

### `destructAtParameter` (default: "ignore")
### `destructureInSignature` (default: "ignore")

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

Examples of **incorrect** code for `destructAtParameter: 'always'` :
Examples of **incorrect** code for `destructureInSignature: 'always'` :

```jsx
function Foo(props) {
Expand All @@ -118,7 +118,7 @@ function Foo(props) {
}
```

Examples of **correct** code for `destructAtParameter: 'always'` :
Examples of **correct** code for `destructureInSignature: 'always'` :

```jsx
function Foo({a}) {
Expand Down
12 changes: 6 additions & 6 deletions lib/rules/destructuring-assignment.js
Expand Up @@ -50,7 +50,7 @@ const messages = {
noDestructContextInSFCArg: 'Must never use destructuring context assignment in SFC argument',
noDestructAssignment: 'Must never use destructuring {{type}} assignment',
useDestructAssignment: 'Must use destructuring {{type}} assignment',
destructAtParameter: 'Must destructure props in the function signature.',
destructureInSignature: 'Must destructure props in the function signature.',
};

module.exports = {
Expand All @@ -76,7 +76,7 @@ module.exports = {
ignoreClassFields: {
type: 'boolean',
},
destructAtParameter: {
destructureInSignature: {
type: 'string',
enum: [
'always',
Expand All @@ -91,7 +91,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 destructAtParameter = (context.options[1] && context.options[1].destructAtParameter) || 'ignore';
const destructureInSignature = (context.options[1] && context.options[1].destructureInSignature) || 'ignore';
const sfcParams = createSFCParams();

/**
Expand Down Expand Up @@ -244,9 +244,9 @@ module.exports = {
SFCComponent
&& destructuringSFC
&& configuration === 'always'
&& destructAtParameter === 'always'
&& destructureInSignature === 'always'
&& node.init.name === 'props'
) {
) {
const scopeSetProps = context.getScope().set.get('props');
const propsRefs = scopeSetProps && scopeSetProps.references;
if (!propsRefs) {
Expand All @@ -256,7 +256,7 @@ module.exports = {
if (propsRefs.length > 1) {
return;
}
report(context, messages.destructAtParameter, 'destructAtParameter', {
report(context, messages.destructureInSignature, 'destructureInSignature', {
node,
fix(fixer) {
const param = SFCComponent.node.params[0];
Expand Down
99 changes: 52 additions & 47 deletions tests/lib/rules/destructuring-assignment.js
Expand Up @@ -5,6 +5,8 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const semver = require('semver');
const eslintPkg = require('eslint/package.json');
const rule = require('../../../lib/rules/destructuring-assignment');

const parsers = require('../../helpers/parsers');
Expand Down Expand Up @@ -345,7 +347,7 @@ ruleTester.run('destructuring-assignment', rule, {
return <Goo {...props}>{a}</Goo>;
}
`,
options: ['always', { destructAtParameter: 'always' }],
options: ['always', { destructureInSignature: 'always' }],
},
{
code: `
Expand All @@ -354,11 +356,11 @@ ruleTester.run('destructuring-assignment', rule, {
return <Goo f={() => props}>{a}</Goo>;
}
`,
options: ['always', { destructAtParameter: 'always' }],
options: ['always', { destructureInSignature: 'always' }],
},
]),

invalid: parsers.all([
invalid: parsers.all([].concat(
{
code: `
const MyComponent = (props) => {
Expand Down Expand Up @@ -738,48 +740,51 @@ ruleTester.run('destructuring-assignment', rule, {
},
],
},
{
code: `
function Foo(props) {
const {a} = props;
return <p>{a}</p>;
}
`,
options: ['always', { destructAtParameter: 'always' }],
errors: [
{
messageId: 'destructAtParameter',
line: 3,
},
],
output: `
function Foo({a}) {
return <p>{a}</p>;
}
`,
},
{
code: `
function Foo(props: FooProps) {
const {a} = props;
return <p>{a}</p>;
}
`,
options: ['always', { destructAtParameter: 'always' }],
errors: [
{
messageId: 'destructAtParameter',
line: 3,
},
],
output: `
function Foo({a}: FooProps) {
return <p>{a}</p>;
}
`,
features: ['ts', 'no-babel'],
},
]),
// Ignore for ESLint < 4 because ESLint < 4 does not support array fixer.
semver.satisfies(eslintPkg.version, '>= 4') ? [
{
code: `
function Foo(props) {
const {a} = props;
return <p>{a}</p>;
}
`,
options: ['always', { destructureInSignature: 'always' }],
errors: [
{
messageId: 'destructureInSignature',
line: 3,
},
],
output: `
function Foo({a}) {
return <p>{a}</p>;
}
`,
},
{
code: `
function Foo(props: FooProps) {
const {a} = props;
return <p>{a}</p>;
}
`,
options: ['always', { destructureInSignature: 'always' }],
errors: [
{
messageId: 'destructureInSignature',
line: 3,
},
],
output: `
function Foo({a}: FooProps) {
return <p>{a}</p>;
}
`,
features: ['ts', 'no-babel'],
},
] : []
)),
});

0 comments on commit ef8c784

Please sign in to comment.