Skip to content

Commit e9f984d

Browse files
Use an emit helper for JSX Spread Attributes.
1 parent 9ef7553 commit e9f984d

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

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;
@@ -1289,6 +1294,10 @@ namespace ts {
12891294
case SyntaxKind.EnumMember:
12901295
return bindPropertyOrMethodOrAccessor(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes);
12911296

1297+
case SyntaxKind.JsxSpreadAttribute:
1298+
hasJsxSpreadAttribute = true;
1299+
return;
1300+
12921301
case SyntaxKind.CallSignature:
12931302
case SyntaxKind.ConstructSignature:
12941303
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++) {
@@ -7610,11 +7620,16 @@ const _super = (function (geti, seti) {
76107620
if (!compilerOptions.noEmitHelpers) {
76117621
// Only Emit __extends function when target ES5.
76127622
// For target ES6 and above, we can emit classDeclaration as is.
7613-
if ((languageVersion < ScriptTarget.ES6) && (!extendsEmitted && node.flags & NodeFlags.HasClassExtends)) {
7623+
if (languageVersion < ScriptTarget.ES6 && !extendsEmitted && node.flags & NodeFlags.HasClassExtends) {
76147624
writeLines(extendsHelper);
76157625
extendsEmitted = true;
76167626
}
76177627

7628+
if (compilerOptions.jsx !== JsxEmit.Preserve && !assignEmitted && (node.flags & NodeFlags.HasJsxSpreadAttribute)) {
7629+
writeLines(assignHelper);
7630+
assignEmitted = true;
7631+
}
7632+
76187633
if (!decorateEmitted && node.flags & NodeFlags.HasDecorators) {
76197634
writeLines(decorateHelper);
76207635
if (compilerOptions.emitDecoratorMetadata) {

src/compiler/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ namespace ts {
369369
FirstNode = QualifiedName,
370370
}
371371

372-
export const enum NodeFlags {
372+
export const enum NodeFlags {
373373
None = 0,
374374
Export = 1 << 1, // Declarations
375375
Ambient = 1 << 2, // Declarations
@@ -397,6 +397,10 @@ namespace ts {
397397
HasParamDecorators = 1 << 24, // If the file has parameter decorators (initialized by binding)
398398
HasAsyncFunctions = 1 << 25, // If the file has async functions (initialized by binding)
399399

400+
// This was picked out from the 'master' branch.
401+
// To keep the flags consistent, we're skipping a few ahead.
402+
HasJsxSpreadAttribute = 1 << 30,
403+
400404
Modifier = Export | Ambient | Public | Private | Protected | Static | Abstract | Default | Async,
401405
AccessibilityModifier = Public | Private | Protected,
402406
BlockScoped = Let | Const,

0 commit comments

Comments
 (0)