-
Notifications
You must be signed in to change notification settings - Fork 1
/
constraint-variants.js
162 lines (143 loc) · 4.14 KB
/
constraint-variants.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var Utils = require('./utils.js');
function TypeConstraint(scope, nodes, type) {
this.name = 'TypeConstraint';
this.nodes = scope.constraintReduceAll(nodes);
this.type = type;
}
/**
* @param {AssignmentFinder} assignments
* @return {Array<Type>}
*/
TypeConstraint.prototype.getSatisfiableTypes = function(assignments) {
return [this.type];
};
/**
* @return {Array<Node>}
*/
TypeConstraint.prototype.getNodes = function() {
return this.nodes;
};
function TypeEqualityConstraint(scope, nodes) {
this.name = 'TypeEqualityConstraint';
this.nodes = scope.constraintReduceAll(nodes);
}
/**
* @param {AssignmentFinder} assignments
* @return {Array<Type>}
*/
TypeEqualityConstraint.prototype.getSatisfiableTypes = function(assignments) {
var allTypes = this.nodes.map(function(node) {
return assignments.getTypes(node);
});
return Utils.intersectionByType(allTypes);
};
/**
* @return {Array<Node>}
*/
TypeEqualityConstraint.prototype.getNodes = function() {
return this.nodes;
};
function FunctionReturnsConstraint(scope, functionNodes, returnNode) {
this.name = 'FunctionReturnsConstraint';
this.functionNodes = scope.constraintReduceAll(functionNodes);
this.returnNode = scope.constraintReduce(returnNode);
}
/**
* @param {AssignmentFinder} assignments
* @return {Array<Type>}
*/
FunctionReturnsConstraint.prototype.getSatisfiableTypes =
function(assignments) {
return assignments.getTypes(this.returnNode);
};
/**
* @return {Array<Node>}
*/
FunctionReturnsConstraint.prototype.getNodes = function() {
return this.functionNodes.concat([this.returnNode]);
};
function UnionConstraint(constraints) {
this.name = 'UnionConstraint';
this.constraints = constraints;
}
/**
* @param {AssignmentFinder} assignments
* @return {Array<Type>}
*/
UnionConstraint.prototype.getSatisfiableTypes = function(assignments) {
var allSatisfiableTypes = this.constraints.map(function(constraint) {
return constraint.getSatisfiableTypes(assignments);
});
return Utils.union(allSatisfiableTypes);
};
/**
* @return {Array<Node>}
*/
UnionConstraint.prototype.getNodes = function() {
// All constraint members are applied to the same set of nodes
// TODO: not actually enforced
return this.constraints[0].getNodes();
};
function IntersectionConstraint(constraints) {
this.name = 'IntersectionConstraint';
this.constraints = constraints;
}
/**
* @param {AssignmentFinder} assignments
* @return {Array<Type>}
*/
IntersectionConstraint.prototype.getSatisfiableTypes = function(assignments) {
var allSatisfiableTypes = this.constraints.map(function(constraint) {
return constraint.getSatisfiableTypes(assignments);
});
return Utils.intersectionByType(allSatisfiableTypes);
};
/**
* @return {Array<Node>}
*/
IntersectionConstraint.prototype.getNodes = function() {
// All constraint members are applied to the same set of nodes
// TODO: not actually enforced
var nodes = [];
for (var i = 0; i < this.constraints.length; i++) {
nodes = nodes.concat(this.constraints[i].getNodes());
}
return nodes;
};
function HasPropertyConstraint(scope, nodes, propertyName) {
this.name = 'HasPropertyConstraint';
this.nodes = scope.constraintReduceAll(nodes);
this.nodes.forEach(function(node) {
if (node.type) {
console.warn('YOU WERE WRONG: ' + node.type);
}
});
this.propertyName = propertyName;
}
/**
* @return {Array<Node>}
*/
HasPropertyConstraint.prototype.getNodes = function() {
return this.nodes;
};
/**
* @param {AssignmentFinder} assignments
* @return {Array<Type>}
*/
HasPropertyConstraint.prototype.getSatisfiableTypes = function(assignments) {
var sharedTypes = Utils.intersectionByType(this.nodes.map(function(node) {
return assignments.getTypes(node);
}));
return sharedTypes.filter(function(sharedType) {
return sharedType.hasMember(this.propertyName);
});
};
/** Export all constraint variants */
module.exports = {
TypeConstraint: TypeConstraint,
TypeEqualityConstraint: TypeEqualityConstraint,
FunctionReturnsConstraint: FunctionReturnsConstraint,
UnionConstraint: UnionConstraint,
IntersectionConstraint: IntersectionConstraint,
HasPropertyConstraint: HasPropertyConstraint
};