From 05b17255f69b11ca2911762e3a1fb6aa57854dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=81rlis=20Ga=C5=86=C4=A3is?= Date: Mon, 12 Dec 2016 21:40:00 +0200 Subject: [PATCH 1/3] Fixed missing whitespace in jsDoc comments. Fixes https://github.com/Microsoft/TypeScript/issues/12236 --- src/compiler/parser.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 4aefa1600776a..0100f9538bcb4 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6358,7 +6358,9 @@ namespace ts { case SyntaxKind.WhitespaceTrivia: // only collect whitespace if we're already saving comments or have just crossed the comment indent margin const whitespace = scanner.getTokenText(); - if (state === JSDocState.SavingComments || margin !== undefined && indent + whitespace.length > margin) { + if (state === JSDocState.SavingComments) { + comments.push(whitespace); + } else if (margin !== undefined && indent + whitespace.length > margin) { comments.push(whitespace.slice(margin - indent - 1)); } indent += whitespace.length; From 19070648eb53614e38a456eb3cce0231486896bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=81rlis=20Ga=C5=86=C4=A3is?= Date: Mon, 12 Dec 2016 22:33:45 +0200 Subject: [PATCH 2/3] Added test for https://github.com/Microsoft/TypeScript/issues/12236 --- src/compiler/parser.ts | 3 ++- tests/cases/fourslash/commentsLinePreservation.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0100f9538bcb4..0fae515d837a0 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6360,7 +6360,8 @@ namespace ts { const whitespace = scanner.getTokenText(); if (state === JSDocState.SavingComments) { comments.push(whitespace); - } else if (margin !== undefined && indent + whitespace.length > margin) { + } + else if (margin !== undefined && indent + whitespace.length > margin) { comments.push(whitespace.slice(margin - indent - 1)); } indent += whitespace.length; diff --git a/tests/cases/fourslash/commentsLinePreservation.ts b/tests/cases/fourslash/commentsLinePreservation.ts index 6d085f71f804a..78c863611621b 100644 --- a/tests/cases/fourslash/commentsLinePreservation.ts +++ b/tests/cases/fourslash/commentsLinePreservation.ts @@ -105,6 +105,13 @@ //// * second time information about the param again //// */ ////function /*l*/l(param1: string) { /*9*/param1 = "hello"; } +//// /** +//// * This is firstLine +//// This is second Line +//// @param param1 first Line text +//// second line text +//// */ +////function /*m*/m(param1: string) { /*10*/param1 = "hello"; } verify.quickInfos({ a: ["var a: string", "This is firstLine\nThis is second Line\n\nThis is fourth Line"], @@ -136,5 +143,8 @@ verify.quickInfos({ 8: ["(parameter) param1: string", "hello "], l: ["function l(param1: string): void", "This is firstLine\nThis is second Line"], - 9: ["(parameter) param1: string", "first Line text\nblank line that shouldnt be shown when starting this \nsecond time information about the param again"] + 9: ["(parameter) param1: string", "first Line text\nblank line that shouldnt be shown when starting this \nsecond time information about the param again"], + + m: ["function m(param1: string): void", "This is firstLine\nThis is second Line"], + 10: ["(parameter) param1: string", "first Line text\nsecond line text"] }); From aa4a0b64698c7e00e8ac801c8cf48decac3478ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=81rlis=20Ga=C5=86=C4=A3is?= Date: Tue, 13 Dec 2016 00:05:16 +0200 Subject: [PATCH 3/3] Fixed jsDoc parser - no longer omits asterisks in the middle (if the line does not start with asterisk) and additional case for whitespaces being ignored --- src/compiler/parser.ts | 4 +++- tests/cases/fourslash/commentsLinePreservation.ts | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0fae515d837a0..2f0d6a4d9754e 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6337,7 +6337,7 @@ namespace ts { break; case SyntaxKind.AsteriskToken: const asterisk = scanner.getTokenText(); - if (state === JSDocState.SawAsterisk) { + if (state === JSDocState.SawAsterisk || state === JSDocState.SavingComments) { // If we've already seen an asterisk, then we can no longer parse a tag on this line state = JSDocState.SavingComments; pushComment(asterisk); @@ -6369,6 +6369,8 @@ namespace ts { case SyntaxKind.EndOfFileToken: break; default: + // anything other than whitespace or asterisk at the beginning of the line starts the comment text + state = JSDocState.SavingComments; pushComment(scanner.getTokenText()); break; } diff --git a/tests/cases/fourslash/commentsLinePreservation.ts b/tests/cases/fourslash/commentsLinePreservation.ts index 78c863611621b..3f60bdbf43b0e 100644 --- a/tests/cases/fourslash/commentsLinePreservation.ts +++ b/tests/cases/fourslash/commentsLinePreservation.ts @@ -108,6 +108,7 @@ //// /** //// * This is firstLine //// This is second Line +//// [1]: third * line //// @param param1 first Line text //// second line text //// */ @@ -145,6 +146,6 @@ verify.quickInfos({ l: ["function l(param1: string): void", "This is firstLine\nThis is second Line"], 9: ["(parameter) param1: string", "first Line text\nblank line that shouldnt be shown when starting this \nsecond time information about the param again"], - m: ["function m(param1: string): void", "This is firstLine\nThis is second Line"], + m: ["function m(param1: string): void", "This is firstLine\nThis is second Line\n[1]: third * line"], 10: ["(parameter) param1: string", "first Line text\nsecond line text"] });