Skip to content

Commit bcb180a

Browse files
committed
Fixes generated names and some formatting in system modules.
1 parent 284dacd commit bcb180a

19 files changed

Lines changed: 113 additions & 96 deletions

src/compiler/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,11 @@ namespace ts {
424424
return block;
425425
}
426426

427-
export function createVariableStatement(modifiers: Modifier[], declarationList: VariableDeclarationList, location?: TextRange): VariableStatement {
427+
export function createVariableStatement(modifiers: Modifier[], declarationList: VariableDeclarationList | VariableDeclaration[], location?: TextRange): VariableStatement {
428428
const node = <VariableStatement>createNode(SyntaxKind.VariableStatement, location);
429429
node.decorators = undefined;
430430
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
431-
node.declarationList = declarationList;
431+
node.declarationList = isArray(declarationList) ? createVariableDeclarationList(declarationList) : declarationList;
432432
return node;
433433
}
434434

src/compiler/printer.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,14 +2343,12 @@ const _super = (function (geti, seti) {
23432343
return node;
23442344
}
23452345

2346-
function getTextOfNode(node: Node, includeTrivia?: boolean) {
2347-
if (isIdentifier(node)) {
2348-
if (node.autoGenerateKind) {
2349-
return getGeneratedIdentifier(node);
2350-
}
2351-
else if (nodeIsSynthesized(node) || !node.parent) {
2352-
return unescapeIdentifier(node.text);
2353-
}
2346+
function getTextOfNode(node: Node, includeTrivia?: boolean): string {
2347+
if (isGeneratedIdentifier(node)) {
2348+
return getGeneratedIdentifier(node);
2349+
}
2350+
else if (isIdentifier(node) && (nodeIsSynthesized(node) || !node.parent)) {
2351+
return unescapeIdentifier(node.text);
23542352
}
23552353
else if (isLiteralExpression(node) && (nodeIsSynthesized(node) || !node.parent)) {
23562354
return node.text;
@@ -2452,7 +2450,7 @@ const _super = (function (geti, seti) {
24522450
}
24532451

24542452
function generateNameForModuleOrEnum(node: ModuleDeclaration | EnumDeclaration) {
2455-
const name = node.name.text;
2453+
const name = getTextOfNode(node.name);
24562454
// Use module/enum name itself if it is unique, otherwise make a unique variation
24572455
return isUniqueLocalName(name, node) ? name : makeUniqueName(name);
24582456
}
@@ -2472,10 +2470,10 @@ const _super = (function (geti, seti) {
24722470
return makeUniqueName("class");
24732471
}
24742472

2475-
function generateNameForNode(node: Node) {
2473+
function generateNameForNode(node: Node): string {
24762474
switch (node.kind) {
24772475
case SyntaxKind.Identifier:
2478-
return makeUniqueName((<Identifier>node).text);
2476+
return makeUniqueName(getTextOfNode(node));
24792477
case SyntaxKind.ModuleDeclaration:
24802478
case SyntaxKind.EnumDeclaration:
24812479
return generateNameForModuleOrEnum(<ModuleDeclaration | EnumDeclaration>node);
@@ -2502,14 +2500,37 @@ const _super = (function (geti, seti) {
25022500
case GeneratedIdentifierKind.Unique:
25032501
return makeUniqueName(node.text);
25042502
case GeneratedIdentifierKind.Node:
2505-
return generateNameForNode(getOriginalNode(node));
2503+
return generateNameForNode(getSourceNodeForGeneratedName(node));
25062504
}
25072505
}
25082506

25092507
function getGeneratedIdentifier(node: Identifier) {
2510-
const id = getOriginalNodeId(node);
2508+
const id = getNodeIdForGeneratedIdentifier(node);
25112509
return nodeToGeneratedName[id] || (nodeToGeneratedName[id] = unescapeIdentifier(generateIdentifier(node)));
25122510
}
2511+
2512+
function getSourceNodeForGeneratedName(name: Identifier) {
2513+
let node: Node = name;
2514+
while (node.original !== undefined) {
2515+
node = node.original;
2516+
if (isIdentifier(node) && node.autoGenerateKind === GeneratedIdentifierKind.Node) {
2517+
break;
2518+
}
2519+
}
2520+
2521+
return node;
2522+
}
2523+
2524+
function getNodeIdForGeneratedIdentifier(node: Identifier) {
2525+
switch (node.autoGenerateKind) {
2526+
case GeneratedIdentifierKind.Auto:
2527+
case GeneratedIdentifierKind.Loop:
2528+
case GeneratedIdentifierKind.Unique:
2529+
return getNodeId(node);
2530+
case GeneratedIdentifierKind.Node:
2531+
return getNodeId(getSourceNodeForGeneratedName(node));
2532+
}
2533+
}
25132534
}
25142535

25152536
function createDelimiterMap() {

src/compiler/transformers/destructuring.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,16 @@ namespace ts {
365365
ensureIdentifier(propertyName.expression, /*reuseIdentifierExpressions*/ false, /*location*/ propertyName, emitTempVariableAssignment)
366366
);
367367
}
368-
else if (isIdentifier(propertyName)) {
369-
return createPropertyAccess(
368+
else if (isLiteralExpression(propertyName)) {
369+
return createElementAccess(
370370
expression,
371-
propertyName.text
371+
getSynthesizedClone(propertyName)
372372
);
373373
}
374374
else {
375-
// We create a synthetic copy of the identifier in order to avoid the rewriting that might
376-
// otherwise occur when the identifier is emitted.
377-
return createElementAccess(
375+
return createPropertyAccess(
378376
expression,
379-
getSynthesizedClone(propertyName)
377+
isGeneratedIdentifier(propertyName) ? getSynthesizedClone(propertyName) : createIdentifier(propertyName.text)
380378
);
381379
}
382380
}

src/compiler/transformers/module/system.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ namespace ts {
339339
const setters: Expression[] = [];
340340
for (const group of dependencyGroups) {
341341
// derive a unique name for parameter from the first named entry in the group
342-
const parameterName = createUniqueName(forEach(group.externalImports, getLocalNameTextForExternalImport) || "");
342+
const localName = forEach(group.externalImports, getLocalNameForExternalImport);
343+
const parameterName = localName ? getGeneratedNameForNode(localName) : createUniqueName("");
343344
const statements: Statement[] = [];
344345
for (const entry of group.externalImports) {
345346
const importVariableName = getLocalNameForExternalImport(entry);
@@ -1121,11 +1122,6 @@ namespace ts {
11211122
return undefined;
11221123
}
11231124

1124-
function getLocalNameTextForExternalImport(node: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration): string {
1125-
const name = getLocalNameForExternalImport(node);
1126-
return name ? name.text : undefined;
1127-
}
1128-
11291125
function getLocalNameForExternalImport(node: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration): Identifier {
11301126
const namespaceDeclaration = getNamespaceDeclarationNode(node);
11311127
if (namespaceDeclaration && !isDefaultImport(node)) {
@@ -1182,14 +1178,17 @@ namespace ts {
11821178
]),
11831179
m,
11841180
createBlock([
1185-
createIf(
1186-
condition,
1187-
createStatement(
1188-
createAssignment(
1189-
createElementAccess(exports, n),
1190-
createElementAccess(m, n)
1181+
setNodeEmitFlags(
1182+
createIf(
1183+
condition,
1184+
createStatement(
1185+
createAssignment(
1186+
createElementAccess(exports, n),
1187+
createElementAccess(m, n)
1188+
)
11911189
)
1192-
)
1190+
),
1191+
NodeEmitFlags.SingleLine
11931192
)
11941193
])
11951194
),

src/compiler/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ namespace ts {
480480
// @kind(SyntaxKind.StaticKeyword)
481481
export interface Modifier extends Node { }
482482

483+
/*@internal*/
483484
export const enum GeneratedIdentifierKind {
484485
None, // Not automatically generated.
485486
Auto, // Automatically generated identifier.
@@ -490,9 +491,9 @@ namespace ts {
490491

491492
// @kind(SyntaxKind.Identifier)
492493
export interface Identifier extends PrimaryExpression {
493-
text: string; // Text of identifier (with escapes converted to characters)
494-
originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
495-
autoGenerateKind?: GeneratedIdentifierKind; // Specifies whether to auto-generate the text for an identifier.
494+
text: string; // Text of identifier (with escapes converted to characters)
495+
originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
496+
/*@internal*/ autoGenerateKind?: GeneratedIdentifierKind; // Specifies whether to auto-generate the text for an identifier.
496497
}
497498

498499
// @kind(SyntaxKind.QualifiedName)

tests/baselines/reference/aliasesInSystemModule2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ System.register(["foo"], function (exports_1, context_1) {
2222
var foo_1, cls, cls2, x, y, z, M;
2323
return {
2424
setters: [
25-
function (_1) {
26-
foo_1 = _1;
25+
function (foo_1_1) {
26+
foo_1 = foo_1_1;
2727
}
2828
],
2929
execute: function () {

tests/baselines/reference/allowSyntheticDefaultImports2.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,35 @@ export class Foo {
1010
}
1111

1212
//// [b.js]
13-
System.register([], function(exports_1, context_1) {
13+
System.register([], function (exports_1, context_1) {
1414
"use strict";
1515
var __moduleName = context_1 && context_1.id;
1616
var Foo;
1717
return {
18-
setters:[],
19-
execute: function() {
18+
setters: [],
19+
execute: function () {
2020
Foo = (function () {
2121
function Foo() {
2222
}
2323
return Foo;
2424
}());
2525
exports_1("Foo", Foo);
2626
}
27-
}
27+
};
2828
});
2929
//// [a.js]
30-
System.register(["./b"], function(exports_1, context_1) {
30+
System.register(["./b"], function (exports_1, context_1) {
3131
"use strict";
3232
var __moduleName = context_1 && context_1.id;
33-
var b_1;
34-
var x;
33+
var b_1, x;
3534
return {
36-
setters:[
35+
setters: [
3736
function (b_1_1) {
3837
b_1 = b_1_1;
39-
}],
40-
execute: function() {
38+
}
39+
],
40+
execute: function () {
4141
exports_1("x", x = new b_1["default"].Foo());
4242
}
43-
}
43+
};
4444
});

tests/baselines/reference/allowSyntheticDefaultImports3.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ export class Foo {
1111

1212

1313
//// [b.js]
14-
System.register([], function(exports_1, context_1) {
14+
System.register([], function (exports_1, context_1) {
1515
"use strict";
1616
var __moduleName = context_1 && context_1.id;
1717
var Foo;
1818
return {
19-
setters:[],
20-
execute: function() {
19+
setters: [],
20+
execute: function () {
2121
Foo = (function () {
2222
function Foo() {
2323
}
2424
return Foo;
2525
}());
2626
exports_1("Foo", Foo);
2727
}
28-
}
28+
};
2929
});
3030
//// [a.js]
31-
System.register(["./b"], function(exports_1, context_1) {
31+
System.register(["./b"], function (exports_1, context_1) {
3232
"use strict";
3333
var __moduleName = context_1 && context_1.id;
34-
var b_1;
35-
var x;
34+
var b_1, x;
3635
return {
37-
setters:[
36+
setters: [
3837
function (b_1_1) {
3938
b_1 = b_1_1;
40-
}],
41-
execute: function() {
39+
}
40+
],
41+
execute: function () {
4242
exports_1("x", x = new b_1["default"].Foo());
4343
}
44-
}
44+
};
4545
});

tests/baselines/reference/allowSyntheticDefaultImports5.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ export var x = new Foo();
1212

1313

1414
//// [a.js]
15-
System.register(["./b"], function(exports_1, context_1) {
15+
System.register(["./b"], function (exports_1, context_1) {
1616
"use strict";
1717
var __moduleName = context_1 && context_1.id;
18-
var b_1;
19-
var x;
18+
var b_1, x;
2019
return {
21-
setters:[
20+
setters: [
2221
function (b_1_1) {
2322
b_1 = b_1_1;
24-
}],
25-
execute: function() {
23+
}
24+
],
25+
execute: function () {
2626
exports_1("x", x = new b_1["default"]());
2727
}
28-
}
28+
};
2929
});

tests/baselines/reference/allowSyntheticDefaultImports6.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ export var x = new Foo();
1212

1313

1414
//// [a.js]
15-
System.register(["./b"], function(exports_1, context_1) {
15+
System.register(["./b"], function (exports_1, context_1) {
1616
"use strict";
1717
var __moduleName = context_1 && context_1.id;
18-
var b_1;
19-
var x;
18+
var b_1, x;
2019
return {
21-
setters:[
20+
setters: [
2221
function (b_1_1) {
2322
b_1 = b_1_1;
24-
}],
25-
execute: function() {
23+
}
24+
],
25+
execute: function () {
2626
exports_1("x", x = new b_1["default"]());
2727
}
28-
}
28+
};
2929
});

0 commit comments

Comments
 (0)