Skip to content

Commit 5e2bd6b

Browse files
committed
Move System module transform to end.
1 parent 84dc99b commit 5e2bd6b

28 files changed

+1735
-1273
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,7 @@ namespace ts {
24852485
&& (leftKind === SyntaxKind.ObjectLiteralExpression
24862486
|| leftKind === SyntaxKind.ArrayLiteralExpression)) {
24872487
// Destructuring assignments are ES6 syntax.
2488-
transformFlags |= TransformFlags.AssertES2015 | TransformFlags.DestructuringAssignment;
2488+
transformFlags |= TransformFlags.AssertES2015 | TransformFlags.AssertDestructuringAssignment;
24892489
}
24902490
else if (operatorTokenKind === SyntaxKind.AsteriskAsteriskToken
24912491
|| operatorTokenKind === SyntaxKind.AsteriskAsteriskEqualsToken) {

src/compiler/core.ts

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,16 @@ namespace ts {
424424

425425
export function some<T>(array: T[], predicate?: (value: T) => boolean): boolean {
426426
if (array) {
427-
for (const v of array) {
428-
if (!predicate || predicate(v)) {
429-
return true;
427+
if (predicate) {
428+
for (const v of array) {
429+
if (predicate(v)) {
430+
return true;
431+
}
430432
}
431433
}
434+
else {
435+
return array.length > 0;
436+
}
432437
}
433438
return false;
434439
}
@@ -485,21 +490,35 @@ namespace ts {
485490
return result;
486491
}
487492

493+
/**
494+
* Appends a value to an array, returning the array.
495+
*
496+
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
497+
* is created if `value` was appended.
498+
* @param value The value to append to the array. If `value` is `undefined`, nothing is
499+
* appended.
500+
*/
488501
export function append<T>(to: T[] | undefined, value: T | undefined): T[] | undefined {
489502
if (value === undefined) return to;
490503
if (to === undefined) to = [];
491504
to.push(value);
492505
return to;
493506
}
494507

495-
export function addRange<T>(to: T[], from: T[]): void {
496-
if (to && from) {
497-
for (const v of from) {
498-
if (v !== undefined) {
499-
to.push(v);
500-
}
501-
}
508+
/**
509+
* Appends a range of value to an array, returning the array.
510+
*
511+
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
512+
* is created if `value` was appended.
513+
* @param from The values to append to the array. If `from` is `undefined`, nothing is
514+
* appended. If an element of `from` is `undefined`, that element is not appended.
515+
*/
516+
export function addRange<T>(to: T[] | undefined, from: T[] | undefined): T[] | undefined {
517+
if (from === undefined) return to;
518+
for (const v of from) {
519+
to = append(to, v);
502520
}
521+
return to;
503522
}
504523

505524
export function rangeEquals<T>(array1: T[], array2: T[], pos: number, end: number) {
@@ -512,33 +531,43 @@ namespace ts {
512531
return true;
513532
}
514533

534+
/**
535+
* Returns the first element of an array if non-empty, `undefined` otherwise.
536+
*/
515537
export function firstOrUndefined<T>(array: T[]): T {
516538
return array && array.length > 0
517539
? array[0]
518540
: undefined;
519541
}
520542

543+
/**
544+
* Returns the last element of an array if non-empty, `undefined` otherwise.
545+
*/
546+
export function lastOrUndefined<T>(array: T[]): T {
547+
return array && array.length > 0
548+
? array[array.length - 1]
549+
: undefined;
550+
}
551+
552+
/**
553+
* Returns the only element of an array if it contains only one element, `undefined` otherwise.
554+
*/
521555
export function singleOrUndefined<T>(array: T[]): T {
522556
return array && array.length === 1
523557
? array[0]
524558
: undefined;
525559
}
526560

561+
/**
562+
* Returns the only element of an array if it contains only one element; otheriwse, returns the
563+
* array.
564+
*/
527565
export function singleOrMany<T>(array: T[]): T | T[] {
528566
return array && array.length === 1
529567
? array[0]
530568
: array;
531569
}
532570

533-
/**
534-
* Returns the last element of an array if non-empty, undefined otherwise.
535-
*/
536-
export function lastOrUndefined<T>(array: T[]): T {
537-
return array && array.length > 0
538-
? array[array.length - 1]
539-
: undefined;
540-
}
541-
542571
/**
543572
* Performs a binary search, finding the index at which 'value' occurs in 'array'.
544573
* If no such index is found, returns the 2's-complement of first index at which

src/compiler/factory.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,17 @@ namespace ts {
14721472
return node;
14731473
}
14741474

1475+
/**
1476+
* Creates a synthetic element to act as a placeholder for the beginning of a merged declaration in
1477+
* order to properly emit exports.
1478+
*/
1479+
export function createMergeDeclarationMarker(original: Node) {
1480+
const node = <MergeDeclarationMarker>createNode(SyntaxKind.MergeDeclarationMarker);
1481+
node.emitNode = {};
1482+
node.original = original;
1483+
return node;
1484+
}
1485+
14751486
/**
14761487
* Creates a synthetic expression to act as a placeholder for a not-emitted expression in
14771488
* order to preserve comments or sourcemap positions.
@@ -2206,7 +2217,7 @@ namespace ts {
22062217
* Gets whether an identifier should only be referred to by its local name.
22072218
*/
22082219
export function isLocalName(node: Identifier) {
2209-
return (getEmitFlags(node) & EmitFlags.ExportBindingName) === EmitFlags.LocalName;
2220+
return (getEmitFlags(node) & EmitFlags.LocalName) !== 0;
22102221
}
22112222

22122223
/**
@@ -2228,32 +2239,7 @@ namespace ts {
22282239
* name points to an exported symbol.
22292240
*/
22302241
export function isExportName(node: Identifier) {
2231-
return (getEmitFlags(node) & EmitFlags.ExportBindingName) === EmitFlags.ExportName;
2232-
}
2233-
2234-
/**
2235-
* Gets the export binding name of a declaration for use in the left-hand side of assignment
2236-
* expressions. This is primarily used for declarations that can be referred to by name in the
2237-
* declaration's immediate scope (classes, enums, namespaces). If the declaration is exported
2238-
* and the name is the target of an assignment expression, its export binding name should be
2239-
* substituted with an expression that assigns *both* the local *and* export names of the
2240-
* declaration. If an export binding name appears in any other position it should be treated
2241-
* as a local name.
2242-
*
2243-
* @param node The declaration.
2244-
* @param allowComments A value indicating whether comments may be emitted for the name.
2245-
* @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2246-
*/
2247-
export function getExportBindingName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean): Identifier {
2248-
return getName(node, allowComments, allowSourceMaps, EmitFlags.ExportBindingName);
2249-
}
2250-
2251-
/**
2252-
* Gets whether an identifier should be treated as both an export name and a local name when
2253-
* it is the target of an assignment expression.
2254-
*/
2255-
export function isExportBindingName(node: Identifier) {
2256-
return (getEmitFlags(node) & EmitFlags.ExportBindingName) === EmitFlags.ExportBindingName;
2242+
return (getEmitFlags(node) & EmitFlags.ExportName) !== 0;
22572243
}
22582244

22592245
/**

src/compiler/transformer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ namespace ts {
112112

113113
transformers.push(transformTypeScript);
114114

115-
if (moduleKind === ModuleKind.System) {
116-
transformers.push(moduleTransformerMap[moduleKind] || moduleTransformerMap[ModuleKind.None]);
117-
}
118-
119115
if (jsx === JsxEmit.React) {
120116
transformers.push(transformJsx);
121117
}
@@ -133,10 +129,10 @@ namespace ts {
133129
transformers.push(transformGenerators);
134130
}
135131

136-
if (moduleKind !== ModuleKind.System) {
137-
transformers.push(moduleTransformerMap[moduleKind] || moduleTransformerMap[ModuleKind.None]);
138-
}
132+
transformers.push(moduleTransformerMap[moduleKind] || moduleTransformerMap[ModuleKind.None]);
139133

134+
// The ES5 transformer is last so that it can substitute expressions like `exports.default`
135+
// for ES3.
140136
if (languageVersion < ScriptTarget.ES5) {
141137
transformers.push(transformES5);
142138
}

src/compiler/transformers/destructuring.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,15 @@ namespace ts {
176176
*
177177
* @param node The VariableDeclaration to flatten.
178178
* @param recordTempVariable A callback used to record new temporary variables.
179-
* @param nameSubstitution An optional callback used to substitute binding names.
179+
* @param createAssignmentCallback An optional callback used to create assignment expressions
180+
* for non-temporary variables.
180181
* @param visitor An optional visitor to use to visit expressions.
181182
*/
182183
export function flattenVariableDestructuringToExpression(
183184
context: TransformationContext,
184185
node: VariableDeclaration,
185186
recordTempVariable: (name: Identifier) => void,
186-
nameSubstitution?: (name: Identifier) => Expression,
187+
createAssignmentCallback?: (name: Identifier, value: Expression, location?: TextRange) => Expression,
187188
visitor?: (node: Node) => VisitResult<Node>) {
188189

189190
const pendingAssignments: Expression[] = [];
@@ -195,26 +196,27 @@ namespace ts {
195196
return expression;
196197

197198
function emitAssignment(name: Identifier, value: Expression, location: TextRange, original: Node) {
198-
const left = nameSubstitution && nameSubstitution(name) || name;
199-
emitPendingAssignment(left, value, location, original);
199+
const expression = createAssignmentCallback
200+
? createAssignmentCallback(name, value, location)
201+
: createAssignment(name, value, location);
202+
203+
emitPendingAssignment(expression, original);
200204
}
201205

202206
function emitTempVariableAssignment(value: Expression, location: TextRange) {
203207
const name = createTempVariable(recordTempVariable);
204-
emitPendingAssignment(name, value, location, /*original*/ undefined);
208+
emitPendingAssignment(createAssignment(name, value, location), /*original*/ undefined);
205209
return name;
206210
}
207211

208-
function emitPendingAssignment(name: Expression, value: Expression, location: TextRange, original: Node) {
209-
const expression = createAssignment(name, value, location);
212+
function emitPendingAssignment(expression: Expression, original: Node) {
210213
expression.original = original;
211214

212215
// NOTE: this completely disables source maps, but aligns with the behavior of
213216
// `emitAssignment` in the old emitter.
214217
setEmitFlags(expression, EmitFlags.NoNestedSourceMaps);
215218

216219
pendingAssignments.push(expression);
217-
return expression;
218220
}
219221
}
220222

src/compiler/transformers/es2015.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ namespace ts {
585585
// }());
586586

587587
const variable = createVariableDeclaration(
588-
getDeclarationName(node, /*allowComments*/ true),
588+
getLocalName(node, /*allowComments*/ true),
589589
/*type*/ undefined,
590590
transformClassLikeDeclarationToExpression(node)
591591
);
@@ -601,14 +601,12 @@ namespace ts {
601601

602602
// Add an `export default` statement for default exports (for `--target es5 --module es6`)
603603
if (hasModifier(node, ModifierFlags.Export)) {
604-
if (hasModifier(node, ModifierFlags.Default)) {
605-
const exportStatement = createExportDefault(getLocalName(node));
606-
setOriginalNode(exportStatement, statement);
607-
statements.push(exportStatement);
608-
}
609-
else {
610-
statements.push(createExternalModuleExport(getLocalName(node)));
611-
}
604+
const exportStatement = hasModifier(node, ModifierFlags.Default)
605+
? createExportDefault(getLocalName(node))
606+
: createExternalModuleExport(getLocalName(node));
607+
608+
setOriginalNode(exportStatement, statement);
609+
statements.push(exportStatement);
612610
}
613611

614612
const emitFlags = getEmitFlags(node);
@@ -758,7 +756,7 @@ namespace ts {
758756
if (extendsClauseElement) {
759757
statements.push(
760758
createStatement(
761-
createExtendsHelper(currentSourceFile.externalHelpersModuleName, getDeclarationName(node)),
759+
createExtendsHelper(currentSourceFile.externalHelpersModuleName, getLocalName(node)),
762760
/*location*/ extendsClauseElement
763761
)
764762
);
@@ -1694,7 +1692,7 @@ namespace ts {
16941692
if (decl.initializer) {
16951693
let assignment: Expression;
16961694
if (isBindingPattern(decl.name)) {
1697-
assignment = flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, /*nameSubstitution*/ undefined, visitor);
1695+
assignment = flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, /*createAssignmentCallback*/ undefined, visitor);
16981696
}
16991697
else {
17001698
assignment = createBinary(<Identifier>decl.name, SyntaxKind.EqualsToken, visitNode(decl.initializer, visitor, isExpression));

0 commit comments

Comments
 (0)