Fixed minor difference in string literal emit for AMD modules

This commit is contained in:
Ron Buckton 2016-03-24 16:23:30 -07:00
parent e78b64b040
commit b60cf99c8a
11 changed files with 41 additions and 15 deletions

View File

@ -114,10 +114,11 @@ namespace ts {
// Literals
export function createLiteral(textSource: StringLiteral | Identifier, location?: TextRange): StringLiteral;
export function createLiteral(value: string, location?: TextRange): StringLiteral;
export function createLiteral(value: number, location?: TextRange): LiteralExpression;
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression;
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression {
export function createLiteral(value: string | number | boolean | StringLiteral | Identifier, location?: TextRange): PrimaryExpression {
if (typeof value === "number") {
const node = <LiteralExpression>createNode(SyntaxKind.NumericLiteral, location);
node.text = value.toString();
@ -126,9 +127,15 @@ namespace ts {
else if (typeof value === "boolean") {
return <PrimaryExpression>createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword, location);
}
else if (typeof value === "string") {
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location);
node.text = value;
return node;
}
else {
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location);
node.text = String(value);
node.textSourceNode = value;
node.text = value.text;
return node;
}
}

View File

@ -668,7 +668,7 @@ const _super = (function (geti, seti) {
// SyntaxKind.TemplateMiddle
// SyntaxKind.TemplateTail
function emitLiteral(node: LiteralLikeNode) {
const text = getLiteralText(node, currentSourceFile, languageVersion);
const text = getLiteralTextOfNode(node);
if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap)
&& (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writer.writeLiteral(text);
@ -980,7 +980,7 @@ const _super = (function (geti, seti) {
function needsDotDotForPropertyAccess(expression: Expression) {
if (expression.kind === SyntaxKind.NumericLiteral) {
// check if numeric literal was originally written with a dot
const text = getLiteralText(<LiteralExpression>expression, currentSourceFile, languageVersion);
const text = getLiteralTextOfNode(<LiteralExpression>expression);
return text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
}
else {
@ -2350,6 +2350,9 @@ const _super = (function (geti, seti) {
else if (isIdentifier(node) && (nodeIsSynthesized(node) || !node.parent)) {
return unescapeIdentifier(node.text);
}
else if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
return getTextOfNode((<StringLiteral>node).textSourceNode, includeTrivia);
}
else if (isLiteralExpression(node) && (nodeIsSynthesized(node) || !node.parent)) {
return node.text;
}
@ -2357,6 +2360,20 @@ const _super = (function (geti, seti) {
return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia);
}
function getLiteralTextOfNode(node: LiteralLikeNode): string {
if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
const textSourceNode = (<StringLiteral>node).textSourceNode;
if (isIdentifier(textSourceNode)) {
return "\"" + escapeNonAsciiCharacters(escapeString(getTextOfNode(textSourceNode))) + "\"";
}
else {
return getLiteralTextOfNode(textSourceNode);
}
}
return getLiteralText(node, currentSourceFile, languageVersion);
}
function tryGetConstEnumValue(node: Node): number {
if (compilerOptions.isolatedModules) {
return undefined;

View File

@ -720,7 +720,7 @@ namespace ts {
const moduleName = getExternalModuleName(importNode);
if (moduleName.kind === SyntaxKind.StringLiteral) {
return tryRenameExternalModule(<StringLiteral>moduleName)
|| getSynthesizedClone(<StringLiteral>moduleName);
|| createLiteral(<StringLiteral>moduleName);
}
return undefined;

View File

@ -5,7 +5,7 @@
namespace ts {
export function transformSystemModule(context: TransformationContext) {
interface DependencyGroup {
name: Identifier;
name: StringLiteral;
externalImports: (ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration)[];
}
@ -640,7 +640,7 @@ namespace ts {
return [
node,
createExportStatement(name, name)
]
];
}
return node;
}
@ -1101,7 +1101,7 @@ namespace ts {
else {
return operator === SyntaxKind.PlusPlusToken
? createSubtract(call, createLiteral(1))
: createAdd(call, createLiteral(1))
: createAdd(call, createLiteral(1));
}
}
}

View File

@ -804,6 +804,8 @@ namespace ts {
// @kind(SyntaxKind.StringLiteral)
export interface StringLiteral extends LiteralExpression {
_stringLiteralBrand: any;
/* @internal */
textSourceNode?: Identifier | StringLiteral; // Allows a StringLiteral to get its text from another node (used by transforms).
}
// Note: 'brands' in our syntax nodes serve to give us a small amount of nominal typing.

View File

@ -2982,7 +2982,7 @@ namespace ts {
break;
case SyntaxKind.ImportEqualsDeclaration:
if ((<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference && resolver.isReferencedAliasDeclaration(node)) {
if ((<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference && resolver.isReferencedAliasDeclaration(getOriginalNode(node))) {
// import x = require("mod") where x is referenced
externalImports.push(<ImportEqualsDeclaration>node);
}
@ -2995,7 +2995,7 @@ namespace ts {
externalImports.push(<ExportDeclaration>node);
hasExportStars = true;
}
else if (resolver.isValueAliasDeclaration(node)) {
else if (resolver.isValueAliasDeclaration(getOriginalNode(node))) {
// export { x, y } from "mod" where at least one export is a value symbol
externalImports.push(<ExportDeclaration>node);
}

View File

@ -29,6 +29,6 @@ var n: number;
//// [consumer.js]
"use strict";
// Ambient external module members are always exported with or without export keyword when module lacks export assignment
var imp3 = require("equ2");
var imp3 = require('equ2');
var n = imp3.x;
var n;

View File

@ -20,7 +20,7 @@ var c = new A();
//// [ambientExternalModuleWithInternalImportDeclaration_0.js]
//// [ambientExternalModuleWithInternalImportDeclaration_1.js]
define(["require", "exports", "M"], function (require, exports, A) {
define(["require", "exports", 'M'], function (require, exports, A) {
"use strict";
var c = new A();
});

View File

@ -19,7 +19,7 @@ var c = new A();
//// [ambientExternalModuleWithoutInternalImportDeclaration_0.js]
//// [ambientExternalModuleWithoutInternalImportDeclaration_1.js]
define(["require", "exports", "M"], function (require, exports, A) {
define(["require", "exports", 'M'], function (require, exports, A) {
"use strict";
var c = new A();
});

View File

@ -10,7 +10,7 @@ export {n2}
export {n2 as n3}
//// [systemModule10.js]
System.register(["file1", "file2"], function (exports_1, context_1) {
System.register(['file1', 'file2'], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var file1_1, n2;

View File

@ -10,7 +10,7 @@ export {n2}
export {n2 as n3}
//// [systemModule10_ES5.js]
System.register(["file1", "file2"], function (exports_1, context_1) {
System.register(['file1', 'file2'], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var file1_1, n2;