-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.js
99 lines (82 loc) · 3.08 KB
/
common.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
(function (ns) {
var ANNO = require("../type-system/annotation.js").ANNO,
estraverse = require('estraverse'),
ErrorHandler = require("../type-system/errors.js");
var Syntax = estraverse.Syntax;
/**
*
* @param {object|Array.<object>} node
* @param scope
* @returns {TypeInfo|Array.<TypeInfo>}
*/
ns.createTypeInfo = function (node, scope) {
if(Array.isArray(node)) {
return node.map(function (arg) {
return scope.createTypeInfo(arg);
});
}
var result = ANNO(node);
if (node.type == Syntax.Identifier || node.type == Syntax.ThisExpression) {
var name = node.type == Syntax.Identifier ? node.name : 'this';
var binding = scope.getBindingByName(name);
if (binding) {
result.copy(binding);
return binding;
}
}
return result;
};
/**
*
* @param {object|Array.<object>} node
* @param scope
* @param {Array?} constants Additional array of constants
* @param {boolean} check
* @returns {TypeInfo|Array.<TypeInfo>}
*/
ns.getTypeInfo = function getTypeInfo(node, scope, constants, check) {
if(!node)
return null;
check = check == undefined ? false : check;
if(Array.isArray(node)) {
return node.map(function (arg) {
return getTypeInfo(arg, scope, constants, check);
});
}
var binding;
if (node.type == Syntax.Identifier) {
var name = node.name;
if(name == 'undefined')
return ANNO(node);
binding = scope.getBindingByName(name);
if(binding == undefined && check) {
ANNO(node).setInvalid(ErrorHandler.generateErrorInformation(node, ErrorHandler.ERROR_TYPES.REFERENCE_ERROR, name, "is not defined"));
return ANNO(node);
}
if(binding) {
var result = ANNO(node, binding.getExtra());
// A variable is dynamic per default. Only if it's listed in constant
// we can assume a static value
result.setDynamicValue();
binding.setDynamicValue();
if (constants && !binding.isNullOrUndefined()) {
var propagatedConstant = constants.filter(function (constant) {
return constant.name == name;
});
if (propagatedConstant.length) {
binding.setStaticValue(propagatedConstant[0].constant);
result.setStaticValue(propagatedConstant[0].constant);
}
}
return binding;
}
} else if (node.type == Syntax.ThisExpression) {
binding = scope.getBindingByName('this');
}
return binding || ANNO(node);
};
ns.Syntax = Syntax;
ns.VisitorOption = estraverse.VisitorOption;
ns.ANNO = ANNO;
ns.getObjectReferenceFromNode = ns.getTypeInfo;
}(exports));