[Pkg-javascript-commits] [node-lexical-scope] 22/83: gigantic speedup using esprima directly
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 15 09:45:47 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-lexical-scope.
commit 9125bf1ec0cf78c77a852e0547a4cc69db7797bf
Author: James Halliday <mail at substack.net>
Date: Fri Mar 1 22:00:35 2013 -0800
gigantic speedup using esprima directly
---
index.js | 28 +++++++++++++++++++++++++---
package.json | 4 ++--
2 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/index.js b/index.js
index 7724809..65e8161 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,4 @@
-var falafel = require('falafel');
+var esprima = require('esprima');
module.exports = function (src) {
var locals = {};
@@ -6,8 +6,9 @@ module.exports = function (src) {
var exported = {};
src = String(src).replace(/^#![^\n]*\n/, '');
+ var ast = esprima.parse(src);
- falafel(src, function (node) {
+ walk(ast, undefined, function (node) {
if (node.type === 'VariableDeclaration') {
// take off the leading `var `
var id = getScope(node);
@@ -22,7 +23,7 @@ module.exports = function (src) {
}
});
- falafel(src, function (node) {
+ walk(ast, undefined, function (node) {
if (node.type === 'Identifier'
&& lookup(node) === undefined) {
if (node.parent.type === 'MemberExpression'
@@ -154,3 +155,24 @@ function indexOf (xs, x) {
}
return -1;
}
+
+function walk (node, parent, cb) {
+ Object.keys(node).forEach(function (key) {
+ if (key === 'parent') return;
+
+ var child = node[key];
+ if (Array.isArray(child)) {
+ child.forEach(function (c) {
+ if (c && typeof c.type === 'string') {
+ c.parent = node;
+ walk(c, node, cb);
+ }
+ });
+ }
+ else if (child && typeof child.type === 'string') {
+ child.parent = node;
+ walk(child, node, cb);
+ }
+ });
+ cb(node);
+}
diff --git a/package.json b/package.json
index 9b30c9a..c0b2f23 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,10 @@
{
"name": "lexical-scope",
- "version": "0.0.4",
+ "version": "0.0.5",
"description": "detect global and local lexical identifiers from javascript source code",
"main": "index.js",
"dependencies": {
- "falafel": "~0.1.4"
+ "esprima": "1.0.2"
},
"devDependencies": {
"tap": "~0.4.0",
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-lexical-scope.git
More information about the Pkg-javascript-commits
mailing list