Skip to content

Commit 0724d18

Browse files
committed
Fixed inconsistent scoping of top-level variable declarations
1 parent 17bb60f commit 0724d18

File tree

5 files changed

+64
-45
lines changed

5 files changed

+64
-45
lines changed

src/transformation/utils/lua-ast.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ export function createLocalOrExportedOrGlobalDeclaration(
122122
let declaration: lua.VariableDeclarationStatement | undefined;
123123
let assignment: lua.AssignmentStatement | undefined;
124124

125-
const isVariableDeclaration = tsOriginal !== undefined && ts.isVariableDeclaration(tsOriginal);
126125
const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal);
127126

128127
const identifiers = Array.isArray(lhs) ? lhs : [lhs];
@@ -145,7 +144,7 @@ export function createLocalOrExportedOrGlobalDeclaration(
145144
} else {
146145
const insideFunction = findScope(context, ScopeType.Function) !== undefined;
147146

148-
if (context.isModule || getCurrentNamespace(context) || insideFunction || isVariableDeclaration) {
147+
if (context.isModule || getCurrentNamespace(context) || insideFunction) {
149148
const scope = peekScope(context);
150149

151150
const isPossibleWrappedFunction =

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

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

33
exports[`Transformation (characterEscapeSequence) 1`] = `
4-
"local quoteInDoubleQuotes = \\"' ' '\\"
5-
local quoteInTemplateString = \\"' ' '\\"
6-
local doubleQuoteInQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
7-
local doubleQuoteInDoubleQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
8-
local doubleQuoteInTemplateString = \\"\\\\\\" \\\\\\" \\\\\\"\\"
9-
local backQuoteInQuotes = \\"\` \` \`\\"
10-
local backQuoteInDoubleQuotes = \\"\` \` \`\\"
11-
local backQuoteInTemplateString = \\"\` \` \`\\"
12-
local escapedCharsInQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
13-
local escapedCharsInDoubleQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
14-
local escapedCharsInTemplateString = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
15-
local nonEmptyTemplateString = \\"Level 0: \\\\n\\\\t \\" .. \\"Level 1: \\\\n\\\\t\\\\t \\" .. \\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. \\"Last level \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\""
4+
"quoteInDoubleQuotes = \\"' ' '\\"
5+
quoteInTemplateString = \\"' ' '\\"
6+
doubleQuoteInQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
7+
doubleQuoteInDoubleQuotes = \\"\\\\\\" \\\\\\" \\\\\\"\\"
8+
doubleQuoteInTemplateString = \\"\\\\\\" \\\\\\" \\\\\\"\\"
9+
backQuoteInQuotes = \\"\` \` \`\\"
10+
backQuoteInDoubleQuotes = \\"\` \` \`\\"
11+
backQuoteInTemplateString = \\"\` \` \`\\"
12+
escapedCharsInQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
13+
escapedCharsInDoubleQuotes = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
14+
escapedCharsInTemplateString = \\"\\\\\\\\ \\\\0 \\\\b \\\\t \\\\n \\\\v \\\\f \\\\\\" ' \`\\"
15+
nonEmptyTemplateString = \\"Level 0: \\\\n\\\\t \\" .. \\"Level 1: \\\\n\\\\t\\\\t \\" .. \\"Level 3: \\\\n\\\\t\\\\t\\\\t \\" .. \\"Last level \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\" .. \\" \\\\n --\\""
1616
`;
1717

1818
exports[`Transformation (classExtension1) 1`] = `
@@ -244,7 +244,7 @@ ____exports.foo = \\"bar\\"
244244
return ____exports"
245245
`;
246246

247-
exports[`Transformation (modulesVariableNoExport) 1`] = `"local foo = \\"bar\\""`;
247+
exports[`Transformation (modulesVariableNoExport) 1`] = `"foo = \\"bar\\""`;
248248

249249
exports[`Transformation (namespacePhantom) 1`] = `
250250
"function nsMember(self)
@@ -257,6 +257,16 @@ exports[`Transformation (returnDefault) 1`] = `
257257
end"
258258
`;
259259

260+
exports[`Transformation (topLevelVariables) 1`] = `
261+
"obj = {value1 = 1, value2 = 2}
262+
value1 = obj.value1
263+
value2 = obj.value2
264+
function fun1(self)
265+
end
266+
fun2 = function()
267+
end"
268+
`;
269+
260270
exports[`Transformation (unusedDefaultWithNamespaceImport) 1`] = `
261271
"local ____exports = {}
262272
local x = require(\\"module\\")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const obj = { value1: 1, value2: 2 };
2+
const value1 = obj.value1;
3+
const { value2 } = obj;
4+
5+
function fun1(): void {}
6+
const fun2 = () => {};

test/unit/assignments.spec.ts

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

33
test("const declaration", () => {
4-
const lua = util.transpileString(`const foo = true;`);
5-
expect(lua).toBe(`local foo = true`);
4+
const lua = util.testFunction`const foo = true;`.getMainLuaCodeChunk();
5+
expect(lua).toContain(`local foo = true`);
66
});
77

88
test("let declaration", () => {
9-
const lua = util.transpileString(`let foo = true;`);
10-
expect(lua).toBe(`local foo = true`);
9+
const lua = util.testFunction`let foo = true;`.getMainLuaCodeChunk();
10+
expect(lua).toContain(`local foo = true`);
11+
});
12+
13+
test("const declaration top-level is global", () => {
14+
const lua = util.testModule`const foo = true;`.getMainLuaCodeChunk();
15+
expect(lua).toBe(`foo = true`);
16+
});
17+
18+
test("let declaration top-level is global", () => {
19+
const lua = util.testModule`let foo = true;`.getMainLuaCodeChunk();
20+
expect(lua).toBe(`foo = true`);
1121
});
1222

1323
test("var declaration is disallowed", () => {

test/unit/objectLiteral.spec.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,53 @@ test.each([
88
{ inp: `{[myFunc()]:3,b:"4"}`, out: '{\n [myFunc(_G)] = 3,\n b = "4"\n}' },
99
{ inp: `{x}`, out: `{x = x}` },
1010
])("Object Literal (%p)", ({ inp, out }) => {
11-
const lua = util.transpileString(`const myvar = ${inp};`);
12-
expect(lua).toBe(`local myvar = ${out}`);
11+
const lua = util.testModule`const myvar = ${inp};`.getMainLuaCodeChunk();
12+
expect(lua).toBe(`myvar = ${out}`);
1313
});
1414

1515
describe("property shorthand", () => {
1616
test("should support property shorthand", () => {
17-
const result = util.transpileAndExecute(`
17+
util.testFunction`
1818
const x = 1;
1919
const o = { x };
2020
return o.x;
21-
`);
22-
23-
expect(result).toBe(1);
21+
`.expectToMatchJsResult();
2422
});
2523

2624
test.each([NaN, Infinity])("should support %p shorthand", identifier => {
27-
const result = util.transpileAndExecute(`return ({ ${identifier} }).${identifier}`);
28-
29-
expect(result).toBe(identifier);
25+
util.testFunction`return ({ ${identifier} }).${identifier}`.expectToMatchJsResult();
3026
});
3127

3228
test("should support _G shorthand", () => {
33-
const result = util.transpileAndExecute(
34-
`return ({ _G })._G.foobar;`,
35-
undefined,
36-
`foobar = "foobar"`,
37-
"declare const _G: any;"
38-
);
39-
40-
expect(result).toBe("foobar");
29+
const luaResult = util.testFunction`
30+
return ({ _G })._G.foobar;
31+
`
32+
.setTsHeader(`declare const _G: any;`)
33+
.setLuaHeader(`foobar = "foobar"`)
34+
.getLuaExecutionResult();
35+
36+
expect(luaResult).toEqual("foobar");
4137
});
4238

4339
test("should support export property shorthand", () => {
44-
const code = `
40+
util.testModule`
4541
export const x = 1;
4642
const o = { x };
4743
export const y = o.x;
48-
`;
49-
expect(util.transpileExecuteAndReturnExport(code, "y")).toBe(1);
44+
`.expectToMatchJsResult();
5045
});
5146
});
5247

5348
test("undefined as object key", () => {
54-
const code = `const foo = {undefined: "foo"};
55-
return foo.undefined;`;
56-
expect(util.transpileAndExecute(code)).toBe("foo");
49+
util.testFunction`
50+
const foo = {undefined: "foo"};
51+
return foo.undefined;
52+
`.expectToMatchJsResult();
5753
});
5854

5955
test.each([`({x: "foobar"}.x)`, `({x: "foobar"}["x"])`, `({x: () => "foobar"}.x())`, `({x: () => "foobar"}["x"]())`])(
6056
"object literal property access (%p)",
6157
expression => {
62-
const code = `return ${expression}`;
63-
const expectResult = eval(expression);
64-
expect(util.transpileAndExecute(code)).toBe(expectResult);
58+
util.testFunction`return ${expression}`.expectToMatchJsResult();
6559
}
6660
);

0 commit comments

Comments
 (0)