From 0d32480114a2a8de28d6649be36250fea440c843 Mon Sep 17 00:00:00 2001 From: Takuto Wada Date: Mon, 7 Apr 2014 16:08:08 +0900 Subject: [PATCH] Refactoring --- lib/espower.js | 73 ++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/lib/espower.js b/lib/espower.js index fea7b7c..db56496 100644 --- a/lib/espower.js +++ b/lib/espower.js @@ -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; @@ -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); @@ -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; @@ -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; } @@ -173,7 +177,7 @@ //console.log('leave ' + currentNode.type + ' path: ' + path); - if (capturing === '') { + if (capturingPath === '') { return undefined; } @@ -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)); }