@defold-typescript/transpiler 0.8.2 → 0.8.4

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/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 ts6 from "typescript";
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(ts6.sys, noopWriteFile, {
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 ts8 from "typescript";
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 ts7 from "typescript";
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 (ts7.isCallExpression(node) && ts7.isPropertyAccessExpression(node.expression) && node.expression.name.text === "property" && ts7.isIdentifier(node.expression.expression) && node.expression.expression.text === "go") {
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
- ts7.forEachChild(node, visit);
550
+ ts8.forEachChild(node, visit);
511
551
  };
512
- ts7.forEachChild(sourceFile, visit);
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"),
@@ -647,11 +688,13 @@ function transpileProject(input) {
647
688
  const result = tstl2.transpileVirtualProject(merged, {
648
689
  luaTarget: tstl2.LuaTarget.Lua51,
649
690
  sourceMap: true,
691
+ lib: ["lib.es2022.d.ts"],
650
692
  skipLibCheck: true,
651
693
  noImplicitSelf: true,
652
694
  luaPlugins: [
653
695
  { plugin: lifecycleErasurePlugin },
654
696
  { plugin: messageGuardLoweringPlugin },
697
+ { plugin: windowEventGuardLoweringPlugin },
655
698
  { plugin: messageDispatchLoweringPlugin },
656
699
  { plugin: timersLoweringPlugin }
657
700
  ]
@@ -672,11 +715,13 @@ var requireFromHere2 = createRequire2(import.meta.url);
672
715
  var COMPILER_OPTIONS = {
673
716
  luaTarget: tstl3.LuaTarget.Lua51,
674
717
  sourceMap: true,
718
+ lib: ["lib.es2022.d.ts"],
675
719
  skipLibCheck: true,
676
720
  noImplicitSelf: true,
677
721
  luaPlugins: [
678
722
  { plugin: lifecycleErasurePlugin },
679
723
  { plugin: messageGuardLoweringPlugin },
724
+ { plugin: windowEventGuardLoweringPlugin },
680
725
  { plugin: messageDispatchLoweringPlugin },
681
726
  { plugin: timersLoweringPlugin }
682
727
  ]
@@ -719,7 +764,7 @@ function createTranspileSession() {
719
764
  if (cached) {
720
765
  return cached;
721
766
  }
722
- const created = ts8.createSourceFile(fileName, content, ts8.ScriptTarget.Latest, false);
767
+ const created = ts9.createSourceFile(fileName, content, ts9.ScriptTarget.Latest, false);
723
768
  sourceFileCache.set(normalized, created);
724
769
  return created;
725
770
  }
@@ -738,7 +783,7 @@ function createTranspileSession() {
738
783
  return cached;
739
784
  }
740
785
  const fileContent = readFileSync2(filePath, "utf8");
741
- const created = ts8.createSourceFile(filePath, fileContent, ts8.ScriptTarget.Latest, false);
786
+ const created = ts9.createSourceFile(filePath, fileContent, ts9.ScriptTarget.Latest, false);
742
787
  libCache.set(fileName, created);
743
788
  return created;
744
789
  }
@@ -751,10 +796,10 @@ function createTranspileSession() {
751
796
  return AMBIENT_FILES[normalized];
752
797
  }
753
798
  const host = {
754
- fileExists: (fileName) => mergedContent(normalizeSlashes(fileName)) !== undefined || ts8.sys.fileExists(fileName),
799
+ fileExists: (fileName) => mergedContent(normalizeSlashes(fileName)) !== undefined || ts9.sys.fileExists(fileName),
755
800
  getCanonicalFileName: (fileName) => fileName,
756
801
  getCurrentDirectory: () => "",
757
- getDefaultLibFileName: ts8.getDefaultLibFileName,
802
+ getDefaultLibFileName: ts9.getDefaultLibFileName,
758
803
  readFile: () => "",
759
804
  getNewLine: () => `
760
805
  `,
@@ -775,15 +820,15 @@ function createTranspileSession() {
775
820
  }
776
821
  const userKeys = new Set(userFiles.keys());
777
822
  const rootNames = [...Object.keys(AMBIENT_FILES).map(normalizeSlashes), ...userKeys];
778
- program = ts8.createProgram(rootNames, COMPILER_OPTIONS, host, program);
779
- const preEmitDiagnostics = ts8.getPreEmitDiagnostics(program);
823
+ program = ts9.createProgram(rootNames, COMPILER_OPTIONS, host, program);
824
+ const preEmitDiagnostics = ts9.getPreEmitDiagnostics(program);
780
825
  const collector = createOutputCollector();
781
826
  const { diagnostics: transpileDiagnostics } = new tstl3.Transpiler().emit({
782
827
  program,
783
828
  writeFile: collector.writeFile
784
829
  });
785
830
  const diagnostics = [
786
- ...ts8.sortAndDeduplicateDiagnostics([...preEmitDiagnostics, ...transpileDiagnostics])
831
+ ...ts9.sortAndDeduplicateDiagnostics([...preEmitDiagnostics, ...transpileDiagnostics])
787
832
  ];
788
833
  return collectOutputs(collector.files, diagnostics, userKeys);
789
834
  }
@@ -0,0 +1,2 @@
1
+ import { type Plugin } from "typescript-to-lua";
2
+ export declare const windowEventGuardLoweringPlugin: Plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defold-typescript/transpiler",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
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.8.2",
38
+ "@defold-typescript/types": "0.8.4",
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 four lua plugins the build path lists (transpile.ts / session.ts),
9
- // so the editor and the build agree on what is unsupported.
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;
@@ -19,6 +20,10 @@ const requireFromHere = createRequire(import.meta.url);
19
20
  const COMPILER_OPTIONS: tstl.CompilerOptions = {
20
21
  luaTarget: tstl.LuaTarget.Lua51,
21
22
  sourceMap: true,
23
+ // Already-resolved CompilerOptions, so the value is the lib file name, not the
24
+ // tsconfig spelling "ES2022". Drops the default `lib.dom`, whose `declare var
25
+ // window` shadows Defold's `window` namespace. Keep in lockstep with transpile.ts.
26
+ lib: ["lib.es2022.d.ts"],
22
27
  // Don't cross-check the seeded ambient .d.ts surface against itself; only user
23
28
  // files matter (mirrors transpileProject and the editor's skipLibCheck).
24
29
  skipLibCheck: true,
@@ -28,6 +33,7 @@ const COMPILER_OPTIONS: tstl.CompilerOptions = {
28
33
  luaPlugins: [
29
34
  { plugin: lifecycleErasurePlugin },
30
35
  { plugin: messageGuardLoweringPlugin },
36
+ { plugin: windowEventGuardLoweringPlugin },
31
37
  { plugin: messageDispatchLoweringPlugin },
32
38
  { plugin: timersLoweringPlugin },
33
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
  ),
@@ -246,6 +250,10 @@ export function transpileProject(input: TranspileProjectInput): TranspileProject
246
250
  const result = tstl.transpileVirtualProject(merged, {
247
251
  luaTarget: tstl.LuaTarget.Lua51,
248
252
  sourceMap: true,
253
+ // Already-resolved CompilerOptions, so the value is the lib file name, not
254
+ // the tsconfig spelling "ES2022". Drops the default `lib.dom`, whose
255
+ // `declare var window` shadows Defold's `window` namespace.
256
+ lib: ["lib.es2022.d.ts"],
249
257
  // Don't cross-check the seeded ambient .d.ts surface against itself; we only
250
258
  // care about diagnostics on user files (mirrors the editor's skipLibCheck).
251
259
  skipLibCheck: true,
@@ -255,6 +263,7 @@ export function transpileProject(input: TranspileProjectInput): TranspileProject
255
263
  luaPlugins: [
256
264
  { plugin: lifecycleErasurePlugin },
257
265
  { plugin: messageGuardLoweringPlugin },
266
+ { plugin: windowEventGuardLoweringPlugin },
258
267
  { plugin: messageDispatchLoweringPlugin },
259
268
  { plugin: timersLoweringPlugin },
260
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
+ };