Debian Codegraph @

esniff/test/_ast-accessed-properties.js

raw
1 "use strict";
2
3 var esprima = require("esprima")
4 , isArray = Array.isArray
5 , keys = Object.keys
6 , walker;
7
8 walker = function (ast) {
9 if (!ast || typeof ast !== "object") return;
10 if (isArray(ast)) {
11 ast.forEach(walker, this);
12 return;
13 }
14 keys(ast).forEach(function (key) { if (key !== "range") walker.call(this, ast[key]); }, this);
15 if (!ast.type) return;
16 if (ast.type === "MemberExpression" && ast.object.name === "foo") {
17 this.deps.push({
18 name: ast.property.name,
19 start: ast.property.range[0],
20 end: ast.property.range[1]
21 });
22 }
23 };
24
25 module.exports = function (code) {
26 var ctx = { code: code, deps: [] };
27 walker.call(ctx, esprima.parse(code, { range: true, loc: true }));
28 return ctx.deps;
29 };