Skip to content

Commit 2982567

Browse files
authored
Fix nested namespace merging (#1435)
* Upgrade ts-jest * Fix nested namespaces leading to invalid Lua
1 parent d13f800 commit 2982567

29 files changed

+4767
-7652
lines changed

jest.config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ module.exports = {
1515
testEnvironment: "node",
1616
testRunner: "jest-circus/runner",
1717
preset: "ts-jest",
18-
globals: {
19-
"ts-jest": {
20-
tsconfig: "<rootDir>/test/tsconfig.json",
21-
diagnostics: { warnOnly: !isCI },
22-
},
18+
transform: {
19+
"^.+\\.ts?$": [
20+
"ts-jest",
21+
{
22+
tsconfig: "<rootDir>/test/tsconfig.json",
23+
diagnostics: { warnOnly: !isCI },
24+
},
25+
],
2326
},
2427
};

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
"eslint-plugin-jest": "^26.9.0",
6464
"fs-extra": "^8.1.0",
6565
"javascript-stringify": "^2.0.1",
66-
"jest": "^28.1.3",
66+
"jest": "^29.5.0",
6767
"jest-circus": "^29.5.0",
6868
"lua-types": "^2.13.0",
6969
"lua-wasm-bindings": "^0.3.1",
7070
"prettier": "^2.8.4",
71-
"ts-jest": "^28.0.8",
71+
"ts-jest": "^29.1.0",
7272
"ts-node": "^10.9.1",
7373
"typescript": "^5.0.2"
7474
}

src/transformation/utils/export.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
4-
import { createModuleLocalNameIdentifier } from "../visitors/namespace";
4+
import { createModuleLocalName } from "../visitors/namespace";
55
import { createExportsIdentifier } from "./lua-ast";
66
import { getSymbolInfo } from "./symbols";
77
import { findFirstNodeAbove } from "./typescript";
@@ -144,7 +144,7 @@ export function createExportedIdentifier(
144144

145145
const exportTable =
146146
exportScope && ts.isModuleDeclaration(exportScope)
147-
? createModuleLocalNameIdentifier(context, exportScope)
147+
? createModuleLocalName(context, exportScope)
148148
: createExportsIdentifier();
149149

150150
return lua.createTableIndexExpression(exportTable, lua.createStringLiteral(identifier.text));

src/transformation/visitors/namespace.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import { performHoisting, ScopeType } from "../utils/scope";
1111
import { getSymbolIdOfSymbol } from "../utils/symbols";
1212
import { transformIdentifier } from "./identifier";
1313

14+
export function createModuleLocalName(context: TransformationContext, module: ts.ModuleDeclaration): lua.Expression {
15+
if (!ts.isSourceFile(module.parent) && ts.isModuleDeclaration(module.parent)) {
16+
const parentDeclaration = createModuleLocalName(context, module.parent);
17+
const name = createModuleLocalNameIdentifier(context, module);
18+
return lua.createTableIndexExpression(parentDeclaration, lua.createStringLiteral(name.text), module.name);
19+
}
20+
21+
return createModuleLocalNameIdentifier(context, module);
22+
}
23+
1424
export function createModuleLocalNameIdentifier(
1525
context: TransformationContext,
1626
declaration: ts.ModuleDeclaration
@@ -25,7 +35,6 @@ export function createModuleLocalNameIdentifier(
2535
);
2636
}
2737

28-
// TODO: Should synthetic name nodes be escaped as well?
2938
return transformIdentifier(context, declaration.name as ts.Identifier);
3039
}
3140

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

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ end"
1010
`;
1111

1212
exports[`Transformation (characterEscapeSequence) 1`] = `
13-
"quoteInDoubleQuotes = \\"' ' '\\"
14-
quoteInTemplateString = \\"' ' '\\"
15-
doubleQuoteInQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
16-
doubleQuoteInDoubleQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
17-
doubleQuoteInTemplateString = \\"\\\\\\" \\\\\\" \\\\\\"\\"
18-
backQuoteInQuotes = \\"\` \` \`\\"
19-
backQuoteInDoubleQuotes = \\"\` \` \`\\"
20-
backQuoteInTemplateString = \\"\` \` \`\\"
21-
escapedCharsInQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
22-
escapedCharsInDoubleQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
23-
escapedCharsInTemplateString = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
24-
nonEmptyTemplateString = (\\"Level 0: \\\\n\\\\t \\" .. (\\"Level 1: \\\\n\\\\t\\\\t \\" .. (\\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. \\"Last level \\\\n --\\") .. \\" \\\\n --\\") .. \\" \\\\n --\\") .. \\" \\\\n --\\""
13+
"quoteInDoubleQuotes = "' ' '"
14+
quoteInTemplateString = "' ' '"
15+
doubleQuoteInQuotes = "\\" \\" \\""
16+
doubleQuoteInDoubleQuotes = "\\" \\" \\""
17+
doubleQuoteInTemplateString = "\\" \\" \\""
18+
backQuoteInQuotes = "\` \` \`"
19+
backQuoteInDoubleQuotes = "\` \` \`"
20+
backQuoteInTemplateString = "\` \` \`"
21+
escapedCharsInQuotes = "\\\\ \\0 \\b \\t \\n \\v \\f \\" ' \`"
22+
escapedCharsInDoubleQuotes = "\\\\ \\0 \\b \\t \\n \\v \\f \\" ' \`"
23+
escapedCharsInTemplateString = "\\\\ \\0 \\b \\t \\n \\v \\f \\" ' \`"
24+
nonEmptyTemplateString = ("Level 0: \\n\\t " .. ("Level 1: \\n\\t\\t " .. ("Level 3: \\n\\t\\t\\t " .. "Last level \\n --") .. " \\n --") .. " \\n --") .. " \\n --""
2525
`;
2626

2727
exports[`Transformation (exportStatement) 1`] = `
@@ -30,22 +30,22 @@ local xyz = 4
3030
____exports.xyz = xyz
3131
____exports.uwv = xyz
3232
do
33-
local ____export = require(\\"xyz\\")
33+
local ____export = require("xyz")
3434
for ____exportKey, ____exportValue in pairs(____export) do
35-
if ____exportKey ~= \\"default\\" then
35+
if ____exportKey ~= "default" then
3636
____exports[____exportKey] = ____exportValue
3737
end
3838
end
3939
end
4040
do
41-
local ____xyz = require(\\"xyz\\")
41+
local ____xyz = require("xyz")
4242
local abc = ____xyz.abc
4343
local def = ____xyz.def
4444
____exports.abc = abc
4545
____exports.def = def
4646
end
4747
do
48-
local ____xyz = require(\\"xyz\\")
48+
local ____xyz = require("xyz")
4949
local def = ____xyz.abc
5050
____exports.def = def
5151
end
@@ -58,10 +58,10 @@ return ____exports"
5858
`;
5959

6060
exports[`Transformation (methodRestArguments) 1`] = `
61-
"local ____lualib = require(\\"lualib_bundle\\")
61+
"local ____lualib = require("lualib_bundle")
6262
local __TS__Class = ____lualib.__TS__Class
6363
MyClass = __TS__Class()
64-
MyClass.name = \\"MyClass\\"
64+
MyClass.name = "MyClass"
6565
function MyClass.prototype.____constructor(self)
6666
end
6767
function MyClass.prototype.varargsFunction(self, a, ...)
@@ -75,24 +75,24 @@ return ____exports"
7575
`;
7676

7777
exports[`Transformation (modulesClassExport) 1`] = `
78-
"local ____lualib = require(\\"lualib_bundle\\")
78+
"local ____lualib = require("lualib_bundle")
7979
local __TS__Class = ____lualib.__TS__Class
8080
local ____exports = {}
8181
____exports.TestClass = __TS__Class()
8282
local TestClass = ____exports.TestClass
83-
TestClass.name = \\"TestClass\\"
83+
TestClass.name = "TestClass"
8484
function TestClass.prototype.____constructor(self)
8585
end
8686
return ____exports"
8787
`;
8888

8989
exports[`Transformation (modulesClassWithMemberExport) 1`] = `
90-
"local ____lualib = require(\\"lualib_bundle\\")
90+
"local ____lualib = require("lualib_bundle")
9191
local __TS__Class = ____lualib.__TS__Class
9292
local ____exports = {}
9393
____exports.TestClass = __TS__Class()
9494
local TestClass = ____exports.TestClass
95-
TestClass.name = \\"TestClass\\"
95+
TestClass.name = "TestClass"
9696
function TestClass.prototype.____constructor(self)
9797
end
9898
function TestClass.prototype.memberFunc(self)
@@ -114,30 +114,30 @@ end"
114114

115115
exports[`Transformation (modulesImportAll) 1`] = `
116116
"local ____exports = {}
117-
local Test = require(\\"test\\")
117+
local Test = require("test")
118118
local ____ = Test
119119
return ____exports"
120120
`;
121121

122122
exports[`Transformation (modulesImportNamed) 1`] = `
123123
"local ____exports = {}
124-
local ____test = require(\\"test\\")
124+
local ____test = require("test")
125125
local TestClass = ____test.TestClass
126126
local ____ = TestClass
127127
return ____exports"
128128
`;
129129

130130
exports[`Transformation (modulesImportNamedSpecialChars) 1`] = `
131131
"local ____exports = {}
132-
local ____kebab_2Dmodule = require(\\"kebab-module\\")
132+
local ____kebab_2Dmodule = require("kebab-module")
133133
local TestClass1 = ____kebab_2Dmodule.TestClass1
134-
local ____dollar_24module = require(\\"dollar$module\\")
134+
local ____dollar_24module = require("dollar$module")
135135
local TestClass2 = ____dollar_24module.TestClass2
136-
local ____singlequote_27module = require(\\"singlequote'module\\")
136+
local ____singlequote_27module = require("singlequote'module")
137137
local TestClass3 = ____singlequote_27module.TestClass3
138-
local ____hash_23module = require(\\"hash#module\\")
138+
local ____hash_23module = require("hash#module")
139139
local TestClass4 = ____hash_23module.TestClass4
140-
local ____space_20module = require(\\"space module\\")
140+
local ____space_20module = require("space module")
141141
local TestClass5 = ____space_20module.TestClass5
142142
local ____ = TestClass1
143143
local ____ = TestClass2
@@ -149,23 +149,23 @@ return ____exports"
149149

150150
exports[`Transformation (modulesImportRenamed) 1`] = `
151151
"local ____exports = {}
152-
local ____test = require(\\"test\\")
152+
local ____test = require("test")
153153
local RenamedClass = ____test.TestClass
154154
local ____ = RenamedClass
155155
return ____exports"
156156
`;
157157

158158
exports[`Transformation (modulesImportRenamedSpecialChars) 1`] = `
159159
"local ____exports = {}
160-
local ____kebab_2Dmodule = require(\\"kebab-module\\")
160+
local ____kebab_2Dmodule = require("kebab-module")
161161
local RenamedClass1 = ____kebab_2Dmodule.TestClass
162-
local ____dollar_24module = require(\\"dollar$module\\")
162+
local ____dollar_24module = require("dollar$module")
163163
local RenamedClass2 = ____dollar_24module.TestClass
164-
local ____singlequote_27module = require(\\"singlequote'module\\")
164+
local ____singlequote_27module = require("singlequote'module")
165165
local RenamedClass3 = ____singlequote_27module.TestClass
166-
local ____hash_23module = require(\\"hash#module\\")
166+
local ____hash_23module = require("hash#module")
167167
local RenamedClass4 = ____hash_23module.TestClass
168-
local ____space_20module = require(\\"space module\\")
168+
local ____space_20module = require("space module")
169169
local RenamedClass5 = ____space_20module.TestClass
170170
local ____ = RenamedClass1
171171
local ____ = RenamedClass2
@@ -177,7 +177,7 @@ return ____exports"
177177

178178
exports[`Transformation (modulesImportWithoutFromClause) 1`] = `
179179
"local ____exports = {}
180-
require(\\"test\\")
180+
require("test")
181181
return ____exports"
182182
`;
183183

@@ -227,19 +227,19 @@ return ____exports"
227227

228228
exports[`Transformation (modulesVariableExport) 1`] = `
229229
"local ____exports = {}
230-
____exports.foo = \\"bar\\"
230+
____exports.foo = "bar"
231231
return ____exports"
232232
`;
233233

234-
exports[`Transformation (modulesVariableNoExport) 1`] = `"foo = \\"bar\\""`;
234+
exports[`Transformation (modulesVariableNoExport) 1`] = `"foo = "bar""`;
235235

236236
exports[`Transformation (printFormat) 1`] = `
237-
"stringConcat = ((\\"a\\" .. \\"b\\" .. \\"c\\") .. \\"d\\") .. \\"e\\"
237+
"stringConcat = (("a" .. "b" .. "c") .. "d") .. "e"
238238
numbers = 2 * 2 + 3 + 4 * (5 + 6) ~= 7
239239
function func(...)
240240
end
241241
func(function()
242-
local b = \\"A function\\"
242+
local b = "A function"
243243
end)
244244
func(func())
245245
array = {func()}
@@ -252,8 +252,8 @@ bigObject = {
252252
a = 1,
253253
b = 2,
254254
c = 3,
255-
d = \\"value1\\",
256-
e = \\"value2\\"
255+
d = "value1",
256+
e = "value2"
257257
}"
258258
`;
259259

@@ -279,7 +279,7 @@ end"
279279

280280
exports[`Transformation (unusedDefaultWithNamespaceImport) 1`] = `
281281
"local ____exports = {}
282-
local x = require(\\"module\\")
282+
local x = require("module")
283283
local ____ = x
284284
return ____exports"
285285
`;

test/transpile/__snapshots__/directories.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 1`] = `
4-
Array [
4+
[
55
"directories/basic/src/lib/file.lua",
66
"directories/basic/src/main.lua",
77
]
88
`;
99

1010
exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 2`] = `
11-
Array [
11+
[
1212
"directories/basic/out/lib/file.lua",
1313
"directories/basic/out/main.lua",
1414
]
1515
`;
1616

1717
exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 3`] = `
18-
Array [
18+
[
1919
"directories/basic/src/lib/file.lua",
2020
"directories/basic/src/main.lua",
2121
]
2222
`;
2323

2424
exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 4`] = `
25-
Array [
25+
[
2626
"directories/basic/out/lib/file.lua",
2727
"directories/basic/out/main.lua",
2828
]

test/transpile/__snapshots__/module-resolution.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`supports complicated paths configuration 1`] = `
4-
Array [
4+
[
55
"/paths-base-tsconfig/dist/packages/tstl-program/packages/mypackage/src/bar.lua",
66
"/paths-base-tsconfig/dist/packages/tstl-program/packages/mypackage/src/index.lua",
77
"/paths-base-tsconfig/dist/packages/tstl-program/packages/myprogram/src/main.lua",
88
]
99
`;
1010

1111
exports[`supports paths configuration 1`] = `
12-
Array [
12+
[
1313
"/paths-simple/myprogram/dist/mypackage/bar.lua",
1414
"/paths-simple/myprogram/dist/mypackage/index.lua",
1515
"/paths-simple/myprogram/dist/myprogram/main.lua",

test/transpile/__snapshots__/project.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`should give verbose output 1`] = `
4-
Array [
4+
[
55
"Loaded 0 plugins",
66
"Parsing project settings",
77
"Transforming <cwd>/test/transpile/project/otherFile.ts",
@@ -11,7 +11,7 @@ Array [
1111
"Constructing emit plan",
1212
"Resolving dependencies for <cwd>/test/transpile/project/otherFile.ts",
1313
"Resolving dependencies for <cwd>/test/transpile/project/index.ts",
14-
"Resolving \\"./otherFile\\" from <cwd>/test/transpile/project/index.ts",
14+
"Resolving "./otherFile" from <cwd>/test/transpile/project/index.ts",
1515
"Resolved ./otherFile to <cwd>/test/transpile/project/otherFile.ts",
1616
"Emitting output",
1717
"Emitting <cwd>/test/transpile/project/otherFile.lua",
@@ -21,8 +21,8 @@ Array [
2121
`;
2222
2323
exports[`should transpile 1`] = `
24-
Array [
25-
Object {
24+
[
25+
{
2626
"filePath": "otherFile.lua",
2727
"lua": "local ____exports = {}
2828
function ____exports.getNumber(self)
@@ -31,10 +31,10 @@ end
3131
return ____exports
3232
",
3333
},
34-
Object {
34+
{
3535
"filePath": "index.lua",
3636
"lua": "local ____exports = {}
37-
local ____otherFile = require(\\"otherFile\\")
37+
local ____otherFile = require("otherFile")
3838
local getNumber = ____otherFile.getNumber
3939
local myNumber = getNumber(nil)
4040
setAPIValue(myNumber * 5)

0 commit comments

Comments
 (0)