Comparing version 0.1.3 to 0.1.4
@@ -41,6 +41,29 @@ /** | ||
var deepCopy, | ||
DEFAULT_OPTIONS = { | ||
SUPPORTED_NODE_TYPES = [ | ||
'Identifier', | ||
'MemberExpression', | ||
'CallExpression', | ||
'BinaryExpression', | ||
'UnaryExpression', | ||
'ConditionalExpression', | ||
'LogicalExpression', | ||
'ArrayExpression', | ||
'AssignmentExpression', | ||
'UpdateExpression' | ||
//// unsupported expressions | ||
// 'FunctionExpression', | ||
// 'NewExpression', | ||
// 'ObjectExpression', | ||
// 'RegularExpression', | ||
// 'SequenceExpression', | ||
// 'ThisExpression', | ||
]; | ||
function defaultOptions () { | ||
return { | ||
destructive: false, | ||
powerAssertVariableName: 'assert' | ||
}; | ||
} | ||
@@ -56,3 +79,3 @@ | ||
var errorMessage, result; | ||
options = extend(deepCopy(DEFAULT_OPTIONS), (options || {})); | ||
options = extend(defaultOptions(), (options || {})); | ||
if (typeof ast.loc === 'undefined') { | ||
@@ -138,4 +161,4 @@ errorMessage = 'JavaScript AST should contain location information.'; | ||
case 'Identifier': | ||
return this.captureIdent(node, this.locationOf(node)); | ||
case 'MemberExpression': // ex: foo.bar.baz | ||
return this.captureIdentifier(node); | ||
case 'MemberExpression': | ||
return this.captureMemberExpression(node); | ||
@@ -147,9 +170,11 @@ case 'CallExpression': | ||
case 'BinaryExpression': | ||
return this.captureExpressionWithInfixOperator(node, 'binary'); | ||
return this.captureBinaryExpression(node); | ||
case 'LogicalExpression': | ||
return this.captureExpressionWithInfixOperator(node, 'logical'); | ||
return this.captureLogicalExpression(node); | ||
case 'AssignmentExpression': | ||
return this.captureExpressionWithInfixOperator(node, 'assignment'); | ||
return this.captureAssignmentExpression(node); | ||
case 'ArrayExpression': | ||
return this.captureArrayExpression(node); | ||
case 'ConditionalExpression': | ||
return this.captureConditionalExpression(node); | ||
case 'UpdateExpression': | ||
@@ -162,23 +187,6 @@ return this.captureUpdateExpression(node); | ||
Instrumentor.prototype.captureUpdateExpression = function (node) { | ||
return this.captureNode('update', node, this.locationOf(node)); | ||
Instrumentor.prototype.captureIdentifier = function (target, location) { | ||
return this.captureNode('ident', target, (location || this.locationOf(target))); | ||
}; | ||
Instrumentor.prototype.captureArrayExpression = function (node) { | ||
var that = this; | ||
node.elements = node.elements.map(function (elem) { | ||
return that.captureRecursively(elem); | ||
}); | ||
return node; | ||
}; | ||
Instrumentor.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)); | ||
}; | ||
Instrumentor.prototype.captureMemberExpression = function (node) { | ||
@@ -190,3 +198,3 @@ var propLocation = this.propertyLocationOf(node); | ||
} | ||
return this.captureIdent(node, propLocation); | ||
return this.captureIdentifier(node, propLocation); | ||
}; | ||
@@ -201,8 +209,48 @@ | ||
node.callee.object = that.captureRecursively(node.callee.object); | ||
return that.captureFuncall(node, that.propertyLocationOf(node.callee)); | ||
return that.captureNode('funcall', node, that.propertyLocationOf(node.callee)); | ||
} else { | ||
return that.captureFuncall(node, that.locationOf(node)); | ||
return that.captureNode('funcall', node, that.locationOf(node)); | ||
} | ||
}; | ||
Instrumentor.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)); | ||
}; | ||
Instrumentor.prototype.captureBinaryExpression = function (node) { | ||
return this.captureExpressionWithInfixOperator(node, 'binary'); | ||
}; | ||
Instrumentor.prototype.captureLogicalExpression = function (node) { | ||
return this.captureExpressionWithInfixOperator(node, 'logical'); | ||
}; | ||
Instrumentor.prototype.captureAssignmentExpression = function (node) { | ||
return this.captureExpressionWithInfixOperator(node, 'assignment'); | ||
}; | ||
Instrumentor.prototype.captureArrayExpression = function (node) { | ||
var that = this; | ||
node.elements = node.elements.map(function (elem) { | ||
return that.captureRecursively(elem); | ||
}); | ||
return node; | ||
}; | ||
Instrumentor.prototype.captureConditionalExpression = function (node) { | ||
node.test = this.captureRecursively(node.test); | ||
node.consequent = this.captureRecursively(node.consequent); | ||
node.alternate = this.captureRecursively(node.alternate); | ||
return node; | ||
}; | ||
Instrumentor.prototype.captureUpdateExpression = function (node) { | ||
return this.captureNode('update', node, this.locationOf(node)); | ||
}; | ||
Instrumentor.prototype.captureExpressionWithInfixOperator = function (expression, kind) { | ||
@@ -325,10 +373,2 @@ // BK: need to detect operator location before left/right instrumentation | ||
Instrumentor.prototype.captureIdent = function (target, location) { | ||
return this.captureNode('ident', target, location); | ||
}; | ||
Instrumentor.prototype.captureFuncall = function (target, location) { | ||
return this.captureNode('funcall', target, location); | ||
}; | ||
Instrumentor.prototype.captureNode = function (kind, target, location) { | ||
@@ -442,22 +482,3 @@ var n = newNodeWithLocationCopyOf(target); | ||
function isSupportedNodeType (node) { | ||
var supported = [ | ||
'Identifier', | ||
'UnaryExpression', | ||
'BinaryExpression', | ||
'MemberExpression', | ||
'CallExpression', | ||
'AssignmentExpression', | ||
'ArrayExpression', | ||
'UpdateExpression', | ||
'LogicalExpression' | ||
//// unsupported expressions | ||
// 'ConditionalExpression', | ||
// 'FunctionExpression', | ||
// 'NewExpression', | ||
// 'ObjectExpression', | ||
// 'RegularExpression', | ||
// 'SequenceExpression', | ||
// 'ThisExpression', | ||
]; | ||
return supported.some(function (kind) { return node.type === kind; }); | ||
return SUPPORTED_NODE_TYPES.indexOf(node.type) !== -1; | ||
} | ||
@@ -539,4 +560,4 @@ | ||
espower.traverse = traverse; | ||
espower.DEFAULT_OPTIONS = DEFAULT_OPTIONS; | ||
espower.defaultOptions = defaultOptions; | ||
return espower; | ||
})); |
{ | ||
"name": "espower", | ||
"description": "Power Assert feature instrumentor based on the Mozilla JavaScript AST", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"keywords": [ | ||
"test", | ||
"assert", | ||
"testing" | ||
"testing", | ||
"ecmascript", | ||
"ast" | ||
], | ||
@@ -28,8 +30,8 @@ "homepage": "http://github.com/twada/espower", | ||
"devDependencies": { | ||
"esprima": "1.0.3", | ||
"esprima": "1.0.4", | ||
"escodegen": "0.0.26", | ||
"coffee-script-redux": "2.0.0-beta6", | ||
"mocha": "1.12.0", | ||
"coffee-script-redux": "2.0.0-beta7", | ||
"mocha": "1.12.1", | ||
"grunt": "~0.4.1", | ||
"grunt-mocha-test": "~0.6.2", | ||
"grunt-mocha-test": "~0.6.3", | ||
"grunt-contrib-jshint": "~0.6.4" | ||
@@ -36,0 +38,0 @@ }, |
@@ -6,3 +6,5 @@ espower | ||
[![NPM version](https://badge.fury.io/js/espower.png)](http://badge.fury.io/js/espower) | ||
[![Dependency Status](https://gemnasium.com/twada/espower.png)](https://gemnasium.com/twada/espower) | ||
Power Assert feature instrumentor based on the Mozilla JavaScript AST. | ||
@@ -9,0 +11,0 @@ |
@@ -8,17 +8,9 @@ var espower = require('../lib/espower'), | ||
it('with CoffeeScriptRedux toolchain', function () { | ||
var csCode = 'assert.ok dog.speak() == says'; | ||
var parseOptions = {raw: true}; | ||
var csAST = CoffeeScript.parse(csCode, parseOptions); | ||
var compileOptions = {bare: false}; | ||
var jsAST = CoffeeScript.compile(csAST, compileOptions); | ||
var espoweredAst = espower(jsAST, {destructive: false, source: csCode, path: '/path/to/foo_test.coffee', powerAssertVariableName: 'assert'}); | ||
var jsGenerateOptions = {compact: true}; | ||
var jsCode = CoffeeScript.js(espoweredAst, jsGenerateOptions); | ||
assert.equal(jsCode, "assert.ok(assert._expr(assert._capt(assert._capt(assert._capt(dog,'ident',{start:{line:1,column:10}}).speak(),'funcall',{start:{line:1,column:14}})===assert._capt(says,'ident',{start:{line:1,column:25}}),'binary',{start:{line:1,column:22}}),{start:{line:1,column:10},path:'/path/to/foo_test.coffee'},'assert.ok dog.speak() == says'))"); | ||
var csCode = 'assert.ok dog.age is three', | ||
csAST = CoffeeScript.parse(csCode, {raw: true}), | ||
jsAST = CoffeeScript.compile(csAST, {bare: false}), | ||
espoweredAst = espower(jsAST, {destructive: false, source: csCode, path: '/path/to/foo_test.coffee', powerAssertVariableName: 'assert'}), | ||
jsCode = CoffeeScript.js(espoweredAst, {compact: true}); | ||
assert.equal(jsCode, "assert.ok(assert._expr(assert._capt(assert._capt(assert._capt(dog,'ident',{start:{line:1,column:10}}).age,'ident',{start:{line:1,column:14}})===assert._capt(three,'ident',{start:{line:1,column:21}}),'binary',{start:{line:1,column:18}}),{start:{line:1,column:10},path:'/path/to/foo_test.coffee'},'assert.ok dog.age is three'))"); | ||
}); | ||
}); |
@@ -7,5 +7,5 @@ var espower = require('../lib/espower'), | ||
describe('espower.DEFAULT_OPTIONS', function () { | ||
describe('espower.defaultOptions()', function () { | ||
beforeEach(function () { | ||
this.options = espower.DEFAULT_OPTIONS; | ||
this.options = espower.defaultOptions(); | ||
}); | ||
@@ -12,0 +12,0 @@ it('destructive: false', function () { |
@@ -21,2 +21,11 @@ var espower = require('../lib/espower'), | ||
describe('Literal', function () { | ||
inst("assert(false);", | ||
"assert(false);"); | ||
inst("assert(0);", | ||
"assert(0);"); | ||
}); | ||
describe('Identifier', function () { | ||
@@ -154,3 +163,3 @@ inst("assert(falsyStr);", | ||
describe('ArrayExpression', function () { | ||
describe('UpdateExpression', function () { | ||
inst("assert(++foo);", | ||
@@ -162,2 +171,15 @@ "assert(assert._expr(assert._capt(++foo,'update',{start:{line:1,column:7}}),{start:{line:1,column:7}}));"); | ||
}); | ||
describe('ConditionalExpression', function () { | ||
inst("assert(foo ? bar : baz);", | ||
"assert(assert._expr(assert._capt(foo,'ident',{start:{line:1,column:7}})?assert._capt(bar,'ident',{start:{line:1,column:13}}):assert._capt(baz,'ident',{start:{line:1,column:19}}),{start:{line:1,column:7}}));"); | ||
inst("assert(falsy ? truthy : truthy ? anotherFalsy : truthy);", | ||
"assert(assert._expr(assert._capt(falsy,'ident',{start:{line:1,column:7}})?assert._capt(truthy,'ident',{start:{line:1,column:15}}):assert._capt(truthy,'ident',{start:{line:1,column:24}})?assert._capt(anotherFalsy,'ident',{start:{line:1,column:33}}):assert._capt(truthy,'ident',{start:{line:1,column:48}}),{start:{line:1,column:7}}));"); | ||
inst("assert(foo() ? bar.baz : (typeof goo));", | ||
"assert(assert._expr(assert._capt(foo(),'funcall',{start:{line:1,column:7}})?assert._capt(assert._capt(bar,'ident',{start:{line:1,column:15}}).baz,'ident',{start:{line:1,column:19}}):assert._capt(typeof goo,'unary',{start:{line:1,column:26}}),{start:{line:1,column:7}}));"); | ||
}); | ||
}); |
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
45575
810
31