forked from JSMonk/hegel
/
property-type.js
207 lines (199 loc) · 5.68 KB
/
property-type.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import HegelError from "../../utils/errors";
import { Type } from "./type";
import { $Keys } from "./keys-type";
import { TypeVar } from "./type-var";
import { TupleType } from "./tuple-type";
import { UnionType } from "./union-type";
import { TypeScope } from "../type-scope";
import { ObjectType } from "./object-type";
import { $BottomType } from "./bottom-type";
import { GenericType } from "./generic-type";
import { VariableInfo } from "../variable-info";
import { CollectionType } from "./collection-type";
export class $PropertyType extends GenericType {
static get name() {
return "$PropertyType";
}
constructor(_, meta = {}, isForInit = false) {
const parent = new TypeScope(meta.parent);
super(
"$PropertyType",
meta,
[
TypeVar.term("target", { parent }),
TypeVar.term("property", { parent })
],
parent,
null
);
this.isForInit = isForInit;
}
findRealTarget(target, loc) {
if (target instanceof UnionType) {
return target;
}
let obj = target;
while (obj !== null && !(obj instanceof ObjectType)) {
obj = obj.isSubtypeOf;
}
return obj;
}
isPrincipalTypeFor() {
return false;
}
equalsTo() {
return false;
}
isSuperTypeFor() {
return false;
}
applyGeneric(
parameters,
loc,
shouldBeMemoize = true,
isCalledAsBottom = false,
initializing = false
) {
super.assertParameters(parameters, loc);
const [currentTarget, property] = parameters;
const realTarget = this.getOponentType(currentTarget);
const realProperty = this.getOponentType(property);
const propertyName =
realProperty.isSubtypeOf && realProperty.isSubtypeOf.name === "string"
? realProperty.name.slice(1, -1)
: realProperty.name;
const isTargetVariable = realTarget instanceof TypeVar;
const isPropertyVariable = realProperty instanceof TypeVar;
if (isTargetVariable && !realTarget.isUserDefined) {
if (realTarget.constraint === undefined) {
if (isPropertyVariable) {
realTarget.constraint = ObjectType.Object.root;
} else {
const props = [
[
propertyName,
new VariableInfo(
TypeVar.term(`${realTarget.name}0`, {
parent: realTarget.parent
})
)
]
];
realTarget.constraint = ObjectType.term(
null,
{ isSoft: true },
props
);
}
} else if (
realTarget.constraint instanceof ObjectType &&
!isPropertyVariable &&
!realTarget.constraint.properties.has(propertyName)
) {
const props = [
...realTarget.constraint.properties,
[
propertyName,
new VariableInfo(
TypeVar.term(
`${realTarget.name}${realTarget.constraint.properties.size}`,
{ parent: realTarget.parent }
)
)
]
];
realTarget.constraint = ObjectType.term(null, { isSoft: true }, props);
}
}
if (
isPropertyVariable &&
!realProperty.isUserDefined &&
realProperty.constraint === undefined
) {
let constraint = undefined;
if (realTarget instanceof CollectionType) {
constraint = realTarget.keyType;
} else if (realTarget instanceof TupleType) {
constraint = Array.from({ length: realTarget.items.length }).map(
(_, i) => Type.term(i + 1, { isSubtypeOf: Type.Number })
);
} else if (realTarget instanceof ObjectType) {
constraint = UnionType.term(
null,
{},
[...realTarget.properties].map(([key]) =>
Type.term(`'${key}'`, { isSubtypeOf: Type.String })
)
);
} else if (isTargetVariable) {
constraint = new $BottomType({}, new $Keys(), [realTarget]);
}
realProperty.constraint = constraint;
}
if (isTargetVariable && !realTarget.isUserDefined && !isPropertyVariable) {
return realTarget.constraint.properties.get(propertyName).type;
}
if (isPropertyVariable) {
return this.bottomizeWith(
[realTarget, realProperty],
realTarget.parent,
loc
);
}
if (realProperty instanceof UnionType) {
try {
const variants = realProperty.variants.map(p =>
this.applyGeneric(
[realTarget, p],
loc,
shouldBeMemoize,
isCalledAsBottom
)
);
return UnionType.term(null, {}, variants);
} catch {
throw new HegelError(
`Property "${propertyName}" does not exist in "${
currentTarget.name
}"`,
loc
);
}
}
if (realTarget instanceof UnionType) {
try {
const variants = realTarget.variants.map(v =>
this.applyGeneric(
[v, realProperty],
loc,
shouldBeMemoize,
isCalledAsBottom
)
);
return UnionType.term(null, {}, variants);
} catch {
throw new HegelError(
`Property "${propertyName}" does not exist in "${
currentTarget.name
}"`,
loc
);
}
}
const fieldType = realTarget.getPropertyType(
propertyName,
initializing,
this.isForInit
);
if (!realProperty.isSubtypeOf && !isCalledAsBottom) {
throw new HegelError("Second parameter should be an literal", loc);
}
if (fieldType !== null) {
return fieldType;
}
throw new HegelError(
`Property "${propertyName}" does not exist in "${currentTarget.name}"`,
loc
);
}
}