Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
twada committed Apr 7, 2014
1 parent d05e80d commit 0d32480
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions lib/espower.js
Expand Up @@ -85,6 +85,32 @@
}


function isLeftHandSideOfAssignment(parentNode, currentPath) {
// Do not instrument left due to 'Invalid left-hand side in assignment'
return parentNode.type === syntax.AssignmentExpression && currentPath === 'left';
}

function isObjectLiteralKey(parentNode, currentPath) {
// Do not instrument Object literal key
return parentNode.type === syntax.Property && parentNode.kind === 'init' && currentPath === 'key';
}

function isUpdateExpression(parentNode) {
// Just wrap UpdateExpression, not digging in.
return parentNode.type === syntax.UpdateExpression;
}

function isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentPath) {
// Do not instrument non-computed property of MemberExpression within CallExpression.
return currentNode.type === syntax.Identifier && parentNode.type === syntax.MemberExpression && !parentNode.computed && currentPath === 'property';
}

function isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentPath) {
// 'typeof Identifier' or 'delete Identifier' is not instrumented
return currentNode.type === syntax.Identifier && parentNode.type === syntax.UnaryExpression && (parentNode.operator === 'typeof' || parentNode.operator === 'delete') && currentPath === 'argument';
}


function SourceInstrumentor (options) {
ensureOptionPrerequisites(options);
this.options = options;
Expand All @@ -94,7 +120,7 @@
SourceInstrumentor.prototype.instrument = function (ast) {
ensureAstPrerequisites(ast, this.options);
var that = this,
capturing = '',
capturingPath = '',
skipping = false,
instrumentor,
result = (this.options.destructive) ? ast : deepCopy(ast);
Expand All @@ -108,38 +134,16 @@

//console.log('enter currentNode:' + currentNode.type + ' parentNode: ' + parentNode.type + ' path: ' + path);

if (capturing !== '') {
if (!isSupportedNodeType(currentNode)) {
if (capturingPath !== '') {
if ((!isSupportedNodeType(currentNode)) ||
(isLeftHandSideOfAssignment(parentNode, currentPath)) ||
(isObjectLiteralKey(parentNode, currentPath)) ||
(isUpdateExpression(parentNode)) ||
(isCallExpressionWithNonComputedMemberExpression(currentNode, parentNode, currentPath)) ||
(isTypeOfOrDeleteUnaryExpression(currentNode, parentNode, currentPath))) {
skipping = true;
return estraverse.VisitorOption.Skip;
}

if(parentNode.type === syntax.AssignmentExpression && currentPath === 'left') {
// Do not instrument left due to 'Invalid left-hand side in assignment'
skipping = true;
return estraverse.VisitorOption.Skip;
}
if(parentNode.type === syntax.Property && parentNode.kind === 'init' && currentPath === 'key') {
// Do not instrument Object literal key
skipping = true;
return estraverse.VisitorOption.Skip;
}
if(parentNode.type === syntax.UpdateExpression) {
// Just wrap UpdateExpression, not digging in.
skipping = true;
return estraverse.VisitorOption.Skip;
}
if (currentNode.type === syntax.Identifier && parentNode.type === syntax.MemberExpression && !parentNode.computed && currentPath === 'property') {
// Do not instrument non-computed property of MemberExpression within CallExpression.
skipping = true;
return estraverse.VisitorOption.Skip;
}
if (currentNode.type === syntax.Identifier && parentNode.type === syntax.UnaryExpression && (parentNode.operator === 'typeof' || parentNode.operator === 'delete') && currentPath === 'argument') {
// 'typeof Identifier' or 'delete Identifier' is not instrumented
skipping = true;
return estraverse.VisitorOption.Skip;
}

} else {
if (!parentNode || parentNode.type !== syntax.CallExpression) {
return undefined;
Expand All @@ -156,7 +160,7 @@
}
indexOfCurrentArg = parentNode.arguments.indexOf(currentNode);
if (indexOfCurrentArg < numTargetArgs) {
capturing = path.join('/');
capturingPath = path.join('/');
instrumentor = that.instrumentArgument(parentNode, currentNode);
return undefined;
}
Expand All @@ -173,7 +177,7 @@

//console.log('leave ' + currentNode.type + ' path: ' + path);

if (capturing === '') {
if (capturingPath === '') {
return undefined;
}

Expand Down Expand Up @@ -233,9 +237,8 @@
}
//console.log('leave ' + currentNode.type + ' path: ' + path);

if (modifiedTree && path && capturing === path.join('/')) {
//console.log('leaving ' + capturing);
capturing = '';
if (modifiedTree && path && capturingPath === path.join('/')) {
capturingPath = '';
return instrumentor.captureAssertion(modifiedTree, instrumentor.locationOf(currentNode));
}

Expand Down

0 comments on commit 0d32480

Please sign in to comment.