diff --git a/lib/rules/destructuring-assignment.js b/lib/rules/destructuring-assignment.js index 2e09440819..9f7d28255f 100644 --- a/lib/rules/destructuring-assignment.js +++ b/lib/rules/destructuring-assignment.js @@ -82,6 +82,17 @@ module.exports = { } } + function isInClassProperty(node) { + let curNode = node.parent; + while (curNode) { + if (curNode.type === 'ClassProperty') { + return true; + } + curNode = curNode.parent; + } + return false; + } + function handleClassUsage(node) { // this.props.Aprop || this.context.aProp || this.state.aState const isPropUsed = ( @@ -92,7 +103,7 @@ module.exports = { if ( isPropUsed && configuration === 'always' && - !(ignoreClassFields && node.parent.type === 'ClassProperty') + !(ignoreClassFields && isInClassProperty(node)) ) { context.report({ node: node, diff --git a/tests/lib/rules/destructuring-assignment.js b/tests/lib/rules/destructuring-assignment.js index 164a41ed0a..e4b80f6108 100644 --- a/tests/lib/rules/destructuring-assignment.js +++ b/tests/lib/rules/destructuring-assignment.js @@ -173,6 +173,17 @@ ruleTester.run('destructuring-assignment', rule, { `, options: ['always', {ignoreClassFields: true}], parser: 'babel-eslint' + }, { + code: [ + 'class Input extends React.Component {', + ' id = `${this.props.name}`;', + ' render() {', + ' return
;', + ' }', + '}' + ].join('\n'), + options: ['always', {ignoreClassFields: true}], + parser: 'babel-eslint' }], invalid: [{