Skip to content

Commit 4fa8118

Browse files
authored
Merge pull request #13188 from Microsoft/fix13147
Fix UMD header to work with r.js
2 parents 11dd368 + 3b114f4 commit 4fa8118

24 files changed

+298
-207
lines changed

src/compiler/emitter.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,6 @@ namespace ts {
670670
// Transformation nodes
671671
case SyntaxKind.PartiallyEmittedExpression:
672672
return emitPartiallyEmittedExpression(<PartiallyEmittedExpression>node);
673-
case SyntaxKind.RawExpression:
674-
return writeLines((<RawExpression>node).text);
675673
}
676674
}
677675

src/compiler/factory.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,19 +1530,6 @@ namespace ts {
15301530
return node;
15311531
}
15321532

1533-
/**
1534-
* Creates a node that emits a string of raw text in an expression position. Raw text is never
1535-
* transformed, should be ES3 compliant, and should have the same precedence as
1536-
* PrimaryExpression.
1537-
*
1538-
* @param text The raw text of the node.
1539-
*/
1540-
export function createRawExpression(text: string) {
1541-
const node = <RawExpression>createNode(SyntaxKind.RawExpression);
1542-
node.text = text;
1543-
return node;
1544-
}
1545-
15461533
// Compound nodes
15471534

