Skip to content

Commit b60cf99

Browse files
committed
Fixed minor difference in string literal emit for AMD modules
1 parent e78b64b commit b60cf99

11 files changed

Lines changed: 41 additions & 15 deletions

src/compiler/factory.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ namespace ts {
114114

115115
// Literals
116116

117+
export function createLiteral(textSource: StringLiteral | Identifier, location?: TextRange): StringLiteral;
117118
export function createLiteral(value: string, location?: TextRange): StringLiteral;
118119
export function createLiteral(value: number, location?: TextRange): LiteralExpression;
119120
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression;
120-
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression {
121+
export function createLiteral(value: string | number | boolean | StringLiteral | Identifier, location?: TextRange): PrimaryExpression {
121122
if (typeof value === "number") {
122123
const node = <LiteralExpression>createNode(SyntaxKind.NumericLiteral, location);
123124
node.text = value.toString();
@@ -126,9 +127,15 @@ namespace ts {
126127
else if (typeof value === "boolean") {
127128
return <PrimaryExpression>createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword, location);
128129
}
130+
else if (typeof value === "string") {
131+
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location);
132+
node.text = value;
133+
return node;
134+
}
129135
else {
130136
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location);
131-
node.text = String(value);
137+
node.textSourceNode = value;
138+
node.text = value.text;
132139
return node;
133140
}
134141
}

src/compiler/printer.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ const _super = (function (geti, seti) {
668668
// SyntaxKind.TemplateMiddle
669669
// SyntaxKind.TemplateTail
670670
function emitLiteral(node: LiteralLikeNode) {
671-
const text = getLiteralText(node, currentSourceFile, languageVersion);
671+
const text = getLiteralTextOfNode(node);
672672
if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap)
673673
&& (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
674674
writer.writeLiteral(text);
@@ -980,7 +980,7 @@ const _super = (function (geti, seti) {
980980
function needsDotDotForPropertyAccess(expression: Expression) {
981981
if (expression.kind === SyntaxKind.NumericLiteral) {
982982
// check if numeric literal was originally written with a dot
983-
const text = getLiteralText(<LiteralExpression>expression, currentSourceFile, languageVersion);
983+
const text = getLiteralTextOfNode(<LiteralExpression>expression);
984984
return text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
985985
}
986986
else {
@@ -2350,13 +2350,30 @@ const _super = (function (geti, seti) {
23502350
else if (isIdentifier(node) && (nodeIsSynthesized(node) || !node.parent)) {
23512351
return unescapeIdentifier(node.text);
23522352
}
2353+
else if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
2354+
return getTextOfNode((<StringLiteral>node).textSourceNode, includeTrivia);
2355+
}
23532356
else if (isLiteralExpression(node) && (nodeIsSynthesized(node) || !node.parent)) {
23542357
return node.text;
23552358
}
23562359

23572360
return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia);
23582361
}
23592362

2363+
function getLiteralTextOfNode(node: LiteralLikeNode): string {
2364+
if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
2365+
const textSourceNode = (<StringLiteral>node).textSourceNode;
2366+
if (isIdentifier(textSourceNode)) {
2367+
return "\"" + escapeNonAsciiCharacters(escapeString(getTextOfNode(textSourceNode))) + "\"";
2368+
}
2369+
else {
2370+
return getLiteralTextOfNode(textSourceNode);
2371+
}
2372+
}
2373+
2374+
return getLiteralText(node, currentSourceFile, languageVersion);
2375+
}
2376+
23602377
function tryGetConstEnumValue(node: Node): number {
23612378
if (compilerOptions.isolatedModules) {
23622379
return undefined;

src/compiler/transformers/module/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ namespace ts {
720720
const moduleName = getExternalModuleName(importNode);
721721
if (moduleName.kind === SyntaxKind.StringLiteral) {
722722
return tryRenameExternalModule(<StringLiteral>moduleName)
723-
|| getSynthesizedClone(<StringLiteral>moduleName);
723+
|| createLiteral(<StringLiteral>moduleName);
724724
}
725725

726726
return undefined;

src/compiler/transformers/module/system.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace ts {
66
export function transformSystemModule(context: TransformationContext) {
77
interface DependencyGroup {
8-
name: Identifier;
8+
name: StringLiteral;
99
externalImports: (ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration)[];
1010
}
1111

@@ -640,7 +640,7 @@ namespace ts {
640640
return [
641641
node,
642642
createExportStatement(name, name)
643-
]
643+
];
644644
}
645645
return node;
646646
}
@@ -1101,7 +1101,7 @@ namespace ts {
11011101
else {
11021102
return operator === SyntaxKind.PlusPlusToken
11031103
? createSubtract(call, createLiteral(1))
1104-
: createAdd(call, createLiteral(1))
1104+
: createAdd(call, createLiteral(1));
11051105
}
11061106
}
11071107
}

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ namespace ts {
804804
// @kind(SyntaxKind.StringLiteral)
805805
export interface StringLiteral extends LiteralExpression {
806806
_stringLiteralBrand: any;
807+
/* @internal */
808+
textSourceNode?: Identifier | StringLiteral; // Allows a StringLiteral to get its text from another node (used by transforms).
807809
}
808810

809811
// Note: 'brands' in our syntax nodes serve to give us a small amount of nominal typing.

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,7 @@ namespace ts {
29822982
break;
29832983

29842984
case SyntaxKind.ImportEqualsDeclaration:
2985-
if ((<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference && resolver.isReferencedAliasDeclaration(node)) {
2985+
if ((<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference && resolver.isReferencedAliasDeclaration(getOriginalNode(node))) {
29862986
// import x = require("mod") where x is referenced
29872987
externalImports.push(<ImportEqualsDeclaration>node);
29882988
}
@@ -2995,7 +2995,7 @@ namespace ts {
29952995
externalImports.push(<ExportDeclaration>node);
29962996
hasExportStars = true;
29972997
}
2998-
else if (resolver.isValueAliasDeclaration(node)) {
2998+
else if (resolver.isValueAliasDeclaration(getOriginalNode(node))) {
29992999
// export { x, y } from "mod" where at least one export is a value symbol
30003000
externalImports.push(<ExportDeclaration>node);
30013001
}

tests/baselines/reference/ambientDeclarationsExternal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ var n: number;
2929
//// [consumer.js]
3030
"use strict";
3131
// Ambient external module members are always exported with or without export keyword when module lacks export assignment
32-
var imp3 = require("equ2");
32+
var imp3 = require('equ2');
3333
var n = imp3.x;
3434
var n;

tests/baselines/reference/ambientExternalModuleWithInternalImportDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var c = new A();
2020

2121
//// [ambientExternalModuleWithInternalImportDeclaration_0.js]
2222
//// [ambientExternalModuleWithInternalImportDeclaration_1.js]
23-
define(["require", "exports", "M"], function (require, exports, A) {
23+
define(["require", "exports", 'M'], function (require, exports, A) {
2424
"use strict";
2525
var c = new A();
2626
});

tests/baselines/reference/ambientExternalModuleWithoutInternalImportDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var c = new A();
1919

2020
//// [ambientExternalModuleWithoutInternalImportDeclaration_0.js]
2121
//// [ambientExternalModuleWithoutInternalImportDeclaration_1.js]
22-
define(["require", "exports", "M"], function (require, exports, A) {
22+
define(["require", "exports", 'M'], function (require, exports, A) {
2323
"use strict";
2424
var c = new A();
2525
});

tests/baselines/reference/systemModule10.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export {n2}
1010
export {n2 as n3}
1111

1212
//// [systemModule10.js]
13-
System.register(["file1", "file2"], function (exports_1, context_1) {
13+
System.register(['file1', 'file2'], function (exports_1, context_1) {
1414
"use strict";
1515
var __moduleName = context_1 && context_1.id;
1616
var file1_1, n2;

0 commit comments

Comments
 (0)