Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ module.exports = {
testEnvironment: "node",
testRunner: "jest-circus/runner",
preset: "ts-jest",
globals: {
"ts-jest": {
tsconfig: "<rootDir>/test/tsconfig.json",
diagnostics: { warnOnly: !isCI },
},
transform: {
"^.+\\.ts?$": [
"ts-jest",
{
tsconfig: "<rootDir>/test/tsconfig.json",
diagnostics: { warnOnly: !isCI },
},
],
},
};
11,961 changes: 4,526 additions & 7,435 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@
"eslint-plugin-jest": "^26.9.0",
"fs-extra": "^8.1.0",
"javascript-stringify": "^2.0.1",
"jest": "^28.1.3",
"jest": "^29.5.0",
"jest-circus": "^29.5.0",
"lua-types": "^2.13.0",
"lua-wasm-bindings": "^0.3.1",
"prettier": "^2.8.4",
"ts-jest": "^28.0.8",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
}
Expand Down
4 changes: 2 additions & 2 deletions src/transformation/utils/export.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { createModuleLocalNameIdentifier } from "../visitors/namespace";
import { createModuleLocalName } from "../visitors/namespace";
import { createExportsIdentifier } from "./lua-ast";
import { getSymbolInfo } from "./symbols";
import { findFirstNodeAbove } from "./typescript";
Expand Down Expand Up @@ -144,7 +144,7 @@ export function createExportedIdentifier(

const exportTable =
exportScope && ts.isModuleDeclaration(exportScope)
? createModuleLocalNameIdentifier(context, exportScope)
? createModuleLocalName(context, exportScope)
: createExportsIdentifier();

return lua.createTableIndexExpression(exportTable, lua.createStringLiteral(identifier.text));
Expand Down
11 changes: 10 additions & 1 deletion src/transformation/visitors/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import { performHoisting, ScopeType } from "../utils/scope";
import { getSymbolIdOfSymbol } from "../utils/symbols";
import { transformIdentifier } from "./identifier";

export function createModuleLocalName(context: TransformationContext, module: ts.ModuleDeclaration): lua.Expression {
if (!ts.isSourceFile(module.parent) && ts.isModuleDeclaration(module.parent)) {
const parentDeclaration = createModuleLocalName(context, module.parent);
const name = createModuleLocalNameIdentifier(context, module);
return lua.createTableIndexExpression(parentDeclaration, lua.createStringLiteral(name.text), module.name);
}

return createModuleLocalNameIdentifier(context, module);
}

export function createModuleLocalNameIdentifier(
context: TransformationContext,
declaration: ts.ModuleDeclaration
Expand All @@ -25,7 +35,6 @@ export function createModuleLocalNameIdentifier(
);
}

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

Expand Down
86 changes: 43 additions & 43 deletions test/translation/__snapshots__/transformation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ end"
`;

exports[`Transformation (characterEscapeSequence) 1`] = `
"quoteInDoubleQuotes = \\"' ' '\\"
quoteInTemplateString = \\"' ' '\\"
doubleQuoteInQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
doubleQuoteInDoubleQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
doubleQuoteInTemplateString = \\"\\\\\\" \\\\\\" \\\\\\"\\"
backQuoteInQuotes = \\"\` \` \`\\"
backQuoteInDoubleQuotes = \\"\` \` \`\\"
backQuoteInTemplateString = \\"\` \` \`\\"
escapedCharsInQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
escapedCharsInDoubleQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
escapedCharsInTemplateString = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
nonEmptyTemplateString = (\\"Level 0: \\\\n\\\\t \\" .. (\\"Level 1: \\\\n\\\\t\\\\t \\" .. (\\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. \\"Last level \\\\n --\\") .. \\" \\\\n --\\") .. \\" \\\\n --\\") .. \\" \\\\n --\\""
"quoteInDoubleQuotes = "' ' '"
quoteInTemplateString = "' ' '"
doubleQuoteInQuotes = "\\" \\" \\""
doubleQuoteInDoubleQuotes = "\\" \\" \\""
doubleQuoteInTemplateString = "\\" \\" \\""
backQuoteInQuotes = "\` \` \`"
backQuoteInDoubleQuotes = "\` \` \`"
backQuoteInTemplateString = "\` \` \`"
escapedCharsInQuotes = "\\\\ \\0 \\b \\t \\n \\v \\f \\" ' \`"
escapedCharsInDoubleQuotes = "\\\\ \\0 \\b \\t \\n \\v \\f \\" ' \`"
escapedCharsInTemplateString = "\\\\ \\0 \\b \\t \\n \\v \\f \\" ' \`"
nonEmptyTemplateString = ("Level 0: \\n\\t " .. ("Level 1: \\n\\t\\t " .. ("Level 3: \\n\\t\\t\\t " .. "Last level \\n --") .. " \\n --") .. " \\n --") .. " \\n --""
`;

exports[`Transformation (exportStatement) 1`] = `
Expand All @@ -30,22 +30,22 @@ local xyz = 4
____exports.xyz = xyz
____exports.uwv = xyz
do
local ____export = require(\\"xyz\\")
local ____export = require("xyz")
for ____exportKey, ____exportValue in pairs(____export) do
if ____exportKey ~= \\"default\\" then
if ____exportKey ~= "default" then
____exports[____exportKey] = ____exportValue
end
end
end
do
local ____xyz = require(\\"xyz\\")
local ____xyz = require("xyz")
local abc = ____xyz.abc
local def = ____xyz.def
____exports.abc = abc
____exports.def = def
end
do
local ____xyz = require(\\"xyz\\")
local ____xyz = require("xyz")
local def = ____xyz.abc
____exports.def = def
end
Expand All @@ -58,10 +58,10 @@ return ____exports"
`;

exports[`Transformation (methodRestArguments) 1`] = `
"local ____lualib = require(\\"lualib_bundle\\")
"local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
MyClass = __TS__Class()
MyClass.name = \\"MyClass\\"
MyClass.name = "MyClass"
function MyClass.prototype.____constructor(self)
end
function MyClass.prototype.varargsFunction(self, a, ...)
Expand All @@ -75,24 +75,24 @@ return ____exports"
`;

exports[`Transformation (modulesClassExport) 1`] = `
"local ____lualib = require(\\"lualib_bundle\\")
"local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local ____exports = {}
____exports.TestClass = __TS__Class()
local TestClass = ____exports.TestClass
TestClass.name = \\"TestClass\\"
TestClass.name = "TestClass"
function TestClass.prototype.____constructor(self)
end
return ____exports"
`;

exports[`Transformation (modulesClassWithMemberExport) 1`] = `
"local ____lualib = require(\\"lualib_bundle\\")
"local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local ____exports = {}
____exports.TestClass = __TS__Class()
local TestClass = ____exports.TestClass
TestClass.name = \\"TestClass\\"
TestClass.name = "TestClass"
function TestClass.prototype.____constructor(self)
end
function TestClass.prototype.memberFunc(self)
Expand All @@ -114,30 +114,30 @@ end"

exports[`Transformation (modulesImportAll) 1`] = `
"local ____exports = {}
local Test = require(\\"test\\")
local Test = require("test")
local ____ = Test
return ____exports"
`;

exports[`Transformation (modulesImportNamed) 1`] = `
"local ____exports = {}
local ____test = require(\\"test\\")
local ____test = require("test")
local TestClass = ____test.TestClass
local ____ = TestClass
return ____exports"
`;

exports[`Transformation (modulesImportNamedSpecialChars) 1`] = `
"local ____exports = {}
local ____kebab_2Dmodule = require(\\"kebab-module\\")
local ____kebab_2Dmodule = require("kebab-module")
local TestClass1 = ____kebab_2Dmodule.TestClass1
local ____dollar_24module = require(\\"dollar$module\\")
local ____dollar_24module = require("dollar$module")
local TestClass2 = ____dollar_24module.TestClass2
local ____singlequote_27module = require(\\"singlequote'module\\")
local ____singlequote_27module = require("singlequote'module")
local TestClass3 = ____singlequote_27module.TestClass3
local ____hash_23module = require(\\"hash#module\\")
local ____hash_23module = require("hash#module")
local TestClass4 = ____hash_23module.TestClass4
local ____space_20module = require(\\"space module\\")
local ____space_20module = require("space module")
local TestClass5 = ____space_20module.TestClass5
local ____ = TestClass1
local ____ = TestClass2
Expand All @@ -149,23 +149,23 @@ return ____exports"

exports[`Transformation (modulesImportRenamed) 1`] = `
"local ____exports = {}
local ____test = require(\\"test\\")
local ____test = require("test")
local RenamedClass = ____test.TestClass
local ____ = RenamedClass
return ____exports"
`;

exports[`Transformation (modulesImportRenamedSpecialChars) 1`] = `
"local ____exports = {}
local ____kebab_2Dmodule = require(\\"kebab-module\\")
local ____kebab_2Dmodule = require("kebab-module")
local RenamedClass1 = ____kebab_2Dmodule.TestClass
local ____dollar_24module = require(\\"dollar$module\\")
local ____dollar_24module = require("dollar$module")
local RenamedClass2 = ____dollar_24module.TestClass
local ____singlequote_27module = require(\\"singlequote'module\\")
local ____singlequote_27module = require("singlequote'module")
local RenamedClass3 = ____singlequote_27module.TestClass
local ____hash_23module = require(\\"hash#module\\")
local ____hash_23module = require("hash#module")
local RenamedClass4 = ____hash_23module.TestClass
local ____space_20module = require(\\"space module\\")
local ____space_20module = require("space module")
local RenamedClass5 = ____space_20module.TestClass
local ____ = RenamedClass1
local ____ = RenamedClass2
Expand All @@ -177,7 +177,7 @@ return ____exports"

exports[`Transformation (modulesImportWithoutFromClause) 1`] = `
"local ____exports = {}
require(\\"test\\")
require("test")
return ____exports"
`;

Expand Down Expand Up @@ -227,19 +227,19 @@ return ____exports"

exports[`Transformation (modulesVariableExport) 1`] = `
"local ____exports = {}
____exports.foo = \\"bar\\"
____exports.foo = "bar"
return ____exports"
`;

exports[`Transformation (modulesVariableNoExport) 1`] = `"foo = \\"bar\\""`;
exports[`Transformation (modulesVariableNoExport) 1`] = `"foo = "bar""`;

exports[`Transformation (printFormat) 1`] = `
"stringConcat = ((\\"a\\" .. \\"b\\" .. \\"c\\") .. \\"d\\") .. \\"e\\"
"stringConcat = (("a" .. "b" .. "c") .. "d") .. "e"
numbers = 2 * 2 + 3 + 4 * (5 + 6) ~= 7
function func(...)
end
func(function()
local b = \\"A function\\"
local b = "A function"
end)
func(func())
array = {func()}
Expand All @@ -252,8 +252,8 @@ bigObject = {
a = 1,
b = 2,
c = 3,
d = \\"value1\\",
e = \\"value2\\"
d = "value1",
e = "value2"
}"
`;

Expand All @@ -279,7 +279,7 @@ end"

exports[`Transformation (unusedDefaultWithNamespaceImport) 1`] = `
"local ____exports = {}
local x = require(\\"module\\")
local x = require("module")
local ____ = x
return ____exports"
`;
8 changes: 4 additions & 4 deletions test/transpile/__snapshots__/directories.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 1`] = `
Array [
[
"directories/basic/src/lib/file.lua",
"directories/basic/src/main.lua",
]
`;

exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 2`] = `
Array [
[
"directories/basic/out/lib/file.lua",
"directories/basic/out/main.lua",
]
`;

exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 3`] = `
Array [
[
"directories/basic/src/lib/file.lua",
"directories/basic/src/main.lua",
]
`;

exports[`should be able to resolve ({"name": "basic", "options": [Object]}) 4`] = `
Array [
[
"directories/basic/out/lib/file.lua",
"directories/basic/out/main.lua",
]
Expand Down
4 changes: 2 additions & 2 deletions test/transpile/__snapshots__/module-resolution.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`supports complicated paths configuration 1`] = `
Array [
[
"/paths-base-tsconfig/dist/packages/tstl-program/packages/mypackage/src/bar.lua",
"/paths-base-tsconfig/dist/packages/tstl-program/packages/mypackage/src/index.lua",
"/paths-base-tsconfig/dist/packages/tstl-program/packages/myprogram/src/main.lua",
]
`;

exports[`supports paths configuration 1`] = `
Array [
[
"/paths-simple/myprogram/dist/mypackage/bar.lua",
"/paths-simple/myprogram/dist/mypackage/index.lua",
"/paths-simple/myprogram/dist/myprogram/main.lua",
Expand Down
12 changes: 6 additions & 6 deletions test/transpile/__snapshots__/project.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should give verbose output 1`] = `
Array [
[
"Loaded 0 plugins",
"Parsing project settings",
"Transforming <cwd>/test/transpile/project/otherFile.ts",
Expand All @@ -11,7 +11,7 @@ Array [
"Constructing emit plan",
"Resolving dependencies for <cwd>/test/transpile/project/otherFile.ts",
"Resolving dependencies for <cwd>/test/transpile/project/index.ts",
"Resolving \\"./otherFile\\" from <cwd>/test/transpile/project/index.ts",
"Resolving "./otherFile" from <cwd>/test/transpile/project/index.ts",
"Resolved ./otherFile to <cwd>/test/transpile/project/otherFile.ts",
"Emitting output",
"Emitting <cwd>/test/transpile/project/otherFile.lua",
Expand All @@ -21,8 +21,8 @@ Array [
`;

exports[`should transpile 1`] = `
Array [
Object {
[
{
"filePath": "otherFile.lua",
"lua": "local ____exports = {}
function ____exports.getNumber(self)
Expand All @@ -31,10 +31,10 @@ end
return ____exports
",
},
Object {
{
"filePath": "index.lua",
"lua": "local ____exports = {}
local ____otherFile = require(\\"otherFile\\")
local ____otherFile = require("otherFile")
local getNumber = ____otherFile.getNumber
local myNumber = getNumber(nil)
setAPIValue(myNumber * 5)
Expand Down
Loading