15481535
export function createComma(left: Expression, right: Expression) {

src/compiler/transformers/module/module.ts

Lines changed: 132 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,7 @@ namespace ts {
102102
function transformAMDModule(node: SourceFile) {
103103
const define = createIdentifier("define");
104104
const moduleName = tryGetModuleNameFromFile(node, host, compilerOptions);
105-
return transformAsynchronousModule(node, define, moduleName, /*includeNonAmdDependencies*/ true);
106-
}
107105

108-
/**
109-
* Transforms a SourceFile into a UMD module.
110-
*
111-
* @param node The SourceFile node.
112-
*/
113-
function transformUMDModule(node: SourceFile) {
114-
const define = createRawExpression(umdHelper);
115-
return transformAsynchronousModule(node, define, /*moduleName*/ undefined, /*includeNonAmdDependencies*/ false);
116-
}
117-
118-
/**
119-
* Transforms a SourceFile into an AMD or UMD module.
120-
*
121-
* @param node The SourceFile node.
122-
* @param define The expression used to define the module.
123-
* @param moduleName An expression for the module name, if available.
124-
* @param includeNonAmdDependencies A value indicating whether to incldue any non-AMD dependencies.
125-
*/
126-
function transformAsynchronousModule(node: SourceFile, define: Expression, moduleName: Expression, includeNonAmdDependencies: boolean) {
127106
// An AMD define function has the following shape:
128107
//
129108
// define(id?, dependencies?, factory);
@@ -145,7 +124,7 @@ namespace ts {
145124
//
146125
// we need to add modules without alias names to the end of the dependencies list
147126

148-
const { aliasedModuleNames, unaliasedModuleNames, importAliasNames } = collectAsynchronousDependencies(node, includeNonAmdDependencies);
127+
const { aliasedModuleNames, unaliasedModuleNames, importAliasNames } = collectAsynchronousDependencies(node, /*includeNonAmdDependencies*/ true);
149128

150129
// Create an updated SourceFile:
151130
//
@@ -194,6 +173,137 @@ namespace ts {
194173
);
195174
}
196175

176+
/**
177+
* Transforms a SourceFile into a UMD module.
178+
*
179+
* @param node The SourceFile node.
180+
*/
181+
function transformUMDModule(node: SourceFile) {
182+
const { aliasedModuleNames, unaliasedModuleNames, importAliasNames } = collectAsynchronousDependencies(node, /*includeNonAmdDependencies*/ false);
183+
const umdHeader = createFunctionExpression(
184+
/*modifiers*/ undefined,
185+
/*asteriskToken*/ undefined,
186+
/*name*/ undefined,
187+
/*typeParameters*/ undefined,
188+
[createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, "factory")],
189+
/*type*/ undefined,
190+
createBlock(
191+
[
192+
createIf(
193+
createLogicalAnd(
194+
createTypeCheck(createIdentifier("module"), "object"),
195+
createTypeCheck(createPropertyAccess(createIdentifier("module"), "exports"), "object")
196+
),
197+
createBlock([
198+
createVariableStatement(
199+
/*modifiers*/ undefined,
200+
[
201+
createVariableDeclaration(
202+
"v",
203+
/*type*/ undefined,
204+
createCall(
205+
createIdentifier("factory"),
206+
/*typeArguments*/ undefined,
207+
[
208+
createIdentifier("require"),
209+
createIdentifier("exports")
210+
]
211+
)
212+
)
213+
]
214+
),
215+
setEmitFlags(
216+
createIf(
217+
createStrictInequality(
218+
createIdentifier("v"),
219+
createIdentifier("undefined")
220+
),
221+
createStatement(
222+
createAssignment(
223+
createPropertyAccess(createIdentifier("module"), "exports"),
224+
createIdentifier("v")
225+
)
226+
)
227+
),
228+
EmitFlags.SingleLine
229+
)
230+
]),
231+
createIf(
232+
createLogicalAnd(
233+
createTypeCheck(createIdentifier("define"), "function"),
234+
createPropertyAccess(createIdentifier("define"), "amd")
235+
),
236+
createBlock([
237+
createStatement(
238+
createCall(
239+
createIdentifier("define"),
240+
/*typeArguments*/ undefined,
241+
[
242+
createArrayLiteral([
243+
createLiteral("require"),
244+
createLiteral("exports"),
245+
...aliasedModuleNames,
246+
...unaliasedModuleNames
247+
]),
248+
createIdentifier("factory")
249+
]
250+
)
251+
)
252+
])
253+
)
254+
)
255+
],
256+
/*location*/ undefined,
257+
/*multiLine*/ true
258+
)
259+
);
260+
261+
// Create an updated SourceFile:
262+
//
263+
// (function (factory) {
264+
// if (typeof module === "object" && typeof module.exports === "object") {
265+
// var v = factory(require, exports);
266+
// if (v !== undefined) module.exports = v;
267+
// }
268+
// else if (typeof define === 'function' && define.amd) {
269+
// define(["require", "exports"], factory);
270+
// }
271+
// })(function ...)
272+
273+
return updateSourceFileNode(
274+
node,
275+
createNodeArray(
276+
[
277+
createStatement(
278+
createCall(
279+
umdHeader,
280+
/*typeArguments*/ undefined,
281+
[
282+
// Add the module body function argument:
283+
//
284+
// function (require, exports) ...
285+
createFunctionExpression(
286+
/*modifiers*/ undefined,
287+
/*asteriskToken*/ undefined,
288+
/*name*/ undefined,
289+
/*typeParameters*/ undefined,
290+
[
291+
createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, "require"),
292+
createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, "exports"),
293+
...importAliasNames
294+
],
295+
/*type*/ undefined,
296+
transformAsynchronousModuleBody(node)
297+
)
298+
]
299+
)
300+
)
301+
],
302+
/*location*/ node.statements
303+
)
304+
);
305+
}
306+
197307
/**
198308
* Collect the additional asynchronous dependencies for the module.
199309
*
@@ -1333,15 +1443,4 @@ namespace ts {
13331443
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
13341444
}`
13351445
};
1336-
1337-
// emit output for the UMD helper function.
1338-
const umdHelper = `
1339-
(function (dependencies, factory) {
1340-
if (typeof module === 'object' && typeof module.exports === 'object') {
1341-
var v = factory(require, exports); if (v !== undefined) module.exports = v;
1342-
}
1343-
else if (typeof define === 'function' && define.amd) {
1344-
define(dependencies, factory);
1345-
}
1346-
})`;
13471446
}

src/compiler/types.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ namespace ts {
370370
PartiallyEmittedExpression,
371371
MergeDeclarationMarker,
372372
EndOfDeclarationMarker,
373-
RawExpression,
374373

375374
// Enum value count
376375
Count,
@@ -1521,16 +1520,6 @@ namespace ts {
15211520
kind: SyntaxKind.EndOfDeclarationMarker;
15221521
}
15231522

1524-
/**
1525-
* Emits a string of raw text in an expression position. Raw text is never transformed, should
1526-
* be ES3 compliant, and should have the same precedence as PrimaryExpression.
1527-
*/
1528-
/* @internal */
1529-
export interface RawExpression extends PrimaryExpression {
1530-
kind: SyntaxKind.RawExpression;
1531-
text: string;
1532-
}
1533-
15341523
/**
15351524
* Marks the beginning of a merged transformed declaration.
15361525
*/

