Skip to content

Commit

Permalink
Defensively code BabylonParser object accessors (jestjs#3259)
Browse files Browse the repository at this point in the history
  • Loading branch information
James King authored and cpojer committed Apr 11, 2017
1 parent 7eb9d6a commit 8b07eee
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/jest-editor-support/src/parsers/BabylonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const parse = (file: string) => {

const isFunctionCall = node => {
return node.type === 'ExpressionStatement' &&
node.expression &&
node.expression.type === 'CallExpression';
};

Expand All @@ -107,8 +108,16 @@ const parse = (file: string) => {
if (!isFunctionCall(node)) {
return false;
}
let {name} = node.expression.callee;
if (!name && node.expression.callee.object) {
let name = node && node.expression && node.expression.callee
? node.expression.callee.name
: undefined;
if (
!name &&
node &&
node.expression &&
node.expression.callee &&
node.expression.callee.object
) {
name = node.expression.callee.object.name;
}
return name;
Expand All @@ -127,9 +136,9 @@ const parse = (file: string) => {
if (!isFunctionCall(node)) {
return false;
}
let name: string = '';
let element = node.expression.callee;
while (!name) {
let name = '';
let element = node && node.expression ? node.expression.callee : undefined;
while (!name && element) {
name = element.name;
// Because expect may have acccessors taked on (.to.be) or
// nothing (expect()) we have to check mulitple levels for the name
Expand All @@ -153,16 +162,19 @@ const parse = (file: string) => {
foundItNode(element, file);
} else if (isAnExpect(element)) {
foundExpectNode(element, file);
} else if (element.type === 'VariableDeclaration') {
} else if (element && element.type === 'VariableDeclaration') {
element.declarations
.filter(
declaration =>
declaration.init && isFunctionDeclaration(declaration.init.type),
)
.forEach(declaration => searchNodes(declaration.init.body, file));
} else if (
element &&
element.type === 'ExpressionStatement' &&
element.expression &&
element.expression.type === 'AssignmentExpression' &&
element.expression.right &&
isFunctionDeclaration(element.expression.right.type)
) {
searchNodes(element.expression.right.body, file);
Expand Down

0 comments on commit 8b07eee

Please sign in to comment.