Skip to content

Transform JSX spread children #45693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 24, 2021
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
3 changes: 2 additions & 1 deletion src/compiler/transformers/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ namespace ts {
}

function visitJsxExpression(node: JsxExpression) {
return visitNode(node.expression, visitor, isExpression);
const expression = visitNode(node.expression, visitor, isExpression);
return node.dotDotDotToken ? factory.createSpreadElement(expression!) : expression;
}
}

Expand Down
36 changes: 24 additions & 12 deletions tests/baselines/reference/inlineJsxFactoryDeclarationsLocalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,36 @@ const _brokenTree2 = <DOMSFC x={1} y={2}>{tree}{tree}</DOMSFC>

//// [component.js]
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var _this = this;
exports.__esModule = true;
exports.tree = exports.MyClass = exports.MySFC = void 0;
/** @jsx predom */
var renderer2_1 = require("./renderer2");
var MySFC = function (props) { return (0, renderer2_1.predom)("p", null,
props.x,
var MySFC = function (props) { return renderer2_1.predom.apply(void 0, __spreadArray(["p", null, props.x,
" + ",
props.y,
" = ",
props.x + props.y,
_this.props.children); };
props.x + props.y], _this.props.children, false)); };
exports.MySFC = MySFC;
var MyClass = /** @class */ (function () {
function MyClass(props) {
this.props = props;
}
MyClass.prototype.render = function () {
return (0, renderer2_1.predom)("p", null,
this.props.x,
return renderer2_1.predom.apply(void 0, __spreadArray(["p", null, this.props.x,
" + ",
this.props.y,
" = ",
this.props.x + this.props.y,
this.props.children);
this.props.x + this.props.y], this.props.children, false));
};
return MyClass;
}());
Expand All @@ -124,6 +129,15 @@ exports.tree = (0, renderer2_1.predom)(exports.MySFC, { x: 1, y: 2 },
exports["default"] = (0, renderer2_1.predom)("h", null);
//// [index.js]
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
/** @jsx dom */
var renderer_1 = require("./renderer");
Expand All @@ -142,13 +156,11 @@ var DOMClass = /** @class */ (function () {
this.props = props;
}
DOMClass.prototype.render = function () {
return (0, renderer_1.dom)("p", null,
this.props.x,
return renderer_1.dom.apply(void 0, __spreadArray(["p", null, this.props.x,
" + ",
this.props.y,
" = ",
this.props.x + this.props.y,
this.props.children);
this.props.x + this.props.y], this.props.children, false));
};
return DOMClass;
}());
Expand Down
13 changes: 11 additions & 2 deletions tests/baselines/reference/tsxSpreadChildrenInvalidType.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@ var __assign = (this && this.__assign) || function () {
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
function Todo(prop) {
return React.createElement("div", null, prop.key.toString() + prop.todo);
}
function TodoList(_a) {
var todos = _a.todos;
return React.createElement("div", null, React.createElement(Todo, { key: todos[0].id, todo: todos[0].todo }));
return React.createElement.apply(React, __spreadArray(["div", null], React.createElement(Todo, { key: todos[0].id, todo: todos[0].todo }), false));
}
function TodoListNoError(_a) {
var todos = _a.todos;
// any is not checked
return React.createElement("div", null, React.createElement(Todo, { key: todos[0].id, todo: todos[0].todo }));
return React.createElement.apply(React, __spreadArray(["div", null], React.createElement(Todo, { key: todos[0].id, todo: todos[0].todo }), false));
}
var x;
React.createElement(TodoList, __assign({}, x));