Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions src/compiler/transformers/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace ts {
function hasKeyAfterPropsSpread(node: JsxOpeningLikeElement) {
let spread = false;
for (const elem of node.attributes.properties) {
if (isJsxSpreadAttribute(elem)) {
if (isJsxSpreadAttribute(elem) && (!isObjectLiteralExpression(elem.expression) || elem.expression.properties.some(isSpreadAssignment))) {
spread = true;
}
else if (spread && isJsxAttribute(elem) && elem.name.escapedText === "key") {
Expand Down Expand Up @@ -348,7 +348,10 @@ namespace ts {
return element;
}

function transformJsxSpreadAttributeToSpreadAssignment(node: JsxSpreadAttribute) {
function transformJsxSpreadAttributeToProps(node: JsxSpreadAttribute) {
if (isObjectLiteralExpression(node.expression)) {
return node.expression.properties;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't like 100% correct when it comes to JS semantics, but TS never was 100% correct anyway (eg. TS doesn't emit 100% spec-compliant output for a case like this).

Copy link
Member

Choose a reason for hiding this comment

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

What isn't correct about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this should be OK for the "sane code" but probably once you start thinking about malicious getters, setters, and stuff then you could figure out some edge case that would change behavior with this change. But then it means that the code compiled with TS doesn't work exactly the same as the code compiled with Babel because Babel applies this optimization.

As mentioned though... TS isn't 100% correct even today so I'm not sure if a super subtle (and only for weird code) behavior change changes the "final balance" all that much.

TS is using Object.assign semantics for spreads (and that calls setters on the target object), whereas the correct semantic is to define properties (and that doesn't call setters). There are also cases like in the referenced Babel PR:

var p = { a: 1 }
var classicAssign = Object.assign({}, { get nefariousGetter() { (p = { evil: 'muahauhauha' }); return 42 } }, { p }) // {"nefariousGetter":42,"p":{"a":1}}
var correct = { ...{ get nefariousGetter() { (p = { evil: 'muahauhauha' }); return 42 } }, p } // {"nefariousGetter":42,"p":{"evil":"muahauhauha"}}

😅

}
return factory.createSpreadAssignment(visitNode(node.expression, visitor, isExpression));
}

Expand All @@ -359,39 +362,61 @@ namespace ts {
}

function transformJsxAttributesToProps(attrs: readonly(JsxSpreadAttribute | JsxAttribute)[], children?: PropertyAssignment) {
const props = flatten<SpreadAssignment | PropertyAssignment>(spanMap(attrs, isJsxSpreadAttribute, (attrs, isSpread) =>
map(attrs, attr => isSpread ? transformJsxSpreadAttributeToSpreadAssignment(attr as JsxSpreadAttribute) : transformJsxAttributeToObjectLiteralElement(attr as JsxAttribute))));
const props = flatten(spanMap(attrs, isJsxSpreadAttribute, (attrs, isSpread) =>
flatten(map(attrs, attr => isSpread ? transformJsxSpreadAttributeToProps(attr as JsxSpreadAttribute) : transformJsxAttributeToObjectLiteralElement(attr as JsxAttribute)))));
if (children) {
props.push(children);
}
return props;
}

function transformJsxAttributesToExpression(attrs: readonly(JsxSpreadAttribute | JsxAttribute)[], children?: PropertyAssignment) {
// Map spans of JsxAttribute nodes into object literals and spans
// of JsxSpreadAttribute nodes into expressions.
const expressions = flatten(
spanMap(attrs, isJsxSpreadAttribute, (attrs, isSpread) => isSpread
? map(attrs, transformJsxSpreadAttributeToExpression)
: factory.createObjectLiteralExpression(map(attrs, transformJsxAttributeToObjectLiteralElement))
)
);

if (isJsxSpreadAttribute(attrs[0])) {
// We must always emit at least one object literal before a spread
// argument.factory.createObjectLiteral
expressions.unshift(factory.createObjectLiteralExpression());
const expressions: Expression[] = [];
let properties: ObjectLiteralElementLike[] = [];

for (const attr of attrs) {
if (isJsxSpreadAttribute(attr)) {
// as an optimization we try to flatten the first level of spread inline object
// as if its props would be passed as JSX attributes
if (isObjectLiteralExpression(attr.expression)) {
for (const prop of attr.expression.properties) {
if (isSpreadAssignment(prop)) {
finishObjectLiteralIfNeeded();
expressions.push(prop.expression);
continue;
}
properties.push(prop);
}
continue;
}
finishObjectLiteralIfNeeded();
expressions.push(attr.expression);
continue;
}
properties.push(transformJsxAttributeToObjectLiteralElement(attr));
}

if (children) {
expressions.push(factory.createObjectLiteralExpression([children]));
properties.push(children);
}

finishObjectLiteralIfNeeded();

if (expressions.length && !isObjectLiteralExpression(expressions[0])) {
// We must always emit at least one object literal before a spread attribute
// as the JSX always factory expects a fresh object, so we need to make a copy here
// we also avoid mutating an external reference by doing this (first expression is used as assign's target)
expressions.unshift(factory.createObjectLiteralExpression());
}

return singleOrUndefined(expressions) || emitHelpers().createAssignHelper(expressions);
}

function transformJsxSpreadAttributeToExpression(node: JsxSpreadAttribute) {
return visitNode(node.expression, visitor, isExpression);
function finishObjectLiteralIfNeeded() {
if (properties.length) {
expressions.push(factory.createObjectLiteralExpression(properties));
properties = [];
}
}
}

function transformJsxAttributeToObjectLiteralElement(node: JsxAttribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export function T3(a, b) {
return React.createElement("div", Object.assign({}, a, { className: "T3" }, b), "T3");
}
export function T4(a, b) {
return React.createElement("div", Object.assign({ className: "T4" }, Object.assign(Object.assign({}, a), b)), "T4");
return React.createElement("div", Object.assign({ className: "T4" }, a, b), "T4");
}
export function T5(a, b, c, d) {
return React.createElement("div", Object.assign({ className: "T5" }, Object.assign(Object.assign(Object.assign({}, a), b), { c, d })), "T5");
return React.createElement("div", Object.assign({ className: "T5" }, a, b, { c, d }), "T5");
}
export function T6(a, b, c, d) {
return React.createElement("div", Object.assign({ className: "T6" }, Object.assign(Object.assign(Object.assign({}, a), b), Object.assign(Object.assign({}, c), d))), "T6");
return React.createElement("div", Object.assign({ className: "T6" }, a, b, Object.assign(Object.assign({}, c), d)), "T6");
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export function T3(a, b) {
return React.createElement("div", { ...a, className: "T3", ...b }, "T3");
}
export function T4(a, b) {
return React.createElement("div", { className: "T4", ...{ ...a, ...b } }, "T4");
return React.createElement("div", { className: "T4", ...a, ...b }, "T4");
}
export function T5(a, b, c, d) {
return React.createElement("div", { className: "T5", ...{ ...a, ...b, ...{ c, d } } }, "T5");
return React.createElement("div", { className: "T5", ...a, ...b, ...{ c, d } }, "T5");
}
export function T6(a, b, c, d) {
return React.createElement("div", { className: "T6", ...{ ...a, ...b, ...{ ...c, ...d } } }, "T6");
return React.createElement("div", { className: "T6", ...a, ...b, ...{ ...c, ...d } }, "T6");
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export function T3(a, b) {
return React.createElement("div", { ...a, className: "T3", ...b }, "T3");
}
export function T4(a, b) {
return React.createElement("div", { className: "T4", ...{ ...a, ...b } }, "T4");
return React.createElement("div", { className: "T4", ...a, ...b }, "T4");
}
export function T5(a, b, c, d) {
return React.createElement("div", { className: "T5", ...{ ...a, ...b, ...{ c, d } } }, "T5");
return React.createElement("div", { className: "T5", ...a, ...b, ...{ c, d } }, "T5");
}
export function T6(a, b, c, d) {
return React.createElement("div", { className: "T6", ...{ ...a, ...b, ...{ ...c, ...d } } }, "T6");
return React.createElement("div", { className: "T6", ...a, ...b, ...{ ...c, ...d } }, "T6");
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function T3(a, b) {
return _jsx("div", Object.assign({}, a, { className: "T3" }, b, { children: "T3" }));
}
export function T4(a, b) {
return _jsx("div", Object.assign({ className: "T4" }, Object.assign(Object.assign({}, a), b), { children: "T4" }));
return _jsx("div", Object.assign({ className: "T4" }, a, b, { children: "T4" }));
}
export function T5(a, b, c, d) {
return _jsx("div", Object.assign({ className: "T5" }, Object.assign(Object.assign(Object.assign({}, a), b), { c, d }), { children: "T5" }));
return _jsx("div", Object.assign({ className: "T5" }, a, b, { c, d }, { children: "T5" }));
}
export function T6(a, b, c, d) {
return _jsx("div", Object.assign({ className: "T6" }, Object.assign(Object.assign(Object.assign({}, a), b), Object.assign(Object.assign({}, c), d)), { children: "T6" }));
return _jsx("div", Object.assign({ className: "T6" }, a, b, Object.assign(Object.assign({}, c), d), { children: "T6" }));
}
export function T7(a, b, c, d) {
return _jsx("div", { children: "T7" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function T3(a, b) {
return _jsx("div", { ...a, className: "T3", ...b, children: "T3" });
}
export function T4(a, b) {
return _jsx("div", { className: "T4", ...{ ...a, ...b }, children: "T4" });
return _jsx("div", { className: "T4", ...a, ...b, children: "T4" });
}
export function T5(a, b, c, d) {
return _jsx("div", { className: "T5", ...{ ...a, ...b, ...{ c, d } }, children: "T5" });
return _jsx("div", { className: "T5", ...a, ...b, ...{ c, d }, children: "T5" });
}
export function T6(a, b, c, d) {
return _jsx("div", { className: "T6", ...{ ...a, ...b, ...{ ...c, ...d } }, children: "T6" });
return _jsx("div", { className: "T6", ...a, ...b, ...{ ...c, ...d }, children: "T6" });
}
export function T7(a, b, c, d) {
return _jsx("div", { children: "T7" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function T3(a, b) {
return _jsx("div", { ...a, className: "T3", ...b, children: "T3" });
}
export function T4(a, b) {
return _jsx("div", { className: "T4", ...{ ...a, ...b }, children: "T4" });
return _jsx("div", { className: "T4", ...a, ...b, children: "T4" });
}
export function T5(a, b, c, d) {
return _jsx("div", { className: "T5", ...{ ...a, ...b, ...{ c, d } }, children: "T5" });
return _jsx("div", { className: "T5", ...a, ...b, ...{ c, d }, children: "T5" });
}
export function T6(a, b, c, d) {
return _jsx("div", { className: "T6", ...{ ...a, ...b, ...{ ...c, ...d } }, children: "T6" });
return _jsx("div", { className: "T6", ...a, ...b, ...{ ...c, ...d }, children: "T6" });
}
export function T7(a, b, c, d) {
return _jsx("div", { children: "T7" });
Expand Down