Skip to content

Commit

Permalink
fix(babel-plugin-espower): eliminate babel-core dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
twada committed May 21, 2015
1 parent 87c160e commit 7735ed5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
var createEspowerVisitor = require('./lib/create-espower-visitor');

module.exports = function createEspowerPlugin (babel, options) {
return new babel.Transformer('babel-plugin-espower', createEspowerVisitor(options));
return new babel.Transformer('babel-plugin-espower', createEspowerVisitor(babel, options));
};
42 changes: 22 additions & 20 deletions lib/babel-estree-path.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
'use strict';

var types = require('babel-core').types;
module.exports = function (babel) {
var types = babel.types;

function traverseUp (ancestors, traversalPath) {
ancestors.push(traversalPath.key);
if (Array.isArray(traversalPath.container)) { // traversing array via TraversalContext#visitMultiple
var parentNode = traversalPath.parent;
var candidateKeys = types.VISITOR_KEYS[parentNode.type].filter(function (key) {
return parentNode[key] === traversalPath.container; // searching for current key in parentNode
});
if (candidateKeys.length === 1) {
var currentTraversingKey = candidateKeys[0]; // ex. 'arguments' in CallExpression
ancestors.push(currentTraversingKey);
function traverseUp (ancestors, traversalPath) {
ancestors.push(traversalPath.key);
if (Array.isArray(traversalPath.container)) { // traversing array via TraversalContext#visitMultiple
var parentNode = traversalPath.parent;
var candidateKeys = types.VISITOR_KEYS[parentNode.type].filter(function (key) {
return parentNode[key] === traversalPath.container; // searching for current key in parentNode
});
if (candidateKeys.length === 1) {
var currentTraversingKey = candidateKeys[0]; // ex. 'arguments' in CallExpression
ancestors.push(currentTraversingKey);
}
}
if (traversalPath.parentPath) {
traverseUp(ancestors, traversalPath.parentPath);
}
}
if (traversalPath.parentPath) {
traverseUp(ancestors, traversalPath.parentPath);
}
}

module.exports = function estreePath (traversalPath) {
var ancestors = [];
traverseUp(ancestors, traversalPath);
ancestors.reverse();
return ancestors;
return function estreePath (traversalPath) {
var ancestors = [];
traverseUp(ancestors, traversalPath);
ancestors.reverse();
return ancestors;
};
};
17 changes: 8 additions & 9 deletions lib/create-espower-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

var espower = require('espower');
var escallmatch = require('escallmatch');
var esTreePath = require('./babel-estree-path');
var types = require('babel-core').types;
var extend = require('xtend');

function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file, matchers) {
function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file, matchers, esTreePath) {
var treePath;
var currentKey = traversalPath.key;
var assertionVisitor = traversalPath.state.data['espowerAssertionVisitor'];
Expand Down Expand Up @@ -38,7 +36,7 @@ function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file
}
}

function exitTraversalPath (traversalPath, currentNode, parentNode, scope, file) {
function exitTraversalPath (traversalPath, currentNode, parentNode, scope, file, esTreePath) {
var currentKey = traversalPath.key;
var resultTree = currentNode;
var isTreeModified = false;
Expand Down Expand Up @@ -76,16 +74,17 @@ function isCalleeOfParentCallExpression(parentNode, currentKey) {
return parentNode.type === 'CallExpression' && currentKey === 'callee';
}

module.exports = function createEspowerVisitor (options) {
var opts = extend(espower.defaultOptions(), { visitorKeys: types.VISITOR_KEYS }, options);
module.exports = function createEspowerVisitor (babel, options) {
var esTreePath = require('./babel-estree-path')(babel);
var opts = extend(espower.defaultOptions(), { visitorKeys: babel.types.VISITOR_KEYS }, options);
var matchers = opts.patterns.map(function (pattern) { return escallmatch(pattern, opts); });
return Object.keys(types.VISITOR_KEYS).reduce(function (handlers, nodeType) {
return Object.keys(babel.types.VISITOR_KEYS).reduce(function (handlers, nodeType) {
handlers[nodeType] = {
enter: function (currentNode, parentNode, scope, file) {
enterTraversalPath(this, currentNode, parentNode, scope, file, matchers);
enterTraversalPath(this, currentNode, parentNode, scope, file, matchers, esTreePath);
},
exit: function (currentNode, parentNode, scope, file) {
exitTraversalPath(this, currentNode, parentNode, scope, file);
exitTraversalPath(this, currentNode, parentNode, scope, file, esTreePath);
}
};
return handlers;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"xtend": "^4.0.0"
},
"devDependencies": {
"babel-core": "^5.2.0",
"babel": "^5.2.0",
"espurify": "^1.2.0",
"mocha": "^2.2.4"
},
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var babel = require('babel-core');
var babel = require('babel');
var extend = require('xtend');
var espurify = require('espurify');
var createEspowerPlugin = require('../create');
Expand Down

0 comments on commit 7735ed5

Please sign in to comment.