From 4382aa547d2ca73b1ba2c2e51d3a45a16bbe44c3 Mon Sep 17 00:00:00 2001 From: Alexander Nanberg Date: Mon, 30 Jul 2018 17:48:47 +0200 Subject: [PATCH] fix: destructuring-assignment ignore class properties Add ignoreClassFields option --- docs/rules/destructuring-assignment.md | 21 +++++++++++++++++++++ lib/rules/destructuring-assignment.js | 20 +++++++++++++++++--- tests/lib/rules/destructuring-assignment.js | 8 ++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/docs/rules/destructuring-assignment.md b/docs/rules/destructuring-assignment.md index def9567bcd..d539d47898 100644 --- a/docs/rules/destructuring-assignment.md +++ b/docs/rules/destructuring-assignment.md @@ -85,3 +85,24 @@ const Foo = class extends React.PureComponent { return
{this.state.title}
; } }; +``` + +## Rule Options + +```js +... +"react/destructuring-assignment": [, "always", { "ignoreClassFields": }] +... +``` + +### `ignoreClassFields` + +When `true` the rule will ignore class field declarations. + +The following patterns are then considered okay and do not cause warnings: + +```jsx +class Foo extends React.PureComponent { + bar = this.props.bar +} +``` diff --git a/lib/rules/destructuring-assignment.js b/lib/rules/destructuring-assignment.js index 45c2af91f7..2e09440819 100644 --- a/lib/rules/destructuring-assignment.js +++ b/lib/rules/destructuring-assignment.js @@ -22,12 +22,20 @@ module.exports = { 'always', 'never' ] + }, { + type: 'object', + properties: { + ignoreClassFields: { + type: 'boolean' + } + }, + additionalProperties: false }] }, create: Components.detect((context, components, utils) => { const configuration = context.options[0] || DEFAULT_OPTION; - + const ignoreClassFields = context.options[1] && context.options[1].ignoreClassFields === true || false; /** * Checks if a prop is being assigned a value props.bar = 'bar' @@ -82,7 +90,10 @@ module.exports = { !isAssignmentToProp(node) ); - if (isPropUsed && configuration === 'always') { + if ( + isPropUsed && configuration === 'always' && + !(ignoreClassFields && node.parent.type === 'ClassProperty') + ) { context.report({ node: node, message: `Must use destructuring ${node.object.property.name} assignment` @@ -128,7 +139,10 @@ module.exports = { }); } - if (classComponent && destructuringClass && configuration === 'never') { + if ( + classComponent && destructuringClass && configuration === 'never' && + !(ignoreClassFields && node.parent.type === 'ClassProperty') + ) { context.report({ node: node, message: `Must never use destructuring ${node.init.property.name} assignment` diff --git a/tests/lib/rules/destructuring-assignment.js b/tests/lib/rules/destructuring-assignment.js index b1200c2d06..164a41ed0a 100644 --- a/tests/lib/rules/destructuring-assignment.js +++ b/tests/lib/rules/destructuring-assignment.js @@ -165,6 +165,14 @@ ruleTester.run('destructuring-assignment', rule, { } } ` + }, { + code: ` + class Foo extends React.Component { + bar = this.props.bar + } + `, + options: ['always', {ignoreClassFields: true}], + parser: 'babel-eslint' }], invalid: [{