-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolver.js
41 lines (36 loc) · 1.49 KB
/
resolver.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
import { NullableType, SumType, ChainType, ObjectValueAssertion, ValueAssertion } from './type_assert.js'
export const RefTy = (function () {
function RefTy(typeName) {
this.typeName = typeName;
}
return RefTy
}())
export const resolveTypeReferences = (context) => {
for (const assertionId in context) {
const assertion = context[assertionId]
context[assertionId] = resolveTypeRef(context, assertion)
}
}
const resolveTypeRef = (context, assertion) => {
if (assertion instanceof RefTy) {
return context[assertion.typeName]
} else if (assertion instanceof NullableType) {
assertion.origin = resolveTypeRef(context, assertion.origin)
} else if (assertion instanceof SumType) {
assertion.types = assertion.types.map(type => resolveTypeRef(context, type))
} else if (assertion instanceof ChainType) {
assertion.types = assertion.types.map(type => resolveTypeRef(context, type))
} else if (assertion instanceof ObjectValueAssertion) {
assertion.valueAssertion = resolveTypeRef(context, assertion.valueAssertion)
} else if (assertion instanceof ValueAssertion) {
} else if (Array.isArray(assertion)) {
if (assertion.length === 1) {
assertion[0] = resolveTypeRef(context, assertion[0])
}
} else if (typeof assertion === 'object') {
for (const key in assertion) {
assertion[key] = resolveTypeRef(context, assertion[key])
}
}
return assertion
}