@defold-typescript/transpiler 0.52.0 → 0.53.0
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/dist/companion-closure.d.ts +7 -0
- package/dist/debug-bootstrap.d.ts +21 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +328 -250
- package/package.json +5 -2
- package/src/companion-closure.ts +28 -6
- package/src/companion-emit.ts +35 -4
- package/src/debug-bootstrap.ts +88 -0
- package/src/index.ts +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defold-typescript/transpiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.53.0",
|
|
4
4
|
"description": "TypeScript-to-Lua build pipeline tuned for Defold's runtime.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,9 +35,12 @@
|
|
|
35
35
|
"test": "bun test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@defold-typescript/types": "0.
|
|
38
|
+
"@defold-typescript/types": "0.53.0",
|
|
39
39
|
"@typescript-to-lua/language-extensions": "1.19.0",
|
|
40
40
|
"typescript": "6.0.2",
|
|
41
41
|
"typescript-to-lua": "^1.36.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"lua-wasm-bindings": "0.5.3"
|
|
42
45
|
}
|
|
43
46
|
}
|
package/src/companion-closure.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
|
+
import { findManagedBootstrap } from "./debug-bootstrap";
|
|
2
3
|
import { resolvesToFactoryExport } from "./lifecycle-erasure";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -49,6 +50,13 @@ export interface CompanionClosure {
|
|
|
49
50
|
* companion takes its own `require` of the same module instead.
|
|
50
51
|
*/
|
|
51
52
|
readonly importBindings: readonly string[];
|
|
53
|
+
/**
|
|
54
|
+
* The unedited `setup-debug` managed block's statements, in source order, or
|
|
55
|
+
* empty. They are exempt from the order rule because the emit runs them ahead
|
|
56
|
+
* of the companion's `require`, so the debugger starts before anything the
|
|
57
|
+
* companion loads.
|
|
58
|
+
*/
|
|
59
|
+
readonly bootstrap: readonly ts.Statement[];
|
|
52
60
|
/** Shapes a split cannot preserve. Empty means the source is splittable. */
|
|
53
61
|
readonly violations: readonly ClosureViolation[];
|
|
54
62
|
}
|
|
@@ -717,11 +725,20 @@ export function computeCompanionClosure(
|
|
|
717
725
|
});
|
|
718
726
|
}
|
|
719
727
|
|
|
728
|
+
const managed = findManagedBootstrap(sourceFile);
|
|
729
|
+
const bootstrap = managed?.canonical === true ? managed.statements : [];
|
|
730
|
+
const editedBlock = new Set(managed?.canonical === false ? managed.statements : []);
|
|
731
|
+
const exempt = new Set(bootstrap);
|
|
732
|
+
|
|
720
733
|
// The companion loads as a unit, so the constraint is pairwise: any effectful
|
|
721
734
|
// script statement with any closure declaration below it is reordered by the
|
|
722
735
|
// split, whatever the first member's position.
|
|
723
736
|
for (const statement of sourceFile.statements) {
|
|
724
|
-
if (
|
|
737
|
+
if (
|
|
738
|
+
closureStatements.has(statement) ||
|
|
739
|
+
exempt.has(statement) ||
|
|
740
|
+
isEffectFreeStatement(statement, checker)
|
|
741
|
+
) {
|
|
725
742
|
continue;
|
|
726
743
|
}
|
|
727
744
|
const start = statement.getStart(sourceFile);
|
|
@@ -735,14 +752,18 @@ export function computeCompanionClosure(
|
|
|
735
752
|
siteOf(statement, "runs in the script"),
|
|
736
753
|
siteOf(hoisted.declaration.node, "moves into the companion"),
|
|
737
754
|
];
|
|
755
|
+
const name = hoisted.declaration.name;
|
|
738
756
|
violations.push({
|
|
739
757
|
kind: "initialization-order",
|
|
740
|
-
member:
|
|
758
|
+
member: name,
|
|
741
759
|
sites,
|
|
742
|
-
message:
|
|
743
|
-
`top-level work
|
|
744
|
-
|
|
745
|
-
|
|
760
|
+
message: editedBlock.has(statement)
|
|
761
|
+
? `the setup-debug managed block was edited and now runs top-level work before "${name}", ` +
|
|
762
|
+
`which the companion would run first (${formatSites(sites)}). ` +
|
|
763
|
+
"Run `defold-typescript setup-debug` to restore the block."
|
|
764
|
+
: `top-level work precedes "${name}", which the companion would run first, ` +
|
|
765
|
+
`reversing their order (${formatSites(sites)}). ` +
|
|
766
|
+
"Move that work below the companion's declarations, or into a lifecycle hook.",
|
|
746
767
|
});
|
|
747
768
|
}
|
|
748
769
|
|
|
@@ -756,6 +777,7 @@ export function computeCompanionClosure(
|
|
|
756
777
|
internals: ordered
|
|
757
778
|
.filter(({ symbol }) => !exportSet.has(symbol) && scriptReferences.has(symbol))
|
|
758
779
|
.map(({ declaration }) => declaration.name),
|
|
780
|
+
bootstrap,
|
|
759
781
|
violations,
|
|
760
782
|
};
|
|
761
783
|
}
|
package/src/companion-emit.ts
CHANGED
|
@@ -66,6 +66,8 @@ function isRuntimeScriptSource(sourceFile: ts.SourceFile, checker: ts.TypeChecke
|
|
|
66
66
|
interface CompanionPlan {
|
|
67
67
|
/** Full start of each top-level statement the companion takes over. */
|
|
68
68
|
readonly closureStarts: ReadonlySet<number>;
|
|
69
|
+
/** Full start of each unedited `setup-debug` block statement. */
|
|
70
|
+
readonly bootstrapStarts: ReadonlySet<number>;
|
|
69
71
|
/** Members the script reaches by bare name, which the companion must expose. */
|
|
70
72
|
readonly internals: readonly string[];
|
|
71
73
|
/** Import bindings the companion needs its own `require` of. */
|
|
@@ -86,6 +88,7 @@ function planFor(sourceFile: ts.SourceFile, checker: ts.TypeChecker): CompanionP
|
|
|
86
88
|
}
|
|
87
89
|
return {
|
|
88
90
|
closureStarts: new Set(closure.statements.map((statement) => statement.pos)),
|
|
91
|
+
bootstrapStarts: new Set(closure.bootstrap.map((statement) => statement.pos)),
|
|
89
92
|
internals: closure.internals,
|
|
90
93
|
importBindings: closure.importBindings,
|
|
91
94
|
requirePath: requirePathForRel(sourceFile.fileName),
|
|
@@ -466,6 +469,10 @@ interface SplitHalves {
|
|
|
466
469
|
* first. The run stops at the first statement that reads the companion, and at
|
|
467
470
|
* the first non-declaration — a lifecycle hook closes over the internals it
|
|
468
471
|
* reads, so its declaration must precede the hook's.
|
|
472
|
+
*
|
|
473
|
+
* The `setup-debug` block's gate is taken out of `script` before this runs and
|
|
474
|
+
* placed immediately ahead of the `require`, so the debugger starts before any
|
|
475
|
+
* companion code does while `lldebugger` itself is still required above it.
|
|
469
476
|
*/
|
|
470
477
|
function requireInsertionPoint(script: readonly Statement[], reads: ReadonlySet<string>): number {
|
|
471
478
|
let index = 0;
|
|
@@ -524,6 +531,7 @@ export interface CompanionEmitter {
|
|
|
524
531
|
export function createCompanionEmitPlugin(): CompanionEmitter {
|
|
525
532
|
const plans = new Map<string, CompanionPlan | undefined>();
|
|
526
533
|
const owned = new Map<string, Set<Statement>>();
|
|
534
|
+
const bootstraps = new Map<string, Set<Statement>>();
|
|
527
535
|
const companions = new Map<string, string>();
|
|
528
536
|
|
|
529
537
|
function planOf(sourceFile: ts.SourceFile, checker: ts.TypeChecker): CompanionPlan | undefined {
|
|
@@ -552,6 +560,24 @@ export function createCompanionEmitPlugin(): CompanionEmitter {
|
|
|
552
560
|
return statements;
|
|
553
561
|
}
|
|
554
562
|
|
|
563
|
+
function captureBootstrap(node: ts.IfStatement, context: TransformationContext): Statement[] {
|
|
564
|
+
const statements = context.superTransformStatements(node);
|
|
565
|
+
const sourceFile = context.sourceFile;
|
|
566
|
+
const plan = planOf(sourceFile, context.checker);
|
|
567
|
+
if (plan === undefined || !plan.bootstrapStarts.has(node.pos)) {
|
|
568
|
+
return statements;
|
|
569
|
+
}
|
|
570
|
+
let bucket = bootstraps.get(sourceFile.fileName);
|
|
571
|
+
if (bucket === undefined) {
|
|
572
|
+
bucket = new Set();
|
|
573
|
+
bootstraps.set(sourceFile.fileName, bucket);
|
|
574
|
+
}
|
|
575
|
+
for (const statement of statements) {
|
|
576
|
+
bucket.add(statement);
|
|
577
|
+
}
|
|
578
|
+
return statements;
|
|
579
|
+
}
|
|
580
|
+
|
|
555
581
|
const visitors: Plugin["visitors"] = {
|
|
556
582
|
[ts.SyntaxKind.VariableStatement]: capture,
|
|
557
583
|
[ts.SyntaxKind.FunctionDeclaration]: capture,
|
|
@@ -561,6 +587,7 @@ export function createCompanionEmitPlugin(): CompanionEmitter {
|
|
|
561
587
|
// because TSTL emits its `require` bindings through its own hoisting. The
|
|
562
588
|
// companion takes a copy of that prelude instead — see `copiedPrelude`.
|
|
563
589
|
[ts.SyntaxKind.ExportDeclaration]: capture,
|
|
590
|
+
[ts.SyntaxKind.IfStatement]: captureBootstrap,
|
|
564
591
|
};
|
|
565
592
|
|
|
566
593
|
let configuredNoResolvePaths: readonly string[] = [];
|
|
@@ -582,7 +609,8 @@ export function createCompanionEmitPlugin(): CompanionEmitter {
|
|
|
582
609
|
if (plan === undefined || bucket === undefined || bucket.size === 0) {
|
|
583
610
|
continue;
|
|
584
611
|
}
|
|
585
|
-
|
|
612
|
+
const bootstrap = bootstraps.get(sourceFile.fileName) ?? new Set();
|
|
613
|
+
emitCompanion(program, emitHost, file, sourceFile, plan, bucket, bootstrap, companions);
|
|
586
614
|
emitted.push(plan.requirePath);
|
|
587
615
|
}
|
|
588
616
|
// The companion is written by the CLI, not by TSTL's emit plan, so TSTL's
|
|
@@ -606,6 +634,7 @@ function emitCompanion(
|
|
|
606
634
|
sourceFile: ts.SourceFile,
|
|
607
635
|
plan: CompanionPlan,
|
|
608
636
|
owned: ReadonlySet<Statement>,
|
|
637
|
+
bootstrap: ReadonlySet<Statement>,
|
|
609
638
|
companions: Map<string, string>,
|
|
610
639
|
): void {
|
|
611
640
|
const luaAst = file.luaAst;
|
|
@@ -644,9 +673,11 @@ function emitCompanion(
|
|
|
644
673
|
];
|
|
645
674
|
|
|
646
675
|
const reads = new Set([EXPORTS_NAME, ...plan.internals]);
|
|
647
|
-
const
|
|
676
|
+
const rest = halves.script.filter((statement) => !bootstrap.has(statement));
|
|
677
|
+
const insertAt = requireInsertionPoint(rest, reads);
|
|
648
678
|
const scriptStatements: Statement[] = [
|
|
649
|
-
...
|
|
679
|
+
...rest.slice(0, insertAt),
|
|
680
|
+
...halves.script.filter((statement) => bootstrap.has(statement)),
|
|
650
681
|
createVariableDeclarationStatement(
|
|
651
682
|
exportsIdentifier(),
|
|
652
683
|
createCallExpression(createIdentifier("require"), [createStringLiteral(plan.requirePath)]),
|
|
@@ -654,7 +685,7 @@ function emitCompanion(
|
|
|
654
685
|
...plan.internals.map((name) =>
|
|
655
686
|
createVariableDeclarationStatement(createIdentifier(name), internalsField(name)),
|
|
656
687
|
),
|
|
657
|
-
...
|
|
688
|
+
...rest.slice(insertAt),
|
|
658
689
|
createReturnStatement([exportsIdentifier()]),
|
|
659
690
|
];
|
|
660
691
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
|
|
3
|
+
// Managed-block sentinels. A re-run locates the single BEGIN…END pair and
|
|
4
|
+
// refreshes the enclosed text if it drifted, so wording changes self-heal.
|
|
5
|
+
// They are `//` line comments, stripped by TSTL, so they never reach the
|
|
6
|
+
// emitted `.ts.script`.
|
|
7
|
+
export const DEBUG_BLOCK_BEGIN =
|
|
8
|
+
"// defold-typescript:setup-debug BEGIN — managed block, do not edit";
|
|
9
|
+
export const DEBUG_BLOCK_END = "// defold-typescript:setup-debug END";
|
|
10
|
+
|
|
11
|
+
// The canonical managed block: import + gated start, marker-free, no
|
|
12
|
+
// `declare module` (that lives in the ambient `.d.ts`). Mirrors the snippet in
|
|
13
|
+
// `packages/docs/guide/debugging.md` exactly; a guide-comparison test locks the two.
|
|
14
|
+
// The blank line before END keeps Biome's organizeImports assist from gluing
|
|
15
|
+
// the END comment onto a following import as its leading comment (which would
|
|
16
|
+
// then demand a blank line before the whole group). With blank lines on both
|
|
17
|
+
// sides of END, an import injected above the user's own imports stays clean.
|
|
18
|
+
export const DEBUG_MANAGED_BLOCK = `${DEBUG_BLOCK_BEGIN}
|
|
19
|
+
import * as lldebugger from "lldebugger.debug";
|
|
20
|
+
|
|
21
|
+
if (sys.get_engine_info().is_debug) {
|
|
22
|
+
lldebugger.start();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
${DEBUG_BLOCK_END}`;
|
|
26
|
+
|
|
27
|
+
export interface ManagedBootstrap {
|
|
28
|
+
/** The top-level statements wholly between the sentinels, in source order. */
|
|
29
|
+
readonly statements: readonly ts.Statement[];
|
|
30
|
+
/**
|
|
31
|
+
* Whether the text between the sentinels is the canonical body token for
|
|
32
|
+
* token, so line endings, quote style, comments and optional semicolons never
|
|
33
|
+
* decide it.
|
|
34
|
+
*/
|
|
35
|
+
readonly canonical: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function occurrences(haystack: string, needle: string): number {
|
|
39
|
+
return haystack.split(needle).length - 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function bodyTokens(text: string): string[] {
|
|
43
|
+
const scanner = ts.createScanner(ts.ScriptTarget.Latest, true, ts.LanguageVariant.Standard, text);
|
|
44
|
+
const tokens: string[] = [];
|
|
45
|
+
for (let kind = scanner.scan(); kind !== ts.SyntaxKind.EndOfFileToken; kind = scanner.scan()) {
|
|
46
|
+
if (kind === ts.SyntaxKind.SemicolonToken) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const spelling =
|
|
50
|
+
kind === ts.SyntaxKind.StringLiteral ? scanner.getTokenValue() : scanner.getTokenText();
|
|
51
|
+
tokens.push(`${kind}:${spelling}`);
|
|
52
|
+
}
|
|
53
|
+
return tokens;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const CANONICAL_BODY_TOKENS = bodyTokens(
|
|
57
|
+
DEBUG_MANAGED_BLOCK.slice(
|
|
58
|
+
DEBUG_BLOCK_BEGIN.length,
|
|
59
|
+
DEBUG_MANAGED_BLOCK.length - DEBUG_BLOCK_END.length,
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The `setup-debug` managed block in a source, located the way
|
|
65
|
+
* `upsertManagedBlock` locates it: exactly one BEGIN and one END, BEGIN first.
|
|
66
|
+
* Any other sentinel shape is one `setup-debug` itself refuses to edit, so it
|
|
67
|
+
* is not a managed block at all.
|
|
68
|
+
*/
|
|
69
|
+
export function findManagedBootstrap(sourceFile: ts.SourceFile): ManagedBootstrap | undefined {
|
|
70
|
+
const text = sourceFile.text;
|
|
71
|
+
if (occurrences(text, DEBUG_BLOCK_BEGIN) !== 1 || occurrences(text, DEBUG_BLOCK_END) !== 1) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const beginAt = text.indexOf(DEBUG_BLOCK_BEGIN);
|
|
75
|
+
const endAt = text.indexOf(DEBUG_BLOCK_END);
|
|
76
|
+
if (endAt < beginAt) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
const bodyStart = beginAt + DEBUG_BLOCK_BEGIN.length;
|
|
80
|
+
const statements = sourceFile.statements.filter(
|
|
81
|
+
(statement) => statement.getStart(sourceFile) >= bodyStart && statement.end <= endAt,
|
|
82
|
+
);
|
|
83
|
+
const tokens = bodyTokens(text.slice(bodyStart, endAt));
|
|
84
|
+
const canonical =
|
|
85
|
+
tokens.length === CANONICAL_BODY_TOKENS.length &&
|
|
86
|
+
tokens.every((token, index) => token === CANONICAL_BODY_TOKENS[index]);
|
|
87
|
+
return { statements, canonical };
|
|
88
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,13 @@ export {
|
|
|
11
11
|
componentIdOfSameObjectAddress,
|
|
12
12
|
} from "./component-animation-index";
|
|
13
13
|
export { buildConfigKeyIndex, readGameProjectSetting } from "./config-key-index";
|
|
14
|
+
export type { ManagedBootstrap } from "./debug-bootstrap";
|
|
15
|
+
export {
|
|
16
|
+
DEBUG_BLOCK_BEGIN,
|
|
17
|
+
DEBUG_BLOCK_END,
|
|
18
|
+
DEBUG_MANAGED_BLOCK,
|
|
19
|
+
findManagedBootstrap,
|
|
20
|
+
} from "./debug-bootstrap";
|
|
14
21
|
export { findEmittedRequires, LUALIB_REQUIRE_NAME } from "./emitted-requires";
|
|
15
22
|
export type { GuiFlipbookIndex } from "./gui-flipbook-index";
|
|
16
23
|
export { buildGuiFlipbookIndex } from "./gui-flipbook-index";
|