Skip to content

Commit c1505a0

Browse files
committed
Added RawExpression to move the UMD helper out of the emitter
1 parent 83f5d3f commit c1505a0

File tree

5 files changed

+66
-45
lines changed

5 files changed

+66
-45
lines changed

src/compiler/emitter.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ namespace ts {
2020
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile, emitOnlyDtsFiles?: boolean): EmitResult {
2121
const delimiters = createDelimiterMap();
2222
const brackets = createBracketsMap();
23-
24-
// emit output for the UMD helper function.
25-
const umdHelper = `
26-
(function (dependencies, factory) {
27-
if (typeof module === 'object' && typeof module.exports === 'object') {
28-
var v = factory(require, exports); if (v !== undefined) module.exports = v;
29-
}
30-
else if (typeof define === 'function' && define.amd) {
31-
define(dependencies, factory);
32-
}
33-
})`;
34-
3523
const compilerOptions = host.getCompilerOptions();
3624
const languageVersion = getEmitScriptTarget(compilerOptions);
3725
const moduleKind = getEmitModuleKind(compilerOptions);
@@ -674,6 +662,8 @@ namespace ts {
674662
// Transformation nodes
675663
case SyntaxKind.PartiallyEmittedExpression:
676664
return emitPartiallyEmittedExpression(<PartiallyEmittedExpression>node);
665+
case SyntaxKind.RawExpression:
666+
return writeLines((<RawExpression>node).text);
677667
}
678668
}
679669

