Skip to content

Commit

Permalink
Merge pull request #1983 from alexzherdev/1982-destructuring-class-pr…
Browse files Browse the repository at this point in the history
…operty

[Fix] `destructuring-assignment`: handle nested props usage
  • Loading branch information
ljharb committed Sep 14, 2018
2 parents f0e2e1e + 089d545 commit 60b5642
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/rules/destructuring-assignment.js
Expand Up @@ -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 = (
Expand All @@ -92,7 +103,7 @@ module.exports = {

if (
isPropUsed && configuration === 'always' &&
!(ignoreClassFields && node.parent.type === 'ClassProperty')
!(ignoreClassFields && isInClassProperty(node))
) {
context.report({
node: node,
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/destructuring-assignment.js
Expand Up @@ -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 <div />;',
' }',
'}'
].join('\n'),
options: ['always', {ignoreClassFields: true}],
parser: 'babel-eslint'
}],

invalid: [{
Expand Down

0 comments on commit 60b5642

Please sign in to comment.