Comparing version 0.1.5 to 0.1.6
@@ -41,21 +41,89 @@ /** | ||
var deepCopy, | ||
SUPPORTED_NODE_TYPES = [ | ||
'Identifier', | ||
'MemberExpression', | ||
'CallExpression', | ||
'BinaryExpression', | ||
'UnaryExpression', | ||
'ConditionalExpression', | ||
'LogicalExpression', | ||
'ArrayExpression', | ||
'AssignmentExpression', | ||
'UpdateExpression' | ||
handlers = { | ||
Identifier: function (instrumentor, node) { | ||
return instrumentor.captureNode('ident', node, instrumentor.locationOf(node)); | ||
}, | ||
MemberExpression: function (instrumentor, node) { | ||
var propLocation = instrumentor.propertyLocationOf(node); | ||
node.object = instrumentor.captureRecursively(node.object); | ||
if (node.computed) { | ||
node.property = instrumentor.captureRecursively(node.property); | ||
} | ||
return instrumentor.captureNode('ident', node, propLocation); | ||
}, | ||
CallExpression: function (instrumentor, node) { | ||
node.arguments = node.arguments.map(function (arg) { | ||
return instrumentor.captureRecursively(arg); | ||
}); | ||
if (node.callee.type === 'MemberExpression') { | ||
node.callee.object = instrumentor.captureRecursively(node.callee.object); | ||
return instrumentor.captureNode('funcall', node, instrumentor.propertyLocationOf(node.callee)); | ||
} else { | ||
return instrumentor.captureNode('funcall', node, instrumentor.locationOf(node)); | ||
} | ||
}, | ||
UnaryExpression: function (instrumentor, node) { | ||
if ((node.operator === 'typeof' || node.operator === 'delete') && node.argument.type === 'Identifier') { | ||
// 'typeof Identifier' or 'delete Identifier' is not instrumented | ||
} else { | ||
node.argument = instrumentor.captureRecursively(node.argument); | ||
} | ||
return instrumentor.captureNode('unary', node, instrumentor.locationOf(node)); | ||
}, | ||
BinaryExpression: function (instrumentor, node) { | ||
return instrumentor.captureExpressionWithInfixOperator('binary', node); | ||
}, | ||
LogicalExpression: function (instrumentor, node) { | ||
return instrumentor.captureExpressionWithInfixOperator('logical', node); | ||
}, | ||
AssignmentExpression: function (instrumentor, node) { | ||
return instrumentor.captureExpressionWithInfixOperator('assignment', node); | ||
}, | ||
ObjectExpression: function (instrumentor, node) { | ||
node.properties.forEach(function (prop) { | ||
if (prop.type === 'Property' && prop.kind === 'init') { | ||
prop.value = instrumentor.captureRecursively(prop.value); | ||
} | ||
}); | ||
return node; | ||
}, | ||
NewExpression: function (instrumentor, node) { | ||
node.arguments = node.arguments.map(function (arg) { | ||
return instrumentor.captureRecursively(arg); | ||
}); | ||
return instrumentor.captureNode('new', node, instrumentor.locationOf(node)); | ||
}, | ||
ArrayExpression: function (instrumentor, node) { | ||
node.elements = node.elements.map(function (elem) { | ||
return instrumentor.captureRecursively(elem); | ||
}); | ||
return node; | ||
}, | ||
ConditionalExpression: function (instrumentor, node) { | ||
node.test = instrumentor.captureRecursively(node.test); | ||
node.consequent = instrumentor.captureRecursively(node.consequent); | ||
node.alternate = instrumentor.captureRecursively(node.alternate); | ||
return node; | ||
}, | ||
UpdateExpression: function (instrumentor, node) { | ||
return instrumentor.captureNode('update', node, instrumentor.locationOf(node)); | ||
} | ||
//// unsupported expressions | ||
// 'FunctionExpression', | ||
// 'NewExpression', | ||
// 'ObjectExpression', | ||
// 'RegularExpression', | ||
// 'SequenceExpression', | ||
// 'ThisExpression', | ||
]; | ||
// FunctionExpression | ||
// RegularExpression | ||
// SequenceExpression | ||
// ThisExpression | ||
}; | ||
@@ -150,24 +218,5 @@ | ||
LineInstrumentor.prototype.captureRecursively = function (node) { | ||
switch (node.type) { | ||
case 'Identifier': | ||
return this.captureIdentifier(node); | ||
case 'MemberExpression': | ||
return this.captureMemberExpression(node); | ||
case 'CallExpression': | ||
return this.captureCallExpression(node); | ||
case 'UnaryExpression': | ||
return this.captureUnaryExpression(node); | ||
case 'BinaryExpression': | ||
return this.captureBinaryExpression(node); | ||
case 'LogicalExpression': | ||
return this.captureLogicalExpression(node); | ||
case 'AssignmentExpression': | ||
return this.captureAssignmentExpression(node); | ||
case 'ArrayExpression': | ||
return this.captureArrayExpression(node); | ||
case 'ConditionalExpression': | ||
return this.captureConditionalExpression(node); | ||
case 'UpdateExpression': | ||
return this.captureUpdateExpression(node); | ||
default: | ||
if (isSupportedNodeType (node)) { | ||
return nodeHandlerFor(node.type).call(null, this, node); | ||
} else { | ||
return node; | ||
@@ -177,69 +226,3 @@ } | ||
LineInstrumentor.prototype.captureIdentifier = function (target, location) { | ||
return this.captureNode('ident', target, (location || this.locationOf(target))); | ||
}; | ||
LineInstrumentor.prototype.captureMemberExpression = function (node) { | ||
var propLocation = this.propertyLocationOf(node); | ||
node.object = this.captureRecursively(node.object); | ||
if (node.computed) { | ||
node.property = this.captureRecursively(node.property); | ||
} | ||
return this.captureIdentifier(node, propLocation); | ||
}; | ||
LineInstrumentor.prototype.captureCallExpression = function (node) { | ||
var that = this; | ||
node.arguments = node.arguments.map(function (arg) { | ||
return that.captureRecursively(arg); | ||
}); | ||
if (node.callee.type === 'MemberExpression') { | ||
node.callee.object = this.captureRecursively(node.callee.object); | ||
return this.captureNode('funcall', node, this.propertyLocationOf(node.callee)); | ||
} else { | ||
return this.captureNode('funcall', node, this.locationOf(node)); | ||
} | ||
}; | ||
LineInstrumentor.prototype.captureUnaryExpression = function (node) { | ||
if ((node.operator === 'typeof' || node.operator === 'delete') && node.argument.type === 'Identifier') { | ||
// 'typeof Identifier' or 'delete Identifier' is not instrumented | ||
} else { | ||
node.argument = this.captureRecursively(node.argument); | ||
} | ||
return this.captureNode('unary', node, this.locationOf(node)); | ||
}; | ||
LineInstrumentor.prototype.captureBinaryExpression = function (node) { | ||
return this.captureExpressionWithInfixOperator(node, 'binary'); | ||
}; | ||
LineInstrumentor.prototype.captureLogicalExpression = function (node) { | ||
return this.captureExpressionWithInfixOperator(node, 'logical'); | ||
}; | ||
LineInstrumentor.prototype.captureAssignmentExpression = function (node) { | ||
return this.captureExpressionWithInfixOperator(node, 'assignment'); | ||
}; | ||
LineInstrumentor.prototype.captureArrayExpression = function (node) { | ||
var that = this; | ||
node.elements = node.elements.map(function (elem) { | ||
return that.captureRecursively(elem); | ||
}); | ||
return node; | ||
}; | ||
LineInstrumentor.prototype.captureConditionalExpression = function (node) { | ||
node.test = this.captureRecursively(node.test); | ||
node.consequent = this.captureRecursively(node.consequent); | ||
node.alternate = this.captureRecursively(node.alternate); | ||
return node; | ||
}; | ||
LineInstrumentor.prototype.captureUpdateExpression = function (node) { | ||
return this.captureNode('update', node, this.locationOf(node)); | ||
}; | ||
LineInstrumentor.prototype.captureExpressionWithInfixOperator = function (expression, kind) { | ||
LineInstrumentor.prototype.captureExpressionWithInfixOperator = function (kind, expression) { | ||
// BK: need to detect operator location before left/right instrumentation | ||
@@ -467,5 +450,9 @@ var infixOperatorLocation = this.infixOperatorLocationOf(expression); | ||
function isSupportedNodeType (node) { | ||
return SUPPORTED_NODE_TYPES.indexOf(node.type) !== -1; | ||
return typeof nodeHandlerFor(node.type) === 'function'; | ||
} | ||
function nodeHandlerFor (nodeType) { | ||
return handlers[nodeType]; | ||
} | ||
function ensureAstPrerequisites (ast, options) { | ||
@@ -566,2 +553,3 @@ var errorMessage; | ||
// using returnExports UMD pattern with substack pattern | ||
espower.handlers = handlers; | ||
espower.deepCopy = deepCopy; | ||
@@ -568,0 +556,0 @@ espower.traverse = traverse; |
{ | ||
"name": "espower", | ||
"description": "Power Assert feature instrumentor based on the Mozilla JavaScript AST", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"keywords": [ | ||
@@ -33,5 +33,4 @@ "test", | ||
"coffee-script-redux": "2.0.0-beta7", | ||
"mocha": "1.12.1", | ||
"grunt": "~0.4.1", | ||
"grunt-mocha-test": "~0.6.3", | ||
"grunt-mocha-test": "~0.7.0", | ||
"grunt-contrib-jshint": "~0.6.4" | ||
@@ -38,0 +37,0 @@ }, |
@@ -184,2 +184,25 @@ var espower = require('../lib/espower'), | ||
describe('ObjectExpression', function () { | ||
inst("assert({foo: bar, hoge: fuga});", | ||
"assert(assert._expr({foo:assert._capt(bar,'ident',{start:{line:1,column:13}}),hoge:assert._capt(fuga,'ident',{start:{line:1,column:24}})},{start:{line:1,column:7}},'assert({foo: bar, hoge: fuga});'));"); | ||
inst("assert(!({ foo: bar.baz, name: nameOf({firstName: first, lastName: last}) }));", | ||
"assert(assert._expr(assert._capt(!{foo:assert._capt(assert._capt(bar,'ident',{start:{line:1,column:16}}).baz,'ident',{start:{line:1,column:20}}),name:assert._capt(nameOf({firstName:assert._capt(first,'ident',{start:{line:1,column:50}}),lastName:assert._capt(last,'ident',{start:{line:1,column:67}})}),'funcall',{start:{line:1,column:31}})},'unary',{start:{line:1,column:7}}),{start:{line:1,column:7}},'assert(!({ foo: bar.baz, name: nameOf({firstName: first, lastName: last}) }));'));"); | ||
}); | ||
describe('NewExpression', function () { | ||
inst("assert(new Date());", | ||
"assert(assert._expr(assert._capt(new Date(),'new',{start:{line:1,column:7}}),{start:{line:1,column:7}},'assert(new Date());'));"); | ||
inst("assert(!(new Array(foo, bar, baz)));", | ||
"assert(assert._expr(assert._capt(!assert._capt(new Array(assert._capt(foo,'ident',{start:{line:1,column:19}}),assert._capt(bar,'ident',{start:{line:1,column:24}}),assert._capt(baz,'ident',{start:{line:1,column:29}})),'new',{start:{line:1,column:9}}),'unary',{start:{line:1,column:7}}),{start:{line:1,column:7}},'assert(!(new Array(foo, bar, baz)));'));"); | ||
}); | ||
describe('FunctionExpression will not be instrumented', function () { | ||
inst("assert(baz === (function (a, b) { return a + b; })(foo, bar));", | ||
"assert(assert._expr(assert._capt(assert._capt(baz,'ident',{start:{line:1,column:7}})===assert._capt(function(a,b){return a+b;}(assert._capt(foo,'ident',{start:{line:1,column:51}}),assert._capt(bar,'ident',{start:{line:1,column:56}})),'funcall',{start:{line:1,column:15}}),'binary',{start:{line:1,column:11}}),{start:{line:1,column:7}},'assert(baz === (function (a, b) { return a + b; })(foo, bar));'));"); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52429
6
874