Debian Codegraph @

esniff/test/_ast-index.js

raw
1 "use strict";
2
3 var last = require("es5-ext/array/#/last")
4 , esprima = require("esprima")
5 , isArray = Array.isArray
6 , keys = Object.keys
7 , walker
8 , eolRe;
9
10 eolRe = /(?:\r\n|[\n\r\u2028\u2029])/;
11
12 walker = function (ast) {
13 var dep, lines;
14 if (!ast || typeof ast !== "object") return;
15 if (isArray(ast)) {
16 ast.forEach(walker, this);
17 return;
18 }
19 keys(ast).forEach(function (key) { if (key !== "range") walker.call(this, ast[key]); }, this);
20 if (!ast.type) return;
21 if (
22 ast.type === "CallExpression" &&
23 ast.callee.type === "MemberExpression" &&
24 ast.callee.object.name === "foo" &&
25 ast.callee.property.name === "bar"
26 ) {
27 dep = { type: "scope", point: this.code.indexOf("(", ast.range[0]) + 2 };
28 lines = this.code.slice(ast.range[0], dep.point).split(eolRe);
29 dep.line = ast.loc.start.line + lines.length - 1;
30 dep.column =
31 lines.length > 1 ? last.call(lines).length : ast.loc.start.column + lines[0].length;
32 this.deps.push(dep);
33 }
34 };
35
36 module.exports = function (code) {
37 var ctx = { code: code, deps: [] };
38 walker.call(ctx, esprima.parse(code, { range: true, loc: true }));
39 return ctx.deps;
40 };