src/compiler/utilities.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,6 @@ namespace ts {
21412141
case SyntaxKind.TemplateExpression:
21422142
case SyntaxKind.ParenthesizedExpression:
21432143
case SyntaxKind.OmittedExpression:
2144-
case SyntaxKind.RawExpression:
21452144
return 19;
21462145

21472146
case SyntaxKind.TaggedTemplateExpression:
@@ -2365,13 +2364,11 @@ namespace ts {
23652364
* Note that this doesn't actually wrap the input in double quotes.
23662365
*/
23672366
export function escapeString(s: string): string {
2368-
s = escapedCharsRegExp.test(s) ? s.replace(escapedCharsRegExp, getReplacement) : s;
2369-
2370-
return s;
2367+
return s.replace(escapedCharsRegExp, getReplacement);
2368+
}
23712369

2372-
function getReplacement(c: string) {
2373-
return escapedCharsMap[c] || get16BitUnicodeEscapeSequence(c.charCodeAt(0));
2374-
}
2370+
function getReplacement(c: string) {
2371+
return escapedCharsMap[c] || get16BitUnicodeEscapeSequence(c.charCodeAt(0));
23752372
}
23762373

23772374
export function isIntrinsicJsxName(name: string) {
@@ -3888,8 +3885,7 @@ namespace ts {
38883885
|| kind === SyntaxKind.ThisKeyword
38893886
|| kind === SyntaxKind.TrueKeyword
38903887
|| kind === SyntaxKind.SuperKeyword
3891-
|| kind === SyntaxKind.NonNullExpression
3892-
|| kind === SyntaxKind.RawExpression;
3888+
|| kind === SyntaxKind.NonNullExpression;
38933889
}
38943890

38953891
export function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression {
@@ -3919,7 +3915,6 @@ namespace ts {
39193915
|| kind === SyntaxKind.SpreadElement
39203916
|| kind === SyntaxKind.AsExpression
39213917
|| kind === SyntaxKind.OmittedExpression
3922-
|| kind === SyntaxKind.RawExpression
39233918
|| isUnaryExpressionKind(kind);
39243919
}
39253920

tests/baselines/reference/anonymousDefaultExportsUmd.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@ export default class {}
77
export default function() {}
88

99
//// [a.js]
10-
(function (dependencies, factory) {
11-
if (typeof module === 'object' && typeof module.exports === 'object') {
12-
var v = factory(require, exports); if (v !== undefined) module.exports = v;
10+
(function (factory) {
11+
if (typeof module === "object" && typeof module.exports === "object") {
12+
var v = factory(require, exports);
13+
if (v !== undefined) module.exports = v;
1314
}
14-
else if (typeof define === 'function' && define.amd) {
15-
define(dependencies, factory);
15+
else if (typeof define === "function" && define.amd) {
16+
define(["require", "exports"], factory);
1617
}
17-
})(["require", "exports"], function (require, exports) {
18+
})(function (require, exports) {
1819
"use strict";
1920
class default_1 {
2021
}
2122
Object.defineProperty(exports, "__esModule", { value: true });
2223
exports.default = default_1;
2324
});
2425
//// [b.js]
25-
(function (dependencies, factory) {
26-
if (typeof module === 'object' && typeof module.exports === 'object') {
27-
var v = factory(require, exports); if (v !== undefined) module.exports = v;
26+
(function (factory) {
27+
if (typeof module === "object" && typeof module.exports === "object") {
28+
var v = factory(require, exports);
29+
if (v !== undefined) module.exports = v;
2830
}
29-
else if (typeof define === 'function' && define.amd) {
30-
define(dependencies, factory);
31+
else if (typeof define === "function" && define.amd) {
32+
define(["require", "exports"], factory);
3133
}
32-
})(["require", "exports"], function (require, exports) {
34+
})(function (require, exports) {
3335
"use strict";
3436
function default_1() { }
3537
Object.defineProperty(exports, "__esModule", { value: true });

tests/baselines/reference/decoratedDefaultExportsGetExportedUmd.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
2020
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2121
return c > 3 && r && Object.defineProperty(target, key, r), r;
2222
};
23-
(function (dependencies, factory) {
24-
if (typeof module === 'object' && typeof module.exports === 'object') {
25-
var v = factory(require, exports); if (v !== undefined) module.exports = v;
23+
(function (factory) {
24+
if (typeof module === "object" && typeof module.exports === "object") {
25+
var v = factory(require, exports);
26+
if (v !== undefined) module.exports = v;
2627
}
27-
else if (typeof define === 'function' && define.amd) {
28-
define(dependencies, factory);
28+
else if (typeof define === "function" && define.amd) {
29+
define(["require", "exports"], factory);
2930
}
30-
})(["require", "exports"], function (require, exports) {
31+
})(function (require, exports) {
3132
"use strict";
3233
var decorator;
3334
let Foo = class Foo {
@@ -45,14 +46,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4546
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
4647
return c > 3 && r && Object.defineProperty(target, key, r), r;
4748
};
48-
(function (dependencies, factory) {
49-
if (typeof module === 'object' && typeof module.exports === 'object') {
50-
var v = factory(require, exports); if (v !== undefined) module.exports = v;
49+
(function (factory) {
50+
if (typeof module === "object" && typeof module.exports === "object") {
51+
var v = factory(require, exports);
52+
if (v !== undefined) module.exports = v;
5153
}
52-
else if (typeof define === 'function' && define.amd) {
53-
define(dependencies, factory);
54+
else if (typeof define === "function" && define.amd) {
55+
define(["require", "exports"], factory);
5456
}
55-
})(["require", "exports"], function (require, exports) {
57+
})(function (require, exports) {
5658
"use strict";
5759
var decorator;
5860
let default_1 = class {

0 commit comments

Comments
 (0)