This repository was archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathastutil.js
118 lines (103 loc) · 3.45 KB
/
astutil.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/* This module provides an AST visitor function, and several
* other utility functions. */
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
var esprima = require('./esprima');
/* AST visitor */
function visit(root, visitor) {
function doVisit(nd, parent, childProp) {
if(!nd || typeof nd !== 'object')
return;
if(nd.type) {
var res = visitor(nd, doVisit, parent, childProp);
if(res === false)
return;
}
for(var p in nd) {
// skip over magic properties
if(!nd.hasOwnProperty(p) || p.match(/^(range|loc|attr|comments|raw)$/))
continue;
doVisit(nd[p], nd, p);
}
}
doVisit(root);
}
/* Set up `attr` field that can be used to attach attributes to
* nodes, and fill in `enclosingFunction` and `enclosingFile`
* attributes. */
function init(root) {
var enclosingFunction = null, enclosingFile = null;
// global collections containing all functions and all call sites
root.attr.functions = [];
root.attr.calls = [];
visit(root, function(nd, visit, parent, childProp) {
if(nd.type && !nd.attr)
nd.attr = {};
if(enclosingFunction)
nd.attr.enclosingFunction = enclosingFunction;
if(enclosingFile)
nd.attr.enclosingFile = enclosingFile;
if(nd.type === 'Program')
enclosingFile = nd.attr.filename;
if(nd.type === 'FunctionDeclaration' || nd.type === 'FunctionExpression') {
root.attr.functions.push(nd);
nd.attr.parent = parent;
nd.attr.childProp = childProp;
var old_enclosingFunction = enclosingFunction;
enclosingFunction = nd;
visit(nd.id);
visit(nd.params);
visit(nd.body);
enclosingFunction = old_enclosingFunction;
return false;
}
if(nd.type === 'CallExpression' || nd.type === 'NewExpression')
root.attr.calls.push(nd);
});
}
/* Simple version of UNIX basename. */
function basename(filename) {
if(!filename)
return "<???>";
var idx = filename.lastIndexOf('/');
if(idx === -1)
idx = filename.lastIndexOf('\\');
return filename.substring(idx+1);
}
/* Pretty-print position. */
function ppPos(nd) {
return basename(nd.attr.enclosingFile) + "@" + nd.loc.start.line + ":" + nd.range[0] + "-" + nd.range[1];
}
/* Build an AST from a collection of source files. */
function buildAST(sources) {
var ast = {
type: 'ProgramCollection',
programs: [],
attr: {}
};
sources.forEach(function(source) {
var prog = esprima.parse(source.program, { loc: true, range: true });
prog.attr = { filename: source.filename };
ast.programs.push(prog);
});
init(ast);
return ast;
}
exports.visit = visit;
exports.init = init;
exports.ppPos = ppPos;
exports.buildAST = buildAST;
return exports;
});