Skip to content

Omitted expression support#643

Merged
Perryvw merged 7 commits intoTypeScriptToLua:masterfrom
hazzard993:omitted-expression
Jun 29, 2019
Merged

Omitted expression support#643
Perryvw merged 7 commits intoTypeScriptToLua:masterfrom
hazzard993:omitted-expression

Conversation

@hazzard993
Copy link
Copy Markdown
Contributor

@hazzard993 hazzard993 commented Jun 26, 2019

Closes #603

Allows OmittedExpressions to be used in an array.

const array = [0, , 2];
local array = {0, nil, 2}

It is important to note that Lua will stop iterating through an array when it reaches nil.

These OmittedExpressions can also be used in array binding assignment statements:

let a, c;
[a, , c] = [0, 1, 2];


test("Array OmittedExpression", () => {
const result = util.transpileAndExecute(
`const myarray = [,];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd prefer it if you kept the array the same in the two tests, so check index 1 on [1,,2] for this one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've used a test.each to check that array's contents

expression.left.elements.length > 0
? expression.left.elements.map(e => this.transformExpression(e))
? expression.left.elements.map(e =>
ts.isOmittedExpression(e)
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.

Have you considered using transformArrayBindingElement there?

}

public transformOmittedExpression(node: ts.OmittedExpression): ExpressionVisitResult {
return tstl.createNilLiteral(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.

I think it's not really good to always return nil there. nil is valid only in one context it could be used (array literals), but it's not in other (destructuring).
I think it would be better to decide it in the visitor (using node.parent) and use it in transformArrayBindingElement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This neatens things up nicely, I've added isWithinLiteralAssignmentStatement which can be used to check if a node is being used in a binding assignment expression. This should work with nested binding patterns too once those are supported.

I've called elements of an array on the left hand side of an assignment statement "ArrayBindingExpressions" since TS refers to them as Expressions and they seem to be a super-set of ArrayBindingElements with PropertyAccessExpressions being another thing they could be.

@Perryvw Perryvw merged commit 3fa2065 into TypeScriptToLua:master Jun 29, 2019
@hazzard993 hazzard993 deleted the omitted-expression branch June 29, 2019 21:52
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.

Unsupported OmittedExpression in array

3 participants