SpreadAssignment support#693
Conversation
| const tableExpression = tstl.createTableExpression(properties, expression); | ||
| tableExpressions.push(tableExpression); | ||
|
|
||
| return this.transformLuaLibFunction(LuaLibFeature.ObjectAssign, expression, ...tableExpressions); |
There was a problem hiding this comment.
Object.assign should always get a table literal as a first argument to avoid mutation of first spread element value. It probably would be better to check the type of the first element of tableExpressions to handle literal spread cases (check out TypeScript output).
There was a problem hiding this comment.
Really good edge cases, thanks!
src/LuaTransformer.ts
Outdated
| if (tableExpressions.length === 0) { | ||
| return tstl.createTableExpression(properties, expression); | ||
| } else { | ||
| const tableExpression = tstl.createTableExpression(properties, expression); |
There was a problem hiding this comment.
It would be nice to avoid adding empty table when properties is empty.
test/unit/spreadElement.spec.ts
Outdated
| const code = ` | ||
| const obj = ${expression}; | ||
| return obj.value;`; | ||
| expect(JSON.parse(util.transpileAndExecute(code))).toBe(true); |
There was a problem hiding this comment.
The execution result here should already be a boolean, so there is no reason to JSON.parse here.
|
It's also possible to spread arrays: |
test/unit/spreadElement.spec.ts
Outdated
| "{ ...{ value: false }, value: true }", | ||
| "{ ...{ value: false }, value: false, ...{ value: true } }", | ||
| "{ ...{ x: true, y: true } }", | ||
| "{ x: true, y: true }", |
There was a problem hiding this comment.
What's the point of a test without any spread elements?
| "{ value: false, ...{ value: true } }", | ||
| "{ ...{ value: false }, value: true }", | ||
| "{ ...{ value: false }, value: false, ...{ value: true } }", | ||
| "{ ...{ x: true, y: true } }", |
There was a problem hiding this comment.
Do we really need a single-property test if we have a multi-property one?
Closes #121
SpreadAssignment elements can be used in object literal expressions.
Strings can also be spread.