Skip to content

Add in missing 4.0 compound assignments#919

Merged
Perryvw merged 4 commits intomasterfrom
feature/4.0-compound-assignment
Oct 3, 2020
Merged

Add in missing 4.0 compound assignments#919
Perryvw merged 4 commits intomasterfrom
feature/4.0-compound-assignment

Conversation

@Perryvw
Copy link
Copy Markdown
Member

@Perryvw Perryvw commented Oct 2, 2020

Closes #916

@Perryvw Perryvw requested a review from ark120202 October 2, 2020 13:14
Comment on lines +263 to +264
context.diagnostics.push(unsupportedNodeKind(node, operator));
return [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since operator is a union, and all it's values are handled above, you can use assertNever to statically verify that this branch would never be executed

Suggested change
context.diagnostics.push(unsupportedNodeKind(node, operator));
return [];
assertNever(operator);

} else if (operator === ts.SyntaxKind.QuestionQuestionToken) {
return [
lua.createIfStatement(
lua.createBinaryExpression(lhs, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these 3 branches appear to be different only in the first parameter here. Thoughts on just extracting it into a condition variable?

{ operator: "||=", initialValue: false },
{ operator: "&&=", initialValue: true },
{ operator: "??=", initialValue: undefined },
])("compound assignment side effects", ({ operator, initialValue }) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be the case with other compound assignment operators too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, adding some more.

export let setterCalled = 0;

class MyClass {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines +56 to +59
if (operator === ts.SyntaxKind.QuestionQuestionToken) {
assert(ts.isBinaryExpression(node));
return transformNullishCoalescingExpression(context, node);
}
Copy link
Copy Markdown
Contributor

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.QuestionQuestionToken from transformBinaryExpression

Copy link
Copy Markdown
Member Author

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.

* x.y &&= z does NOT call the x.y setter if x.y is already false.
* x.y ??= z does NOT call the x.y setter if x.y is already not nullish.
*/
type SetterSkippingCompoundAssignmentOperator =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After checking out the proposal it seems to be intrinsic behavior for logical assignment concept, so I'd call it LogicalAssignmentOperator

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually TS already has a type LogicalOperator = AmpersandAmpersandToken | BarBarToken, but it does not include ??, I propose we keep this as it is to avoid confusing naming, I did update the type to use ts.LogicalOperator instead of the individual tokens.

@Perryvw Perryvw merged commit 849f630 into master Oct 3, 2020
@Perryvw Perryvw deleted the feature/4.0-compound-assignment branch October 3, 2020 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support all CompoundAssignmentOperators

2 participants