Omitted expression support#643
Conversation
test/unit/array.spec.ts
Outdated
|
|
||
| test("Array OmittedExpression", () => { | ||
| const result = util.transpileAndExecute( | ||
| `const myarray = [,]; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I've used a test.each to check that array's contents
src/LuaTransformer.ts
Outdated
| expression.left.elements.length > 0 | ||
| ? expression.left.elements.map(e => this.transformExpression(e)) | ||
| ? expression.left.elements.map(e => | ||
| ts.isOmittedExpression(e) |
There was a problem hiding this comment.
Have you considered using transformArrayBindingElement there?
src/LuaTransformer.ts
Outdated
| } | ||
|
|
||
| public transformOmittedExpression(node: ts.OmittedExpression): ExpressionVisitResult { | ||
| return tstl.createNilLiteral(node); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Closes #603
Allows OmittedExpressions to be used in an array.
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: