Use getTextOfNode over getTextOfNodeFromSourceText(getSourceFileOfNode(node)) (#22387)

This commit is contained in:
Andy
2018-03-08 07:52:35 -08:00
committed by GitHub
parent 0605424852
commit 55bffba5fd
4 changed files with 6 additions and 13 deletions

View File

@@ -1996,7 +1996,7 @@ namespace ts {
/// Should be called only on prologue directives (isPrologueDirective(node) should be true)
function isUseStrictPrologueDirective(node: ExpressionStatement): boolean {
const nodeText = getTextOfNodeFromSourceText(file.text, node.expression);
const nodeText = getSourceTextOfNodeFromSourceFile(file, node.expression);
// Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the
// string to contain unicode escapes (as per ES5).

View File

@@ -22851,8 +22851,7 @@ namespace ts {
return "quit";
}
if (current.kind === SyntaxKind.LabeledStatement && (<LabeledStatement>current).label.escapedText === node.label.escapedText) {
const sourceFile = getSourceFileOfNode(node);
grammarErrorOnNode(node.label, Diagnostics.Duplicate_label_0, getTextOfNodeFromSourceText(sourceFile.text, node.label));
grammarErrorOnNode(node.label, Diagnostics.Duplicate_label_0, getTextOfNode(node.label));
return true;
}
});

View File

@@ -314,20 +314,15 @@ namespace ts {
}
export function getSourceTextOfNodeFromSourceFile(sourceFile: SourceFile, node: Node, includeTrivia = false): string {
if (nodeIsMissing(node)) {
return "";
}
const text = sourceFile.text;
return text.substring(includeTrivia ? node.pos : skipTrivia(text, node.pos), node.end);
return getTextOfNodeFromSourceText(sourceFile.text, node, includeTrivia);
}
export function getTextOfNodeFromSourceText(sourceText: string, node: Node): string {
export function getTextOfNodeFromSourceText(sourceText: string, node: Node, includeTrivia = false): string {
if (nodeIsMissing(node)) {
return "";
}
return sourceText.substring(skipTrivia(sourceText, node.pos), node.end);
return sourceText.substring(includeTrivia ? node.pos : skipTrivia(sourceText, node.pos), node.end);
}
export function getTextOfNode(node: Node, includeTrivia = false): string {

View File

@@ -72,8 +72,7 @@ class TypeWriterWalker {
private writeTypeOrSymbol(node: ts.Node, isSymbolWalk: boolean): TypeWriterResult {
const actualPos = ts.skipTrivia(this.currentSourceFile.text, node.pos);
const lineAndCharacter = this.currentSourceFile.getLineAndCharacterOfPosition(actualPos);
const sourceText = ts.getTextOfNodeFromSourceText(this.currentSourceFile.text, node);
const sourceText = ts.getSourceTextOfNodeFromSourceFile(this.currentSourceFile, node);
if (!isSymbolWalk) {
// Workaround to ensure we output 'C' instead of 'typeof C' for base class expressions