diff --git a/docs/rules/destructuring-assignment.md b/docs/rules/destructuring-assignment.md index ac97b46cb0..e930b31cbb 100644 --- a/docs/rules/destructuring-assignment.md +++ b/docs/rules/destructuring-assignment.md @@ -91,7 +91,7 @@ const Foo = class extends React.PureComponent { ```js ... -"react/destructuring-assignment": [, "always", { "ignoreClassFields": , "destructAtParameter": "always" | "ignore" }] +"react/destructuring-assignment": [, "always", { "ignoreClassFields": , "destructureInSignature": "always" | "ignore" }] ... ``` @@ -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) { @@ -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}) { diff --git a/lib/rules/destructuring-assignment.js b/lib/rules/destructuring-assignment.js index f0e2044ad6..2536bd671d 100644 --- a/lib/rules/destructuring-assignment.js +++ b/lib/rules/destructuring-assignment.js @@ -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 = { @@ -76,7 +76,7 @@ module.exports = { ignoreClassFields: { type: 'boolean', }, - destructAtParameter: { + destructureInSignature: { type: 'string', enum: [ 'always', @@ -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(); /** @@ -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) { @@ -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]; diff --git a/tests/lib/rules/destructuring-assignment.js b/tests/lib/rules/destructuring-assignment.js index 978fd5cca7..ac104de085 100644 --- a/tests/lib/rules/destructuring-assignment.js +++ b/tests/lib/rules/destructuring-assignment.js @@ -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'); @@ -345,7 +347,7 @@ ruleTester.run('destructuring-assignment', rule, { return {a}; } `, - options: ['always', { destructAtParameter: 'always' }], + options: ['always', { destructureInSignature: 'always' }], }, { code: ` @@ -354,11 +356,11 @@ ruleTester.run('destructuring-assignment', rule, { return props}>{a}; } `, - options: ['always', { destructAtParameter: 'always' }], + options: ['always', { destructureInSignature: 'always' }], }, ]), - invalid: parsers.all([ + invalid: parsers.all([].concat( { code: ` const MyComponent = (props) => { @@ -738,48 +740,51 @@ ruleTester.run('destructuring-assignment', rule, { }, ], }, - { - code: ` - function Foo(props) { - const {a} = props; - return

{a}

; - } - `, - options: ['always', { destructAtParameter: 'always' }], - errors: [ - { - messageId: 'destructAtParameter', - line: 3, - }, - ], - output: ` - function Foo({a}) { - - return

{a}

; - } - `, - }, - { - code: ` - function Foo(props: FooProps) { - const {a} = props; - return

{a}

; - } - `, - options: ['always', { destructAtParameter: 'always' }], - errors: [ - { - messageId: 'destructAtParameter', - line: 3, - }, - ], - output: ` - function Foo({a}: FooProps) { - - return

{a}

; - } - `, - 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

{a}

; + } + `, + options: ['always', { destructureInSignature: 'always' }], + errors: [ + { + messageId: 'destructureInSignature', + line: 3, + }, + ], + output: ` + function Foo({a}) { + + return

{a}

; + } + `, + }, + { + code: ` + function Foo(props: FooProps) { + const {a} = props; + return

{a}

; + } + `, + options: ['always', { destructureInSignature: 'always' }], + errors: [ + { + messageId: 'destructureInSignature', + line: 3, + }, + ], + output: ` + function Foo({a}: FooProps) { + + return

{a}

; + } + `, + features: ['ts', 'no-babel'], + }, + ] : [] + )), });