Skip to content

Commit

Permalink
[Fix] no-danger-with-children: prevent infinite loop
Browse files Browse the repository at this point in the history
Fixes #1571
  • Loading branch information
ljharb committed Jan 31, 2018
1 parent 70e8a02 commit 5e1a64c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/rules/no-danger-with-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
* @param {object} node - ObjectExpression node
* @param {string} propName - name of the prop to look for
*/
function findObjectProp(node, propName) {
function findObjectProp(node, propName, seenProps) {
if (!node.properties) {
return false;
}
Expand All @@ -39,7 +39,11 @@ module.exports = {
} else if (prop.type === 'ExperimentalSpreadProperty') {
const variable = findSpreadVariable(prop.argument.name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
return findObjectProp(variable.defs[0].node.init, propName);
if (seenProps.indexOf(prop.argument.name) > -1) {
return false;
}
const newSeenProps = seenProps.concat(prop.argument.name || []);
return findObjectProp(variable.defs[0].node.init, propName, newSeenProps);
}
}
return false;
Expand All @@ -57,7 +61,7 @@ module.exports = {
if (attribute.type === 'JSXSpreadAttribute') {
const variable = findSpreadVariable(attribute.argument.name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
return findObjectProp(variable.defs[0].node.init, propName);
return findObjectProp(variable.defs[0].node.init, propName, []);
}
}
return attribute.name && attribute.name.name === propName;
Expand Down Expand Up @@ -113,10 +117,10 @@ module.exports = {
}
}

const dangerously = findObjectProp(props, 'dangerouslySetInnerHTML');
const dangerously = findObjectProp(props, 'dangerouslySetInnerHTML', []);

if (node.arguments.length === 2) {
if (findObjectProp(props, 'children')) {
if (findObjectProp(props, 'children', [])) {
hasChildren = true;
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-danger-with-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ ruleTester.run('no-danger-with-children', rule, {
},
{
code: 'React.createElement("Hello", undefined, "Children")'
},
{
code: `
const props = {...props, scratch: {mode: 'edit'}};
const component = shallow(<TaskEditableTitle {...props} />);
`
}
],
invalid: [
Expand Down

0 comments on commit 5e1a64c

Please sign in to comment.