From 3e339d88a1b192e86a448ed26643e1351f80afb9 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Tue, 7 Nov 2017 10:33:35 -0800 Subject: [PATCH] Handle other linebreak characters and add boundary checks --- src/services/utilities.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 4f95682d94..179e5edde5 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1330,6 +1330,7 @@ namespace ts { export function getSourceFileImportLocation(node: SourceFile) { // For a source file, it is possible there are detached comments we should not skip const text = node.text; + const textLength = text.length; let ranges = getLeadingCommentRanges(text, 0); if (!ranges) return 0; let position = 0; @@ -1351,12 +1352,15 @@ namespace ts { return position; function AdvancePastLineBreak() { - if (text.charCodeAt(position) === CharacterCodes.carriageReturn) { - position++; - } + if (position < textLength) { + const charCode = text.charCodeAt(position); + if (isLineBreak(charCode)) { + position++; - if (text.charCodeAt(position) === CharacterCodes.lineFeed) { - position++; + if (position < textLength && charCode === CharacterCodes.carriageReturn && text.charCodeAt(position) === CharacterCodes.lineFeed) { + position++; + } + } } } }