@mun-ui/compiler 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/analysis.d.ts +11 -0
- package/dist/analysis.d.ts.map +1 -0
- package/dist/analysis.js +80 -0
- package/dist/analysis.js.map +1 -0
- package/dist/ast.d.ts +121 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +1022 -0
- package/dist/ast.js.map +1 -0
- package/dist/builtins.d.ts +47 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +259 -0
- package/dist/builtins.js.map +1 -0
- package/dist/collection-specialization.d.ts +2 -0
- package/dist/collection-specialization.d.ts.map +1 -0
- package/dist/collection-specialization.js +475 -0
- package/dist/collection-specialization.js.map +1 -0
- package/dist/diagnostics.d.ts +12 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +182 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/effect-analysis.d.ts +54 -0
- package/dist/effect-analysis.d.ts.map +1 -0
- package/dist/effect-analysis.js +318 -0
- package/dist/effect-analysis.js.map +1 -0
- package/dist/execution-plan.d.ts +63 -0
- package/dist/execution-plan.d.ts.map +1 -0
- package/dist/execution-plan.js +223 -0
- package/dist/execution-plan.js.map +1 -0
- package/dist/gpu-island-ir.d.ts +85 -0
- package/dist/gpu-island-ir.d.ts.map +1 -0
- package/dist/gpu-island-ir.js +159 -0
- package/dist/gpu-island-ir.js.map +1 -0
- package/dist/gpu-region.d.ts +28 -0
- package/dist/gpu-region.d.ts.map +1 -0
- package/dist/gpu-region.js +136 -0
- package/dist/gpu-region.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel-ir.d.ts +13 -0
- package/dist/kernel-ir.d.ts.map +1 -0
- package/dist/kernel-ir.js +221 -0
- package/dist/kernel-ir.js.map +1 -0
- package/dist/language-catalog.d.ts +26 -0
- package/dist/language-catalog.d.ts.map +1 -0
- package/dist/language-catalog.js +27 -0
- package/dist/language-catalog.js.map +1 -0
- package/dist/lifecycle.d.ts +15 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +122 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/native-contract.d.ts +27 -0
- package/dist/native-contract.d.ts.map +1 -0
- package/dist/native-contract.js +163 -0
- package/dist/native-contract.js.map +1 -0
- package/dist/native-member-contract.d.ts +13 -0
- package/dist/native-member-contract.d.ts.map +1 -0
- package/dist/native-member-contract.js +10 -0
- package/dist/native-member-contract.js.map +1 -0
- package/dist/native-values.d.ts +43 -0
- package/dist/native-values.d.ts.map +1 -0
- package/dist/native-values.js +450 -0
- package/dist/native-values.js.map +1 -0
- package/dist/observable.d.ts +19 -0
- package/dist/observable.d.ts.map +1 -0
- package/dist/observable.js +203 -0
- package/dist/observable.js.map +1 -0
- package/dist/pipeline.d.ts +11 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +2290 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/resident-fusion.d.ts +102 -0
- package/dist/resident-fusion.d.ts.map +1 -0
- package/dist/resident-fusion.js +423 -0
- package/dist/resident-fusion.js.map +1 -0
- package/dist/resident-js.d.ts +7 -0
- package/dist/resident-js.d.ts.map +1 -0
- package/dist/resident-js.js +106 -0
- package/dist/resident-js.js.map +1 -0
- package/dist/resident-planner.d.ts +58 -0
- package/dist/resident-planner.d.ts.map +1 -0
- package/dist/resident-planner.js +101 -0
- package/dist/resident-planner.js.map +1 -0
- package/dist/resident-wasm.d.ts +56 -0
- package/dist/resident-wasm.d.ts.map +1 -0
- package/dist/resident-wasm.js +867 -0
- package/dist/resident-wasm.js.map +1 -0
- package/dist/scanner.d.ts +63 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +760 -0
- package/dist/scanner.js.map +1 -0
- package/dist/semantic.d.ts +100 -0
- package/dist/semantic.d.ts.map +1 -0
- package/dist/semantic.js +669 -0
- package/dist/semantic.js.map +1 -0
- package/dist/shorthand.d.ts +16 -0
- package/dist/shorthand.d.ts.map +1 -0
- package/dist/shorthand.js +324 -0
- package/dist/shorthand.js.map +1 -0
- package/dist/source-map.d.ts +26 -0
- package/dist/source-map.d.ts.map +1 -0
- package/dist/source-map.js +228 -0
- package/dist/source-map.js.map +1 -0
- package/dist/specialization.d.ts +34 -0
- package/dist/specialization.d.ts.map +1 -0
- package/dist/specialization.js +1996 -0
- package/dist/specialization.js.map +1 -0
- package/dist/state-specialization.d.ts +3 -0
- package/dist/state-specialization.d.ts.map +1 -0
- package/dist/state-specialization.js +227 -0
- package/dist/state-specialization.js.map +1 -0
- package/dist/struct-state.d.ts +39 -0
- package/dist/struct-state.d.ts.map +1 -0
- package/dist/struct-state.js +225 -0
- package/dist/struct-state.js.map +1 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui-ir.d.ts +151 -0
- package/dist/ui-ir.d.ts.map +1 -0
- package/dist/ui-ir.js +2793 -0
- package/dist/ui-ir.js.map +1 -0
- package/dist/value-types.d.ts +74 -0
- package/dist/value-types.d.ts.map +1 -0
- package/dist/value-types.js +246 -0
- package/dist/value-types.js.map +1 -0
- package/dist/vite.d.ts +33 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +233 -0
- package/dist/vite.js.map +1 -0
- package/dist/vue-host.d.ts +25 -0
- package/dist/vue-host.d.ts.map +1 -0
- package/dist/vue-host.js +103 -0
- package/dist/vue-host.js.map +1 -0
- package/dist/web-analysis.d.ts +7 -0
- package/dist/web-analysis.d.ts.map +1 -0
- package/dist/web-analysis.js +81 -0
- package/dist/web-analysis.js.map +1 -0
- package/package.json +29 -3
- package/README.md +0 -3
package/dist/semantic.js
ADDED
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import * as Core from "@mun-ui/core/compat";
|
|
3
|
+
import { SemanticModel, resolveSemanticCall, } from "@mun-ui/core/compat";
|
|
4
|
+
import { parseMunBuilder, parseMunStructs, } from "./ast.js";
|
|
5
|
+
import { createMunSourceMap, mapGeneratedPosition } from "./source-map.js";
|
|
6
|
+
function splitParameterSource(source) {
|
|
7
|
+
const parts = [];
|
|
8
|
+
let start = 0;
|
|
9
|
+
let angle = 0;
|
|
10
|
+
let square = 0;
|
|
11
|
+
let parens = 0;
|
|
12
|
+
let braces = 0;
|
|
13
|
+
let quote;
|
|
14
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
15
|
+
const character = source[index];
|
|
16
|
+
if (quote) {
|
|
17
|
+
if (character === "\\") {
|
|
18
|
+
index += 1;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (character === quote)
|
|
22
|
+
quote = undefined;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (character === "\"" || character === "'" || character === "`") {
|
|
26
|
+
quote = character;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (character === "<")
|
|
30
|
+
angle += 1;
|
|
31
|
+
else if (character === ">")
|
|
32
|
+
angle = Math.max(0, angle - 1);
|
|
33
|
+
else if (character === "[")
|
|
34
|
+
square += 1;
|
|
35
|
+
else if (character === "]")
|
|
36
|
+
square = Math.max(0, square - 1);
|
|
37
|
+
else if (character === "(")
|
|
38
|
+
parens += 1;
|
|
39
|
+
else if (character === ")")
|
|
40
|
+
parens = Math.max(0, parens - 1);
|
|
41
|
+
else if (character === "{")
|
|
42
|
+
braces += 1;
|
|
43
|
+
else if (character === "}")
|
|
44
|
+
braces = Math.max(0, braces - 1);
|
|
45
|
+
else if (character === "," && angle === 0 && square === 0 && parens === 0 && braces === 0) {
|
|
46
|
+
parts.push(source.slice(start, index).trim());
|
|
47
|
+
start = index + 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
parts.push(source.slice(start).trim());
|
|
51
|
+
return parts.filter(Boolean);
|
|
52
|
+
}
|
|
53
|
+
function topLevelCharacter(source, expected) {
|
|
54
|
+
let angle = 0;
|
|
55
|
+
let square = 0;
|
|
56
|
+
let parens = 0;
|
|
57
|
+
let braces = 0;
|
|
58
|
+
let quote;
|
|
59
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
60
|
+
const character = source[index];
|
|
61
|
+
if (quote) {
|
|
62
|
+
if (character === "\\") {
|
|
63
|
+
index += 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (character === quote)
|
|
67
|
+
quote = undefined;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (character === "\"" || character === "'" || character === "`") {
|
|
71
|
+
quote = character;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (character === "<")
|
|
75
|
+
angle += 1;
|
|
76
|
+
else if (character === ">")
|
|
77
|
+
angle = Math.max(0, angle - 1);
|
|
78
|
+
else if (character === "[")
|
|
79
|
+
square += 1;
|
|
80
|
+
else if (character === "]")
|
|
81
|
+
square = Math.max(0, square - 1);
|
|
82
|
+
else if (character === "(")
|
|
83
|
+
parens += 1;
|
|
84
|
+
else if (character === ")")
|
|
85
|
+
parens = Math.max(0, parens - 1);
|
|
86
|
+
else if (character === "{")
|
|
87
|
+
braces += 1;
|
|
88
|
+
else if (character === "}")
|
|
89
|
+
braces = Math.max(0, braces - 1);
|
|
90
|
+
else if (character === expected && angle === 0 && square === 0 && parens === 0 && braces === 0)
|
|
91
|
+
return index;
|
|
92
|
+
}
|
|
93
|
+
return -1;
|
|
94
|
+
}
|
|
95
|
+
function semanticInitializerParameters(source) {
|
|
96
|
+
const parsed = splitParameterSource(source).map(parameterSource => {
|
|
97
|
+
const kind = parameterSource.includes("@ViewBuilder")
|
|
98
|
+
? "viewBuilder"
|
|
99
|
+
: parameterSource.includes("@Action")
|
|
100
|
+
? "action"
|
|
101
|
+
: parameterSource.includes("@Binding")
|
|
102
|
+
? "binding"
|
|
103
|
+
: "value";
|
|
104
|
+
const clean = parameterSource.replace(/@(?:ViewBuilder|Action|Binding)\s*/g, "").trim();
|
|
105
|
+
const equals = topLevelCharacter(clean, "=");
|
|
106
|
+
const declaration = equals < 0 ? clean : clean.slice(0, equals).trim();
|
|
107
|
+
const defaultValue = equals < 0 ? undefined : clean.slice(equals + 1).trim();
|
|
108
|
+
const colon = topLevelCharacter(declaration, ":");
|
|
109
|
+
const head = (colon < 0 ? declaration : declaration.slice(0, colon)).trim();
|
|
110
|
+
const words = head.split(/\s+/).filter(Boolean);
|
|
111
|
+
const name = (words.at(-1) ?? "value").replace(/^_+/, "");
|
|
112
|
+
return {
|
|
113
|
+
name,
|
|
114
|
+
label: words[0] === "_" ? undefined : words[0],
|
|
115
|
+
kind,
|
|
116
|
+
required: defaultValue === undefined,
|
|
117
|
+
type: colon < 0 ? undefined : declaration.slice(colon + 1).trim(),
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
return parsed.map((parameter, index) => {
|
|
121
|
+
const trailing = index === parsed.length - 1 && (parameter.kind === "viewBuilder" || parameter.kind === "action");
|
|
122
|
+
return {
|
|
123
|
+
...parameter,
|
|
124
|
+
trailing,
|
|
125
|
+
labelRequired: parameter.label !== undefined && !trailing,
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function synthesizedMemberwiseInitializer(declaration) {
|
|
130
|
+
// @State is identity-owned storage and private members are internal to the
|
|
131
|
+
// View: neither is a memberwise parameter.
|
|
132
|
+
const constructorFields = declaration.fields.filter(field => field.kind !== "state" && field.access !== "private" && field.access !== "fileprivate");
|
|
133
|
+
const parameters = constructorFields.map(field => ({
|
|
134
|
+
name: field.name,
|
|
135
|
+
label: field.name,
|
|
136
|
+
labelRequired: true,
|
|
137
|
+
kind: field.kind === "binding" ? "binding" : "value",
|
|
138
|
+
required: field.initializer === undefined,
|
|
139
|
+
type: field.kind === "binding" && field.type ? `Binding<${field.type}>` : field.type,
|
|
140
|
+
}));
|
|
141
|
+
const parametersSource = constructorFields.map((field, index) => {
|
|
142
|
+
const parameter = parameters[index];
|
|
143
|
+
const binding = field.kind === "binding" ? "@Binding " : "";
|
|
144
|
+
const type = parameter.type ? `: ${parameter.type}` : "";
|
|
145
|
+
const fallback = field.initializer === undefined ? "" : ` = ${field.initializer}`;
|
|
146
|
+
return `${binding}${field.name}${type}${fallback}`;
|
|
147
|
+
}).join(", ");
|
|
148
|
+
const signature = `${declaration.name}(${parametersSource})`;
|
|
149
|
+
const symbol = {
|
|
150
|
+
kind: "initializer",
|
|
151
|
+
index: 0,
|
|
152
|
+
signature,
|
|
153
|
+
parameters,
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
index: 0,
|
|
157
|
+
signature,
|
|
158
|
+
parametersSource,
|
|
159
|
+
parameters,
|
|
160
|
+
symbol,
|
|
161
|
+
range: declaration.range,
|
|
162
|
+
synthesized: "memberwise",
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Build the canonical semantic View symbols for parsed source structs.
|
|
167
|
+
*
|
|
168
|
+
* A struct without an explicit init receives the same synthesized memberwise
|
|
169
|
+
* initializer contract used by native lowering and language-service call
|
|
170
|
+
* resolution. @State storage is identity-owned and therefore not an initializer
|
|
171
|
+
* argument; @Binding remains an explicit binding input.
|
|
172
|
+
*/
|
|
173
|
+
export function semanticViewsForStructs(structs, prefix = "") {
|
|
174
|
+
const result = [];
|
|
175
|
+
for (const declaration of structs) {
|
|
176
|
+
const qualifiedName = prefix ? `${prefix}.${declaration.name}` : declaration.name;
|
|
177
|
+
const initializers = declaration.initializers.length > 0
|
|
178
|
+
? declaration.initializers.map((initializer, index) => {
|
|
179
|
+
const parameters = semanticInitializerParameters(initializer.parametersSource);
|
|
180
|
+
const symbol = {
|
|
181
|
+
kind: "initializer",
|
|
182
|
+
index,
|
|
183
|
+
signature: `${declaration.name}(${initializer.parametersSource.trim()})`,
|
|
184
|
+
parameters,
|
|
185
|
+
};
|
|
186
|
+
return {
|
|
187
|
+
index,
|
|
188
|
+
signature: symbol.signature,
|
|
189
|
+
parametersSource: initializer.parametersSource,
|
|
190
|
+
parameters,
|
|
191
|
+
symbol,
|
|
192
|
+
range: initializer.range,
|
|
193
|
+
};
|
|
194
|
+
})
|
|
195
|
+
: [synthesizedMemberwiseInitializer(declaration)];
|
|
196
|
+
const fields = declaration.fields.map(field => ({
|
|
197
|
+
name: field.name,
|
|
198
|
+
kind: field.kind,
|
|
199
|
+
type: field.type,
|
|
200
|
+
initializer: field.initializer,
|
|
201
|
+
range: field.range,
|
|
202
|
+
}));
|
|
203
|
+
const symbol = {
|
|
204
|
+
kind: "view",
|
|
205
|
+
name: declaration.name,
|
|
206
|
+
qualifiedName,
|
|
207
|
+
genericParameters: declaration.genericParameters,
|
|
208
|
+
fields: fields.map(field => ({ name: field.name, kind: field.kind, type: field.type, defaultValue: field.initializer })),
|
|
209
|
+
initializers: initializers.map(initializer => initializer.symbol),
|
|
210
|
+
};
|
|
211
|
+
result.push({
|
|
212
|
+
name: declaration.name,
|
|
213
|
+
qualifiedName,
|
|
214
|
+
genericParameters: declaration.genericParameters,
|
|
215
|
+
fields,
|
|
216
|
+
initializers,
|
|
217
|
+
symbol,
|
|
218
|
+
bodyRange: declaration.bodyExpressionRange,
|
|
219
|
+
range: declaration.range,
|
|
220
|
+
});
|
|
221
|
+
result.push(...semanticViewsForStructs(declaration.nested ?? [], qualifiedName));
|
|
222
|
+
}
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Canonical lexical lookup order for a View constructor used inside another View.
|
|
227
|
+
* The current View's nested types win, then each enclosing scope, then a top-level type.
|
|
228
|
+
*/
|
|
229
|
+
export function semanticViewLookupCandidates(name, scope) {
|
|
230
|
+
if (name.includes("."))
|
|
231
|
+
return [name];
|
|
232
|
+
const candidates = [];
|
|
233
|
+
let current = scope;
|
|
234
|
+
while (current) {
|
|
235
|
+
candidates.push(`${current}.${name}`);
|
|
236
|
+
const separator = current.lastIndexOf(".");
|
|
237
|
+
current = separator >= 0 ? current.slice(0, separator) : undefined;
|
|
238
|
+
}
|
|
239
|
+
candidates.push(name);
|
|
240
|
+
return candidates;
|
|
241
|
+
}
|
|
242
|
+
function collectCalls(program, output, scope) {
|
|
243
|
+
const visit = (node) => {
|
|
244
|
+
if (node.kind === "call") {
|
|
245
|
+
output.push({
|
|
246
|
+
callee: node.callee,
|
|
247
|
+
...(scope ? { scope } : {}),
|
|
248
|
+
arguments: node.arguments.map(argument => ({
|
|
249
|
+
label: argument.label,
|
|
250
|
+
kind: argument.value.kind === "closure" ? "closure" : "expression",
|
|
251
|
+
source: argument.value.kind === "closure" ? "" : argument.value.source,
|
|
252
|
+
range: argument.range,
|
|
253
|
+
})),
|
|
254
|
+
trailingClosure: node.trailing !== undefined,
|
|
255
|
+
range: node.range,
|
|
256
|
+
resolution: resolveSemanticCall(undefined, []),
|
|
257
|
+
});
|
|
258
|
+
for (const argument of node.arguments) {
|
|
259
|
+
if (argument.value.kind === "closure")
|
|
260
|
+
collectCalls(argument.value.body, output, scope);
|
|
261
|
+
}
|
|
262
|
+
if (node.trailing)
|
|
263
|
+
collectCalls(node.trailing.body, output, scope);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (node.kind === "conditional") {
|
|
267
|
+
for (const child of node.then.statements)
|
|
268
|
+
visit(child);
|
|
269
|
+
if (node.otherwise) {
|
|
270
|
+
if (node.otherwise.kind === "conditional")
|
|
271
|
+
visit(node.otherwise);
|
|
272
|
+
else
|
|
273
|
+
for (const child of node.otherwise.statements)
|
|
274
|
+
visit(child);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
for (const node of program.statements)
|
|
279
|
+
visit(node);
|
|
280
|
+
}
|
|
281
|
+
function runtimeViewSymbols() {
|
|
282
|
+
const result = new Map();
|
|
283
|
+
for (const [name, value] of Object.entries(Core)) {
|
|
284
|
+
if (typeof value !== "function")
|
|
285
|
+
continue;
|
|
286
|
+
const symbol = value.viewType?.semanticSymbol;
|
|
287
|
+
if (symbol)
|
|
288
|
+
result.set(name, symbol);
|
|
289
|
+
}
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
export function canonicalViewSymbols() {
|
|
293
|
+
const result = runtimeViewSymbols();
|
|
294
|
+
// The canonical language manifest must be sufficient on its own. @mun-ui/core
|
|
295
|
+
// intentionally excludes legacy runtime View constructors, so runtime metadata
|
|
296
|
+
// can enrich a symbol when present but can never be required for source semantics.
|
|
297
|
+
for (const name of Core.swiftUIViewNames()) {
|
|
298
|
+
const initializers = Core.swiftUIInitializerSymbols(name);
|
|
299
|
+
if (!initializers)
|
|
300
|
+
continue;
|
|
301
|
+
const runtime = result.get(name);
|
|
302
|
+
result.set(name, runtime
|
|
303
|
+
? { ...runtime, initializers }
|
|
304
|
+
: {
|
|
305
|
+
kind: "view",
|
|
306
|
+
name,
|
|
307
|
+
qualifiedName: name,
|
|
308
|
+
fields: [],
|
|
309
|
+
initializers,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
function checkerTypeForExpression(source, checker, sourceFile) {
|
|
315
|
+
const wanted = source.trim();
|
|
316
|
+
if (!wanted)
|
|
317
|
+
return undefined;
|
|
318
|
+
let candidate;
|
|
319
|
+
const visit = (node) => {
|
|
320
|
+
if (candidate || !ts.isExpression(node)) {
|
|
321
|
+
if (!candidate)
|
|
322
|
+
ts.forEachChild(node, visit);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (node.getText(sourceFile).trim() === wanted)
|
|
326
|
+
candidate = node;
|
|
327
|
+
if (!candidate)
|
|
328
|
+
ts.forEachChild(node, visit);
|
|
329
|
+
};
|
|
330
|
+
visit(sourceFile);
|
|
331
|
+
if (!candidate)
|
|
332
|
+
return undefined;
|
|
333
|
+
try {
|
|
334
|
+
const type = checker.getTypeAtLocation(candidate);
|
|
335
|
+
if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never))
|
|
336
|
+
return undefined;
|
|
337
|
+
// `const label = "x"` has the literal type `"x"`, but a normal Mun
|
|
338
|
+
// initializer accepting `string` must still accept it. Preserve literal
|
|
339
|
+
// precision in TypeScript itself while normalizing the compiler-facing
|
|
340
|
+
// semantic category used for overload matching.
|
|
341
|
+
const primitiveCategory = (value) => {
|
|
342
|
+
if (value.flags & (ts.TypeFlags.String | ts.TypeFlags.StringLiteral))
|
|
343
|
+
return "string";
|
|
344
|
+
if (value.flags & (ts.TypeFlags.Number | ts.TypeFlags.NumberLiteral))
|
|
345
|
+
return "number";
|
|
346
|
+
if (value.flags & (ts.TypeFlags.Boolean | ts.TypeFlags.BooleanLiteral))
|
|
347
|
+
return "boolean";
|
|
348
|
+
if (value.isUnion()) {
|
|
349
|
+
const categories = new Set(value.types.map(primitiveCategory));
|
|
350
|
+
if (categories.size === 1 && !categories.has(undefined))
|
|
351
|
+
return [...categories][0];
|
|
352
|
+
}
|
|
353
|
+
return undefined;
|
|
354
|
+
};
|
|
355
|
+
return primitiveCategory(type) ?? checker.typeToString(type);
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
// Type inference is an optional refinement for semantic overload scoring.
|
|
359
|
+
// Generated Mun expressions can occasionally drive TypeScript's
|
|
360
|
+
// contextual-type machinery into an internal error; preserve compilation
|
|
361
|
+
// by falling back to the declared/unknown semantic type instead.
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
function compilerSemanticArgument(source, label, checker, sourceFile, declaredTypes = new Map()) {
|
|
366
|
+
const value = source.trim();
|
|
367
|
+
if (/^(?:\$[A-Za-z_$][A-Za-z0-9_$]*|Binding\s*\()/.test(value)) {
|
|
368
|
+
const declared = declaredTypes.get(value.slice(1));
|
|
369
|
+
const underlyingType = declared ? ({ String: "string", Int: "number", Double: "number", Bool: "boolean" }[declared] ?? declared) : "dynamic";
|
|
370
|
+
return { label, kind: "binding", type: "binding", underlyingType };
|
|
371
|
+
}
|
|
372
|
+
const implicitMember = /^\.([A-Za-z_$][A-Za-z0-9_$]*)$/.exec(value);
|
|
373
|
+
if (implicitMember)
|
|
374
|
+
return { label, type: "string", value: implicitMember[1] };
|
|
375
|
+
if (/^(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)$/.test(value)) {
|
|
376
|
+
const literalFile = ts.createSourceFile("literal.ts", `(${value})`, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
377
|
+
const statement = literalFile.statements[0];
|
|
378
|
+
const expression = statement && ts.isExpressionStatement(statement) ? statement.expression : undefined;
|
|
379
|
+
const unwrapped = expression && ts.isParenthesizedExpression(expression) ? expression.expression : expression;
|
|
380
|
+
return unwrapped && ts.isStringLiteralLike(unwrapped)
|
|
381
|
+
? { label, type: "string", value: unwrapped.text }
|
|
382
|
+
: { label, type: "string" };
|
|
383
|
+
}
|
|
384
|
+
if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/.test(value))
|
|
385
|
+
return { label, type: "number" };
|
|
386
|
+
if (/^(?:true|false)$/.test(value))
|
|
387
|
+
return { label, type: "boolean" };
|
|
388
|
+
if (/^null$/.test(value))
|
|
389
|
+
return { label, type: "null" };
|
|
390
|
+
if (/^undefined$/.test(value))
|
|
391
|
+
return { label, type: "undefined" };
|
|
392
|
+
if (/^(?:\[|Array\s*\()/.test(value))
|
|
393
|
+
return { label, type: "array" };
|
|
394
|
+
if (/^\{[\s\S]*\}$/.test(value))
|
|
395
|
+
return { label, type: "object" };
|
|
396
|
+
if (/=>|^function\b/.test(value))
|
|
397
|
+
return { label, type: "function" };
|
|
398
|
+
const declared = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value) ? declaredTypes.get(value) : undefined;
|
|
399
|
+
return { label, type: checkerTypeForExpression(value, checker, sourceFile) ?? declared ?? "unknown" };
|
|
400
|
+
}
|
|
401
|
+
function resolvedCalls(calls, views, checker, sourceFile) {
|
|
402
|
+
const runtimeSymbols = runtimeViewSymbols();
|
|
403
|
+
const canonicalSymbols = canonicalViewSymbols();
|
|
404
|
+
const sourceViews = new Map(views.map(view => [view.qualifiedName, view]));
|
|
405
|
+
const sourceViewFor = (call) => {
|
|
406
|
+
for (const candidate of semanticViewLookupCandidates(call.callee, call.scope)) {
|
|
407
|
+
const view = sourceViews.get(candidate);
|
|
408
|
+
if (view)
|
|
409
|
+
return view;
|
|
410
|
+
}
|
|
411
|
+
return undefined;
|
|
412
|
+
};
|
|
413
|
+
return calls.map(call => {
|
|
414
|
+
const scopedView = call.scope ? sourceViews.get(call.scope) : undefined;
|
|
415
|
+
const declaredTypes = new Map();
|
|
416
|
+
for (const field of scopedView?.fields ?? [])
|
|
417
|
+
if (field.type)
|
|
418
|
+
declaredTypes.set(field.name, field.type);
|
|
419
|
+
const arguments_ = call.arguments.map((argument, argumentIndex) => argument.kind === "closure"
|
|
420
|
+
? { label: argument.label, type: "function" }
|
|
421
|
+
: call.callee === "ForEach" && argumentIndex === 1 && /^\{\s*(?:id|key)\s*:/.test(argument.source) && /=>/.test(argument.source)
|
|
422
|
+
? { label: "key", type: "function" }
|
|
423
|
+
: compilerSemanticArgument(argument.source, argument.label, checker, sourceFile, declaredTypes));
|
|
424
|
+
if (call.trailingClosure)
|
|
425
|
+
arguments_.push({ type: "function", trailing: true });
|
|
426
|
+
const swiftStyleSource = call.trailingClosure || call.arguments.some(argument => argument.label !== undefined);
|
|
427
|
+
const sourceView = sourceViewFor(call);
|
|
428
|
+
const symbol = sourceView?.symbol
|
|
429
|
+
?? (swiftStyleSource ? canonicalSymbols : runtimeSymbols).get(call.callee);
|
|
430
|
+
return {
|
|
431
|
+
...call,
|
|
432
|
+
resolution: resolveSemanticCall(symbol, arguments_),
|
|
433
|
+
};
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
function builderProgramsFor(source, structs) {
|
|
437
|
+
const programs = [];
|
|
438
|
+
const seen = new Set();
|
|
439
|
+
const add = (program, scope) => {
|
|
440
|
+
const key = `${program.range.start}:${program.range.end}:${scope ?? ""}`;
|
|
441
|
+
if (seen.has(key))
|
|
442
|
+
return;
|
|
443
|
+
seen.add(key);
|
|
444
|
+
programs.push({ program, ...(scope ? { scope } : {}) });
|
|
445
|
+
};
|
|
446
|
+
const visit = (declarations, prefix = "") => {
|
|
447
|
+
for (const declaration of declarations) {
|
|
448
|
+
const qualifiedName = prefix ? `${prefix}.${declaration.name}` : declaration.name;
|
|
449
|
+
add(parseMunBuilder(declaration.bodyExpressionSource, declaration.bodyExpressionRange.start), qualifiedName);
|
|
450
|
+
visit(declaration.nested ?? [], qualifiedName);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
if (structs.length > 0) {
|
|
454
|
+
// Struct bodies are indexed separately above. Mask declarations while
|
|
455
|
+
// preserving offsets so top-level builder calls are not lost when a file
|
|
456
|
+
// also contains custom Views.
|
|
457
|
+
let masked = source;
|
|
458
|
+
for (const declaration of [...structs].sort((left, right) => right.range.start - left.range.start)) {
|
|
459
|
+
masked = masked.slice(0, declaration.range.start) + " ".repeat(declaration.range.end - declaration.range.start) + masked.slice(declaration.range.end);
|
|
460
|
+
}
|
|
461
|
+
add(parseMunBuilder(masked));
|
|
462
|
+
}
|
|
463
|
+
visit(structs);
|
|
464
|
+
if (programs.length === 0 && /\b[A-Z][A-Za-z0-9_$]*\s*\(/.test(source))
|
|
465
|
+
add(parseMunBuilder(source));
|
|
466
|
+
return programs;
|
|
467
|
+
}
|
|
468
|
+
function importsOf(source, generatedSource, sourceFile, sourceMap) {
|
|
469
|
+
return sourceFile.statements.flatMap(statement => {
|
|
470
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
471
|
+
return [];
|
|
472
|
+
const generatedRange = { start: statement.getStart(sourceFile), end: statement.end };
|
|
473
|
+
return [{
|
|
474
|
+
module: statement.moduleSpecifier.text,
|
|
475
|
+
range: mapRange(source, generatedSource, sourceMap, generatedRange),
|
|
476
|
+
}];
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
function positionAt(source, offset) {
|
|
480
|
+
const bounded = Math.max(0, Math.min(source.length, offset));
|
|
481
|
+
const prefix = source.slice(0, bounded);
|
|
482
|
+
const line = prefix.split("\n");
|
|
483
|
+
return { line: line.length, column: (line[line.length - 1]?.length ?? 0) + 1 };
|
|
484
|
+
}
|
|
485
|
+
function offsetAt(source, position) {
|
|
486
|
+
const lines = source.split("\n");
|
|
487
|
+
const line = Math.max(1, Math.min(lines.length, position.line));
|
|
488
|
+
const offset = lines.slice(0, line - 1).reduce((sum, value) => sum + value.length + 1, 0);
|
|
489
|
+
return Math.min(source.length, offset + Math.max(0, position.column - 1));
|
|
490
|
+
}
|
|
491
|
+
function mapRange(source, generatedSource, map, generatedRange) {
|
|
492
|
+
const start = mapGeneratedPosition(map, positionAt(generatedSource, generatedRange.start));
|
|
493
|
+
const end = mapGeneratedPosition(map, positionAt(generatedSource, generatedRange.end));
|
|
494
|
+
return { start: offsetAt(source, start), end: Math.max(offsetAt(source, start), offsetAt(source, end)) };
|
|
495
|
+
}
|
|
496
|
+
function matchingDelimiter(source, open, opener, closer) {
|
|
497
|
+
let depth = 0;
|
|
498
|
+
for (let index = open; index < source.length; index += 1) {
|
|
499
|
+
if (source[index] === "\"" || source[index] === "'" || source[index] === "`") {
|
|
500
|
+
const quote = source[index];
|
|
501
|
+
index += 1;
|
|
502
|
+
while (index < source.length) {
|
|
503
|
+
if (source[index] === "\\") {
|
|
504
|
+
index += 2;
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
if (source[index] === quote)
|
|
508
|
+
break;
|
|
509
|
+
index += 1;
|
|
510
|
+
}
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (source[index] === opener)
|
|
514
|
+
depth += 1;
|
|
515
|
+
else if (source[index] === closer && --depth === 0)
|
|
516
|
+
return index;
|
|
517
|
+
}
|
|
518
|
+
return source.length - 1;
|
|
519
|
+
}
|
|
520
|
+
function originalCallRange(source, name) {
|
|
521
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
522
|
+
const expression = new RegExp(`\\b${escaped}\\s*\\(`, "g");
|
|
523
|
+
const match = expression.exec(source);
|
|
524
|
+
if (!match)
|
|
525
|
+
return undefined;
|
|
526
|
+
const open = source.indexOf("(", match.index + name.length);
|
|
527
|
+
if (open < 0)
|
|
528
|
+
return undefined;
|
|
529
|
+
const close = matchingDelimiter(source, open, "(", ")");
|
|
530
|
+
return { start: match.index, end: Math.min(source.length, close + 1) };
|
|
531
|
+
}
|
|
532
|
+
function typescriptGraphSymbols(source, generatedSource, sourceFile, sourceMap) {
|
|
533
|
+
const foreignComponents = [];
|
|
534
|
+
const vueImports = new Map();
|
|
535
|
+
const reactImports = new Map();
|
|
536
|
+
for (const statement of sourceFile.statements) {
|
|
537
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
538
|
+
continue;
|
|
539
|
+
const module = statement.moduleSpecifier.text;
|
|
540
|
+
if (/\.vue$/i.test(module) && statement.importClause?.name)
|
|
541
|
+
vueImports.set(statement.importClause.name.text, module);
|
|
542
|
+
if (/\.(?:tsx|jsx)$/i.test(module) && statement.importClause?.name)
|
|
543
|
+
reactImports.set(statement.importClause.name.text, module);
|
|
544
|
+
}
|
|
545
|
+
const visit = (node) => {
|
|
546
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer && ts.isCallExpression(node.initializer)) {
|
|
547
|
+
const expression = node.initializer.expression;
|
|
548
|
+
const argument = node.initializer.arguments[0];
|
|
549
|
+
if (ts.isIdentifier(expression) && /^(?:__munForeignComponent|__munVueComponent|__munReactComponent)/.test(expression.text) && ts.isIdentifier(argument)) {
|
|
550
|
+
const isReact = expression.text === "__munReactComponent";
|
|
551
|
+
const module = (isReact ? reactImports : vueImports).get(argument.text);
|
|
552
|
+
if (module) {
|
|
553
|
+
const generatedRange = { start: node.getStart(sourceFile), end: node.end };
|
|
554
|
+
foreignComponents.push({
|
|
555
|
+
localName: node.name.text,
|
|
556
|
+
module,
|
|
557
|
+
range: originalCallRange(source, node.name.text) ?? mapRange(source, generatedSource, sourceMap, generatedRange),
|
|
558
|
+
generatedRange,
|
|
559
|
+
symbol: {
|
|
560
|
+
kind: "foreign-component",
|
|
561
|
+
localName: node.name.text,
|
|
562
|
+
module,
|
|
563
|
+
rendererAdapter: isReact ? "@mun-ui/react" : "@mun-ui/vue",
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
ts.forEachChild(node, visit);
|
|
570
|
+
};
|
|
571
|
+
visit(sourceFile);
|
|
572
|
+
return { foreignComponents };
|
|
573
|
+
}
|
|
574
|
+
function typescriptSnapshot(fileName, source) {
|
|
575
|
+
const options = {
|
|
576
|
+
allowJs: false,
|
|
577
|
+
module: ts.ModuleKind.ESNext,
|
|
578
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
579
|
+
noEmit: true,
|
|
580
|
+
noResolve: true,
|
|
581
|
+
skipLibCheck: true,
|
|
582
|
+
target: ts.ScriptTarget.ES2022,
|
|
583
|
+
};
|
|
584
|
+
const host = ts.createCompilerHost(options, true);
|
|
585
|
+
const normalize = (value) => value.replaceAll("\\", "/");
|
|
586
|
+
const requestedRoot = normalize(fileName);
|
|
587
|
+
const originalGetSourceFile = host.getSourceFile.bind(host);
|
|
588
|
+
const originalReadFile = host.readFile.bind(host);
|
|
589
|
+
const originalFileExists = host.fileExists.bind(host);
|
|
590
|
+
host.fileExists = requested => normalize(requested) === requestedRoot || originalFileExists(requested);
|
|
591
|
+
host.readFile = requested => normalize(requested) === requestedRoot ? source : originalReadFile(requested);
|
|
592
|
+
host.getSourceFile = (requested, languageVersion, onError, shouldCreateNewSourceFile) => normalize(requested) === requestedRoot
|
|
593
|
+
? ts.createSourceFile(requested, source, languageVersion, true, /\.tsx$/i.test(fileName) ? ts.ScriptKind.TSX : ts.ScriptKind.TS)
|
|
594
|
+
: originalGetSourceFile(requested, languageVersion, onError, shouldCreateNewSourceFile);
|
|
595
|
+
const program = ts.createProgram([fileName], options, host);
|
|
596
|
+
const sourceFile = program.getSourceFile(fileName) ?? ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
597
|
+
return {
|
|
598
|
+
sourceFile,
|
|
599
|
+
checker: program.getTypeChecker(),
|
|
600
|
+
diagnostics: program.getSyntacticDiagnostics(sourceFile),
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
export function createSemanticModel(source, fileName, generatedSource) {
|
|
604
|
+
const snapshot = typescriptSnapshot(fileName, generatedSource);
|
|
605
|
+
const typescript = snapshot.sourceFile;
|
|
606
|
+
const structs = parseMunStructs(source);
|
|
607
|
+
const scopedBuilderPrograms = builderProgramsFor(source, structs);
|
|
608
|
+
const builderPrograms = scopedBuilderPrograms.map(({ program }) => program);
|
|
609
|
+
const collectedCalls = [];
|
|
610
|
+
for (const { program, scope } of scopedBuilderPrograms)
|
|
611
|
+
collectCalls(program, collectedCalls, scope);
|
|
612
|
+
const views = semanticViewsForStructs(structs);
|
|
613
|
+
const calls = resolvedCalls(collectedCalls, views, snapshot.checker, typescript);
|
|
614
|
+
const sourceMap = createMunSourceMap(source, generatedSource, fileName);
|
|
615
|
+
const graphSymbols = typescriptGraphSymbols(source, generatedSource, typescript, sourceMap);
|
|
616
|
+
const symbolTable = new SemanticModel();
|
|
617
|
+
for (const view of canonicalViewSymbols().values()) {
|
|
618
|
+
symbolTable.register(view);
|
|
619
|
+
for (const initializer of view.initializers)
|
|
620
|
+
symbolTable.register(initializer);
|
|
621
|
+
}
|
|
622
|
+
for (const view of views) {
|
|
623
|
+
symbolTable.register(view.symbol);
|
|
624
|
+
for (const initializer of view.symbol.initializers)
|
|
625
|
+
symbolTable.register(initializer);
|
|
626
|
+
for (const field of view.symbol.fields) {
|
|
627
|
+
if (field.kind === "state")
|
|
628
|
+
symbolTable.register({ kind: "state", name: `${view.qualifiedName}.${field.name}`, type: field.type });
|
|
629
|
+
if (field.kind === "binding")
|
|
630
|
+
symbolTable.register({ kind: "binding", name: `${view.qualifiedName}.${field.name}`, type: field.type });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
symbolTable.register({
|
|
634
|
+
kind: "builder",
|
|
635
|
+
name: "ViewBuilder",
|
|
636
|
+
contentType: "View",
|
|
637
|
+
operations: ["buildBlock", "buildOptional", "buildEither", "buildArray"],
|
|
638
|
+
});
|
|
639
|
+
for (const foreign of graphSymbols.foreignComponents)
|
|
640
|
+
symbolTable.register(foreign.symbol);
|
|
641
|
+
return {
|
|
642
|
+
kind: "MunSemanticModel",
|
|
643
|
+
fileName,
|
|
644
|
+
source,
|
|
645
|
+
generatedSource,
|
|
646
|
+
typescript,
|
|
647
|
+
typeChecker: snapshot.checker,
|
|
648
|
+
typescriptDiagnostics: snapshot.diagnostics,
|
|
649
|
+
structs,
|
|
650
|
+
views,
|
|
651
|
+
builderPrograms,
|
|
652
|
+
calls,
|
|
653
|
+
imports: importsOf(source, generatedSource, typescript, sourceMap),
|
|
654
|
+
foreignComponents: graphSymbols.foreignComponents,
|
|
655
|
+
symbolTable,
|
|
656
|
+
symbols: symbolTable.values(),
|
|
657
|
+
view(name) {
|
|
658
|
+
const qualified = views.find(view => view.qualifiedName === name);
|
|
659
|
+
if (qualified)
|
|
660
|
+
return qualified;
|
|
661
|
+
const simple = views.filter(view => view.name === name);
|
|
662
|
+
return simple.length === 1 ? simple[0] : undefined;
|
|
663
|
+
},
|
|
664
|
+
symbol(name) {
|
|
665
|
+
return symbolTable.get(name);
|
|
666
|
+
},
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
//# sourceMappingURL=semantic.js.map
|