Skip to content

Commit 920f3e5

Browse files
authored
Upgrade to TypeScript 5.6 (#1581)
* Upgrade to TypeScript 5.6 * Fix ts version * ignore prettier for test file
1 parent da0774c commit 920f3e5

File tree

11 files changed

+112
-69
lines changed

11 files changed

+112
-69
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/dist
22
/coverage
33
/test/translation/transformation/characterEscapeSequence.ts
4+
/test/translation/transformation/exportStatement.ts
45
/benchmark/dist
56
/test/transpile/module-resolution/**/node_modules
67
/test/transpile/module-resolution/**/dist

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"node": ">=16.10.0"
4343
},
4444
"peerDependencies": {
45-
"typescript": "5.5.2"
45+
"typescript": "5.6.2"
4646
},
4747
"dependencies": {
4848
"@typescript-to-lua/language-extensions": "1.19.0",
@@ -67,10 +67,10 @@
6767
"jest-circus": "^29.5.0",
6868
"lua-types": "^2.13.0",
6969
"lua-wasm-bindings": "^0.3.1",
70-
"prettier": "^2.8.4",
70+
"prettier": "^2.8.8",
7171
"ts-jest": "^29.1.0",
7272
"ts-node": "^10.9.1",
73-
"typescript": "^5.5.2",
73+
"typescript": "^5.6.2",
7474
"typescript-eslint": "^7.13.1"
7575
}
7676
}

src/transformation/visitors/modules/export.ts

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@ import * as ts from "typescript";
22
import * as lua from "../../../LuaAST";
33
import { assert } from "../../../utils";
44
import { FunctionVisitor, TransformationContext } from "../../context";
5-
import {
6-
createDefaultExportExpression,
7-
createDefaultExportStringLiteral,
8-
createExportedIdentifier,
9-
} from "../../utils/export";
5+
import { createDefaultExportExpression, createDefaultExportStringLiteral } from "../../utils/export";
106
import { createExportsIdentifier } from "../../utils/lua-ast";
11-
import { ScopeType } from "../../utils/scope";
12-
import { transformScopeBlock } from "../block";
13-
import { transformIdentifier } from "../identifier";
14-
import { createShorthandIdentifier } from "../literal";
7+
import { createShorthandIdentifier, transformPropertyName } from "../literal";
158
import { createModuleRequire } from "./import";
9+
import { createSafeName } from "../../utils/safe-names";
10+
import * as path from "path";
1611

