Skip to content

Commit bb0ec3b

Browse files
Use an emit helper for JSX Spread Attributes.
1 parent 3acff6d commit bb0ec3b

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/compiler/binder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ namespace ts {
122122
let hasAsyncFunctions: boolean;
123123
let hasDecorators: boolean;
124124
let hasParameterDecorators: boolean;
125+
let hasJsxSpreadAttribute: boolean;
125126

126127
// If this file is an external module, then it is automatically in strict-mode according to
127128
// ES6. If it is not an external module, then we'll determine if it is in strict mode or
@@ -161,6 +162,7 @@ namespace ts {
161162
hasAsyncFunctions = false;
162163
hasDecorators = false;
163164
hasParameterDecorators = false;
165+
hasJsxSpreadAttribute = false;
164166
}
165167

166168
return bindSourceFile;
@@ -498,6 +500,9 @@ namespace ts {
498500
if (hasAsyncFunctions) {
499501
flags |= NodeFlags.HasAsyncFunctions;
500502
}
503+
if (hasJsxSpreadAttribute) {
504+
flags |= NodeFlags.HasJsxSpreadAttribute;
505+
}
501506
}
502507

503508
node.flags = flags;
@@ -1298,6 +1303,10 @@ namespace ts {
12981303
case SyntaxKind.EnumMember:
12991304
return bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes);
13001305

1306+
case SyntaxKind.JsxSpreadAttribute:
1307+
hasJsxSpreadAttribute = true;
1308+
return;
1309+
13011310
case SyntaxKind.CallSignature:
13021311
case SyntaxKind.ConstructSignature:
13031312
case SyntaxKind.IndexSignature:

src/compiler/emitter.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@ var __extends = (this && this.__extends) || function (d, b) {
345345
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
346346
};`;
347347

348+
const assignHelper = `
349+
var __assign = (this && this.__assign) || Object.assign || function(t) {
350+
for (var i = 1, n = arguments.length; i < n; i++) {
351+
var s = arguments[i];
352+
if (s != null) for (var p in s) if (s.hasOwnProperty(p)) t[p] = s[p];
353+
}
354+
return t;
355+
};`;
356+
348357
// emit output for the __decorate helper function
349358
const decorateHelper = `
350359
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -540,6 +549,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
540549
let convertedLoopState: ConvertedLoopState;
541550

542551
let extendsEmitted: boolean;
552+
let assignEmitted: boolean;
543553
let decorateEmitted: boolean;
544554
let paramEmitted: boolean;
545555
let awaiterEmitted: boolean;
@@ -623,6 +633,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
623633
decorateEmitted = false;
624634
paramEmitted = false;
625635
awaiterEmitted = false;
636+
assignEmitted = false;
626637
tempFlags = 0;
627638
tempVariables = undefined;
628639
tempParameters = undefined;
@@ -1259,11 +1270,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12591270
}
12601271
else {
12611272
// Either emit one big object literal (no spread attribs), or
1262-
// a call to React.__spread
1273+
// a call to the __assign helper
12631274
const attrs = openingNode.attributes;
12641275
if (forEach(attrs, attr => attr.kind === SyntaxKind.JsxSpreadAttribute)) {
1265-
emitExpressionIdentifier(syntheticReactRef);
1266-
write(".__spread(");
1276+
write("__assign(");
12671277

12681278
let haveOpenedObjectLiteral = false;
12691279
for (let i = 0; i < attrs.length; i++) {
@@ -7701,11 +7711,16 @@ const _super = (function (geti, seti) {
77017711
if (!compilerOptions.noEmitHelpers) {
77027712
// Only Emit __extends function when target ES5.
77037713
// For target ES6 and above, we can emit classDeclaration as is.
7704-
if ((languageVersion < ScriptTarget.ES6) && (!extendsEmitted && node.flags & NodeFlags.HasClassExtends)) {
7714+
if (languageVersion < ScriptTarget.ES6 && !extendsEmitted && node.flags & NodeFlags.HasClassExtends) {
77057715
writeLines(extendsHelper);
77067716
extendsEmitted = true;
77077717
}
77087718

7719+
if (compilerOptions.jsx !== JsxEmit.Preserve && !assignEmitted && (node.flags & NodeFlags.HasJsxSpreadAttribute)) {
7720+
writeLines(assignHelper);
7721+
assignEmitted = true;
7722+
}
7723+
77097724
if (!decorateEmitted && node.flags & NodeFlags.HasDecorators) {
77107725
writeLines(decorateHelper);
77117726
if (compilerOptions.emitDecoratorMetadata) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ namespace ts {
405405
JavaScriptFile = 1 << 27, // If node was parsed in a JavaScript
406406
ThisNodeOrAnySubNodesHasError = 1 << 28, // If this node or any of its children had an error
407407
HasAggregatedChildData = 1 << 29, // If we've computed data from children and cached it in this node
408+
HasJsxSpreadAttribute = 1 << 30,
408409

409410
Modifier = Export | Ambient | Public | Private | Protected | Static | Abstract | Default | Async,
410411
AccessibilityModifier = Public | Private | Protected,

0 commit comments

Comments
 (0)