@defold-typescript/transpiler 0.8.3 → 0.9.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/README.md +1 -1
- package/dist/index.js +57 -14
- package/dist/window-event-guard-lowering.d.ts +2 -0
- package/package.json +2 -2
- package/src/program-diagnostics.ts +4 -2
- package/src/session.ts +2 -0
- package/src/transpile.ts +5 -0
- package/src/window-event-guard-lowering.ts +47 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Most users do not depend on this directly — the
|
|
|
13
13
|
drives it for you.
|
|
14
14
|
|
|
15
15
|
See the repository [README](https://github.com/defold-typescript/toolchain#readme)
|
|
16
|
-
and [`docs/guide/`](https://github.com/defold-typescript/toolchain/tree/main/docs/guide)
|
|
16
|
+
and [`packages/docs/guide/`](https://github.com/defold-typescript/toolchain/tree/main/packages/docs/guide)
|
|
17
17
|
for the full workflow.
|
|
18
18
|
|
|
19
19
|
## License
|
package/dist/index.js
CHANGED
|
@@ -355,7 +355,7 @@ function findMainEntryFactoryImports(fileName, source) {
|
|
|
355
355
|
return factories;
|
|
356
356
|
}
|
|
357
357
|
// src/program-diagnostics.ts
|
|
358
|
-
import * as
|
|
358
|
+
import * as ts7 from "typescript";
|
|
359
359
|
import * as tstl from "typescript-to-lua";
|
|
360
360
|
|
|
361
361
|
// src/message-guard-lowering.ts
|
|
@@ -466,10 +466,50 @@ var timersLoweringPlugin = {
|
|
|
466
466
|
}
|
|
467
467
|
};
|
|
468
468
|
|
|
469
|
+
// src/window-event-guard-lowering.ts
|
|
470
|
+
import * as ts6 from "typescript";
|
|
471
|
+
import {
|
|
472
|
+
createBinaryExpression as createBinaryExpression4,
|
|
473
|
+
SyntaxKind as SyntaxKind9
|
|
474
|
+
} from "typescript-to-lua";
|
|
475
|
+
var GUARD_MODULE2 = "@defold-typescript/types";
|
|
476
|
+
var GUARD_NAME2 = "isWindowEvent";
|
|
477
|
+
function resolvesToGuardExport2(callee, checker) {
|
|
478
|
+
let symbol = checker.getSymbolAtLocation(callee);
|
|
479
|
+
if (symbol === undefined) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
if (symbol.flags & ts6.SymbolFlags.Alias) {
|
|
483
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
484
|
+
}
|
|
485
|
+
if (symbol.getName() !== GUARD_NAME2) {
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
489
|
+
if (declaration === undefined) {
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
return declaration.getSourceFile().fileName.includes(GUARD_MODULE2);
|
|
493
|
+
}
|
|
494
|
+
var windowEventGuardLoweringPlugin = {
|
|
495
|
+
visitors: {
|
|
496
|
+
[ts6.SyntaxKind.CallExpression]: (node, context) => {
|
|
497
|
+
const [eventArg, , expectedArg] = node.arguments;
|
|
498
|
+
if (node.arguments.length === 3 && eventArg !== undefined && expectedArg !== undefined && resolvesToGuardExport2(node.expression, context.checker)) {
|
|
499
|
+
const event = context.transformExpression(eventArg);
|
|
500
|
+
const expected = context.transformExpression(expectedArg);
|
|
501
|
+
return createBinaryExpression4(event, expected, SyntaxKind9.EqualityOperator, node);
|
|
502
|
+
}
|
|
503
|
+
return context.superTransformExpression(node);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
|
|
469
508
|
// src/program-diagnostics.ts
|
|
470
509
|
var LUA_PLUGINS = [
|
|
471
510
|
lifecycleErasurePlugin,
|
|
472
511
|
messageGuardLoweringPlugin,
|
|
512
|
+
windowEventGuardLoweringPlugin,
|
|
473
513
|
messageDispatchLoweringPlugin,
|
|
474
514
|
timersLoweringPlugin
|
|
475
515
|
];
|
|
@@ -478,7 +518,7 @@ function isAmbient(fileName) {
|
|
|
478
518
|
return /[\\/]node_modules[\\/]/.test(fileName) || /(^|[\\/])lib\.[^\\/]*\.d\.ts$/.test(fileName);
|
|
479
519
|
}
|
|
480
520
|
function getProgramDiagnostics(program, sourceFile) {
|
|
481
|
-
const { diagnostics } = tstl.getProgramTranspileResult(
|
|
521
|
+
const { diagnostics } = tstl.getProgramTranspileResult(ts7.sys, noopWriteFile, {
|
|
482
522
|
program,
|
|
483
523
|
plugins: LUA_PLUGINS,
|
|
484
524
|
...sourceFile ? { sourceFiles: [sourceFile] } : {}
|
|
@@ -489,7 +529,7 @@ function getProgramDiagnostics(program, sourceFile) {
|
|
|
489
529
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
490
530
|
import { createRequire as createRequire2 } from "node:module";
|
|
491
531
|
import * as path2 from "node:path";
|
|
492
|
-
import * as
|
|
532
|
+
import * as ts9 from "typescript";
|
|
493
533
|
import * as tstl3 from "typescript-to-lua";
|
|
494
534
|
|
|
495
535
|
// src/transpile.ts
|
|
@@ -499,17 +539,17 @@ import * as path from "node:path";
|
|
|
499
539
|
import * as tstl2 from "typescript-to-lua";
|
|
500
540
|
|
|
501
541
|
// src/go-property-direct-call.ts
|
|
502
|
-
import * as
|
|
542
|
+
import * as ts8 from "typescript";
|
|
503
543
|
var GO_PROPERTY_DIRECT_CALL_MESSAGE = "`go.property` called directly is deprecated; declare the property in `defineScript({ properties })` so it types onto `self` (the transpiler emits the registration for you).";
|
|
504
544
|
function findDirectGoPropertyCalls(sourceFile) {
|
|
505
545
|
const calls = [];
|
|
506
546
|
const visit = (node) => {
|
|
507
|
-
if (
|
|
547
|
+
if (ts8.isCallExpression(node) && ts8.isPropertyAccessExpression(node.expression) && node.expression.name.text === "property" && ts8.isIdentifier(node.expression.expression) && node.expression.expression.text === "go") {
|
|
508
548
|
calls.push(node);
|
|
509
549
|
}
|
|
510
|
-
|
|
550
|
+
ts8.forEachChild(node, visit);
|
|
511
551
|
};
|
|
512
|
-
|
|
552
|
+
ts8.forEachChild(sourceFile, visit);
|
|
513
553
|
return calls;
|
|
514
554
|
}
|
|
515
555
|
|
|
@@ -557,6 +597,7 @@ function buildAmbientFiles() {
|
|
|
557
597
|
"node_modules/@defold-typescript/types/src/go-overloads.d.ts": readAmbient("src/go-overloads.d.ts"),
|
|
558
598
|
"node_modules/@defold-typescript/types/src/msg-overloads.d.ts": readAmbient("src/msg-overloads.d.ts"),
|
|
559
599
|
"node_modules/@defold-typescript/types/src/message-guard.d.ts": readAmbient("src/message-guard.d.ts"),
|
|
600
|
+
"node_modules/@defold-typescript/types/src/window-event-guard.d.ts": readAmbient("src/window-event-guard.d.ts"),
|
|
560
601
|
"node_modules/@defold-typescript/types/src/message-dispatch.d.ts": readAmbient("src/message-dispatch.d.ts"),
|
|
561
602
|
"node_modules/@defold-typescript/types/src/lifecycle.ts": readAmbient("src/lifecycle.ts"),
|
|
562
603
|
"node_modules/@defold-typescript/types/src/timers.d.ts": readAmbient("src/timers.d.ts"),
|
|
@@ -653,6 +694,7 @@ function transpileProject(input) {
|
|
|
653
694
|
luaPlugins: [
|
|
654
695
|
{ plugin: lifecycleErasurePlugin },
|
|
655
696
|
{ plugin: messageGuardLoweringPlugin },
|
|
697
|
+
{ plugin: windowEventGuardLoweringPlugin },
|
|
656
698
|
{ plugin: messageDispatchLoweringPlugin },
|
|
657
699
|
{ plugin: timersLoweringPlugin }
|
|
658
700
|
]
|
|
@@ -679,6 +721,7 @@ var COMPILER_OPTIONS = {
|
|
|
679
721
|
luaPlugins: [
|
|
680
722
|
{ plugin: lifecycleErasurePlugin },
|
|
681
723
|
{ plugin: messageGuardLoweringPlugin },
|
|
724
|
+
{ plugin: windowEventGuardLoweringPlugin },
|
|
682
725
|
{ plugin: messageDispatchLoweringPlugin },
|
|
683
726
|
{ plugin: timersLoweringPlugin }
|
|
684
727
|
]
|
|
@@ -721,7 +764,7 @@ function createTranspileSession() {
|
|
|
721
764
|
if (cached) {
|
|
722
765
|
return cached;
|
|
723
766
|
}
|
|
724
|
-
const created =
|
|
767
|
+
const created = ts9.createSourceFile(fileName, content, ts9.ScriptTarget.Latest, false);
|
|
725
768
|
sourceFileCache.set(normalized, created);
|
|
726
769
|
return created;
|
|
727
770
|
}
|
|
@@ -740,7 +783,7 @@ function createTranspileSession() {
|
|
|
740
783
|
return cached;
|
|
741
784
|
}
|
|
742
785
|
const fileContent = readFileSync2(filePath, "utf8");
|
|
743
|
-
const created =
|
|
786
|
+
const created = ts9.createSourceFile(filePath, fileContent, ts9.ScriptTarget.Latest, false);
|
|
744
787
|
libCache.set(fileName, created);
|
|
745
788
|
return created;
|
|
746
789
|
}
|
|
@@ -753,10 +796,10 @@ function createTranspileSession() {
|
|
|
753
796
|
return AMBIENT_FILES[normalized];
|
|
754
797
|
}
|
|
755
798
|
const host = {
|
|
756
|
-
fileExists: (fileName) => mergedContent(normalizeSlashes(fileName)) !== undefined ||
|
|
799
|
+
fileExists: (fileName) => mergedContent(normalizeSlashes(fileName)) !== undefined || ts9.sys.fileExists(fileName),
|
|
757
800
|
getCanonicalFileName: (fileName) => fileName,
|
|
758
801
|
getCurrentDirectory: () => "",
|
|
759
|
-
getDefaultLibFileName:
|
|
802
|
+
getDefaultLibFileName: ts9.getDefaultLibFileName,
|
|
760
803
|
readFile: () => "",
|
|
761
804
|
getNewLine: () => `
|
|
762
805
|
`,
|
|
@@ -777,15 +820,15 @@ function createTranspileSession() {
|
|
|
777
820
|
}
|
|
778
821
|
const userKeys = new Set(userFiles.keys());
|
|
779
822
|
const rootNames = [...Object.keys(AMBIENT_FILES).map(normalizeSlashes), ...userKeys];
|
|
780
|
-
program =
|
|
781
|
-
const preEmitDiagnostics =
|
|
823
|
+
program = ts9.createProgram(rootNames, COMPILER_OPTIONS, host, program);
|
|
824
|
+
const preEmitDiagnostics = ts9.getPreEmitDiagnostics(program);
|
|
782
825
|
const collector = createOutputCollector();
|
|
783
826
|
const { diagnostics: transpileDiagnostics } = new tstl3.Transpiler().emit({
|
|
784
827
|
program,
|
|
785
828
|
writeFile: collector.writeFile
|
|
786
829
|
});
|
|
787
830
|
const diagnostics = [
|
|
788
|
-
...
|
|
831
|
+
...ts9.sortAndDeduplicateDiagnostics([...preEmitDiagnostics, ...transpileDiagnostics])
|
|
789
832
|
];
|
|
790
833
|
return collectOutputs(collector.files, diagnostics, userKeys);
|
|
791
834
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defold-typescript/transpiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "TypeScript-to-Lua build pipeline tuned for Defold's runtime.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"test": "bun test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@defold-typescript/types": "0.
|
|
38
|
+
"@defold-typescript/types": "0.9.0",
|
|
39
39
|
"@typescript-to-lua/language-extensions": "1.19.0",
|
|
40
40
|
"typescript-to-lua": "^1.36.0"
|
|
41
41
|
}
|
|
@@ -4,12 +4,14 @@ import { lifecycleErasurePlugin } from "./lifecycle-erasure";
|
|
|
4
4
|
import { messageDispatchLoweringPlugin } from "./message-dispatch-lowering";
|
|
5
5
|
import { messageGuardLoweringPlugin } from "./message-guard-lowering";
|
|
6
6
|
import { timersLoweringPlugin } from "./timers-lowering";
|
|
7
|
+
import { windowEventGuardLoweringPlugin } from "./window-event-guard-lowering";
|
|
7
8
|
|
|
8
|
-
// The same
|
|
9
|
-
//
|
|
9
|
+
// The same lua plugins the build path lists (transpile.ts / session.ts), so the
|
|
10
|
+
// editor and the build agree on what is unsupported.
|
|
10
11
|
const LUA_PLUGINS: tstl.Plugin[] = [
|
|
11
12
|
lifecycleErasurePlugin,
|
|
12
13
|
messageGuardLoweringPlugin,
|
|
14
|
+
windowEventGuardLoweringPlugin,
|
|
13
15
|
messageDispatchLoweringPlugin,
|
|
14
16
|
timersLoweringPlugin,
|
|
15
17
|
];
|
package/src/session.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { messageDispatchLoweringPlugin } from "./message-dispatch-lowering";
|
|
|
8
8
|
import { messageGuardLoweringPlugin } from "./message-guard-lowering";
|
|
9
9
|
import { timersLoweringPlugin } from "./timers-lowering";
|
|
10
10
|
import { AMBIENT_FILES, collectOutputs, type TranspileProjectResult } from "./transpile";
|
|
11
|
+
import { windowEventGuardLoweringPlugin } from "./window-event-guard-lowering";
|
|
11
12
|
|
|
12
13
|
export interface TranspileSession {
|
|
13
14
|
update(changes: Readonly<Record<string, string | null>>): TranspileProjectResult;
|
|
@@ -32,6 +33,7 @@ const COMPILER_OPTIONS: tstl.CompilerOptions = {
|
|
|
32
33
|
luaPlugins: [
|
|
33
34
|
{ plugin: lifecycleErasurePlugin },
|
|
34
35
|
{ plugin: messageGuardLoweringPlugin },
|
|
36
|
+
{ plugin: windowEventGuardLoweringPlugin },
|
|
35
37
|
{ plugin: messageDispatchLoweringPlugin },
|
|
36
38
|
{ plugin: timersLoweringPlugin },
|
|
37
39
|
],
|
package/src/transpile.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { messageDispatchLoweringPlugin } from "./message-dispatch-lowering";
|
|
|
12
12
|
import { messageGuardLoweringPlugin } from "./message-guard-lowering";
|
|
13
13
|
import { importsTimersModule, timersLoweringPlugin } from "./timers-lowering";
|
|
14
14
|
import { TIMERS_RUNTIME } from "./timers-runtime";
|
|
15
|
+
import { windowEventGuardLoweringPlugin } from "./window-event-guard-lowering";
|
|
15
16
|
|
|
16
17
|
export interface TranspileResult {
|
|
17
18
|
readonly lua: string;
|
|
@@ -115,6 +116,9 @@ function buildAmbientFiles(): Record<string, string> {
|
|
|
115
116
|
readAmbient("src/msg-overloads.d.ts"),
|
|
116
117
|
"node_modules/@defold-typescript/types/src/message-guard.d.ts":
|
|
117
118
|
readAmbient("src/message-guard.d.ts"),
|
|
119
|
+
"node_modules/@defold-typescript/types/src/window-event-guard.d.ts": readAmbient(
|
|
120
|
+
"src/window-event-guard.d.ts",
|
|
121
|
+
),
|
|
118
122
|
"node_modules/@defold-typescript/types/src/message-dispatch.d.ts": readAmbient(
|
|
119
123
|
"src/message-dispatch.d.ts",
|
|
120
124
|
),
|
|
@@ -259,6 +263,7 @@ export function transpileProject(input: TranspileProjectInput): TranspileProject
|
|
|
259
263
|
luaPlugins: [
|
|
260
264
|
{ plugin: lifecycleErasurePlugin },
|
|
261
265
|
{ plugin: messageGuardLoweringPlugin },
|
|
266
|
+
{ plugin: windowEventGuardLoweringPlugin },
|
|
262
267
|
{ plugin: messageDispatchLoweringPlugin },
|
|
263
268
|
{ plugin: timersLoweringPlugin },
|
|
264
269
|
],
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
createBinaryExpression,
|
|
4
|
+
type Expression,
|
|
5
|
+
type Plugin,
|
|
6
|
+
SyntaxKind,
|
|
7
|
+
} from "typescript-to-lua";
|
|
8
|
+
|
|
9
|
+
const GUARD_MODULE = "@defold-typescript/types";
|
|
10
|
+
const GUARD_NAME = "isWindowEvent";
|
|
11
|
+
|
|
12
|
+
function resolvesToGuardExport(callee: ts.Expression, checker: ts.TypeChecker): boolean {
|
|
13
|
+
let symbol = checker.getSymbolAtLocation(callee);
|
|
14
|
+
if (symbol === undefined) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
18
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
19
|
+
}
|
|
20
|
+
if (symbol.getName() !== GUARD_NAME) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
24
|
+
if (declaration === undefined) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return declaration.getSourceFile().fileName.includes(GUARD_MODULE);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const windowEventGuardLoweringPlugin: Plugin = {
|
|
31
|
+
visitors: {
|
|
32
|
+
[ts.SyntaxKind.CallExpression]: (node, context): Expression => {
|
|
33
|
+
const [eventArg, , expectedArg] = node.arguments;
|
|
34
|
+
if (
|
|
35
|
+
node.arguments.length === 3 &&
|
|
36
|
+
eventArg !== undefined &&
|
|
37
|
+
expectedArg !== undefined &&
|
|
38
|
+
resolvesToGuardExport(node.expression, context.checker)
|
|
39
|
+
) {
|
|
40
|
+
const event = context.transformExpression(eventArg);
|
|
41
|
+
const expected = context.transformExpression(expectedArg);
|
|
42
|
+
return createBinaryExpression(event, expected, SyntaxKind.EqualityOperator, node);
|
|
43
|
+
}
|
|
44
|
+
return context.superTransformExpression(node);
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|