Skip to content

Commit 4724d48

Browse files
authored
Merge pull request #1365 from DianaSuvorova/i1288
[prop types] fix an assignment to prop
2 parents b9bb564 + 6272a1d commit 4724d48

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/rules/prop-types.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ module.exports = {
116116
return false;
117117
}
118118

119+
/**
120+
* Checks if a prop is being assigned a value props.bar = 'bar'
121+
* @param {ASTNode} node The AST node being checked.
122+
* @returns {Boolean}
123+
*/
124+
125+
function isAssignmentToProp(node) {
126+
return (
127+
node.parent &&
128+
node.parent.type === 'AssignmentExpression' &&
129+
node.parent.left === node
130+
);
131+
}
132+
119133
/**
120134
* Checks if we are using a prop
121135
* @param {ASTNode} node The AST node being checked.
@@ -126,7 +140,7 @@ module.exports = {
126140
(utils.getParentES6Component() || utils.getParentES5Component()) &&
127141
node.object.type === 'ThisExpression' && node.property.name === 'props'
128142
);
129-
const isStatelessFunctionUsage = node.object.name === 'props';
143+
const isStatelessFunctionUsage = node.object.name === 'props' && !isAssignmentToProp(node);
130144
const isNextPropsUsage = node.object.name === 'nextProps' && inComponentWillReceiveProps();
131145
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
132146
}

tests/lib/rules/prop-types.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,18 @@ ruleTester.run('prop-types', rule, {
15571557
'}'
15581558
].join('\n'),
15591559
parser: 'babel-eslint'
1560-
}
1560+
},
1561+
// issue #1288
1562+
`function Foo() {
1563+
const props = {}
1564+
props.bar = 'bar'
1565+
return <div {...props} />
1566+
}`,
1567+
// issue #1288
1568+
`function Foo(props) {
1569+
props.bar = 'bar';
1570+
return <div {...props} />;
1571+
}`
15611572
],
15621573

15631574
invalid: [

0 commit comments

Comments
 (0)