diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 6be617e0e1b..f6e5c0f7f8e 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -1740,7 +1740,12 @@ namespace ts { pos++; } - commentDirectives = appendIfCommentDirective(commentDirectives, text.slice(tokenPos, pos), commentDirectiveRegExSingleLine); + commentDirectives = appendIfCommentDirective( + commentDirectives, + text.slice(tokenPos, pos), + commentDirectiveRegExSingleLine, + tokenPos, + ); if (skipTrivia) { continue; @@ -2119,7 +2124,12 @@ namespace ts { return token; } - function appendIfCommentDirective(commentDirectives: CommentDirective[] | undefined, text: string, commentDirectiveRegEx: RegExp, lineStart = tokenPos) { + function appendIfCommentDirective( + commentDirectives: CommentDirective[] | undefined, + text: string, + commentDirectiveRegEx: RegExp, + lineStart: number, + ) { const type = getDirectiveFromComment(text, commentDirectiveRegEx); if (type === undefined) { return commentDirectives; diff --git a/src/testRunner/unittests/incrementalParser.ts b/src/testRunner/unittests/incrementalParser.ts index cd06c60d096..f60d26aa73b 100644 --- a/src/testRunner/unittests/incrementalParser.ts +++ b/src/testRunner/unittests/incrementalParser.ts @@ -833,7 +833,7 @@ module m3 { }\ }); describe("comment directives", () => { - const tsIgnoreComment = "// @ts-ignore"; + const tsIgnoreComment = "@ts-ignore"; const textWithIgnoreComment = `const x = 10; function foo() { // @ts-ignore @@ -845,14 +845,27 @@ function bar() { let z : string = x; return z; } -function bar3() { +function bar2() { // @ts-ignore let z : string = x; return z; } +function bar3() { + /* @ts-ignore */ + let z : string = x; + return z; +} +function bar4() { + /* + @ts-ignore */ + let z : string = x; + return z; +} foo(); bar(); -bar3();`; +bar3(); +bar4(); +bar5()`; verifyScenario("when deleting ts-ignore comment", verifyDelete); verifyScenario("when inserting ts-ignore comment", verifyInsert); verifyScenario("when changing ts-ignore comment to blah", verifyChangeToBlah); @@ -876,17 +889,23 @@ bar3();`; it(`${scenario} - 2`, () => { verifyChange(2); }); + it(`${scenario} - 3`, () => { + verifyChange(3); + }); + it(`${scenario} - 4`, () => { + verifyChange(4); + }); it(`${scenario} - with single ts-ignore`, () => { verifyChange(0, /*singleIgnore*/ true); }); } function getIndexOfTsIgnoreComment(atIndex: number) { - let index: number; + let index = 0; for (let i = 0; i <= atIndex; i++) { - index = textWithIgnoreComment.indexOf(tsIgnoreComment); + index = textWithIgnoreComment.indexOf(tsIgnoreComment, index); } - return index!; + return index; } function textWithIgnoreCommentFrom(text: string, singleIgnore: true | undefined) {