[Pkg-javascript-commits] [node-detective] 32/119: 4x faster esprima version
Bastien Roucariès
rouca at moszumanska.debian.org
Wed Sep 6 09:44:32 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-detective.
commit 55f10d9f52e09ef320929da9ade550b3c0660233
Author: James Halliday <mail at substack.net>
Date: Mon Jul 30 07:16:31 2012 -0700
4x faster esprima version
---
index.js | 53 ++++++++++++++++++++++++-----------------------------
1 file changed, 24 insertions(+), 29 deletions(-)
diff --git a/index.js b/index.js
index 8df9358..15d9e7d 100644
--- a/index.js
+++ b/index.js
@@ -1,26 +1,22 @@
-var uglify = require('uglify-js');
+var esprima = require('esprima');
-var traverse = function (node, cb, parent, grandparent) {
- // Call cb on all good AST nodes.
- if (Array.isArray(node) && node[0]
- && typeof node[0] === 'object' && node[0].name) {
- cb({ name : node[0].name, value : node.slice(1) , grandparent: grandparent});
+var traverse = function (node, cb) {
+ if (Array.isArray(node)) {
+ node.forEach(function (x) {
+ traverse(x, cb);
+ });
}
-
- // Traverse down the tree on arrays and objects.
- if (Array.isArray(node)
- || Object.prototype.toString.call(node) === "[object Object]") {
- for (var key in node) traverse(node[key], cb, node, parent);
+ else if (node && typeof node === 'object') {
+ cb(node);
+ Object.keys(node).forEach(function (key) {
+ traverse(node[key], cb);
+ });
}
};
-var walk = function (src, cb) {
- var ast = uglify.parser.parse(src.toString(), false, true);
- traverse(ast, cb);
-};
-
-var deparse = function (ast) {
- return uglify.uglify.gen_code(ast);
+var walk = function (src, cb, opts) {
+ var ast = esprima.parse(src.toString());
+ traverse(ast, cb);
};
var exports = module.exports = function (src, opts) {
@@ -36,18 +32,17 @@ exports.find = function (src, opts) {
if (src.toString().indexOf(word) == -1) return modules;
walk(src, function (node) {
- var gp = node.grandparent;
- var isRequire = Array.isArray(gp)
- && gp[0]
- && (gp[0] === 'call' || gp[0].name === 'call')
- && gp[1][0] === 'name'
- && gp[1][1] === word
+ var isRequire =
+ node.type === 'CallExpression'
+ && node.callee.type === 'Identifier'
+ && node.callee.name === word
;
- if(isRequire) {
- if(node.name === 'string') {
- modules.strings.push(node.value[0]);
- } else {
- modules.expressions.push(deparse(gp[2][0]));
+ if (isRequire) {
+ if (node.arguments.length && node.arguments[0].type === 'Literal') {
+ modules.strings.push(node.arguments[0].value);
+ }
+ else {
+ modules.expressions.push('...');
}
}
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-detective.git
More information about the Pkg-javascript-commits
mailing list