Skip to content

Commit

Permalink
fix(espower): throw Error if AST is already instrumented
Browse files Browse the repository at this point in the history
  • Loading branch information
twada committed Feb 24, 2015
1 parent 8a0028e commit 1d47bdc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/assertion-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var estraverse = require('estraverse'),
escodegen = require('escodegen'),
espurify = require('espurify'),
clone = require('clone'),
deepEqual = require('deep-equal'),
syntax = estraverse.Syntax,
canonicalCodeOptions = {
format: {
Expand Down Expand Up @@ -57,13 +58,31 @@ AssertionVisitor.prototype.enter = function (currentNode, parentNode) {
}
};

AssertionVisitor.prototype.ensureNotInstrumented = function (currentNode) {
if (currentNode.type === syntax.CallExpression) {
if (currentNode.callee.type === syntax.MemberExpression) {
var prop = currentNode.callee.property;
if (prop.type === syntax.Identifier && prop.name === '_expr') {
if (deepEqual(espurify(currentNode.callee.object), espurify(this.powerAssertCallee))) {
var errorMessage = 'Attempted to transform AST twice.';
if (this.options.path) {
errorMessage += ' path: ' + this.options.path;
}
throw new Error(errorMessage);
}
}
}
}
};

AssertionVisitor.prototype.enterArgument = function (currentNode, parentNode, path) {
var argMatchResult = this.matcher.matchArgument(currentNode, parentNode);
if (argMatchResult) {
if (argMatchResult.name === 'message' && argMatchResult.kind === 'optional') {
// skip optional message argument
return undefined;
}
this.ensureNotInstrumented(currentNode);
// entering target argument
this.currentArgumentPath = [].concat(path);
return undefined;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"clone": "~0.2.0",
"deep-equal": "~1.0.0",
"escallmatch": "~1.0.1",
"escodegen": "~1.4.1",
"espurify": "~1.0.0",
Expand Down

0 comments on commit 1d47bdc

Please sign in to comment.