Skip to content

Commit

Permalink
guard infinite loop against chained, static methods
Browse files Browse the repository at this point in the history
  * Add `isValidCalleeType()` check for CallExpresion
    and ArrayExpression and Literal
  * Fixes #136
  • Loading branch information
tchon authored and fkling committed Dec 21, 2016
1 parent ab37d8b commit 193499b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/__tests__/fixtures/component_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ export class Button extends React.Component {
};
}
}

export function foo() {
return [].join();
}

export function chained() {
return foo.bar().join();
}

export function templateLiteral() {
return `foo bar`.split(' ');
}
12 changes: 11 additions & 1 deletion src/utils/isStatelessComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ const validPossibleStatelessComponentTypes = [
'ArrowFunctionExpression',
];

function isValidCalleeType(type) {
return [
'Identifier',
'CallExpression',
'ArrayExpression',
'TemplateLiteral',
'Literal'
].indexOf(type) < 0;
}

function isJSXElementOrReactCreateElement(path) {
return (
path.node.type === 'JSXElement' ||
Expand Down Expand Up @@ -94,7 +104,7 @@ function returnsJSXElementOrReactCreateElementCall(path) {
resolvedValue = resolveToValue(calleeValue.get('object'));
}
else {
while (calleeValue.get('object').node.type !== 'Identifier') {
while (isValidCalleeType(calleeValue.get('object').node.type)) {
calleeValue = calleeValue.get('object');
namesToResolve.unshift(calleeValue.get('property'));
}
Expand Down

0 comments on commit 193499b

Please sign in to comment.