From e77e88dd63a0e0aacfd163ab2e65ae7342faa16c Mon Sep 17 00:00:00 2001 From: Andy Hanson <anhans@microsoft.com> Date: Wed, 7 Mar 2018 13:04:04 -0800 Subject: [PATCH] Use getTextOfNode over getTextOfNodeFromSourceText(getSourceFileOfNode(node)) --- src/compiler/binder.ts | 2 +- src/compiler/checker.ts | 3 +-- src/compiler/utilities.ts | 11 +++-------- src/harness/typeWriter.ts | 3 +-- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 170364db3fe17..b3942d1e84d85 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -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). diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2d9d0d189449d..ab32fa95db99b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22811,8 +22811,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; } }); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b6099c3959cb5..fc55d3cc05bb7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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 { diff --git a/src/harness/typeWriter.ts b/src/harness/typeWriter.ts index 060dacebdcad4..0a2d2d7e6571a 100644 --- a/src/harness/typeWriter.ts +++ b/src/harness/typeWriter.ts @@ -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