Skip to content

Commit 22a871d

Browse files
committed
Added handling for variables in block scope, addressed some PR feedback
1 parent 8df49dd commit 22a871d

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/transformation/utils/lua-ast.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ export function createLocalOrExportedOrGlobalDeclaration(
143143
}
144144
} else {
145145
const insideFunction = findScope(context, ScopeType.Function) !== undefined;
146+
const insideBlock = findScope(context, ScopeType.Block) !== undefined;
146147

147-
if (context.isModule || getCurrentNamespace(context) || insideFunction) {
148+
if (context.isModule || getCurrentNamespace(context) || insideFunction || insideBlock) {
148149
const scope = peekScope(context);
149150

150151
const isPossibleWrappedFunction =

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

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

3+
exports[`Transformation (blockScopeVariables) 1`] = `
4+
"do
5+
local a = 1
6+
local b = 1
7+
local ____ = {c = 1}
8+
local c = ____.c
9+
end"
10+
`;
11+
312
exports[`Transformation (characterEscapeSequence) 1`] = `
413
"quoteInDoubleQuotes = \\"' ' '\\"
514
quoteInTemplateString = \\"' ' '\\"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
const a = 1;
3+
const [b] = [1];
4+
const { c } = { c: 1 };
5+
}

test/unit/assignments.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ test("let declaration", () => {
1010
expect(lua).toContain(`local foo = true`);
1111
});
1212

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`);
13+
test.each(["const", "let"])("%s declaration top-level is global", declarationKind => {
14+
util.testModule`
15+
${declarationKind} foo = true;
16+
// @ts-ignore
17+
return (globalThis as any).foo;
18+
`.expectToEqual(true);
2119
});
2220

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

test/unit/objectLiteral.spec.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ describe("property shorthand", () => {
2222
});
2323

2424
test.each([NaN, Infinity])("should support %p shorthand", identifier => {
25-
util.testFunction`return ({ ${identifier} }).${identifier}`.expectToMatchJsResult();
25+
util.testExpression`({ ${identifier} }).${identifier}`.expectToMatchJsResult();
2626
});
2727

2828
test("should support _G shorthand", () => {
29-
const luaResult = util.testFunction`
30-
return ({ _G })._G.foobar;
31-
`
29+
util.testExpression`({ _G })._G.foobar`
3230
.setTsHeader(`declare const _G: any;`)
3331
.setLuaHeader(`foobar = "foobar"`)
34-
.getLuaExecutionResult();
35-
36-
expect(luaResult).toEqual("foobar");
32+
.expectToEqual("foobar");
3733
});
3834

3935
test("should support export property shorthand", () => {
@@ -55,6 +51,6 @@ test("undefined as object key", () => {
5551
test.each([`({x: "foobar"}.x)`, `({x: "foobar"}["x"])`, `({x: () => "foobar"}.x())`, `({x: () => "foobar"}["x"]())`])(
5652
"object literal property access (%p)",
5753
expression => {
58-
util.testFunction`return ${expression}`.expectToMatchJsResult();
54+
util.testExpression(expression).expectToMatchJsResult();
5955
}
6056
);

0 commit comments

Comments
 (0)