Never escape string literals from textChanges (#26971)

* Never escape string literals from textChanges

* Use `boolean | undefined`
This commit is contained in:
Andy 2018-09-17 11:06:39 -07:00 committed by GitHub
parent a57467a317
commit e710645bf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 12 deletions

View file

@ -1041,7 +1041,7 @@ namespace ts {
// SyntaxKind.TemplateMiddle // SyntaxKind.TemplateMiddle
// SyntaxKind.TemplateTail // SyntaxKind.TemplateTail
function emitLiteral(node: LiteralLikeNode) { function emitLiteral(node: LiteralLikeNode) {
const text = getLiteralTextOfNode(node); const text = getLiteralTextOfNode(node, printerOptions.neverAsciiEscape);
if ((printerOptions.sourceMap || printerOptions.inlineSourceMap) if ((printerOptions.sourceMap || printerOptions.inlineSourceMap)
&& (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) { && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writeLiteral(text); writeLiteral(text);
@ -1532,7 +1532,7 @@ namespace ts {
expression = skipPartiallyEmittedExpressions(expression); expression = skipPartiallyEmittedExpressions(expression);
if (isNumericLiteral(expression)) { if (isNumericLiteral(expression)) {
// check if numeric literal is a decimal literal that was originally written with a dot // check if numeric literal is a decimal literal that was originally written with a dot
const text = getLiteralTextOfNode(<LiteralExpression>expression); const text = getLiteralTextOfNode(<LiteralExpression>expression, /*neverAsciiEscape*/ true);
return !expression.numericLiteralFlags return !expression.numericLiteralFlags
&& !stringContains(text, tokenToString(SyntaxKind.DotToken)!); && !stringContains(text, tokenToString(SyntaxKind.DotToken)!);
} }
@ -3306,20 +3306,20 @@ namespace ts {
return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia);
} }
function getLiteralTextOfNode(node: LiteralLikeNode): string { function getLiteralTextOfNode(node: LiteralLikeNode, neverAsciiEscape: boolean | undefined): string {
if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) { if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
const textSourceNode = (<StringLiteral>node).textSourceNode!; const textSourceNode = (<StringLiteral>node).textSourceNode!;
if (isIdentifier(textSourceNode)) { if (isIdentifier(textSourceNode)) {
return getEmitFlags(node) & EmitFlags.NoAsciiEscaping ? return neverAsciiEscape || (getEmitFlags(node) & EmitFlags.NoAsciiEscaping) ?
`"${escapeString(getTextOfNode(textSourceNode))}"` : `"${escapeString(getTextOfNode(textSourceNode))}"` :
`"${escapeNonAsciiString(getTextOfNode(textSourceNode))}"`; `"${escapeNonAsciiString(getTextOfNode(textSourceNode))}"`;
} }
else { else {
return getLiteralTextOfNode(textSourceNode); return getLiteralTextOfNode(textSourceNode, neverAsciiEscape);
} }
} }
return getLiteralText(node, currentSourceFile); return getLiteralText(node, currentSourceFile, neverAsciiEscape);
} }
/** /**

View file

@ -5327,6 +5327,7 @@ namespace ts {
/*@internal*/ inlineSourceMap?: boolean; /*@internal*/ inlineSourceMap?: boolean;
/*@internal*/ extendedDiagnostics?: boolean; /*@internal*/ extendedDiagnostics?: boolean;
/*@internal*/ onlyPrintJsDocStyle?: boolean; /*@internal*/ onlyPrintJsDocStyle?: boolean;
/*@internal*/ neverAsciiEscape?: boolean;
} }
/* @internal */ /* @internal */

View file

@ -524,14 +524,14 @@ namespace ts {
return emitNode && emitNode.flags || 0; return emitNode && emitNode.flags || 0;
} }
export function getLiteralText(node: LiteralLikeNode, sourceFile: SourceFile) { export function getLiteralText(node: LiteralLikeNode, sourceFile: SourceFile, neverAsciiEscape: boolean | undefined) {
// If we don't need to downlevel and we can reach the original source text using // If we don't need to downlevel and we can reach the original source text using
// the node's parent reference, then simply get the text as it was originally written. // the node's parent reference, then simply get the text as it was originally written.
if (!nodeIsSynthesized(node) && node.parent && !(isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator)) { if (!nodeIsSynthesized(node) && node.parent && !(isNumericLiteral(node) && node.numericLiteralFlags & TokenFlags.ContainsSeparator)) {
return getSourceTextOfNodeFromSourceFile(sourceFile, node); return getSourceTextOfNodeFromSourceFile(sourceFile, node);
} }
const escapeText = getEmitFlags(node) & EmitFlags.NoAsciiEscaping ? escapeString : escapeNonAsciiString; const escapeText = neverAsciiEscape || (getEmitFlags(node) & EmitFlags.NoAsciiEscaping) ? escapeString : escapeNonAsciiString;
// If we can't reach the original source text, use the canonical form if it's a number, // If we can't reach the original source text, use the canonical form if it's a number,
// or a (possibly escaped) quoted form of the original text if it's string-like. // or a (possibly escaped) quoted form of the original text if it's string-like.

View file

@ -781,7 +781,7 @@ namespace ts.textChanges {
function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLineCharacter: string): { text: string, node: Node } { function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLineCharacter: string): { text: string, node: Node } {
const writer = new Writer(newLineCharacter); const writer = new Writer(newLineCharacter);
const newLine = newLineCharacter === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed; const newLine = newLineCharacter === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed;
createPrinter({ newLine }, writer).writeNode(EmitHint.Unspecified, node, sourceFile, writer); createPrinter({ newLine, neverAsciiEscape: true }, writer).writeNode(EmitHint.Unspecified, node, sourceFile, writer);
return { text: writer.getText(), node: assignPositionsToNode(node) }; return { text: writer.getText(), node: assignPositionsToNode(node) };
} }
} }

View file

@ -2,12 +2,12 @@
//// [|f1/*0*/();|] //// [|f1/*0*/();|]
// @Filename: module.ts // @Filename: jalapeño.ts
//// export function f1() {} //// export function f1() {}
//// export var v1 = 5; //// export var v1 = 5;
verify.importFixAtPosition([ verify.importFixAtPosition([
`import { f1 } from "./module"; `import { f1 } from "./jalapeño";
f1();` f1();`
]); ]);