@defold-typescript/transpiler 0.50.1 → 0.51.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/index.js +151 -105
- package/dist/physics-event-guard-lowering.d.ts +2 -0
- package/package.json +2 -2
- package/src/physics-event-guard-lowering.ts +56 -0
- package/src/program-diagnostics.ts +2 -0
- package/src/session.ts +2 -0
- package/src/transpile.ts +2 -0
package/dist/index.js
CHANGED
|
@@ -2225,7 +2225,7 @@ function computeOutputRel(rel, config, kind) {
|
|
|
2225
2225
|
return baseRel.replace(/\.ts$/, SCRIPT_SUFFIX_BY_KIND[kind]);
|
|
2226
2226
|
}
|
|
2227
2227
|
// src/program-diagnostics.ts
|
|
2228
|
-
import * as
|
|
2228
|
+
import * as ts11 from "typescript";
|
|
2229
2229
|
import * as tstl from "typescript-to-lua";
|
|
2230
2230
|
|
|
2231
2231
|
// src/message-guard-lowering.ts
|
|
@@ -2269,38 +2269,81 @@ var messageGuardLoweringPlugin = {
|
|
|
2269
2269
|
}
|
|
2270
2270
|
};
|
|
2271
2271
|
|
|
2272
|
-
// src/
|
|
2272
|
+
// src/physics-event-guard-lowering.ts
|
|
2273
2273
|
import * as ts7 from "typescript";
|
|
2274
|
+
import {
|
|
2275
|
+
createBinaryExpression as createBinaryExpression4,
|
|
2276
|
+
createCallExpression as createCallExpression4,
|
|
2277
|
+
createIdentifier as createIdentifier4,
|
|
2278
|
+
createStringLiteral as createStringLiteral3,
|
|
2279
|
+
createTableIndexExpression as createTableIndexExpression2,
|
|
2280
|
+
SyntaxKind as SyntaxKind10
|
|
2281
|
+
} from "typescript-to-lua";
|
|
2282
|
+
var GUARD_MODULE2 = "@defold-typescript/types";
|
|
2283
|
+
var GUARD_NAME2 = "isPhysicsEvent";
|
|
2284
|
+
function resolvesToGuardExport2(callee, checker) {
|
|
2285
|
+
let symbol = checker.getSymbolAtLocation(callee);
|
|
2286
|
+
if (symbol === undefined) {
|
|
2287
|
+
return false;
|
|
2288
|
+
}
|
|
2289
|
+
if (symbol.flags & ts7.SymbolFlags.Alias) {
|
|
2290
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
2291
|
+
}
|
|
2292
|
+
if (symbol.getName() !== GUARD_NAME2) {
|
|
2293
|
+
return false;
|
|
2294
|
+
}
|
|
2295
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
2296
|
+
if (declaration === undefined) {
|
|
2297
|
+
return false;
|
|
2298
|
+
}
|
|
2299
|
+
return declaration.getSourceFile().fileName.includes(GUARD_MODULE2);
|
|
2300
|
+
}
|
|
2301
|
+
var physicsEventGuardLoweringPlugin = {
|
|
2302
|
+
visitors: {
|
|
2303
|
+
[ts7.SyntaxKind.CallExpression]: (node, context) => {
|
|
2304
|
+
const [eventArg, nameArg] = node.arguments;
|
|
2305
|
+
if (node.arguments.length === 2 && eventArg !== undefined && nameArg !== undefined && resolvesToGuardExport2(node.expression, context.checker)) {
|
|
2306
|
+
const event = context.transformExpression(eventArg);
|
|
2307
|
+
const name = context.transformExpression(nameArg);
|
|
2308
|
+
return createBinaryExpression4(createTableIndexExpression2(event, createStringLiteral3("type")), createCallExpression4(createIdentifier4("hash"), [name]), SyntaxKind10.EqualityOperator, node);
|
|
2309
|
+
}
|
|
2310
|
+
return context.superTransformExpression(node);
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
};
|
|
2314
|
+
|
|
2315
|
+
// src/timers-lowering.ts
|
|
2316
|
+
import * as ts8 from "typescript";
|
|
2274
2317
|
import { LuaLibFeature as LuaLibFeature2 } from "typescript-to-lua";
|
|
2275
2318
|
function isTimersSpecifier(node) {
|
|
2276
|
-
return
|
|
2319
|
+
return ts8.isStringLiteral(node) && node.text === TIMERS_MODULE_SPECIFIER;
|
|
2277
2320
|
}
|
|
2278
2321
|
function importsTimersModule(sourceFile) {
|
|
2279
|
-
return sourceFile.statements.some((statement) =>
|
|
2322
|
+
return sourceFile.statements.some((statement) => ts8.isImportDeclaration(statement) && isTimersSpecifier(statement.moduleSpecifier));
|
|
2280
2323
|
}
|
|
2281
2324
|
var TIMERS_REQUIRE_PATH = `@NoResolution:${TIMERS_REQUIRE_NAME}`;
|
|
2282
2325
|
var timersLoweringPlugin = {
|
|
2283
2326
|
visitors: {
|
|
2284
|
-
[
|
|
2327
|
+
[ts8.SyntaxKind.ImportDeclaration]: (node, context) => {
|
|
2285
2328
|
if (!isTimersSpecifier(node.moduleSpecifier)) {
|
|
2286
2329
|
return context.superTransformStatements(node);
|
|
2287
2330
|
}
|
|
2288
2331
|
context.usedLuaLibFeatures.add(LuaLibFeature2.Promise);
|
|
2289
2332
|
context.usedLuaLibFeatures.add(LuaLibFeature2.New);
|
|
2290
|
-
const rewritten =
|
|
2333
|
+
const rewritten = ts8.factory.updateImportDeclaration(node, node.modifiers, node.importClause, ts8.factory.createStringLiteral(TIMERS_REQUIRE_PATH), node.attributes);
|
|
2291
2334
|
return context.superTransformStatements(rewritten);
|
|
2292
2335
|
}
|
|
2293
2336
|
}
|
|
2294
2337
|
};
|
|
2295
2338
|
|
|
2296
2339
|
// src/type-application-erasure.ts
|
|
2297
|
-
import * as
|
|
2340
|
+
import * as ts9 from "typescript";
|
|
2298
2341
|
var TYPE_APPLICATION_APIS = new Set(["go.get", "go.set"]);
|
|
2299
2342
|
function isTypeApplication(call, checker) {
|
|
2300
2343
|
if (call.arguments.length !== 0)
|
|
2301
2344
|
return false;
|
|
2302
2345
|
const declaration = checker.getResolvedSignature(call)?.declaration;
|
|
2303
|
-
if (declaration === undefined || !
|
|
2346
|
+
if (declaration === undefined || !ts9.isFunctionDeclaration(declaration))
|
|
2304
2347
|
return false;
|
|
2305
2348
|
if (declaration.parameters.length !== 0 || declaration.typeParameters === undefined)
|
|
2306
2349
|
return false;
|
|
@@ -2314,7 +2357,7 @@ function isTypeApplication(call, checker) {
|
|
|
2314
2357
|
}
|
|
2315
2358
|
var typeApplicationErasurePlugin = {
|
|
2316
2359
|
visitors: {
|
|
2317
|
-
[
|
|
2360
|
+
[ts9.SyntaxKind.CallExpression]: (node, context) => {
|
|
2318
2361
|
if (isTypeApplication(node, context.checker)) {
|
|
2319
2362
|
return context.transformExpression(node.expression);
|
|
2320
2363
|
}
|
|
@@ -2324,38 +2367,38 @@ var typeApplicationErasurePlugin = {
|
|
|
2324
2367
|
};
|
|
2325
2368
|
|
|
2326
2369
|
// src/window-event-guard-lowering.ts
|
|
2327
|
-
import * as
|
|
2370
|
+
import * as ts10 from "typescript";
|
|
2328
2371
|
import {
|
|
2329
|
-
createBinaryExpression as
|
|
2330
|
-
SyntaxKind as
|
|
2372
|
+
createBinaryExpression as createBinaryExpression5,
|
|
2373
|
+
SyntaxKind as SyntaxKind14
|
|
2331
2374
|
} from "typescript-to-lua";
|
|
2332
|
-
var
|
|
2333
|
-
var
|
|
2334
|
-
function
|
|
2375
|
+
var GUARD_MODULE3 = "@defold-typescript/types";
|
|
2376
|
+
var GUARD_NAME3 = "isWindowEvent";
|
|
2377
|
+
function resolvesToGuardExport3(callee, checker) {
|
|
2335
2378
|
let symbol = checker.getSymbolAtLocation(callee);
|
|
2336
2379
|
if (symbol === undefined) {
|
|
2337
2380
|
return false;
|
|
2338
2381
|
}
|
|
2339
|
-
if (symbol.flags &
|
|
2382
|
+
if (symbol.flags & ts10.SymbolFlags.Alias) {
|
|
2340
2383
|
symbol = checker.getAliasedSymbol(symbol);
|
|
2341
2384
|
}
|
|
2342
|
-
if (symbol.getName() !==
|
|
2385
|
+
if (symbol.getName() !== GUARD_NAME3) {
|
|
2343
2386
|
return false;
|
|
2344
2387
|
}
|
|
2345
2388
|
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
2346
2389
|
if (declaration === undefined) {
|
|
2347
2390
|
return false;
|
|
2348
2391
|
}
|
|
2349
|
-
return declaration.getSourceFile().fileName.includes(
|
|
2392
|
+
return declaration.getSourceFile().fileName.includes(GUARD_MODULE3);
|
|
2350
2393
|
}
|
|
2351
2394
|
var windowEventGuardLoweringPlugin = {
|
|
2352
2395
|
visitors: {
|
|
2353
|
-
[
|
|
2396
|
+
[ts10.SyntaxKind.CallExpression]: (node, context) => {
|
|
2354
2397
|
const [eventArg, , expectedArg] = node.arguments;
|
|
2355
|
-
if (node.arguments.length === 3 && eventArg !== undefined && expectedArg !== undefined &&
|
|
2398
|
+
if (node.arguments.length === 3 && eventArg !== undefined && expectedArg !== undefined && resolvesToGuardExport3(node.expression, context.checker)) {
|
|
2356
2399
|
const event = context.transformExpression(eventArg);
|
|
2357
2400
|
const expected = context.transformExpression(expectedArg);
|
|
2358
|
-
return
|
|
2401
|
+
return createBinaryExpression5(event, expected, SyntaxKind14.EqualityOperator, node);
|
|
2359
2402
|
}
|
|
2360
2403
|
return context.superTransformExpression(node);
|
|
2361
2404
|
}
|
|
@@ -2367,6 +2410,7 @@ var LUA_PLUGINS = [
|
|
|
2367
2410
|
lifecycleErasurePlugin,
|
|
2368
2411
|
messageGuardLoweringPlugin,
|
|
2369
2412
|
windowEventGuardLoweringPlugin,
|
|
2413
|
+
physicsEventGuardLoweringPlugin,
|
|
2370
2414
|
messageDispatchLoweringPlugin,
|
|
2371
2415
|
timersLoweringPlugin,
|
|
2372
2416
|
typeApplicationErasurePlugin
|
|
@@ -2376,7 +2420,7 @@ function isAmbient(fileName) {
|
|
|
2376
2420
|
return /[\\/]node_modules[\\/]/.test(fileName) || /(^|[\\/])lib\.[^\\/]*\.d\.ts$/.test(fileName);
|
|
2377
2421
|
}
|
|
2378
2422
|
function getProgramDiagnostics(program, sourceFile) {
|
|
2379
|
-
const { diagnostics } = tstl.getProgramTranspileResult(
|
|
2423
|
+
const { diagnostics } = tstl.getProgramTranspileResult(ts11.sys, noopWriteFile, {
|
|
2380
2424
|
program,
|
|
2381
2425
|
plugins: LUA_PLUGINS,
|
|
2382
2426
|
...sourceFile ? { sourceFiles: [sourceFile] } : {}
|
|
@@ -2947,21 +2991,21 @@ function relativeAddressesFrom(index, context) {
|
|
|
2947
2991
|
import { readFileSync as readFileSync2 } from "node:fs";
|
|
2948
2992
|
import { createRequire as createRequire2 } from "node:module";
|
|
2949
2993
|
import * as path4 from "node:path";
|
|
2950
|
-
import * as
|
|
2994
|
+
import * as ts15 from "typescript";
|
|
2951
2995
|
import * as tstl3 from "typescript-to-lua";
|
|
2952
2996
|
|
|
2953
2997
|
// src/companion-emit.ts
|
|
2954
|
-
import * as
|
|
2998
|
+
import * as ts12 from "typescript";
|
|
2955
2999
|
import {
|
|
2956
3000
|
createAssignmentStatement as createAssignmentStatement2,
|
|
2957
|
-
createCallExpression as
|
|
3001
|
+
createCallExpression as createCallExpression5,
|
|
2958
3002
|
createFile,
|
|
2959
|
-
createIdentifier as
|
|
3003
|
+
createIdentifier as createIdentifier5,
|
|
2960
3004
|
createReturnStatement as createReturnStatement2,
|
|
2961
|
-
createStringLiteral as
|
|
3005
|
+
createStringLiteral as createStringLiteral4,
|
|
2962
3006
|
createTableExpression,
|
|
2963
3007
|
createTableFieldExpression,
|
|
2964
|
-
createTableIndexExpression as
|
|
3008
|
+
createTableIndexExpression as createTableIndexExpression3,
|
|
2965
3009
|
createVariableDeclarationStatement as createVariableDeclarationStatement3,
|
|
2966
3010
|
isBlock as isBlock3,
|
|
2967
3011
|
isCallExpression as isCallExpression4,
|
|
@@ -2981,12 +3025,12 @@ import {
|
|
|
2981
3025
|
} from "typescript-to-lua";
|
|
2982
3026
|
var EXPORTS_NAME = "____exports";
|
|
2983
3027
|
function exportsIdentifier() {
|
|
2984
|
-
return
|
|
3028
|
+
return createIdentifier5(EXPORTS_NAME);
|
|
2985
3029
|
}
|
|
2986
3030
|
function isRuntimeScriptSource(sourceFile, checker) {
|
|
2987
3031
|
return sourceFile.statements.some((statement) => {
|
|
2988
|
-
const expression =
|
|
2989
|
-
return expression !== undefined &&
|
|
3032
|
+
const expression = ts12.isExpressionStatement(statement) ? statement.expression : ts12.isExportAssignment(statement) && !statement.isExportEquals ? statement.expression : undefined;
|
|
3033
|
+
return expression !== undefined && ts12.isCallExpression(expression) && resolvesToFactoryExport(expression.expression, checker);
|
|
2990
3034
|
});
|
|
2991
3035
|
}
|
|
2992
3036
|
function planFor(sourceFile, checker) {
|
|
@@ -3138,11 +3182,11 @@ function freeNames(statements) {
|
|
|
3138
3182
|
function resolvedRequirePaths(sourceFile, checker) {
|
|
3139
3183
|
const paths = new Map;
|
|
3140
3184
|
for (const statement of sourceFile.statements) {
|
|
3141
|
-
const specifier =
|
|
3142
|
-
if (specifier === undefined || !
|
|
3185
|
+
const specifier = ts12.isImportDeclaration(statement) || ts12.isExportDeclaration(statement) ? statement.moduleSpecifier : undefined;
|
|
3186
|
+
if (specifier === undefined || !ts12.isStringLiteral(specifier)) {
|
|
3143
3187
|
continue;
|
|
3144
3188
|
}
|
|
3145
|
-
const target = checker.getSymbolAtLocation(specifier)?.declarations?.find((declaration) =>
|
|
3189
|
+
const target = checker.getSymbolAtLocation(specifier)?.declarations?.find((declaration) => ts12.isSourceFile(declaration));
|
|
3146
3190
|
if (target === undefined || target.isDeclarationFile) {
|
|
3147
3191
|
continue;
|
|
3148
3192
|
}
|
|
@@ -3195,7 +3239,7 @@ function withResolvedRequire(statement, paths) {
|
|
|
3195
3239
|
if (resolved === argument.value) {
|
|
3196
3240
|
return statement;
|
|
3197
3241
|
}
|
|
3198
|
-
return createVariableDeclarationStatement3(
|
|
3242
|
+
return createVariableDeclarationStatement3(createIdentifier5(name.text), createCallExpression5(createIdentifier5("require"), [createStringLiteral4(resolved)]));
|
|
3199
3243
|
}
|
|
3200
3244
|
function copiedPrelude(halves, importBindings) {
|
|
3201
3245
|
const needed = new Set(importBindings);
|
|
@@ -3248,7 +3292,7 @@ function isHoistedNameDeclaration(statement) {
|
|
|
3248
3292
|
return isVariableDeclarationStatement(statement) && statement.right === undefined;
|
|
3249
3293
|
}
|
|
3250
3294
|
function internalsField(name) {
|
|
3251
|
-
return
|
|
3295
|
+
return createTableIndexExpression3(createTableIndexExpression3(exportsIdentifier(), createStringLiteral4(COMPANION_INTERNAL_FIELD)), createStringLiteral4(name));
|
|
3252
3296
|
}
|
|
3253
3297
|
function requireInsertionPoint(script, reads) {
|
|
3254
3298
|
let index = 0;
|
|
@@ -3311,11 +3355,11 @@ function createCompanionEmitPlugin() {
|
|
|
3311
3355
|
return statements;
|
|
3312
3356
|
}
|
|
3313
3357
|
const visitors = {
|
|
3314
|
-
[
|
|
3315
|
-
[
|
|
3316
|
-
[
|
|
3317
|
-
[
|
|
3318
|
-
[
|
|
3358
|
+
[ts12.SyntaxKind.VariableStatement]: capture,
|
|
3359
|
+
[ts12.SyntaxKind.FunctionDeclaration]: capture,
|
|
3360
|
+
[ts12.SyntaxKind.ClassDeclaration]: capture,
|
|
3361
|
+
[ts12.SyntaxKind.EnumDeclaration]: capture,
|
|
3362
|
+
[ts12.SyntaxKind.ExportDeclaration]: capture
|
|
3319
3363
|
};
|
|
3320
3364
|
let configuredNoResolvePaths = [];
|
|
3321
3365
|
const plugin = {
|
|
@@ -3353,13 +3397,13 @@ function emitCompanion(program, emitHost, file, sourceFile, plan, owned, compani
|
|
|
3353
3397
|
const halves = split(luaAst.statements, owned);
|
|
3354
3398
|
const requirePaths = resolvedRequirePaths(sourceFile, program.getTypeChecker());
|
|
3355
3399
|
resolveMovedRequires(halves.companion, requirePaths);
|
|
3356
|
-
const closureTable = createTableExpression(plan.internals.map((name) => createTableFieldExpression(
|
|
3400
|
+
const closureTable = createTableExpression(plan.internals.map((name) => createTableFieldExpression(createIdentifier5(name), createStringLiteral4(name))));
|
|
3357
3401
|
const companionStatements = [
|
|
3358
3402
|
createVariableDeclarationStatement3(exportsIdentifier(), createTableExpression()),
|
|
3359
3403
|
...copiedPrelude(halves, plan.importBindings).map((statement) => withResolvedRequire(statement, requirePaths)),
|
|
3360
3404
|
...halves.companion,
|
|
3361
3405
|
...plan.internals.length > 0 ? [
|
|
3362
|
-
createAssignmentStatement2(
|
|
3406
|
+
createAssignmentStatement2(createTableIndexExpression3(exportsIdentifier(), createStringLiteral4(COMPANION_INTERNAL_FIELD)), closureTable)
|
|
3363
3407
|
] : [],
|
|
3364
3408
|
createReturnStatement2([exportsIdentifier()])
|
|
3365
3409
|
];
|
|
@@ -3367,8 +3411,8 @@ function emitCompanion(program, emitHost, file, sourceFile, plan, owned, compani
|
|
|
3367
3411
|
const insertAt = requireInsertionPoint(halves.script, reads);
|
|
3368
3412
|
const scriptStatements = [
|
|
3369
3413
|
...halves.script.slice(0, insertAt),
|
|
3370
|
-
createVariableDeclarationStatement3(exportsIdentifier(),
|
|
3371
|
-
...plan.internals.map((name) => createVariableDeclarationStatement3(
|
|
3414
|
+
createVariableDeclarationStatement3(exportsIdentifier(), createCallExpression5(createIdentifier5("require"), [createStringLiteral4(plan.requirePath)])),
|
|
3415
|
+
...plan.internals.map((name) => createVariableDeclarationStatement3(createIdentifier5(name), internalsField(name))),
|
|
3372
3416
|
...halves.script.slice(insertAt),
|
|
3373
3417
|
createReturnStatement2([exportsIdentifier()])
|
|
3374
3418
|
];
|
|
@@ -3384,7 +3428,7 @@ function emitCompanion(program, emitHost, file, sourceFile, plan, owned, compani
|
|
|
3384
3428
|
}
|
|
3385
3429
|
|
|
3386
3430
|
// src/editor-script-erasure.ts
|
|
3387
|
-
import * as
|
|
3431
|
+
import * as ts13 from "typescript";
|
|
3388
3432
|
import {
|
|
3389
3433
|
createReturnStatement as createReturnStatement3
|
|
3390
3434
|
} from "typescript-to-lua";
|
|
@@ -3404,7 +3448,7 @@ function resolveEditorFactoryExport(callee, checker) {
|
|
|
3404
3448
|
if (symbol === undefined) {
|
|
3405
3449
|
return;
|
|
3406
3450
|
}
|
|
3407
|
-
if (symbol.flags &
|
|
3451
|
+
if (symbol.flags & ts13.SymbolFlags.Alias) {
|
|
3408
3452
|
symbol = checker.getAliasedSymbol(symbol);
|
|
3409
3453
|
}
|
|
3410
3454
|
const name = symbol.getName();
|
|
@@ -3418,7 +3462,7 @@ function resolveEditorFactoryExport(callee, checker) {
|
|
|
3418
3462
|
return declaration.getSourceFile().fileName.includes(EDITOR_FACTORY_MODULE) ? name : undefined;
|
|
3419
3463
|
}
|
|
3420
3464
|
function lowerEditorFactoryCall(expression, context) {
|
|
3421
|
-
if (!
|
|
3465
|
+
if (!ts13.isCallExpression(expression)) {
|
|
3422
3466
|
return;
|
|
3423
3467
|
}
|
|
3424
3468
|
if (resolveEditorFactoryExport(expression.expression, context.checker) !== EDITOR_SCRIPT_FACTORY_NAME) {
|
|
@@ -3431,7 +3475,7 @@ function lowerEditorFactoryCall(expression, context) {
|
|
|
3431
3475
|
return [createReturnStatement3([context.transformExpression(module)])];
|
|
3432
3476
|
}
|
|
3433
3477
|
function isEditorFactoryOnlyImport(node) {
|
|
3434
|
-
if (!
|
|
3478
|
+
if (!ts13.isStringLiteral(node.moduleSpecifier) || !EDITOR_FACTORY_SPECIFIERS.has(node.moduleSpecifier.text)) {
|
|
3435
3479
|
return false;
|
|
3436
3480
|
}
|
|
3437
3481
|
const clause = node.importClause;
|
|
@@ -3442,7 +3486,7 @@ function isEditorFactoryOnlyImport(node) {
|
|
|
3442
3486
|
return false;
|
|
3443
3487
|
}
|
|
3444
3488
|
const bindings = clause.namedBindings;
|
|
3445
|
-
if (bindings === undefined || !
|
|
3489
|
+
if (bindings === undefined || !ts13.isNamedImports(bindings)) {
|
|
3446
3490
|
return false;
|
|
3447
3491
|
}
|
|
3448
3492
|
const runtime = bindings.elements.filter((element) => !element.isTypeOnly);
|
|
@@ -3453,7 +3497,7 @@ function isEditorFactoryOnlyImport(node) {
|
|
|
3453
3497
|
}
|
|
3454
3498
|
var editorScriptErasurePlugin = {
|
|
3455
3499
|
visitors: {
|
|
3456
|
-
[
|
|
3500
|
+
[ts13.SyntaxKind.ExportAssignment]: (node, context) => {
|
|
3457
3501
|
if (!node.isExportEquals) {
|
|
3458
3502
|
const lowered = lowerEditorFactoryCall(node.expression, context);
|
|
3459
3503
|
if (lowered !== undefined) {
|
|
@@ -3462,14 +3506,14 @@ var editorScriptErasurePlugin = {
|
|
|
3462
3506
|
}
|
|
3463
3507
|
return context.superTransformStatements(node);
|
|
3464
3508
|
},
|
|
3465
|
-
[
|
|
3509
|
+
[ts13.SyntaxKind.CallExpression]: (node, context) => {
|
|
3466
3510
|
const command = node.arguments[0];
|
|
3467
3511
|
if (command !== undefined && resolveEditorFactoryExport(node.expression, context.checker) === EDITOR_COMMAND_FACTORY_NAME) {
|
|
3468
3512
|
return context.transformExpression(command);
|
|
3469
3513
|
}
|
|
3470
3514
|
return context.superTransformExpression(node);
|
|
3471
3515
|
},
|
|
3472
|
-
[
|
|
3516
|
+
[ts13.SyntaxKind.ImportDeclaration]: (node, context) => isEditorFactoryOnlyImport(node) ? [] : context.superTransformStatements(node)
|
|
3473
3517
|
}
|
|
3474
3518
|
};
|
|
3475
3519
|
|
|
@@ -3480,17 +3524,17 @@ import * as path3 from "node:path";
|
|
|
3480
3524
|
import * as tstl2 from "typescript-to-lua";
|
|
3481
3525
|
|
|
3482
3526
|
// src/go-property-direct-call.ts
|
|
3483
|
-
import * as
|
|
3527
|
+
import * as ts14 from "typescript";
|
|
3484
3528
|
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).";
|
|
3485
3529
|
function findDirectGoPropertyCalls(sourceFile) {
|
|
3486
3530
|
const calls = [];
|
|
3487
3531
|
const visit = (node) => {
|
|
3488
|
-
if (
|
|
3532
|
+
if (ts14.isCallExpression(node) && ts14.isPropertyAccessExpression(node.expression) && node.expression.name.text === "property" && ts14.isIdentifier(node.expression.expression) && node.expression.expression.text === "go") {
|
|
3489
3533
|
calls.push(node);
|
|
3490
3534
|
}
|
|
3491
|
-
|
|
3535
|
+
ts14.forEachChild(node, visit);
|
|
3492
3536
|
};
|
|
3493
|
-
|
|
3537
|
+
ts14.forEachChild(sourceFile, visit);
|
|
3494
3538
|
return calls;
|
|
3495
3539
|
}
|
|
3496
3540
|
|
|
@@ -3673,6 +3717,7 @@ function transpileVirtual(files, companionEmit, overrides = {}) {
|
|
|
3673
3717
|
{ plugin: editorScriptErasurePlugin },
|
|
3674
3718
|
{ plugin: messageGuardLoweringPlugin },
|
|
3675
3719
|
{ plugin: windowEventGuardLoweringPlugin },
|
|
3720
|
+
{ plugin: physicsEventGuardLoweringPlugin },
|
|
3676
3721
|
{ plugin: messageDispatchLoweringPlugin },
|
|
3677
3722
|
{ plugin: timersLoweringPlugin },
|
|
3678
3723
|
{ plugin: hsmLoweringPlugin },
|
|
@@ -3753,6 +3798,7 @@ function compilerOptions(companionEmit) {
|
|
|
3753
3798
|
{ plugin: editorScriptErasurePlugin },
|
|
3754
3799
|
{ plugin: messageGuardLoweringPlugin },
|
|
3755
3800
|
{ plugin: windowEventGuardLoweringPlugin },
|
|
3801
|
+
{ plugin: physicsEventGuardLoweringPlugin },
|
|
3756
3802
|
{ plugin: messageDispatchLoweringPlugin },
|
|
3757
3803
|
{ plugin: timersLoweringPlugin },
|
|
3758
3804
|
{ plugin: hsmLoweringPlugin },
|
|
@@ -3799,7 +3845,7 @@ function createTranspileSession() {
|
|
|
3799
3845
|
if (cached) {
|
|
3800
3846
|
return cached;
|
|
3801
3847
|
}
|
|
3802
|
-
const created =
|
|
3848
|
+
const created = ts15.createSourceFile(fileName, content, ts15.ScriptTarget.Latest, false);
|
|
3803
3849
|
sourceFileCache.set(normalized, created);
|
|
3804
3850
|
return created;
|
|
3805
3851
|
}
|
|
@@ -3818,7 +3864,7 @@ function createTranspileSession() {
|
|
|
3818
3864
|
return cached;
|
|
3819
3865
|
}
|
|
3820
3866
|
const fileContent = readFileSync2(filePath, "utf8");
|
|
3821
|
-
const created =
|
|
3867
|
+
const created = ts15.createSourceFile(filePath, fileContent, ts15.ScriptTarget.Latest, false);
|
|
3822
3868
|
libCache.set(fileName, created);
|
|
3823
3869
|
return created;
|
|
3824
3870
|
}
|
|
@@ -3831,10 +3877,10 @@ function createTranspileSession() {
|
|
|
3831
3877
|
return AMBIENT_FILES[normalized];
|
|
3832
3878
|
}
|
|
3833
3879
|
const host = {
|
|
3834
|
-
fileExists: (fileName) => mergedContent(normalizeSlashes(fileName)) !== undefined ||
|
|
3880
|
+
fileExists: (fileName) => mergedContent(normalizeSlashes(fileName)) !== undefined || ts15.sys.fileExists(fileName),
|
|
3835
3881
|
getCanonicalFileName: (fileName) => fileName,
|
|
3836
3882
|
getCurrentDirectory: () => "",
|
|
3837
|
-
getDefaultLibFileName:
|
|
3883
|
+
getDefaultLibFileName: ts15.getDefaultLibFileName,
|
|
3838
3884
|
readFile: () => "",
|
|
3839
3885
|
getNewLine: () => `
|
|
3840
3886
|
`,
|
|
@@ -3856,15 +3902,15 @@ function createTranspileSession() {
|
|
|
3856
3902
|
const userKeys = new Set(userFiles.keys());
|
|
3857
3903
|
const rootNames = [...Object.keys(AMBIENT_FILES).map(normalizeSlashes), ...userKeys];
|
|
3858
3904
|
const companionEmit = createCompanionEmitPlugin();
|
|
3859
|
-
program =
|
|
3860
|
-
const preEmitDiagnostics =
|
|
3905
|
+
program = ts15.createProgram(rootNames, compilerOptions(companionEmit), host, program);
|
|
3906
|
+
const preEmitDiagnostics = ts15.getPreEmitDiagnostics(program);
|
|
3861
3907
|
const collector = createOutputCollector();
|
|
3862
3908
|
const { diagnostics: transpileDiagnostics } = new tstl3.Transpiler().emit({
|
|
3863
3909
|
program,
|
|
3864
3910
|
writeFile: collector.writeFile
|
|
3865
3911
|
});
|
|
3866
3912
|
const diagnostics = [
|
|
3867
|
-
...
|
|
3913
|
+
...ts15.sortAndDeduplicateDiagnostics([...preEmitDiagnostics, ...transpileDiagnostics])
|
|
3868
3914
|
];
|
|
3869
3915
|
return collectOutputs(collector.files, diagnostics, userKeys, companionEmit.companions);
|
|
3870
3916
|
}
|
|
@@ -3874,7 +3920,7 @@ function createTranspileSession() {
|
|
|
3874
3920
|
};
|
|
3875
3921
|
}
|
|
3876
3922
|
// src/text-script-property.ts
|
|
3877
|
-
import * as
|
|
3923
|
+
import * as ts16 from "typescript";
|
|
3878
3924
|
function findTextScriptProperties(program, fileNames) {
|
|
3879
3925
|
const checker = program.getTypeChecker();
|
|
3880
3926
|
const findings = [];
|
|
@@ -3885,7 +3931,7 @@ function findTextScriptProperties(program, fileNames) {
|
|
|
3885
3931
|
}
|
|
3886
3932
|
const located = [];
|
|
3887
3933
|
const visit = (node) => {
|
|
3888
|
-
if (
|
|
3934
|
+
if (ts16.isCallExpression(node)) {
|
|
3889
3935
|
const hooks = factoryHooks(node, checker);
|
|
3890
3936
|
for (const property of hooks?.properties ?? []) {
|
|
3891
3937
|
if (!isPropertiesHook(property)) {
|
|
@@ -3898,7 +3944,7 @@ function findTextScriptProperties(program, fileNames) {
|
|
|
3898
3944
|
}
|
|
3899
3945
|
}
|
|
3900
3946
|
}
|
|
3901
|
-
|
|
3947
|
+
ts16.forEachChild(node, visit);
|
|
3902
3948
|
};
|
|
3903
3949
|
visit(sourceFile);
|
|
3904
3950
|
for (const call of findDirectGoPropertyCalls(sourceFile)) {
|
|
@@ -3907,7 +3953,7 @@ function findTextScriptProperties(program, fileNames) {
|
|
|
3907
3953
|
continue;
|
|
3908
3954
|
}
|
|
3909
3955
|
if (mayHoldString(checker.getTypeAtLocation(value), checker)) {
|
|
3910
|
-
const name =
|
|
3956
|
+
const name = ts16.isStringLiteralLike(nameArgument) ? nameArgument.text : nameArgument.getText(sourceFile);
|
|
3911
3957
|
located.push({ name, value });
|
|
3912
3958
|
}
|
|
3913
3959
|
}
|
|
@@ -3924,20 +3970,20 @@ function mayHoldString(type, checker, visited = new Set) {
|
|
|
3924
3970
|
return false;
|
|
3925
3971
|
}
|
|
3926
3972
|
visited.add(type);
|
|
3927
|
-
if (type.flags &
|
|
3973
|
+
if (type.flags & ts16.TypeFlags.StringLike) {
|
|
3928
3974
|
return true;
|
|
3929
3975
|
}
|
|
3930
3976
|
if (type.isUnionOrIntersection()) {
|
|
3931
3977
|
return type.types.some((member) => mayHoldString(member, checker, visited));
|
|
3932
3978
|
}
|
|
3933
|
-
if (type.flags &
|
|
3979
|
+
if (type.flags & ts16.TypeFlags.Instantiable) {
|
|
3934
3980
|
const constraint = checker.getBaseConstraintOfType(type);
|
|
3935
3981
|
return constraint !== undefined && constraint !== type ? mayHoldString(constraint, checker, visited) : false;
|
|
3936
3982
|
}
|
|
3937
3983
|
return false;
|
|
3938
3984
|
}
|
|
3939
3985
|
// src/url-address-slots.ts
|
|
3940
|
-
import * as
|
|
3986
|
+
import * as ts17 from "typescript";
|
|
3941
3987
|
var ADDRESS_CLASSES = {
|
|
3942
3988
|
"game-object": true,
|
|
3943
3989
|
component: true,
|
|
@@ -3956,7 +4002,7 @@ function isFragmentCaret(slot, position) {
|
|
|
3956
4002
|
return slot.fragmentStart !== -1 && position >= slot.fragmentStart;
|
|
3957
4003
|
}
|
|
3958
4004
|
function tableKey(checker, callee) {
|
|
3959
|
-
const symbol = checker.getSymbolAtLocation(
|
|
4005
|
+
const symbol = checker.getSymbolAtLocation(ts17.isIdentifier(callee) ? callee : callee.name);
|
|
3960
4006
|
if (!symbol)
|
|
3961
4007
|
return;
|
|
3962
4008
|
const fqn = checker.getFullyQualifiedName(symbol);
|
|
@@ -3964,43 +4010,43 @@ function tableKey(checker, callee) {
|
|
|
3964
4010
|
}
|
|
3965
4011
|
function parameterName(checker, call, index) {
|
|
3966
4012
|
const declaration = checker.getResolvedSignature(call)?.declaration;
|
|
3967
|
-
if (!declaration || !
|
|
4013
|
+
if (!declaration || !ts17.isFunctionLike(declaration))
|
|
3968
4014
|
return;
|
|
3969
4015
|
const parameter = declaration.parameters[index];
|
|
3970
|
-
if (!parameter || parameter.dotDotDotToken || !
|
|
4016
|
+
if (!parameter || parameter.dotDotDotToken || !ts17.isIdentifier(parameter.name))
|
|
3971
4017
|
return;
|
|
3972
4018
|
return parameter.name.text;
|
|
3973
4019
|
}
|
|
3974
4020
|
var EQUALITY_OPERATORS = new Set([
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
4021
|
+
ts17.SyntaxKind.EqualsEqualsEqualsToken,
|
|
4022
|
+
ts17.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
4023
|
+
ts17.SyntaxKind.EqualsEqualsToken,
|
|
4024
|
+
ts17.SyntaxKind.ExclamationEqualsToken
|
|
3979
4025
|
]);
|
|
3980
4026
|
function comparisonSatisfied(checker, entry, call) {
|
|
3981
4027
|
if (entry.comparedParameter === undefined)
|
|
3982
4028
|
return true;
|
|
3983
4029
|
let node = call;
|
|
3984
|
-
while (
|
|
4030
|
+
while (ts17.isParenthesizedExpression(node.parent))
|
|
3985
4031
|
node = node.parent;
|
|
3986
4032
|
const comparison = node.parent;
|
|
3987
|
-
if (!
|
|
4033
|
+
if (!ts17.isBinaryExpression(comparison))
|
|
3988
4034
|
return false;
|
|
3989
4035
|
if (!EQUALITY_OPERATORS.has(comparison.operatorToken.kind))
|
|
3990
4036
|
return false;
|
|
3991
4037
|
const other = comparison.left === node ? comparison.right : comparison.left;
|
|
3992
4038
|
const symbol = checker.getSymbolAtLocation(other);
|
|
3993
|
-
return (symbol?.declarations ?? []).some((declaration) =>
|
|
4039
|
+
return (symbol?.declarations ?? []).some((declaration) => ts17.isParameter(declaration) && ts17.isIdentifier(declaration.name) && declaration.name.text === entry.comparedParameter);
|
|
3994
4040
|
}
|
|
3995
4041
|
function classifiedEntryOfArgument(checker, table, argument) {
|
|
3996
4042
|
const call = argument.parent;
|
|
3997
|
-
if (!
|
|
4043
|
+
if (!ts17.isCallExpression(call))
|
|
3998
4044
|
return;
|
|
3999
4045
|
const argumentIndex = call.arguments.indexOf(argument);
|
|
4000
4046
|
if (argumentIndex === -1)
|
|
4001
4047
|
return;
|
|
4002
4048
|
const callee = call.expression;
|
|
4003
|
-
if (!
|
|
4049
|
+
if (!ts17.isPropertyAccessExpression(callee) && !ts17.isIdentifier(callee))
|
|
4004
4050
|
return;
|
|
4005
4051
|
const fqn = tableKey(checker, callee);
|
|
4006
4052
|
if (fqn === undefined)
|
|
@@ -4017,10 +4063,10 @@ function classifiedEntryOfArgument(checker, table, argument) {
|
|
|
4017
4063
|
}
|
|
4018
4064
|
function slotThroughHashedCall(checker, table, literal) {
|
|
4019
4065
|
const call = literal.parent;
|
|
4020
|
-
if (!
|
|
4066
|
+
if (!ts17.isCallExpression(call))
|
|
4021
4067
|
return;
|
|
4022
4068
|
const callee = call.expression;
|
|
4023
|
-
if (!
|
|
4069
|
+
if (!ts17.isPropertyAccessExpression(callee) && !ts17.isIdentifier(callee))
|
|
4024
4070
|
return;
|
|
4025
4071
|
if (tableKey(checker, callee) !== "hash")
|
|
4026
4072
|
return;
|
|
@@ -4038,24 +4084,24 @@ function addressTextOf(checker, entry, call) {
|
|
|
4038
4084
|
if (entry.addressParameter === undefined)
|
|
4039
4085
|
return;
|
|
4040
4086
|
const declaration = checker.getResolvedSignature(call)?.declaration;
|
|
4041
|
-
if (!declaration || !
|
|
4087
|
+
if (!declaration || !ts17.isFunctionLike(declaration))
|
|
4042
4088
|
return;
|
|
4043
|
-
const index = declaration.parameters.findIndex((parameter) => !parameter.dotDotDotToken &&
|
|
4089
|
+
const index = declaration.parameters.findIndex((parameter) => !parameter.dotDotDotToken && ts17.isIdentifier(parameter.name) && parameter.name.text === entry.addressParameter);
|
|
4044
4090
|
if (index === -1)
|
|
4045
4091
|
return;
|
|
4046
4092
|
const argument = call.arguments[index];
|
|
4047
|
-
return argument &&
|
|
4093
|
+
return argument && ts17.isStringLiteralLike(argument) ? argument.text : undefined;
|
|
4048
4094
|
}
|
|
4049
4095
|
function innermostLiteralAt(sourceFile, position) {
|
|
4050
4096
|
let found;
|
|
4051
4097
|
const visit = (node) => {
|
|
4052
4098
|
if (position < node.getStart(sourceFile) || position > node.getEnd())
|
|
4053
4099
|
return;
|
|
4054
|
-
if (
|
|
4100
|
+
if (ts17.isStringLiteralLike(node))
|
|
4055
4101
|
found = node;
|
|
4056
|
-
|
|
4102
|
+
ts17.forEachChild(node, visit);
|
|
4057
4103
|
};
|
|
4058
|
-
|
|
4104
|
+
ts17.forEachChild(sourceFile, visit);
|
|
4059
4105
|
return found;
|
|
4060
4106
|
}
|
|
4061
4107
|
function resolveClassifiedSlotAtPosition(input) {
|
|
@@ -4089,23 +4135,23 @@ function resolveClassifiedSlotAtPosition(input) {
|
|
|
4089
4135
|
};
|
|
4090
4136
|
}
|
|
4091
4137
|
// src/url-cross-world-address.ts
|
|
4092
|
-
import * as
|
|
4138
|
+
import * as ts19 from "typescript";
|
|
4093
4139
|
|
|
4094
4140
|
// src/url-address-literals.ts
|
|
4095
|
-
import * as
|
|
4141
|
+
import * as ts18 from "typescript";
|
|
4096
4142
|
function isAmbient2(fileName) {
|
|
4097
4143
|
return /[\\/]node_modules[\\/]/.test(fileName) || /(^|[\\/])lib\.[^\\/]*\.d\.ts$/.test(fileName);
|
|
4098
4144
|
}
|
|
4099
4145
|
function referenceTargetSymbol(type) {
|
|
4100
|
-
if ((type.flags &
|
|
4146
|
+
if ((type.flags & ts18.TypeFlags.Object) === 0)
|
|
4101
4147
|
return;
|
|
4102
|
-
if ((type.objectFlags &
|
|
4148
|
+
if ((type.objectFlags & ts18.ObjectFlags.Reference) === 0)
|
|
4103
4149
|
return;
|
|
4104
4150
|
return type.target.symbol;
|
|
4105
4151
|
}
|
|
4106
4152
|
function ambientHashAliasTargets(checker, anchor) {
|
|
4107
4153
|
const targets = new Set;
|
|
4108
|
-
for (const symbol of checker.getSymbolsInScope(anchor,
|
|
4154
|
+
for (const symbol of checker.getSymbolsInScope(anchor, ts18.SymbolFlags.Type)) {
|
|
4109
4155
|
if (symbol.name !== "Hash")
|
|
4110
4156
|
continue;
|
|
4111
4157
|
const target = referenceTargetSymbol(checker.getDeclaredTypeOfSymbol(symbol));
|
|
@@ -4121,7 +4167,7 @@ function canonicalHashSymbols(checker, program) {
|
|
|
4121
4167
|
if (!anchor)
|
|
4122
4168
|
return canonical;
|
|
4123
4169
|
const aliased = ambientHashAliasTargets(checker, anchor);
|
|
4124
|
-
for (const symbol of checker.getSymbolsInScope(anchor,
|
|
4170
|
+
for (const symbol of checker.getSymbolsInScope(anchor, ts18.SymbolFlags.Function)) {
|
|
4125
4171
|
if (symbol.name !== "hash")
|
|
4126
4172
|
continue;
|
|
4127
4173
|
const declaration = symbol.declarations?.[0];
|
|
@@ -4143,7 +4189,7 @@ function hashedSourceOfType(checker, type, canonical) {
|
|
|
4143
4189
|
return source?.isStringLiteral() ? source.value : undefined;
|
|
4144
4190
|
}
|
|
4145
4191
|
function staticAddressTextOf(checker, node, canonical) {
|
|
4146
|
-
return
|
|
4192
|
+
return ts18.isStringLiteralLike(node) ? node.text : hashedSourceOfType(checker, checker.getTypeAtLocation(node), canonical);
|
|
4147
4193
|
}
|
|
4148
4194
|
|
|
4149
4195
|
// src/url-cross-world-address.ts
|
|
@@ -4167,7 +4213,7 @@ function checkCrossWorldAddresses(input) {
|
|
|
4167
4213
|
if (worlds.length === 0)
|
|
4168
4214
|
continue;
|
|
4169
4215
|
const visit = (node) => {
|
|
4170
|
-
if (
|
|
4216
|
+
if (ts19.isExpression(node)) {
|
|
4171
4217
|
const entry = addressEntryOfArgument(checker, table, node);
|
|
4172
4218
|
if (entry !== undefined && slotRejectsForeignSocket(entry)) {
|
|
4173
4219
|
const address = staticAddressTextOf(checker, node, canonical);
|
|
@@ -4184,14 +4230,14 @@ function checkCrossWorldAddresses(input) {
|
|
|
4184
4230
|
}
|
|
4185
4231
|
}
|
|
4186
4232
|
}
|
|
4187
|
-
|
|
4233
|
+
ts19.forEachChild(node, visit);
|
|
4188
4234
|
};
|
|
4189
|
-
|
|
4235
|
+
ts19.forEachChild(sourceFile, visit);
|
|
4190
4236
|
}
|
|
4191
4237
|
return findings;
|
|
4192
4238
|
}
|
|
4193
4239
|
// src/url-fragment-reachability.ts
|
|
4194
|
-
import * as
|
|
4240
|
+
import * as ts20 from "typescript";
|
|
4195
4241
|
function pathOfKey(key) {
|
|
4196
4242
|
const socket = socketOfAddress(key);
|
|
4197
4243
|
return socket === undefined ? key : key.slice(socket.length + 1);
|
|
@@ -4236,7 +4282,7 @@ function checkUrlFragmentReachability(input) {
|
|
|
4236
4282
|
if (isAmbient2(sourceFile.fileName))
|
|
4237
4283
|
continue;
|
|
4238
4284
|
const visit = (node) => {
|
|
4239
|
-
if (
|
|
4285
|
+
if (ts20.isExpression(node) && isAddressClass(addressClassOfArgument(checker, table, node))) {
|
|
4240
4286
|
const text = staticAddressTextOf(checker, node, canonical);
|
|
4241
4287
|
if (text !== undefined) {
|
|
4242
4288
|
const hash = text.indexOf("#");
|
|
@@ -4255,9 +4301,9 @@ function checkUrlFragmentReachability(input) {
|
|
|
4255
4301
|
}
|
|
4256
4302
|
}
|
|
4257
4303
|
}
|
|
4258
|
-
|
|
4304
|
+
ts20.forEachChild(node, visit);
|
|
4259
4305
|
};
|
|
4260
|
-
|
|
4306
|
+
ts20.forEachChild(sourceFile, visit);
|
|
4261
4307
|
}
|
|
4262
4308
|
return { kind: "checked", findings };
|
|
4263
4309
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defold-typescript/transpiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.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.51.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"
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
createBinaryExpression,
|
|
4
|
+
createCallExpression,
|
|
5
|
+
createIdentifier,
|
|
6
|
+
createStringLiteral,
|
|
7
|
+
createTableIndexExpression,
|
|
8
|
+
type Expression,
|
|
9
|
+
type Plugin,
|
|
10
|
+
SyntaxKind,
|
|
11
|
+
} from "typescript-to-lua";
|
|
12
|
+
|
|
13
|
+
const GUARD_MODULE = "@defold-typescript/types";
|
|
14
|
+
const GUARD_NAME = "isPhysicsEvent";
|
|
15
|
+
|
|
16
|
+
function resolvesToGuardExport(callee: ts.Expression, checker: ts.TypeChecker): boolean {
|
|
17
|
+
let symbol = checker.getSymbolAtLocation(callee);
|
|
18
|
+
if (symbol === undefined) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
22
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
23
|
+
}
|
|
24
|
+
if (symbol.getName() !== GUARD_NAME) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
28
|
+
if (declaration === undefined) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return declaration.getSourceFile().fileName.includes(GUARD_MODULE);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const physicsEventGuardLoweringPlugin: Plugin = {
|
|
35
|
+
visitors: {
|
|
36
|
+
[ts.SyntaxKind.CallExpression]: (node, context): Expression => {
|
|
37
|
+
const [eventArg, nameArg] = node.arguments;
|
|
38
|
+
if (
|
|
39
|
+
node.arguments.length === 2 &&
|
|
40
|
+
eventArg !== undefined &&
|
|
41
|
+
nameArg !== undefined &&
|
|
42
|
+
resolvesToGuardExport(node.expression, context.checker)
|
|
43
|
+
) {
|
|
44
|
+
const event = context.transformExpression(eventArg);
|
|
45
|
+
const name = context.transformExpression(nameArg);
|
|
46
|
+
return createBinaryExpression(
|
|
47
|
+
createTableIndexExpression(event, createStringLiteral("type")),
|
|
48
|
+
createCallExpression(createIdentifier("hash"), [name]),
|
|
49
|
+
SyntaxKind.EqualityOperator,
|
|
50
|
+
node,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return context.superTransformExpression(node);
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
@@ -3,6 +3,7 @@ import * as tstl from "typescript-to-lua";
|
|
|
3
3
|
import { lifecycleErasurePlugin } from "./lifecycle-erasure";
|
|
4
4
|
import { messageDispatchLoweringPlugin } from "./message-dispatch-lowering";
|
|
5
5
|
import { messageGuardLoweringPlugin } from "./message-guard-lowering";
|
|
6
|
+
import { physicsEventGuardLoweringPlugin } from "./physics-event-guard-lowering";
|
|
6
7
|
import { timersLoweringPlugin } from "./timers-lowering";
|
|
7
8
|
import { typeApplicationErasurePlugin } from "./type-application-erasure";
|
|
8
9
|
import { windowEventGuardLoweringPlugin } from "./window-event-guard-lowering";
|
|
@@ -13,6 +14,7 @@ const LUA_PLUGINS: tstl.Plugin[] = [
|
|
|
13
14
|
lifecycleErasurePlugin,
|
|
14
15
|
messageGuardLoweringPlugin,
|
|
15
16
|
windowEventGuardLoweringPlugin,
|
|
17
|
+
physicsEventGuardLoweringPlugin,
|
|
16
18
|
messageDispatchLoweringPlugin,
|
|
17
19
|
timersLoweringPlugin,
|
|
18
20
|
typeApplicationErasurePlugin,
|
package/src/session.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { hsmLoweringPlugin } from "./hsm-builtin";
|
|
|
9
9
|
import { lifecycleErasurePlugin } from "./lifecycle-erasure";
|
|
10
10
|
import { messageDispatchLoweringPlugin } from "./message-dispatch-lowering";
|
|
11
11
|
import { messageGuardLoweringPlugin } from "./message-guard-lowering";
|
|
12
|
+
import { physicsEventGuardLoweringPlugin } from "./physics-event-guard-lowering";
|
|
12
13
|
import { timersLoweringPlugin } from "./timers-lowering";
|
|
13
14
|
import { AMBIENT_FILES, collectOutputs, type TranspileProjectResult } from "./transpile";
|
|
14
15
|
import { typeApplicationErasurePlugin } from "./type-application-erasure";
|
|
@@ -50,6 +51,7 @@ function compilerOptions(companionEmit: CompanionEmitter): tstl.CompilerOptions
|
|
|
50
51
|
{ plugin: editorScriptErasurePlugin },
|
|
51
52
|
{ plugin: messageGuardLoweringPlugin },
|
|
52
53
|
{ plugin: windowEventGuardLoweringPlugin },
|
|
54
|
+
{ plugin: physicsEventGuardLoweringPlugin },
|
|
53
55
|
{ plugin: messageDispatchLoweringPlugin },
|
|
54
56
|
{ plugin: timersLoweringPlugin },
|
|
55
57
|
{ plugin: hsmLoweringPlugin },
|
package/src/transpile.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { lifecycleErasurePlugin } from "./lifecycle-erasure";
|
|
|
21
21
|
import { scanEmittedRequires } from "./lua-require-scan";
|
|
22
22
|
import { messageDispatchLoweringPlugin } from "./message-dispatch-lowering";
|
|
23
23
|
import { messageGuardLoweringPlugin } from "./message-guard-lowering";
|
|
24
|
+
import { physicsEventGuardLoweringPlugin } from "./physics-event-guard-lowering";
|
|
24
25
|
import { importsTimersModule, timersLoweringPlugin } from "./timers-lowering";
|
|
25
26
|
import { TIMERS_RUNTIME } from "./timers-runtime";
|
|
26
27
|
import { typeApplicationErasurePlugin } from "./type-application-erasure";
|
|
@@ -356,6 +357,7 @@ function transpileVirtual(
|
|
|
356
357
|
{ plugin: editorScriptErasurePlugin },
|
|
357
358
|
{ plugin: messageGuardLoweringPlugin },
|
|
358
359
|
{ plugin: windowEventGuardLoweringPlugin },
|
|
360
|
+
{ plugin: physicsEventGuardLoweringPlugin },
|
|
359
361
|
{ plugin: messageDispatchLoweringPlugin },
|
|
360
362
|
{ plugin: timersLoweringPlugin },
|
|
361
363
|
{ plugin: hsmLoweringPlugin },
|