1712
export const transformExportAssignment: FunctionVisitor<ts.ExportAssignment> = (node, context) => {
1813
if (!context.resolver.isValueAliasDeclaration(node)) {
@@ -101,20 +96,37 @@ function transformExportAll(context: TransformationContext, node: ts.ExportDecla
10196
}
10297

10398
const isDefaultExportSpecifier = (node: ts.ExportSpecifier) =>
104-
(node.name && ts.identifierToKeywordKind(node.name) === ts.SyntaxKind.DefaultKeyword) ||
105-
(node.propertyName && ts.identifierToKeywordKind(node.propertyName) === ts.SyntaxKind.DefaultKeyword);
99+
(node.name &&
100+
ts.isIdentifier(node.name) &&
101+
ts.identifierToKeywordKind(node.name) === ts.SyntaxKind.DefaultKeyword) ||
102+
(node.propertyName &&
103+
ts.isIdentifier(node.propertyName) &&
104+
ts.identifierToKeywordKind(node.propertyName) === ts.SyntaxKind.DefaultKeyword);
106105

107106
function transformExportSpecifier(context: TransformationContext, node: ts.ExportSpecifier): lua.AssignmentStatement {
108-
const exportedSymbol = context.checker.getExportSpecifierLocalTargetSymbol(node);
109-
const exportedIdentifier = node.propertyName ? node.propertyName : node.name;
110-
const exportedExpression = createShorthandIdentifier(context, exportedSymbol, exportedIdentifier);
107+
const exportedName = node.name;
108+
const exportedValue = node.propertyName ?? node.name;
109+
let rhs: lua.Expression;
110+
if (ts.isIdentifier(exportedValue)) {
111+
const exportedSymbol = context.checker.getExportSpecifierLocalTargetSymbol(node);
112+
rhs = createShorthandIdentifier(context, exportedSymbol, exportedValue);
113+
} else {
114+
rhs = lua.createStringLiteral(exportedName.text, exportedValue);
115+
}
111116

112-
const isDefault = isDefaultExportSpecifier(node);
113-
const exportAssignmentLeftHandSide = isDefault
114-
? createDefaultExportExpression(node)
115-
: createExportedIdentifier(context, transformIdentifier(context, node.name));
117+
if (isDefaultExportSpecifier(node)) {
118+
const lhs = createDefaultExportExpression(node);
119+
return lua.createAssignmentStatement(lhs, rhs, node);
120+
} else {
121+
const exportsTable = createExportsIdentifier();
122+
const lhs = lua.createTableIndexExpression(
123+
exportsTable,
124+
lua.createStringLiteral(exportedName.text),
125+
exportedName
126+
);
116127

117-
return lua.createAssignmentStatement(exportAssignmentLeftHandSide, exportedExpression, node);
128+
return lua.createAssignmentStatement(lhs, rhs, node);
129+
}
118130
}
119131

120132
function transformExportSpecifiersFrom(
@@ -123,32 +135,32 @@ function transformExportSpecifiersFrom(
123135
moduleSpecifier: ts.Expression,
124136
exportSpecifiers: ts.ExportSpecifier[]
125137
): lua.Statement {
126-
// First transpile as import clause
127-
const importClause = ts.factory.createImportClause(
128-
false,
129-
undefined,
130-
ts.factory.createNamedImports(
131-
exportSpecifiers.map(s => ts.factory.createImportSpecifier(statement.isTypeOnly, s.propertyName, s.name))
132-
)
133-
);
138+
const result: lua.Statement[] = [];
134139

135-
const importDeclaration = ts.factory.createImportDeclaration(statement.modifiers, importClause, moduleSpecifier);
140+
const importPath = ts.isStringLiteral(moduleSpecifier) ? moduleSpecifier.text.replace(/"/g, "") : "module";
136141

137-
// Wrap in block to prevent imports from hoisting out of `do` statement
138-
const [block] = transformScopeBlock(context, ts.factory.createBlock([importDeclaration]), ScopeType.Block);
139-
const result = block.statements;
142+
// Create the require statement to extract values.
143+
// local ____module = require("module")
144+
const importUniqueName = lua.createIdentifier(createSafeName(path.basename(importPath)));
145+
const requireCall = createModuleRequire(context, moduleSpecifier);
146+
result.push(lua.createVariableDeclarationStatement(importUniqueName, requireCall, statement));
140147

141-
// Now the module is imported, add the imports to the export table
142148
for (const specifier of exportSpecifiers) {
143-
result.push(
144-
lua.createAssignmentStatement(
145-
createExportedIdentifier(context, transformIdentifier(context, specifier.name)),
146-
transformIdentifier(context, specifier.name)
147-
)
149+
// Assign to exports table
150+
const exportsTable = createExportsIdentifier();
151+
const exportedName = specifier.name;
152+
const exportedNameTransformed = transformPropertyName(context, exportedName);
153+
const lhs = lua.createTableIndexExpression(exportsTable, exportedNameTransformed, exportedName);
154+
155+
const exportedValue = specifier.propertyName ?? specifier.name;
156+
const rhs = lua.createTableIndexExpression(
157+
lua.cloneIdentifier(importUniqueName),
158+
transformPropertyName(context, exportedValue),
159+
specifier
148160
);
161+
result.push(lua.createAssignmentStatement(lhs, rhs, specifier));
149162
}
150163

151-
// Wrap this in a DoStatement to prevent polluting the scope.
152164
return lua.createDoStatement(result, statement);
153165
}
154166

test/translation/__snapshots__/transformation.spec.ts.snap

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ do
4141
end
4242
do
4343
local ____xyz = require("xyz")
44-
local abc = ____xyz.abc
45-
local def = ____xyz.def
46-
____exports.abc = abc
47-
____exports.def = def
44+
____exports.abc = ____xyz.abc
45+
____exports.def = ____xyz.def
4846
end
4947
do
5048
local ____xyz = require("xyz")
51-
local def = ____xyz.abc
52-
____exports.def = def
49+
____exports.def = ____xyz.abc
50+
end
51+
do
52+
local ____bla = require("bla")
53+
____exports.bar = ____bla["123"]
54+
end
55+
do
56+
local ____bla = require("bla")
57+
____exports["123"] = ____bla.foo
5358
end
5459
return ____exports"
5560
`;

test/translation/transformation/exportStatement.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export { xyz as uwv };
44
export * from "xyz";
55
export { abc, def } from "xyz";
66
export { abc as def } from "xyz";
7+
export { "123" as bar } from "bla";
8+
export { foo as "123" } from "bla";

test/unit/conditionals.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ test.each([
9090
{ condition: 3, lhs: 4, rhs: 5 },
9191
])("Ternary Conditional (%p)", ({ condition, lhs, rhs }) => {
9292
util.testExpressionTemplate`${condition} ? ${lhs} : ${rhs}`
93-
.ignoreDiagnostics([truthyOnlyConditionalValue.code])
93+
.ignoreDiagnostics([
94+
truthyOnlyConditionalValue.code,
95+
2872 /* TS2872: This kind of expression is always truthy. */,
96+
])
9497
.expectToMatchJsResult();
9598
});
9699

test/unit/language-extensions/table.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ describe("Table extensions use as expression", () => {
338338
`
339339
.withLanguageExtensions()
340340
.setReturnExport("result")
341+
.ignoreDiagnostics([2872 /* TS2872: This kind of expression is always truthy. */])
341342
.expectToEqual(value);
342343
});
343344

test/unit/nullishCoalescing.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import * as util from "../util";
22

33
test.each(["null", "undefined"])("nullish-coalesing operator returns rhs", nullishValue => {
4-
util.testExpression`${nullishValue} ?? "Hello, World!"`.expectToMatchJsResult();
4+
util.testExpression`${nullishValue} ?? "Hello, World!"`
5+
.ignoreDiagnostics([2871 /* TS2871: This expression is always nullish. */])
6+
.expectToMatchJsResult();
57
});
68

79
test.each([3, "foo", {}, [], true, false])("nullish-coalesing operator returns lhs", value => {
8-
util.testExpression`${util.formatCode(value)} ?? "Hello, World!"`.expectToMatchJsResult();
10+
util.testExpression`${util.formatCode(value)} ?? "Hello, World!"`
11+
.ignoreDiagnostics([
12+
2869 /* TS2869: Right operand of ?? is unreachable because the left operand is never nullish. */,
13+
])
14+
.expectToMatchJsResult();
915
});
1016

1117
test.each(["any", "unknown"])("nullish-coalesing operator with any/unknown type", type => {
1218
util.testFunction`
1319
const unknownType = false as ${type};
1420
return unknownType ?? "This should not be returned!";
15-
`.expectToMatchJsResult();
21+
`
22+
.ignoreDiagnostics([2871 /* TS2871: This expression is always nullish. */])
23+
.expectToMatchJsResult();
1624
});
1725

1826
test.each(["boolean | string", "number | false", "undefined | true"])(
@@ -38,7 +46,9 @@ test("nullish-coalescing operator with side effect rhs", () => {
3846
let i = 0;
3947
const incI = () => ++i;
4048
return [i, undefined ?? incI(), i];
41-
`.expectToMatchJsResult();
49+
`
50+
.ignoreDiagnostics([2871 /* TS2871: This expression is always nullish. */])
51+
.expectToMatchJsResult();
4252
});
4353

4454
test("nullish-coalescing operator with vararg", () => {

test/unit/precedingStatements.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,11 @@ test("class member initializers", () => {
633633
}
634634
const inst = new MyClass();
635635
return [inst.myField, inst.foo];
636-
`.expectToMatchJsResult();
636+
`
637+
.ignoreDiagnostics([
638+
2869 /* TS2869: Right operand of ?? is unreachable because the left operand is never nullish. */,
639+
])
640+
.expectToMatchJsResult();
637641
});
638642

639643
test("class member initializers in extended class", () => {
@@ -644,5 +648,9 @@ test("class member initializers in extended class", () => {
644648
}
645649
const inst = new MyClass();
646650
return inst.myField;
647-
`.expectToMatchJsResult();
651+
`
652+
.ignoreDiagnostics([
653+
2869 /* TS2869: Right operand of ?? is unreachable because the left operand is never nullish. */,
654+
])
655+
.expectToMatchJsResult();
648656
});

0 commit comments

Comments
 (0)