porffor 0.58.8 → 0.58.9
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/compiler/codegen.js +58 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/runtime/index.js +1 -1
package/compiler/codegen.js
CHANGED
@@ -405,6 +405,9 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
405
405
|
if (Prefs.d) log.warning('codegen', 'with is not supported, treating as expression');
|
406
406
|
return cacheAst(decl, generate(scope, decl.body));
|
407
407
|
|
408
|
+
case 'TSEnumDeclaration':
|
409
|
+
return cacheAst(decl, generateEnum(scope, decl));
|
410
|
+
|
408
411
|
default:
|
409
412
|
// ignore typescript nodes
|
410
413
|
if (decl.type.startsWith('TS') ||
|
@@ -416,6 +419,61 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
416
419
|
}
|
417
420
|
};
|
418
421
|
|
422
|
+
const generateEnum = (scope, decl) => {
|
423
|
+
// todo: opt const enum into compile-time values
|
424
|
+
|
425
|
+
if (decl.body) decl = decl.body; // non-standard ast node :)
|
426
|
+
|
427
|
+
const properties = [];
|
428
|
+
|
429
|
+
let value = -1;
|
430
|
+
for (const x of decl.members) {
|
431
|
+
if (x.initializer) {
|
432
|
+
value = x.initializer;
|
433
|
+
} else {
|
434
|
+
if (typeof value === 'number') {
|
435
|
+
value = {
|
436
|
+
type: 'Literal',
|
437
|
+
value: value + 1
|
438
|
+
};
|
439
|
+
} else {
|
440
|
+
value = {
|
441
|
+
type: 'Identifier',
|
442
|
+
value: undefined
|
443
|
+
};
|
444
|
+
}
|
445
|
+
}
|
446
|
+
|
447
|
+
// enum.key = value
|
448
|
+
properties.push({
|
449
|
+
key: x.id,
|
450
|
+
value,
|
451
|
+
kind: 'init'
|
452
|
+
});
|
453
|
+
|
454
|
+
// enum[value] = key
|
455
|
+
properties.push({
|
456
|
+
key: value,
|
457
|
+
value: {
|
458
|
+
type: 'Literal',
|
459
|
+
value: x.id.name
|
460
|
+
},
|
461
|
+
computed: true,
|
462
|
+
kind: 'init'
|
463
|
+
});
|
464
|
+
|
465
|
+
value = value?.value;
|
466
|
+
}
|
467
|
+
|
468
|
+
return [
|
469
|
+
...generateVarDstr(scope, decl.const ? 'const' : 'let', decl.id, {
|
470
|
+
type: 'ObjectExpression',
|
471
|
+
properties
|
472
|
+
}),
|
473
|
+
number(UNDEFINED)
|
474
|
+
];
|
475
|
+
};
|
476
|
+
|
419
477
|
const optional = (op, clause = op.at(-1)) => clause || clause === 0 ? (Array.isArray(op[0]) ? op : [ op ]) : [];
|
420
478
|
|
421
479
|
const lookupName = (scope, name) => {
|
package/jsr.json
CHANGED
package/package.json
CHANGED