-
-
Notifications
You must be signed in to change notification settings - Fork 185
Fixed inconsistent scoping of top-level variable declarations #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0724d18
Fixed inconsistent scoping of top-level variable declarations
Perryvw 8df49dd
Added let variables in transformation test
Perryvw 22a871d
Added handling for variables in block scope, addressed some PR feedback
Perryvw 3eac075
simplified ast declaration logic
Perryvw 93bddac
reverted if statement refactor, some changes to tests
Perryvw 0a7e10e
Added functional test to check local variables are not in scope
Perryvw d893fe8
Made object literal tests functional
Perryvw fa4a78c
PR comments
Perryvw 68d811e
Removed no longer needed getCurrentNamespace
Perryvw 03a63ef
Removed obsolete tests
Perryvw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| const a = 1; | ||
| const [b] = [1]; | ||
| const { c } = { c: 1 }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const obj = { value1: 1, value2: 2 }; | ||
Perryvw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const value1 = obj.value1; | ||
| const { value2 } = obj; | ||
|
|
||
| let noValueLet; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would be better to transform it to a nil assignment instead of just dropping it?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should decide and do this in another PR |
||
| let obj2 = { value3: 1, value4: 2 }; | ||
| let value3 = obj2.value3; | ||
| let { value4 } = obj2; | ||
|
|
||
| function fun1(): void {} | ||
| const fun2 = () => {}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,65 @@ | ||
| import * as util from "../util"; | ||
|
|
||
| test.each([ | ||
| { inp: `{a:3,b:"4"}`, out: '{a = 3, b = "4"}' }, | ||
| { inp: `{"a":3,b:"4"}`, out: '{a = 3, b = "4"}' }, | ||
| { inp: `{["a"]:3,b:"4"}`, out: '{a = 3, b = "4"}' }, | ||
| { inp: `{["a"+123]:3,b:"4"}`, out: '{["a" .. 123] = 3, b = "4"}' }, | ||
| { inp: `{[myFunc()]:3,b:"4"}`, out: '{\n [myFunc(_G)] = 3,\n b = "4"\n}' }, | ||
| { inp: `{x}`, out: `{x = x}` }, | ||
| ])("Object Literal (%p)", ({ inp, out }) => { | ||
| const lua = util.transpileString(`const myvar = ${inp};`); | ||
| expect(lua).toBe(`local myvar = ${out}`); | ||
| test.each([`{ a: 3, b: "4" }`, `{ "a": 3, b: "4" }`, `{ ["a"]: 3, b: "4" }`, `{ ["a" + 123]: 3, b: "4" }`])( | ||
| "Object Literal (%p)", | ||
| inp => { | ||
| util.testExpression(inp).expectToMatchJsResult(); | ||
| } | ||
| ); | ||
|
|
||
| test("object literal with function call to get key", () => { | ||
| util.testFunction` | ||
| const myFunc = () => "a"; | ||
| return { [myFunc() + "b"]: 3 }; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test("object literal with shorthand property", () => { | ||
| util.testFunction` | ||
| const x = 5; | ||
| return { x }; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| describe("property shorthand", () => { | ||
| test("should support property shorthand", () => { | ||
| const result = util.transpileAndExecute(` | ||
| util.testFunction` | ||
| const x = 1; | ||
| const o = { x }; | ||
| return o.x; | ||
| `); | ||
|
|
||
| expect(result).toBe(1); | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test.each([NaN, Infinity])("should support %p shorthand", identifier => { | ||
| const result = util.transpileAndExecute(`return ({ ${identifier} }).${identifier}`); | ||
|
|
||
| expect(result).toBe(identifier); | ||
| util.testExpression`({ ${identifier} }).${identifier}`.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test("should support _G shorthand", () => { | ||
| const result = util.transpileAndExecute( | ||
| `return ({ _G })._G.foobar;`, | ||
| undefined, | ||
| `foobar = "foobar"`, | ||
| "declare const _G: any;" | ||
| ); | ||
|
|
||
| expect(result).toBe("foobar"); | ||
| util.testExpression`({ _G })._G.foobar` | ||
| .setTsHeader(`declare const _G: any;`) | ||
| .setLuaHeader(`foobar = "foobar"`) | ||
| .expectToEqual("foobar"); | ||
| }); | ||
|
|
||
| test("should support export property shorthand", () => { | ||
| const code = ` | ||
| util.testModule` | ||
| export const x = 1; | ||
| const o = { x }; | ||
| export const y = o.x; | ||
| `; | ||
| expect(util.transpileExecuteAndReturnExport(code, "y")).toBe(1); | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
| }); | ||
|
|
||
| test("undefined as object key", () => { | ||
| const code = `const foo = {undefined: "foo"}; | ||
| return foo.undefined;`; | ||
| expect(util.transpileAndExecute(code)).toBe("foo"); | ||
| util.testFunction` | ||
| const foo = {undefined: "foo"}; | ||
| return foo.undefined; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test.each([`({x: "foobar"}.x)`, `({x: "foobar"}["x"])`, `({x: () => "foobar"}.x())`, `({x: () => "foobar"}["x"]())`])( | ||
| "object literal property access (%p)", | ||
| expression => { | ||
| const code = `return ${expression}`; | ||
| const expectResult = eval(expression); | ||
| expect(util.transpileAndExecute(code)).toBe(expectResult); | ||
| util.testExpression(expression).expectToMatchJsResult(); | ||
| } | ||
| ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.