-
Notifications
You must be signed in to change notification settings - Fork 41
/
scope.js
77 lines (59 loc) · 1.65 KB
/
scope.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
var utils = require('./utils');
function Scope(root, parent) {
this.node = root;
this.parent = parent;
this.definitions = {};
this.using = [];
this.getters = [];
this.setters = [];
this.getDefinition = function(node) {
var value = this.definitions[ node.name ];
if (!value && this.parent) {
value = this.parent.getDefinition(node);
if (value && this.using.indexOf(node.name) === -1) {
this.using.push(node.name);
}
}
return value;
}
this.register = function(node) {
var name = null;
if (node.type == 'VariableDeclarator') {
var dataType = null;
name = node.id.name;
if (node.init && utils.isString(node.init)) {
dataType = "String";
}
node.dataType = dataType;
} else if (node.type == 'Identifier') {
name = node.name;
} else if (node.type === 'MemberExpression'&& node.object.type === 'ThisExpression' && node.property.type === 'Identifier') {
name = node.property.name
} else if (node.type == 'MethodDefinition') {
if (node.kind == "get") {
var getter = utils.clone(node);
getter.kind = null;
this.getters.push(getter);
} else if (node.kind == "set") {
var setter = utils.clone(node);
setter.kind = null;
this.setters.push(setter);
}
}
this.definitions[name] = node;
}
}
module.exports = {
KIND_ROOT : 0,
KIND_NODE : 1,
get: function(node) {
if (node.scope) {
return node.scope;
} else {
return this.get(node.parent);
}
},
create: function(node) {
return node.scope = new Scope(node, node.parent && this.get(node.parent));
}
}