From 2f13222180dee1cbfac550955d98f2da556b7c8a Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Mon, 6 Nov 2017 18:29:38 -0800 Subject: [PATCH 1/4] Handle windows linebreaks in getSourceFileImportLocation --- src/services/utilities.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 36089f94f73..643aae2bae1 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1335,18 +1335,30 @@ namespace ts { let position = 0; // However we should still skip a pinned comment at the top if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) { - position = ranges[0].end + 1; + position = ranges[0].end; + AdvancePastLineBreak(); ranges = ranges.slice(1); } // As well as any triple slash references for (const range of ranges) { if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) { - position = range.end + 1; + position = range.end; + AdvancePastLineBreak(); continue; } break; } return position; + + function AdvancePastLineBreak() { + if (text.charCodeAt(position) === 0xD) { + position++; + } + + if (text.charCodeAt(position) === 0xA) { + position++; + } + } } /** From d1fa006a1e1761e39430f9363768356a5b8f360a Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Tue, 7 Nov 2017 10:10:34 -0800 Subject: [PATCH 2/4] Use CharacterCode enum --- src/services/utilities.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 643aae2bae1..4f95682d949 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1351,11 +1351,11 @@ namespace ts { return position; function AdvancePastLineBreak() { - if (text.charCodeAt(position) === 0xD) { + if (text.charCodeAt(position) === CharacterCodes.carriageReturn) { position++; } - if (text.charCodeAt(position) === 0xA) { + if (text.charCodeAt(position) === CharacterCodes.lineFeed) { position++; } } From 3e339d88a1b192e86a448ed26643e1351f80afb9 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Tue, 7 Nov 2017 10:33:35 -0800 Subject: [PATCH 3/4] 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 4f95682d949..179e5edde52 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++; + } + } } } } From 2715f890b4993a94ca75b7ba1453aa5982dc1803 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Tue, 7 Nov 2017 10:47:36 -0800 Subject: [PATCH 4/4] PascalCase -> camelCase --- src/services/utilities.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 179e5edde52..f59f6e56e91 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1337,21 +1337,21 @@ namespace ts { // However we should still skip a pinned comment at the top if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) { position = ranges[0].end; - AdvancePastLineBreak(); + advancePastLineBreak(); ranges = ranges.slice(1); } // As well as any triple slash references for (const range of ranges) { if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) { position = range.end; - AdvancePastLineBreak(); + advancePastLineBreak(); continue; } break; } return position; - function AdvancePastLineBreak() { + function advancePastLineBreak() { if (position < textLength) { const charCode = text.charCodeAt(position); if (isLineBreak(charCode)) {