Skip to content

JSDoc comment parsing supports multiline backticks #50677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8123,6 +8123,8 @@ namespace ts {
SawAsterisk,
SavingComments,
SavingBackticks, // NOTE: Only used when parsing tag comments
SavingBackticksBeginningOfLine,
SavingBackticksSawAsterisk,
}

const enum PropertyLikeParse {
Expand Down Expand Up @@ -8433,13 +8435,13 @@ namespace ts {
loop: while (true) {
switch (tok) {
case SyntaxKind.NewLineTrivia:
state = JSDocState.BeginningOfLine;
state = (state as JSDocState) === JSDocState.SavingBackticks || (state as JSDocState) === JSDocState.SavingBackticksBeginningOfLine || (state as JSDocState) === JSDocState.SavingBackticksSawAsterisk ? JSDocState.SavingBackticksBeginningOfLine : JSDocState.BeginningOfLine;
// don't use pushComment here because we want to keep the margin unchanged
comments.push(scanner.getTokenText());
indent = 0;
break;
case SyntaxKind.AtToken:
if (state === JSDocState.SavingBackticks
if (state === JSDocState.SavingBackticks || state === JSDocState.SavingBackticksBeginningOfLine || state === JSDocState.SavingBackticksSawAsterisk
|| state === JSDocState.SavingComments && (!previousWhitespace || lookAhead(isNextJSDocTokenWhitespace))) {
// @ doesn't start a new tag inside ``, and inside a comment, only after whitespace or not before whitespace
comments.push(scanner.getTokenText());
Expand All @@ -8464,7 +8466,7 @@ namespace ts {
}
break;
case SyntaxKind.OpenBraceToken:
state = JSDocState.SavingComments;
state = (state as JSDocState) === JSDocState.SavingBackticks || (state as JSDocState) === JSDocState.SavingBackticksBeginningOfLine || (state as JSDocState) === JSDocState.SavingBackticksSawAsterisk ? JSDocState.SavingBackticks : JSDocState.SavingComments;
const commentEnd = scanner.getStartPos();
const linkStart = scanner.getTextPos() - 1;
const link = parseJSDocLink(linkStart);
Expand All @@ -8479,7 +8481,7 @@ namespace ts {
}
break;
case SyntaxKind.BacktickToken:
if (state === JSDocState.SavingBackticks) {
if (state === JSDocState.SavingBackticks || state === JSDocState.SavingBackticksBeginningOfLine || state === JSDocState.SavingBackticksSawAsterisk) {
state = JSDocState.SavingComments;
}
else {
Expand All @@ -8494,11 +8496,22 @@ namespace ts {
indent += 1;
break;
}
if (state === JSDocState.SavingBackticksBeginningOfLine) {
// leading asterisks start recording on the *next* (non-whitespace) token
state = JSDocState.SavingBackticksSawAsterisk;
indent += 1;
break;
}
// record the * as a comment
// falls through
default:
if (state !== JSDocState.SavingBackticks) {
state = JSDocState.SavingComments; // leading identifiers start recording as well
if (state === JSDocState.SavingBackticksBeginningOfLine || state === JSDocState.SavingBackticksSawAsterisk) {
state = JSDocState.SavingBackticks
}
else {
state = JSDocState.SavingComments; // leading identifiers start recording as well
}
}
pushComment(scanner.getTokenText());
break;
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/jsdocParseMatchingBackticks.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests/cases/conformance/jsdoc/jsdocParseMatchingBackticks.js(22,22): error TS7006: Parameter 'gamma' implicitly has an 'any' type.


==== tests/cases/conformance/jsdoc/jsdocParseMatchingBackticks.js (1 errors) ====
/**
* `@param` initial at-param is OK in title comment
* @param {string} x hi there `@param`
* @param {string} y hi there @ * param
* this is the margin
* so we'll drop everything before it
`@param` @param {string} z hello???
* `@param` @param {string} alpha hello???
* `@ * param` @param {string} beta hello???
* @param {string} gamma
*/
export function f(x, y, z, alpha, beta, gamma) {
return x + y + z + alpha + beta + gamma
}
/**
* Unmatched backticks keep going past newlines
* @param {string} y hi there `@ * param
* this is the margin
* so we'll drop everything before it
* @param {string} gamma
*/
export function g(y, gamma) {
~~~~~
!!! error TS7006: Parameter 'gamma' implicitly has an 'any' type.
return y + gamma
}

18 changes: 17 additions & 1 deletion tests/baselines/reference/jsdocParseMatchingBackticks.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* `@param` initial at-param is OK in title comment
* @param {string} x hi there `@param`
* @param {string} y hi there `@ * param
* @param {string} y hi there @ * param
* this is the margin
* so we'll drop everything before it
`@param` @param {string} z hello???
Expand All @@ -27,4 +27,20 @@ export function f(x, y, z, alpha, beta, gamma) {
>beta : Symbol(beta, Decl(jsdocParseMatchingBackticks.js, 11, 33))
>gamma : Symbol(gamma, Decl(jsdocParseMatchingBackticks.js, 11, 39))
}
/**
* Unmatched backticks keep going past newlines
* @param {string} y hi there `@ * param
* this is the margin
* so we'll drop everything before it
* @param {string} gamma
*/
export function g(y, gamma) {
>g : Symbol(g, Decl(jsdocParseMatchingBackticks.js, 13, 1))
>y : Symbol(y, Decl(jsdocParseMatchingBackticks.js, 21, 18))
>gamma : Symbol(gamma, Decl(jsdocParseMatchingBackticks.js, 21, 20))

return y + gamma
>y : Symbol(y, Decl(jsdocParseMatchingBackticks.js, 21, 18))
>gamma : Symbol(gamma, Decl(jsdocParseMatchingBackticks.js, 21, 20))
}

19 changes: 18 additions & 1 deletion tests/baselines/reference/jsdocParseMatchingBackticks.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* `@param` initial at-param is OK in title comment
* @param {string} x hi there `@param`
* @param {string} y hi there `@ * param
* @param {string} y hi there @ * param
* this is the margin
* so we'll drop everything before it
`@param` @param {string} z hello???
Expand Down Expand Up @@ -32,4 +32,21 @@ export function f(x, y, z, alpha, beta, gamma) {
>beta : string
>gamma : string
}
/**
* Unmatched backticks keep going past newlines
* @param {string} y hi there `@ * param
* this is the margin
* so we'll drop everything before it
* @param {string} gamma
*/
export function g(y, gamma) {
>g : (y: string, gamma: any) => string
>y : string
>gamma : any

return y + gamma
>y + gamma : string
>y : string
>gamma : any
}

12 changes: 11 additions & 1 deletion tests/cases/conformance/jsdoc/jsdocParseMatchingBackticks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* `@param` initial at-param is OK in title comment
* @param {string} x hi there `@param`
* @param {string} y hi there `@ * param
* @param {string} y hi there @ * param
* this is the margin
* so we'll drop everything before it
`@param` @param {string} z hello???
Expand All @@ -18,3 +18,13 @@
export function f(x, y, z, alpha, beta, gamma) {
return x + y + z + alpha + beta + gamma
}
/**
* Unmatched backticks keep going past newlines
* @param {string} y hi there `@ * param
* this is the margin
* so we'll drop everything before it
* @param {string} gamma
*/
export function g(y, gamma) {
return y + gamma
}
11 changes: 7 additions & 4 deletions tests/cases/fourslash/quickInfoJSDocBackticks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
//// * @param {string} x hi there `@param`
//// * @param {string} y hi there `@ * param
//// * this is the margin
//// * @param {string} z OOPS, unclosed backtick!
//// */
////export function f(x, y) {
//// return x/*x*/ + y/*y*/
////export function f(x, y, z) {
//// return x/*x*/ + y/*y*/ + z/*z*/
////}
////f/*f*/

goTo.marker("f");
verify.quickInfoIs("function f(x: string, y: string): string", "`@param` initial at-param is OK in title comment");
verify.quickInfoIs("function f(x: string, y: string, z: any): string", "`@param` initial at-param is OK in title comment");
goTo.marker("x");
verify.quickInfoIs("(parameter) x: string", "hi there `@param`");
goTo.marker("y");
verify.quickInfoIs("(parameter) y: string", "hi there `@ * param\nthis is the margin");
verify.quickInfoIs("(parameter) y: string", "hi there `@ * param\nthis is the margin\n@param {string} z OOPS, unclosed backtick!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct. Markdown won't detect this as a code span because there's no closing backtick, so it will treat the backtick as a literal character.

goTo.marker("z");
verify.quickInfoIs("(parameter) z: any", undefined);