@@ -711,12 +701,7 @@ namespace ts {
711701
//
712702

713703
function emitIdentifier(node: Identifier) {
714-
if (getEmitFlags(node) & EmitFlags.UMDDefine) {
715-
writeLines(umdHelper);
716-
}
717-
else {
718-
write(getTextOfNode(node, /*includeTrivia*/ false));
719-
}
704+
write(getTextOfNode(node, /*includeTrivia*/ false));
720705
}
721706

722707
//

src/compiler/factory.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,19 @@ namespace ts {
15041504
return node;
15051505
}
15061506

1507+
/**
1508+
* Creates a node that emits a string of raw text in an expression position. Raw text is never
1509+
* transformed, should be ES3 compliant, and should have the same precedence as
1510+
* PrimaryExpression.
1511+
*
1512+
* @param text The raw text of the node.
1513+
*/
1514+
export function createRawExpression(text: string) {
1515+
const node = <RawExpression>createNode(SyntaxKind.RawExpression);
1516+
node.text = text;
1517+
return node;
1518+
}
1519+
15071520
// Compound nodes
15081521

15091522
export function createComma(left: Expression, right: Expression) {
@@ -1621,7 +1634,7 @@ namespace ts {
16211634
// flag and setting a parent node.
16221635
const react = createIdentifier(reactNamespace || "React");
16231636
react.flags &= ~NodeFlags.Synthesized;
1624-
// Set the parent that is in parse tree
1637+
// Set the parent that is in parse tree
16251638
// this makes sure that parent chain is intact for checker to traverse complete scope tree
16261639
react.parent = getParseTreeNode(parent);
16271640
return react;

src/compiler/transformers/module/module.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ namespace ts {
112112
* @param node The SourceFile node.
113113
*/
114114
function transformUMDModule(node: SourceFile) {
115-
const define = createIdentifier("define");
116-
setEmitFlags(define, EmitFlags.UMDDefine);
115+
const define = createRawExpression(umdHelper);
117116
return transformAsynchronousModule(node, define, /*moduleName*/ undefined, /*includeNonAmdDependencies*/ false);
118117
}
119118

@@ -1324,4 +1323,15 @@ namespace ts {
13241323
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
13251324
}`
13261325
};
1326+
1327+
// emit output for the UMD helper function.
1328+
const umdHelper = `
1329+
(function (dependencies, factory) {
1330+
if (typeof module === 'object' && typeof module.exports === 'object') {
1331+
var v = factory(require, exports); if (v !== undefined) module.exports = v;
1332+
}
1333+
else if (typeof define === 'function' && define.amd) {
1334+
define(dependencies, factory);
1335+
}
1336+
})`;
13271337
}

src/compiler/types.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ namespace ts {
364364
PartiallyEmittedExpression,
365365
MergeDeclarationMarker,
366366
EndOfDeclarationMarker,
367+
RawExpression,
367368

368369
// Enum value count
369370
Count,
@@ -423,7 +424,7 @@ namespace ts {
423424
ThisNodeHasError = 1 << 19, // If the parser encountered an error when parsing the code that created this node
424425
JavaScriptFile = 1 << 20, // If node was parsed in a JavaScript
425426
ThisNodeOrAnySubNodesHasError = 1 << 21, // If this node or any of its children had an error
426-
HasAggregatedChildData = 1 << 22, // If we've computed data from children and cached it in this node
427+
HasAggregatedChildData = 1 << 22, // If we've computed data from children and cached it in this node
427428

428429
BlockScoped = Let | Const,
429430

@@ -1453,6 +1454,16 @@ namespace ts {
14531454
kind: SyntaxKind.EndOfDeclarationMarker;
14541455
}
14551456

1457+
/**
1458+
* Emits a string of raw text in an expression position. Raw text is never transformed, should
1459+
* be ES3 compliant, and should have the same precedence as PrimaryExpression.
1460+
*/
1461+
/* @internal */
1462+
export interface RawExpression extends PrimaryExpression {
1463+
kind: SyntaxKind.RawExpression;
1464+
text: string;
1465+
}
1466+
14561467
/**
14571468
* Marks the beginning of a merged transformed declaration.
14581469
*/
@@ -3486,31 +3497,30 @@ namespace ts {
34863497

34873498
/* @internal */
34883499
export const enum EmitFlags {
3489-
UMDDefine = 1 << 4, // This node should be replaced with the UMD define helper.
3490-
SingleLine = 1 << 5, // The contents of this node should be emitted on a single line.
3491-
AdviseOnEmitNode = 1 << 6, // The printer should invoke the onEmitNode callback when printing this node.
3492-
NoSubstitution = 1 << 7, // Disables further substitution of an expression.
3493-
CapturesThis = 1 << 8, // The function captures a lexical `this`
3494-
NoLeadingSourceMap = 1 << 9, // Do not emit a leading source map location for this node.
3495-
NoTrailingSourceMap = 1 << 10, // Do not emit a trailing source map location for this node.
3500+
SingleLine = 1 << 0, // The contents of this node should be emitted on a single line.
3501+
AdviseOnEmitNode = 1 << 1, // The printer should invoke the onEmitNode callback when printing this node.
3502+
NoSubstitution = 1 << 2, // Disables further substitution of an expression.
3503+
CapturesThis = 1 << 3, // The function captures a lexical `this`
3504+
NoLeadingSourceMap = 1 << 4, // Do not emit a leading source map location for this node.
3505+
NoTrailingSourceMap = 1 << 5, // Do not emit a trailing source map location for this node.
34963506
NoSourceMap = NoLeadingSourceMap | NoTrailingSourceMap, // Do not emit a source map location for this node.
3497-
NoNestedSourceMaps = 1 << 11, // Do not emit source map locations for children of this node.
3498-
NoTokenLeadingSourceMaps = 1 << 12, // Do not emit leading source map location for token nodes.
3499-
NoTokenTrailingSourceMaps = 1 << 13, // Do not emit trailing source map location for token nodes.
3507+
NoNestedSourceMaps = 1 << 6, // Do not emit source map locations for children of this node.
3508+
NoTokenLeadingSourceMaps = 1 << 7, // Do not emit leading source map location for token nodes.
3509+
NoTokenTrailingSourceMaps = 1 << 8, // Do not emit trailing source map location for token nodes.
35003510
NoTokenSourceMaps = NoTokenLeadingSourceMaps | NoTokenTrailingSourceMaps, // Do not emit source map locations for tokens of this node.
3501-
NoLeadingComments = 1 << 14, // Do not emit leading comments for this node.
3502-
NoTrailingComments = 1 << 15, // Do not emit trailing comments for this node.
3511+
NoLeadingComments = 1 << 9, // Do not emit leading comments for this node.
3512+
NoTrailingComments = 1 << 10, // Do not emit trailing comments for this node.
35033513
NoComments = NoLeadingComments | NoTrailingComments, // Do not emit comments for this node.
3504-
NoNestedComments = 1 << 16,
3505-
ExportName = 1 << 17, // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal).
3506-
LocalName = 1 << 18, // Ensure an export prefix is not added for an identifier that points to an exported declaration.
3507-
Indented = 1 << 19, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
3508-
NoIndentation = 1 << 20, // Do not indent the node.
3509-
AsyncFunctionBody = 1 << 21,
3510-
ReuseTempVariableScope = 1 << 22, // Reuse the existing temp variable scope during emit.
3511-
CustomPrologue = 1 << 23, // Treat the statement as if it were a prologue directive (NOTE: Prologue directives are *not* transformed).
3512-
NoHoisting = 1 << 24, // Do not hoist this declaration in --module system
3513-
HasEndOfDeclarationMarker = 1 << 25, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
3514+
NoNestedComments = 1 << 11,
3515+
ExportName = 1 << 12, // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal).
3516+
LocalName = 1 << 13, // Ensure an export prefix is not added for an identifier that points to an exported declaration.
3517+
Indented = 1 << 14, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
3518+
NoIndentation = 1 << 15, // Do not indent the node.
3519+
AsyncFunctionBody = 1 << 16,
3520+
ReuseTempVariableScope = 1 << 17, // Reuse the existing temp variable scope during emit.
3521+
CustomPrologue = 1 << 18, // Treat the statement as if it were a prologue directive (NOTE: Prologue directives are *not* transformed).
3522+
NoHoisting = 1 << 19, // Do not hoist this declaration in --module system
3523+
HasEndOfDeclarationMarker = 1 << 20, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
35143524
}
35153525

35163526
/* @internal */

src/compiler/utilities.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,7 @@ namespace ts {
20872087
case SyntaxKind.TemplateExpression:
20882088
case SyntaxKind.ParenthesizedExpression:
20892089
case SyntaxKind.OmittedExpression:
2090+
case SyntaxKind.RawExpression:
20902091
return 19;
20912092

20922093
case SyntaxKind.TaggedTemplateExpression:
@@ -3796,7 +3797,8 @@ namespace ts {
37963797
|| kind === SyntaxKind.ThisKeyword
37973798
|| kind === SyntaxKind.TrueKeyword
37983799
|| kind === SyntaxKind.SuperKeyword
3799-
|| kind === SyntaxKind.NonNullExpression;
3800+
|| kind === SyntaxKind.NonNullExpression
3801+
|| kind === SyntaxKind.RawExpression;
38003802
}
38013803

38023804
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
@@ -3826,6 +3828,7 @@ namespace ts {
38263828
|| kind === SyntaxKind.SpreadElementExpression
38273829
|| kind === SyntaxKind.AsExpression
38283830
|| kind === SyntaxKind.OmittedExpression
3831+
|| kind === SyntaxKind.RawExpression
38293832
|| isUnaryExpressionKind(kind);
38303833
}
38313834

0 commit comments

Comments
 (0)