Skip to content

SpreadAssignment support#693

Merged
Perryvw merged 14 commits intoTypeScriptToLua:masterfrom
hazzard993:spread-assignment
Jul 31, 2019
Merged

SpreadAssignment support#693
Perryvw merged 14 commits intoTypeScriptToLua:masterfrom
hazzard993:spread-assignment

Conversation

@hazzard993
Copy link
Copy Markdown
Contributor

@hazzard993 hazzard993 commented Jul 28, 2019

Closes #121

SpreadAssignment elements can be used in object literal expressions.

const example = { a: 1, ...{ a: 2, b: 3 } };
local example = __TS__ObjectAssign({a = 1}, {a = 2, b = 3})

Strings can also be spread.

print(..."string"); // --> s t r i n g

const tableExpression = tstl.createTableExpression(properties, expression);
tableExpressions.push(tableExpression);

return this.transformLuaLibFunction(LuaLibFeature.ObjectAssign, expression, ...tableExpressions);
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.

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).

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.

Really good edge cases, thanks!

if (tableExpressions.length === 0) {
return tstl.createTableExpression(properties, expression);
} else {
const tableExpression = tstl.createTableExpression(properties, expression);
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.

It would be nice to avoid adding empty table when properties is empty.

const code = `
const obj = ${expression};
return obj.value;`;
expect(JSON.parse(util.transpileAndExecute(code))).toBe(true);
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.

The execution result here should already be a boolean, so there is no reason to JSON.parse here.

@ark120202
Copy link
Copy Markdown
Contributor

It's also possible to spread arrays: { ...[0, 1, 2] }, which should return { 0: 0, 1: 1, 2: 2 }. It shouldn't be done in Object.assign since it has a different return type, so we probably need a new helper that would shift indexes of arrays.

"{ ...{ value: false }, value: true }",
"{ ...{ value: false }, value: false, ...{ value: true } }",
"{ ...{ x: true, y: true } }",
"{ x: true, y: true }",
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.

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 } }",
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 really need a single-property test if we have a multi-property one?

@Perryvw Perryvw merged commit c11ba34 into TypeScriptToLua:master Jul 31, 2019
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.

Spread operator

3 participants