@reckona/mreact-compiler 0.0.65 → 0.0.67
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/package.json +4 -3
- package/src/compiler-module-context.ts +31 -0
- package/src/diagnostics.ts +184 -0
- package/src/emit-client.ts +837 -0
- package/src/emit-compat.ts +567 -0
- package/src/emit-escape-helper.ts +45 -0
- package/src/emit-server-shared.ts +384 -0
- package/src/emit-server-stream.ts +2558 -0
- package/src/emit-server.ts +1827 -0
- package/src/index.ts +44 -0
- package/src/internal.ts +1905 -0
- package/src/ir.ts +151 -0
- package/src/oxc-analysis-types.ts +5 -0
- package/src/oxc-await-analysis.ts +165 -0
- package/src/oxc-await-ids.ts +62 -0
- package/src/oxc-await-validation.ts +117 -0
- package/src/oxc-bindings.ts +70 -0
- package/src/oxc-body-lowering.ts +430 -0
- package/src/oxc-child-analysis.ts +791 -0
- package/src/oxc-code-utils.ts +19 -0
- package/src/oxc-component-detection.ts +459 -0
- package/src/oxc-component-props.ts +170 -0
- package/src/oxc-component-references.ts +613 -0
- package/src/oxc-dom-lowering.ts +127 -0
- package/src/oxc-expression-utils.ts +42 -0
- package/src/oxc-jsx-attributes.ts +110 -0
- package/src/oxc-jsx-text.ts +84 -0
- package/src/oxc-nested-lowering.ts +319 -0
- package/src/oxc-node-utils.ts +65 -0
- package/src/oxc-raw-jsx.ts +239 -0
- package/src/oxc-render-values.ts +620 -0
- package/src/oxc-runtime-emit.ts +212 -0
- package/src/oxc-transform.ts +77 -0
- package/src/oxc.ts +932 -0
- package/src/transform.ts +634 -0
- package/src/types.ts +117 -0
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import type { OxcBodyStatementJsxMode } from "./oxc-analysis-types.js";
|
|
2
|
+
import { formatStatement } from "./oxc-bindings.js";
|
|
3
|
+
import { stripOxcGeneratedImports } from "./oxc-code-utils.js";
|
|
4
|
+
import { readArray, readObject, readSource, unwrapOxcParentheses } from "./oxc-node-utils.js";
|
|
5
|
+
import { containsOxcJsxSyntax } from "./oxc-render-values.js";
|
|
6
|
+
import { transformJsxWithOxc } from "./oxc-transform.js";
|
|
7
|
+
import type { AnalyzeModuleOptions, CompileTarget, Diagnostic } from "./types.js";
|
|
8
|
+
|
|
9
|
+
export interface OxcBodyLowerers {
|
|
10
|
+
lowerDomNodeExpression(code: string, expression: Record<string, unknown>): string | undefined;
|
|
11
|
+
lowerCompatObjectExpression(
|
|
12
|
+
code: string,
|
|
13
|
+
expression: Record<string, unknown>,
|
|
14
|
+
componentNames: Set<string>,
|
|
15
|
+
target: CompileTarget,
|
|
16
|
+
diagnostics: Diagnostic[],
|
|
17
|
+
): string | undefined;
|
|
18
|
+
lowerServerStringExpression(
|
|
19
|
+
code: string,
|
|
20
|
+
expression: Record<string, unknown>,
|
|
21
|
+
componentNames: Set<string>,
|
|
22
|
+
target: CompileTarget,
|
|
23
|
+
diagnostics: Diagnostic[],
|
|
24
|
+
): string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function lowerOxcTopLevelStatement(
|
|
28
|
+
code: string,
|
|
29
|
+
statement: unknown,
|
|
30
|
+
componentNames: Set<string>,
|
|
31
|
+
target: CompileTarget,
|
|
32
|
+
diagnostics: Diagnostic[],
|
|
33
|
+
options: AnalyzeModuleOptions | undefined,
|
|
34
|
+
lowerers: OxcBodyLowerers,
|
|
35
|
+
): string | undefined {
|
|
36
|
+
const object = readObject(statement);
|
|
37
|
+
|
|
38
|
+
if (object.type !== "VariableDeclaration" || !containsOxcJsxSyntax(object)) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const mode =
|
|
43
|
+
options?.topLevelJsx === "compat-object"
|
|
44
|
+
? "compat-object"
|
|
45
|
+
: options?.topLevelJsx === "server-string"
|
|
46
|
+
? "server-string"
|
|
47
|
+
: "unsupported";
|
|
48
|
+
|
|
49
|
+
return lowerOxcBodyStatementJsx(
|
|
50
|
+
code,
|
|
51
|
+
statement,
|
|
52
|
+
componentNames,
|
|
53
|
+
target,
|
|
54
|
+
diagnostics,
|
|
55
|
+
mode,
|
|
56
|
+
lowerers,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function formatPreservedStatement(
|
|
61
|
+
code: string,
|
|
62
|
+
statement: unknown,
|
|
63
|
+
options?: AnalyzeModuleOptions,
|
|
64
|
+
): string {
|
|
65
|
+
const source = readSource(code, statement);
|
|
66
|
+
|
|
67
|
+
if (options?.topLevelJsx === "compat-object" || options?.bodyStatementJsx === "compat-object") {
|
|
68
|
+
return transformJsxWithOxc(source);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return formatStatement(code, statement);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function formatOxcBodyStatement(
|
|
75
|
+
code: string,
|
|
76
|
+
statement: unknown,
|
|
77
|
+
bodyStatementJsx: OxcBodyStatementJsxMode,
|
|
78
|
+
): string {
|
|
79
|
+
return bodyStatementJsx === "compat-object"
|
|
80
|
+
? stripOxcGeneratedImports(transformJsxWithOxc(readSource(code, statement)))
|
|
81
|
+
: formatStatement(code, statement);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function lowerOxcBodyStatementJsx(
|
|
85
|
+
code: string,
|
|
86
|
+
statement: unknown,
|
|
87
|
+
componentNames: Set<string>,
|
|
88
|
+
target: CompileTarget,
|
|
89
|
+
diagnostics: Diagnostic[],
|
|
90
|
+
mode: OxcBodyStatementJsxMode,
|
|
91
|
+
lowerers: OxcBodyLowerers,
|
|
92
|
+
): string | undefined {
|
|
93
|
+
const object = readObject(statement);
|
|
94
|
+
|
|
95
|
+
if (object.type === "ForOfStatement" || object.type === "ForStatement") {
|
|
96
|
+
return lowerOxcForOfStatementJsx(
|
|
97
|
+
code,
|
|
98
|
+
object,
|
|
99
|
+
componentNames,
|
|
100
|
+
target,
|
|
101
|
+
diagnostics,
|
|
102
|
+
mode,
|
|
103
|
+
lowerers,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (mode === "unsupported") {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (object.type === "IfStatement") {
|
|
112
|
+
return lowerOxcIfStatementJsx(
|
|
113
|
+
code,
|
|
114
|
+
object,
|
|
115
|
+
componentNames,
|
|
116
|
+
target,
|
|
117
|
+
diagnostics,
|
|
118
|
+
mode,
|
|
119
|
+
lowerers,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (object.type === "ReturnStatement") {
|
|
124
|
+
return lowerOxcReturnStatementJsx(
|
|
125
|
+
code,
|
|
126
|
+
object,
|
|
127
|
+
componentNames,
|
|
128
|
+
target,
|
|
129
|
+
diagnostics,
|
|
130
|
+
mode,
|
|
131
|
+
lowerers,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (object.type !== "VariableDeclaration") {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const declarations = readArray(object.declarations);
|
|
140
|
+
|
|
141
|
+
if (declarations.length !== 1) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const declaration = readObject(declarations[0]);
|
|
146
|
+
const id = readObject(declaration.id);
|
|
147
|
+
const initializer = unwrapOxcParentheses(readObject(declaration.init));
|
|
148
|
+
|
|
149
|
+
if (typeof id.name !== "string" || !containsOxcJsxSyntax(initializer)) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const lowered =
|
|
154
|
+
mode === "dom-node"
|
|
155
|
+
? lowerers.lowerDomNodeExpression(code, initializer)
|
|
156
|
+
: mode === "compat-object"
|
|
157
|
+
? lowerers.lowerCompatObjectExpression(code, initializer, componentNames, target, diagnostics)
|
|
158
|
+
: mode === "server-string"
|
|
159
|
+
? lowerers.lowerServerStringExpression(code, initializer, componentNames, target, diagnostics)
|
|
160
|
+
: undefined;
|
|
161
|
+
|
|
162
|
+
if (lowered === undefined) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const kind = typeof object.kind === "string" ? object.kind : "const";
|
|
167
|
+
return `${kind} ${id.name} = ${lowered};`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function lowerOxcIfStatementJsx(
|
|
171
|
+
code: string,
|
|
172
|
+
statement: Record<string, unknown>,
|
|
173
|
+
componentNames: Set<string>,
|
|
174
|
+
target: CompileTarget,
|
|
175
|
+
diagnostics: Diagnostic[],
|
|
176
|
+
mode: OxcBodyStatementJsxMode,
|
|
177
|
+
lowerers: OxcBodyLowerers,
|
|
178
|
+
): string | undefined {
|
|
179
|
+
const consequent = lowerOxcStatementBlockJsx(
|
|
180
|
+
code,
|
|
181
|
+
statement.consequent,
|
|
182
|
+
componentNames,
|
|
183
|
+
target,
|
|
184
|
+
diagnostics,
|
|
185
|
+
mode,
|
|
186
|
+
lowerers,
|
|
187
|
+
);
|
|
188
|
+
const alternate =
|
|
189
|
+
statement.alternate === undefined || statement.alternate === null
|
|
190
|
+
? undefined
|
|
191
|
+
: lowerOxcStatementBlockJsx(
|
|
192
|
+
code,
|
|
193
|
+
statement.alternate,
|
|
194
|
+
componentNames,
|
|
195
|
+
target,
|
|
196
|
+
diagnostics,
|
|
197
|
+
mode,
|
|
198
|
+
lowerers,
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
if (consequent === undefined && alternate === undefined) {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const test = readSource(code, statement.test);
|
|
206
|
+
const formattedConsequent =
|
|
207
|
+
consequent ?? formatOxcStatementBlock(code, statement.consequent, mode);
|
|
208
|
+
|
|
209
|
+
if (statement.alternate === undefined || statement.alternate === null) {
|
|
210
|
+
return `if (${test}) ${formattedConsequent}`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const formattedAlternate = alternate ?? formatOxcStatementBlock(code, statement.alternate, mode);
|
|
214
|
+
return `if (${test}) ${formattedConsequent} else ${formattedAlternate}`;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function lowerOxcStatementBlockJsx(
|
|
218
|
+
code: string,
|
|
219
|
+
statement: unknown,
|
|
220
|
+
componentNames: Set<string>,
|
|
221
|
+
target: CompileTarget,
|
|
222
|
+
diagnostics: Diagnostic[],
|
|
223
|
+
mode: OxcBodyStatementJsxMode,
|
|
224
|
+
lowerers: OxcBodyLowerers,
|
|
225
|
+
): string | undefined {
|
|
226
|
+
const object = readObject(statement);
|
|
227
|
+
|
|
228
|
+
if (object.type !== "BlockStatement") {
|
|
229
|
+
const lowered = lowerOxcBodyStatementJsx(
|
|
230
|
+
code,
|
|
231
|
+
object,
|
|
232
|
+
componentNames,
|
|
233
|
+
target,
|
|
234
|
+
diagnostics,
|
|
235
|
+
mode,
|
|
236
|
+
lowerers,
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
return lowered === undefined ? undefined : lowered;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let didLower = false;
|
|
243
|
+
const statements = readArray(object.body).map((bodyStatement) => {
|
|
244
|
+
const lowered = lowerOxcBodyStatementJsx(
|
|
245
|
+
code,
|
|
246
|
+
bodyStatement,
|
|
247
|
+
componentNames,
|
|
248
|
+
target,
|
|
249
|
+
diagnostics,
|
|
250
|
+
mode,
|
|
251
|
+
lowerers,
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
if (lowered !== undefined) {
|
|
255
|
+
didLower = true;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return lowered ?? formatStatement(code, bodyStatement);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
if (!didLower) {
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return `{\n${statements.map((statementCode) => indentOxcStatement(statementCode)).join("\n")}\n}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function formatOxcStatementBlock(
|
|
269
|
+
code: string,
|
|
270
|
+
statement: unknown,
|
|
271
|
+
mode: OxcBodyStatementJsxMode,
|
|
272
|
+
): string {
|
|
273
|
+
const object = readObject(statement);
|
|
274
|
+
|
|
275
|
+
if (object.type !== "BlockStatement") {
|
|
276
|
+
return formatOxcBodyStatement(code, statement, mode);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return `{\n${readArray(object.body)
|
|
280
|
+
.map((bodyStatement) => indentOxcStatement(formatOxcBodyStatement(code, bodyStatement, mode)))
|
|
281
|
+
.join("\n")}\n}`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function lowerOxcReturnStatementJsx(
|
|
285
|
+
code: string,
|
|
286
|
+
statement: Record<string, unknown>,
|
|
287
|
+
componentNames: Set<string>,
|
|
288
|
+
target: CompileTarget,
|
|
289
|
+
diagnostics: Diagnostic[],
|
|
290
|
+
mode: OxcBodyStatementJsxMode,
|
|
291
|
+
lowerers: OxcBodyLowerers,
|
|
292
|
+
): string | undefined {
|
|
293
|
+
const argument = unwrapOxcParentheses(readObject(statement.argument));
|
|
294
|
+
|
|
295
|
+
if (!containsOxcJsxSyntax(argument)) {
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const lowered =
|
|
300
|
+
mode === "dom-node"
|
|
301
|
+
? lowerers.lowerDomNodeExpression(code, argument)
|
|
302
|
+
: mode === "compat-object"
|
|
303
|
+
? lowerers.lowerCompatObjectExpression(code, argument, componentNames, target, diagnostics)
|
|
304
|
+
: mode === "server-string"
|
|
305
|
+
? lowerers.lowerServerStringExpression(code, argument, componentNames, target, diagnostics)
|
|
306
|
+
: undefined;
|
|
307
|
+
|
|
308
|
+
return lowered === undefined ? undefined : `return ${lowered};`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function indentOxcStatement(statementCode: string): string {
|
|
312
|
+
return statementCode
|
|
313
|
+
.split("\n")
|
|
314
|
+
.map((line) => ` ${line}`)
|
|
315
|
+
.join("\n");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function lowerOxcForOfStatementJsx(
|
|
319
|
+
code: string,
|
|
320
|
+
statement: Record<string, unknown>,
|
|
321
|
+
componentNames: Set<string>,
|
|
322
|
+
target: CompileTarget,
|
|
323
|
+
diagnostics: Diagnostic[],
|
|
324
|
+
mode: OxcBodyStatementJsxMode,
|
|
325
|
+
lowerers: OxcBodyLowerers,
|
|
326
|
+
): string | undefined {
|
|
327
|
+
const body = readObject(statement.body);
|
|
328
|
+
|
|
329
|
+
if (body.type !== "BlockStatement") {
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let didLower = false;
|
|
334
|
+
const loweredStatements = readArray(body.body).map((bodyStatement) => {
|
|
335
|
+
const lowered =
|
|
336
|
+
lowerOxcPushJsxStatement(code, bodyStatement, componentNames, target, diagnostics, mode, lowerers) ??
|
|
337
|
+
lowerOxcBodyStatementJsx(code, bodyStatement, componentNames, target, diagnostics, mode, lowerers);
|
|
338
|
+
|
|
339
|
+
if (lowered !== undefined) {
|
|
340
|
+
didLower = true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return lowered ?? formatStatement(code, bodyStatement);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
if (!didLower) {
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return [
|
|
351
|
+
formatOxcLoopHeader(code, statement),
|
|
352
|
+
...loweredStatements.flatMap((statementCode) =>
|
|
353
|
+
statementCode.split("\n").map((line) => ` ${line}`),
|
|
354
|
+
),
|
|
355
|
+
"}",
|
|
356
|
+
].join("\n");
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function formatOxcLoopHeader(code: string, statement: Record<string, unknown>): string {
|
|
360
|
+
if (statement.type === "ForStatement") {
|
|
361
|
+
const init = readSource(code, statement.init);
|
|
362
|
+
const test = readSource(code, statement.test);
|
|
363
|
+
const update = readSource(code, statement.update);
|
|
364
|
+
return `for (${init}; ${test}; ${update}) {`;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return `for (${formatOxcForLeft(code, statement.left)} of ${readSource(code, statement.right)}) {`;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function formatOxcForLeft(code: string, left: unknown): string {
|
|
371
|
+
const object = readObject(left);
|
|
372
|
+
|
|
373
|
+
if (object.type !== "VariableDeclaration") {
|
|
374
|
+
return readSource(code, left);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const declaration = readObject(readArray(object.declarations)[0]);
|
|
378
|
+
const id = readObject(declaration.id);
|
|
379
|
+
const kind = typeof object.kind === "string" ? object.kind : "const";
|
|
380
|
+
|
|
381
|
+
return typeof id.name === "string" ? `${kind} ${id.name}` : readSource(code, left);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function lowerOxcPushJsxStatement(
|
|
385
|
+
code: string,
|
|
386
|
+
statement: unknown,
|
|
387
|
+
componentNames: Set<string>,
|
|
388
|
+
target: CompileTarget,
|
|
389
|
+
diagnostics: Diagnostic[],
|
|
390
|
+
mode: OxcBodyStatementJsxMode,
|
|
391
|
+
lowerers: OxcBodyLowerers,
|
|
392
|
+
): string | undefined {
|
|
393
|
+
const object = readObject(statement);
|
|
394
|
+
|
|
395
|
+
if (object.type !== "ExpressionStatement") {
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const expression = readObject(object.expression);
|
|
400
|
+
|
|
401
|
+
if (expression.type !== "CallExpression") {
|
|
402
|
+
return undefined;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const callee = readObject(expression.callee);
|
|
406
|
+
const argument = unwrapOxcParentheses(readObject(readArray(expression.arguments)[0]));
|
|
407
|
+
|
|
408
|
+
if (
|
|
409
|
+
callee.type !== "MemberExpression" ||
|
|
410
|
+
readObject(callee.property).name !== "push" ||
|
|
411
|
+
!containsOxcJsxSyntax(argument)
|
|
412
|
+
) {
|
|
413
|
+
return undefined;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const lowered =
|
|
417
|
+
mode === "dom-node"
|
|
418
|
+
? lowerers.lowerDomNodeExpression(code, argument)
|
|
419
|
+
: mode === "compat-object"
|
|
420
|
+
? lowerers.lowerCompatObjectExpression(code, argument, componentNames, target, diagnostics)
|
|
421
|
+
: mode === "server-string"
|
|
422
|
+
? lowerers.lowerServerStringExpression(code, argument, componentNames, target, diagnostics)
|
|
423
|
+
: undefined;
|
|
424
|
+
|
|
425
|
+
if (lowered === undefined) {
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return `${readSource(code, callee)}(${lowered});`;
|
|
430
|
+
}
|