Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defensively code BabylonParser object accessors #3259

Merged
merged 2 commits into from
Apr 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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