-
-
Notifications
You must be signed in to change notification settings - Fork 185
Add in missing 4.0 compound assignments #919
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
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,6 +121,9 @@ test.each([ | |
| "x ^= y", | ||
| "x <<= y", | ||
| "x >>>= y", | ||
| "x &&= y", | ||
| "x ||= y", | ||
| "x ??= y", | ||
| ])("Operator assignment statements (%p)", statement => { | ||
| util.testFunction` | ||
| let x = 3; | ||
|
|
@@ -359,3 +362,134 @@ test("local multiple variable declaration referencing self indirectly", () => { | |
| return bar; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| describe.each(["x &&= y", "x ||= y"])("boolean compound assignment (%p)", assignment => { | ||
| const booleanCases = [ | ||
| [false, false], | ||
| [false, true], | ||
| [true, false], | ||
| [true, true], | ||
| ]; | ||
| test.each(booleanCases)("matches JS", (x, y) => { | ||
| util.testFunction` | ||
| let x = ${x}; | ||
| let y = ${y}; | ||
| ${assignment}; | ||
| return x; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
| }); | ||
|
|
||
| test.each([undefined, 3])("nullish coalescing compound assignment", initialValue => { | ||
| util.testFunction` | ||
| let x: number = ${util.formatCode(initialValue)}; | ||
| x ??= 5; | ||
| return x; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test("nullish coalescing compound assignment lhs false", () => { | ||
| util.testFunction` | ||
| let x = false; | ||
| x ??= true; | ||
| return x; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test("nullish coalescing compound assignment side effect not evaluated", () => { | ||
| util.testFunction` | ||
| let x = 3; | ||
| let y = 10; | ||
| x ??= (y += 5); | ||
| return [x, y]; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| { operator: "||=", initialValue: true }, | ||
| { operator: "&&=", initialValue: false }, | ||
| { operator: "??=", initialValue: false }, | ||
| ])("compound assignment short-circuits and does not call setter", ({ operator, initialValue }) => { | ||
| /* | ||
| In JS if the rhs does not affect the resulting value, the setter is NOT called: | ||
| * x.y ||= z is translated to x.y || (x.y = z). | ||
| * x.y &&= z is translated to x.y && (x.y = z). | ||
| * x.y ||= z is translated to x.y !== undefined && (x.y = z). | ||
|
|
||
| Test if setter in Lua is called same nr of times as in JS. | ||
| */ | ||
| util.testModule` | ||
| export let setterCalled = 0; | ||
|
|
||
| class MyClass { | ||
|
|
||
| get prop(): any { | ||
| return ${initialValue}; | ||
| } | ||
|
|
||
| set prop(value: any) { | ||
| setterCalled++; | ||
| } | ||
| } | ||
|
|
||
| const inst = new MyClass(); | ||
| inst.prop ${operator} 8; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| { operator: "||=", initialValue: true }, | ||
| { operator: "&&=", initialValue: false }, | ||
| { operator: "??=", initialValue: false }, | ||
| ])("compound assignment short-circuits and does not call setter as expression", ({ operator, initialValue }) => { | ||
| /* | ||
| In JS if the rhs does not affect the resulting value, the setter is NOT called: | ||
| * x.y ||= z is translated to x.y || (x.y = z). | ||
| * x.y &&= z is translated to x.y && (x.y = z). | ||
| * x.y ||= z is translated to x.y !== undefined && (x.y = z). | ||
|
|
||
| Test if setter in Lua is called same nr of times as in JS. | ||
| */ | ||
| util.testModule` | ||
| export let setterCalled = 0; | ||
|
|
||
| class MyClass { | ||
|
|
||
| get prop(): any { | ||
| return ${initialValue}; | ||
| } | ||
|
|
||
| set prop(value: any) { | ||
| setterCalled++; | ||
| } | ||
| } | ||
|
|
||
| const inst = new MyClass(); | ||
| export const result = (inst.prop ${operator} 8); | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| { operator: "+=", initialValue: 3 }, | ||
| { operator: "-=", initialValue: 10 }, | ||
| { operator: "*=", initialValue: 4 }, | ||
| { operator: "/=", initialValue: 20 }, | ||
| { operator: "||=", initialValue: false }, | ||
| { operator: "&&=", initialValue: true }, | ||
| { operator: "??=", initialValue: undefined }, | ||
| ])("compound assignment side effects", ({ operator, initialValue }) => { | ||
|
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. Shouldn't it be the case with other compound assignment operators too?
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. true, adding some more. |
||
| // Test if when assigning to something with potential side effects, they are only evaluated once. | ||
| util.testFunction` | ||
| const obj: { prop: any} = { prop: ${initialValue} }; | ||
|
|
||
| let objGot = 0; | ||
| function getObj() { | ||
| objGot++; | ||
| return obj; | ||
| } | ||
|
|
||
| getObj().prop ${operator} 4; | ||
|
|
||
| return [obj, objGot]; | ||
| `.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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this branch? If it's required, we can remove
case ts.SyntaxKind.QuestionQuestionTokenfromtransformBinaryExpressionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep you're right, it is needed here, I removed the case from transformBinaryExpression.