Skip to content

Commit 98a2458

Browse files
Merge pull request microsoft#7299 from Microsoft/avoid-escape
Allow single-quoted strings when using double-quote characters
2 parents 5e53ba0 + dbf8b02 commit 98a2458

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ namespace ts {
12211221

12221222
// Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the
12231223
// string to contain unicode escapes (as per ES5).
1224-
return nodeText === "\"use strict\"" || nodeText === "'use strict'";
1224+
return nodeText === '"use strict"' || nodeText === "'use strict'";
12251225
}
12261226

12271227
function bindWorker(node: Node) {

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ namespace ts {
11591159

11601160
const isRelative = isExternalModuleNameRelative(moduleName);
11611161
if (!isRelative) {
1162-
const symbol = getSymbol(globals, "\"" + moduleName + "\"", SymbolFlags.ValueModule);
1162+
const symbol = getSymbol(globals, '"' + moduleName + '"', SymbolFlags.ValueModule);
11631163
if (symbol) {
11641164
// merged symbol is module declaration symbol combined with all augmentations
11651165
return getMergedSymbol(symbol);

src/compiler/declarationEmitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,9 @@ namespace ts {
753753
if (moduleSpecifier.kind === SyntaxKind.StringLiteral && isBundledEmit && (compilerOptions.out || compilerOptions.outFile)) {
754754
const moduleName = getExternalModuleNameFromDeclaration(host, resolver, parent);
755755
if (moduleName) {
756-
write("\"");
756+
write('"');
757757
write(moduleName);
758-
write("\"");
758+
write('"');
759759
return;
760760
}
761761
}
@@ -1679,7 +1679,7 @@ namespace ts {
16791679
host.getCanonicalFileName,
16801680
/*isAbsolutePathAnUrl*/ false);
16811681

1682-
referencePathsOutput += "/// <reference path=\"" + declFileName + "\" />" + newLine;
1682+
referencePathsOutput += '/// <reference path="' + declFileName + '" />' + newLine;
16831683
}
16841684
return addedBundledEmitReference;
16851685

src/compiler/emitter.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
966966
// Any template literal or string literal with an extended escape
967967
// (e.g. "\u{0067}") will need to be downleveled as a escaped string literal.
968968
if (languageVersion < ScriptTarget.ES6 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) {
969-
return getQuotedEscapedLiteralText("\"", node.text, "\"");
969+
return getQuotedEscapedLiteralText('"', node.text, '"');
970970
}
971971

972972
// If we don't need to downlevel and we can reach the original source text using
@@ -979,7 +979,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
979979
// or an escaped quoted form of the original text if it's string-like.
980980
switch (node.kind) {
981981
case SyntaxKind.StringLiteral:
982-
return getQuotedEscapedLiteralText("\"", node.text, "\"");
982+
return getQuotedEscapedLiteralText('"', node.text, '"');
983983
case SyntaxKind.NoSubstitutionTemplateLiteral:
984984
return getQuotedEscapedLiteralText("`", node.text, "`");
985985
case SyntaxKind.TemplateHead:
@@ -1205,9 +1205,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12051205
/// 'Div' for upper-cased or dotted names
12061206
function emitTagName(name: Identifier | QualifiedName) {
12071207
if (name.kind === SyntaxKind.Identifier && isIntrinsicJsxName((<Identifier>name).text)) {
1208-
write("\"");
1208+
write('"');
12091209
emit(name);
1210-
write("\"");
1210+
write('"');
12111211
}
12121212
else {
12131213
emit(name);
@@ -1222,9 +1222,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12221222
emit(name);
12231223
}
12241224
else {
1225-
write("\"");
1225+
write('"');
12261226
emit(name);
1227-
write("\"");
1227+
write('"');
12281228
}
12291229
}
12301230

@@ -1493,7 +1493,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
14931493
emit((<ComputedPropertyName>node).expression);
14941494
}
14951495
else {
1496-
write("\"");
1496+
write('"');
14971497

14981498
if (node.kind === SyntaxKind.NumericLiteral) {
14991499
write((<LiteralExpression>node).text);
@@ -1502,7 +1502,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15021502
writeTextOfNode(currentText, node);
15031503
}
15041504

1505-
write("\"");
1505+
write('"');
15061506
}
15071507
}
15081508

@@ -1592,7 +1592,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15921592
if (declaration.kind === SyntaxKind.ImportClause) {
15931593
// Identifier references default import
15941594
write(getGeneratedNameForNode(<ImportDeclaration>declaration.parent));
1595-
write(languageVersion === ScriptTarget.ES3 ? "[\"default\"]" : ".default");
1595+
write(languageVersion === ScriptTarget.ES3 ? '["default"]' : ".default");
15961596
return;
15971597
}
15981598
else if (declaration.kind === SyntaxKind.ImportSpecifier) {
@@ -1601,7 +1601,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
16011601
const name = (<ImportSpecifier>declaration).propertyName || (<ImportSpecifier>declaration).name;
16021602
const identifier = getTextOfNodeFromSourceText(currentText, name);
16031603
if (languageVersion === ScriptTarget.ES3 && identifier === "default") {
1604-
write(`["default"]`);
1604+
write('["default"]');
16051605
}
16061606
else {
16071607
write(".");
@@ -3792,7 +3792,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
37923792
if (!isEs6Module) {
37933793
if (languageVersion !== ScriptTarget.ES3) {
37943794
// default value of configurable, enumerable, writable are `false`.
3795-
write("Object.defineProperty(exports, \"__esModule\", { value: true });");
3795+
write('Object.defineProperty(exports, "__esModule", { value: true });');
37963796
writeLine();
37973797
}
37983798
else {
@@ -3828,7 +3828,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
38283828
if (node.flags & NodeFlags.Default) {
38293829
emitEs6ExportDefaultCompat(node);
38303830
if (languageVersion === ScriptTarget.ES3) {
3831-
write("exports[\"default\"]");
3831+
write('exports["default"]');
38323832
}
38333833
else {
38343834
write("exports.default");
@@ -6600,7 +6600,7 @@ const _super = (function (geti, seti) {
66006600
emitEs6ExportDefaultCompat(node);
66016601
emitContainingModuleName(node);
66026602
if (languageVersion === ScriptTarget.ES3) {
6603-
write("[\"default\"] = ");
6603+
write('["default"] = ');
66046604
}
66056605
else {
66066606
write(".default = ");
@@ -7322,11 +7322,11 @@ const _super = (function (geti, seti) {
73227322
// Fill in amd-dependency tags
73237323
for (const amdDependency of node.amdDependencies) {
73247324
if (amdDependency.name) {
7325-
aliasedModuleNames.push("\"" + amdDependency.path + "\"");
7325+
aliasedModuleNames.push('"' + amdDependency.path + '"');
73267326
importAliasNames.push(amdDependency.name);
73277327
}
73287328
else {
7329-
unaliasedModuleNames.push("\"" + amdDependency.path + "\"");
7329+
unaliasedModuleNames.push('"' + amdDependency.path + '"');
73307330
}
73317331
}
73327332

@@ -7368,7 +7368,7 @@ const _super = (function (geti, seti) {
73687368
}
73697369

73707370
function emitAMDDependencyList({ aliasedModuleNames, unaliasedModuleNames }: AMDDependencyNames) {
7371-
write("[\"require\", \"exports\"");
7371+
write('["require", "exports"');
73727372
if (aliasedModuleNames.length) {
73737373
write(", ");
73747374
write(aliasedModuleNames.join(", "));
@@ -7502,7 +7502,7 @@ const _super = (function (geti, seti) {
75027502
if (isLineBreak(c)) {
75037503
if (firstNonWhitespace !== -1 && (lastNonWhitespace - firstNonWhitespace + 1 > 0)) {
75047504
const part = text.substr(firstNonWhitespace, lastNonWhitespace - firstNonWhitespace + 1);
7505-
result = (result ? result + "\" + ' ' + \"" : "") + escapeString(part);
7505+
result = (result ? result + `" + ' ' + "` : "") + escapeString(part);
75067506
}
75077507
firstNonWhitespace = -1;
75087508
}
@@ -7525,7 +7525,7 @@ const _super = (function (geti, seti) {
75257525
if (entities[m] !== undefined) {
75267526
const ch = String.fromCharCode(entities[m]);
75277527
// &quot; needs to be escaped
7528-
return ch === "\"" ? "\\\"" : ch;
7528+
return ch === '"' ? "\\\"" : ch;
75297529
}
75307530
else {
75317531
return s;
@@ -7569,9 +7569,9 @@ const _super = (function (geti, seti) {
75697569
function emitJsxText(node: JsxText) {
75707570
switch (compilerOptions.jsx) {
75717571
case JsxEmit.React:
7572-
write("\"");
7572+
write('"');
75737573
write(trimReactWhitespaceAndApplyEntities(node));
7574-
write("\"");
7574+
write('"');
75757575
break;
75767576

75777577
case JsxEmit.Preserve:

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"no-var-keyword": true,
1515
"quotemark": [true,
16-
"double"
16+
"double",
17+
"avoid-escape"
1718
],
1819
"semicolon": true,
1920
"whitespace": [true,

0 commit comments

Comments
 (0)