Skip to content

Commit 9df2f88

Browse files
committed
Scan backticks in jsdoc as a single token less often
Previously, matching backticks in jsdoc would always be scanned as one token to aid parsing incorrect jsdoc that uses backticks for parameter names: ``js /** @param {string} `nope` * @param {number} `not needed` */ ``` However, this is wrong for code fences, which use triple backticks. This fix parses a single backtick as a single token if it's immediately followed by another backtick or by a newline. It retains the questionable tokenisation of backticks-as-pairs in other cases, however. A better fix might be to have the parser ignore backticks in jsdoc instead.
1 parent 84427ea commit 9df2f88

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/compiler/scanner.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,12 +2093,14 @@ namespace ts {
20932093
case CharacterCodes.dot:
20942094
return token = SyntaxKind.DotToken;
20952095
case CharacterCodes.backtick:
2096-
while (pos < end && text.charCodeAt(pos) !== CharacterCodes.backtick) {
2096+
if (pos < end && text.charCodeAt(pos) !== CharacterCodes.backtick && text.charCodeAt(pos) !== CharacterCodes.carriageReturn && text.charCodeAt(pos) !== CharacterCodes.lineFeed) {
2097+
while (pos < end && text.charCodeAt(pos) !== CharacterCodes.backtick) {
2098+
pos++;
2099+
}
2100+
tokenValue = text.substring(tokenPos + 1, pos);
20972101
pos++;
2102+
return token = SyntaxKind.NoSubstitutionTemplateLiteral;
20982103
}
2099-
tokenValue = text.substring(tokenPos + 1, pos);
2100-
pos++;
2101-
return token = SyntaxKind.NoSubstitutionTemplateLiteral;
21022104
}
21032105

21042106
if (isIdentifierStart(ch, ScriptTarget.Latest)) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[
2+
{
3+
"marker": {
4+
"fileName": "/tests/cases/fourslash/quickInfoForJSDocCodefence.ts",
5+
"position": 54
6+
},
7+
"quickInfo": {
8+
"kind": "function",
9+
"kindModifiers": "",
10+
"textSpan": {
11+
"start": 52,
12+
"length": 3
13+
},
14+
"displayParts": [
15+
{
16+
"text": "function",
17+
"kind": "keyword"
18+
},
19+
{
20+
"text": " ",
21+
"kind": "space"
22+
},
23+
{
24+
"text": "foo",
25+
"kind": "functionName"
26+
},
27+
{
28+
"text": "(",
29+
"kind": "punctuation"
30+
},
31+
{
32+
"text": ")",
33+
"kind": "punctuation"
34+
},
35+
{
36+
"text": ":",
37+
"kind": "punctuation"
38+
},
39+
{
40+
"text": " ",
41+
"kind": "space"
42+
},
43+
{
44+
"text": "string",
45+
"kind": "keyword"
46+
}
47+
],
48+
"documentation": [],
49+
"tags": [
50+
{
51+
"name": "example",
52+
"text": "```\n1 + 2\n```"
53+
}
54+
]
55+
}
56+
}
57+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////**
4+
//// * @example
5+
//// * ```
6+
//// * 1 + 2
7+
//// * ```
8+
//// */
9+
////function fo/*1*/o() {
10+
//// return '2';
11+
////}
12+
verify.baselineQuickInfo();

0 commit comments

Comments
 (0)