Skip to content

Commit

Permalink
Merge pull request #17 from twada/visitor-keys
Browse files Browse the repository at this point in the history
visitorKeys option
  • Loading branch information
twada committed Apr 30, 2015
2 parents 19dfd5a + 36797eb commit c5b2d02
Show file tree
Hide file tree
Showing 7 changed files with 1,770 additions and 14 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -121,6 +121,15 @@ Filepath of `originalAst`. If passed, espower stores filepath information for re
A raw (either as a string which can be JSON.parse'd, or an object) [SourceMap](https://github.com/mozilla/source-map/) associated with `originalAst`. This property is optional. If given, espower uses `options.sourceMap` to adjust information in the power-assert output.


#### (optional) options.visitorKeys

| type | default value |
|:---------|:--------------|
| `object` | N/A |

VisitorKeys for AST traversal. See [estraverse.VisitorKeys](https://github.com/estools/estraverse/blob/4.0.0/estraverse.js#L217-L288) and [babel.types.VISITOR_KEYS](https://github.com/babel/babel/blob/v5.1.13/src/babel/types/visitor-keys.json).


### var options = espower.defaultOptions();

Returns default options object for `espower` function. In other words, returns
Expand Down
26 changes: 17 additions & 9 deletions lib/assertion-visitor.js
Expand Up @@ -42,7 +42,7 @@ function AssertionVisitor (matcher, assertionPath, options) {
}

AssertionVisitor.prototype.enter = function (currentNode, parentNode) {
this.canonicalCode = generateCanonicalCode(currentNode);
this.canonicalCode = generateCanonicalCode(currentNode, this.options.visitorKeys);
this.powerAssertCalleeObject = guessPowerAssertCalleeObjectFor(currentNode.callee);

if (this.sourceMapConsumer) {
Expand Down Expand Up @@ -132,7 +132,7 @@ AssertionVisitor.prototype.isLeavingArgument = function (nodePath) {
AssertionVisitor.prototype.captureArgument = function (node) {
var n = newNodeWithLocationCopyOf(node);
var props = [];
var newCalleeObject = updateLocRecursively(espurify(this.powerAssertCalleeObject), n);
var newCalleeObject = updateLocRecursively(espurify(this.powerAssertCalleeObject), n, this.options.visitorKeys);
addLiteralTo(props, n, 'content', this.canonicalCode);
addLiteralTo(props, n, 'filepath', this.filepath);
addLiteralTo(props, n, 'line', this.lineNum);
Expand All @@ -158,7 +158,7 @@ AssertionVisitor.prototype.captureNode = function (target, path) {
this.argumentModified = true;
var n = newNodeWithLocationCopyOf(target);
var relativeEsPath = path.slice(this.assertionPath.length);
var newCalleeObject = updateLocRecursively(espurify(this.powerAssertCalleeObject), n);
var newCalleeObject = updateLocRecursively(espurify(this.powerAssertCalleeObject), n, this.options.visitorKeys);
return n({
type: syntax.CallExpression,
callee: n({
Expand Down Expand Up @@ -198,9 +198,9 @@ function guessPowerAssertCalleeObjectFor (node) {
return null;
}

function generateCanonicalCode (node) {
function generateCanonicalCode (node, visitorKeys) {
var ast = espurifyWithRaw(node);
estraverse.replace(ast, {
var visitor = {
leave: function (currentNode, parentNode) {
if (currentNode.type === syntax.Literal && typeof currentNode.raw !== 'undefined') {
currentNode['x-verbatim-espower'] = {
Expand All @@ -212,7 +212,11 @@ function generateCanonicalCode (node) {
return undefined;
}
}
});
};
if (visitorKeys) {
visitor.keys = visitorKeys;
}
estraverse.replace(ast, visitor);
return escodegen.generate(ast, canonicalCodeOptions);
}

Expand Down Expand Up @@ -240,12 +244,16 @@ function addToProps (props, createNode, name, value) {
}));
}

function updateLocRecursively (node, n) {
estraverse.replace(node, {
function updateLocRecursively (node, n, visitorKeys) {
var visitor = {
leave: function (currentNode, parentNode) {
return n(currentNode);
}
});
};
if (visitorKeys) {
visitor.keys = visitorKeys;
}
estraverse.replace(node, visitor);
return node;
}

Expand Down
11 changes: 7 additions & 4 deletions lib/instrumentor.js
Expand Up @@ -11,7 +11,7 @@ var typeName = require('type-name');
function Instrumentor (options) {
verifyOptionPrerequisites(options);
this.options = options;
this.matchers = options.patterns.map(escallmatch);
this.matchers = options.patterns.map(function (pattern) { return escallmatch(pattern, options); });
}

Instrumentor.prototype.instrument = function (ast) {
Expand All @@ -20,8 +20,7 @@ Instrumentor.prototype.instrument = function (ast) {
var assertionVisitor;
var skipping = false;
var result = (this.options.destructive) ? ast : cloneAst(ast);

estraverse.replace(result, {
var visitor = {
enter: function (currentNode, parentNode) {
var controller = this;
var path = controller.path();
Expand Down Expand Up @@ -72,7 +71,11 @@ Instrumentor.prototype.instrument = function (ast) {
}
return resultTree;
}
});
};
if (this.options.visitorKeys) {
visitor.keys = this.options.visitorKeys;
}
estraverse.replace(result, visitor);
return result;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"deep-equal": "^1.0.0",
"escallmatch": "^1.3.1",
"escallmatch": "^1.4.0",
"escodegen": "^1.6.1",
"espurify": "^1.2.0",
"estraverse": "^3.1.0",
Expand Down

0 comments on commit c5b2d02

Please sign in to comment.