-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathiterate_metadata_intersection.ts
213 lines (202 loc) · 6.45 KB
/
iterate_metadata_intersection.ts
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
208
209
210
211
212
213
// import ts from "typescript";
import { IMetadataTypeTag } from "../../../schemas/metadata/IMetadataTypeTag";
import { Metadata } from "../../../schemas/metadata/Metadata";
import { MetadataObjectType } from "../../../schemas/metadata/MetadataObjectType";
import { MetadataCollection } from "../../MetadataCollection";
import { MetadataFactory } from "../../MetadataFactory";
import { MetadataTypeTagFactory } from "../../MetadataTypeTagFactory";
import { IMetadataIteratorProps } from "./IMetadataIteratorProps";
import { explore_metadata } from "./explore_metadata";
import { iterate_metadata } from "./iterate_metadata";
export const iterate_metadata_intersection = (
props: IMetadataIteratorProps,
): boolean => {
if (props.intersected === true) return false;
else if (props.type.isIntersection() === false) return false;
// CONSTRUCT FAKE METADATA LIST
const commit: MetadataCollection = props.collection.clone();
props.collection["options"] = undefined;
const fakeErrors: MetadataFactory.IError[] = [];
const children: Metadata[] = props.type.types.map((t) =>
explore_metadata({
...props,
options: {
...props.options,
absorb: true,
functional: false,
},
collection: props.collection,
errors: fakeErrors,
type: t,
explore: {
...props.explore,
aliased: false,
},
intersected: false,
}),
);
// ERROR OR ANY TYPE CASE
const escape = (out: boolean) => {
Object.assign(props.collection, commit);
return out;
};
if (fakeErrors.length) {
props.errors.push(...fakeErrors);
return escape(true);
} else if (children.length === 0) return escape(false);
else if (children.some((m) => m.any === true || m.size() === 0))
return escape(false);
// PREPARE MEATDATAS AND TAGS
const indexes: number[] = [];
const metadatas: Metadata[] = [];
const tagObjects: MetadataObjectType[] = [];
children.forEach((child, i) => {
if (
child.size() === 1 &&
child.objects.length === 1 &&
MetadataTypeTagFactory.is(child.objects[0]!.type)
)
tagObjects.push(child.objects[0]!.type);
else {
indexes.push(i);
metadatas.push(child);
}
});
const nonsensible = () => {
props.errors.push({
name: children.map((c) => c.getName()).join(" & "),
explore: { ...props.explore },
messages: ["nonsensible intersection"],
});
return escape(true);
};
// NO METADATA CASE
if (metadatas.length === 0)
if (tagObjects.length !== 0) {
props.errors.push({
name: children.map((c) => c.getName()).join(" & "),
explore: { ...props.explore },
messages: ["type tag cannot be standalone"],
});
return escape(true);
} else return escape(false);
// ONLY OBJECTS CASE
else if (
metadatas.every((m) => m.objects.length === 1) &&
tagObjects.length === 0
)
return escape(false);
else if (metadatas.length !== 1) {
const indexes: number[] = metadatas
.map((m, i) =>
m.size() === 1 &&
m.objects.length === 1 &&
(m.objects[0]!.type.properties.length === 0 ||
m.objects[0]!.type.properties.every((p) => p.value.optional === true))
? i
: null,
)
.filter((i) => i !== null);
if (indexes.length && metadatas.length !== indexes.length)
for (const i of indexes.reverse()) metadatas.splice(i, 1);
else return nonsensible();
} else if (metadatas.some((m) => m.size() !== 1)) return nonsensible();
const candidates: Map<string, string> = new Map();
const assigners: Array<(tags: IMetadataTypeTag[]) => void> = [];
for (const meta of metadatas) {
for (const a of meta.atomics) {
candidates.set(a.type, a.type);
assigners.push((tags) =>
props.metadata.atomics
.find((atom) => atom.type === a.type)
?.tags.push(tags),
);
}
for (const c of meta.constants)
for (const v of c.values) {
candidates.set(c.type, c.type);
assigners.push((tags) =>
props.metadata.constants
.find((constant) => constant.type === c.type)
?.values.find((value) => value === v)
?.tags?.push(tags),
);
}
for (const t of meta.templates) {
candidates.set("string", "string");
assigners.push((tags) =>
props.metadata.templates
.find((tpl) => tpl.getBaseName() === t.getBaseName())
?.tags.push(tags),
);
}
if (meta.objects.length) {
candidates.set("object", "object");
assigners.push((tags) => props.metadata.objects.at(-1)?.tags.push(tags));
}
if (meta.arrays.length) {
candidates.set("array", "array");
assigners.push((tags) => props.metadata.arrays.at(-1)?.tags.push(tags));
}
if (meta.tuples.length) candidates.set("invalid", "tuple");
for (const n of meta.natives) {
candidates.set(`native::${n.name}`, "object");
assigners.push((tags) =>
props.metadata.natives
.find((native) => native.name === n.name)
?.tags.push(tags),
);
}
for (const s of meta.sets) {
candidates.set(`set::${s.value.getName()}`, "object");
assigners.push((tags) =>
props.metadata.sets
.find((set) => set.value.getName() === s.value.getName())
?.tags.push(tags),
);
}
for (const e of meta.maps) {
candidates.set(`map::${e.key.getName()}::${e.value.getName()}`, "object");
assigners.push((tags) =>
props.metadata.maps
.find(
(map) =>
map.key.getName() === e.key.getName() &&
map.value.getName() === e.value.getName(),
)
?.tags.push(tags),
);
}
}
if (
candidates.size !== 1 ||
candidates.has("nonsensible")
// ||
// (candidates.size !== 1 &&
// Array.from(candidates.keys()).some((v) => v !== "object"))
)
return nonsensible();
const tags: IMetadataTypeTag[] = MetadataTypeTagFactory.analyze({
errors: props.errors,
type: candidates.values().next().value as "string",
objects: tagObjects,
explore: props.explore,
});
Object.assign(props.collection, commit);
iterate_metadata({
...props,
type: props.type.types[indexes[0]!]!,
options: {
...props.options,
functional: false,
},
explore: {
...props.explore,
aliased: false,
escaped: false,
},
intersected: true,
});
if (tags.length) assigners.forEach((fn) => fn(tags));
return true;
};