Skip to content

Commit 089d545

Browse files
committed
[Fix] destructuring-assignment: handle nested props usage
1 parent f0e2e1e commit 089d545

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/rules/destructuring-assignment.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ module.exports = {
8282
}
8383
}
8484

85+
function isInClassProperty(node) {
86+
let curNode = node.parent;
87+
while (curNode) {
88+
if (curNode.type === 'ClassProperty') {
89+
return true;
90+
}
91+
curNode = curNode.parent;
92+
}
93+
return false;
94+
}
95+
8596
function handleClassUsage(node) {
8697
// this.props.Aprop || this.context.aProp || this.state.aState
8798
const isPropUsed = (
@@ -92,7 +103,7 @@ module.exports = {
92103

93104
if (
94105
isPropUsed && configuration === 'always' &&
95-
!(ignoreClassFields && node.parent.type === 'ClassProperty')
106+
!(ignoreClassFields && isInClassProperty(node))
96107
) {
97108
context.report({
98109
node: node,

tests/lib/rules/destructuring-assignment.js

+11
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ ruleTester.run('destructuring-assignment', rule, {
173173
`,
174174
options: ['always', {ignoreClassFields: true}],
175175
parser: 'babel-eslint'
176+
}, {
177+
code: [
178+
'class Input extends React.Component {',
179+
' id = `${this.props.name}`;',
180+
' render() {',
181+
' return <div />;',
182+
' }',
183+
'}'
184+
].join('\n'),
185+
options: ['always', {ignoreClassFields: true}],
186+
parser: 'babel-eslint'
176187
}],
177188

178189
invalid: [{

0 commit comments

Comments
 (0)