Skip to content

Commit 58c5412

Browse files
authored
Fix node.getStart() for nodes spanning multiline JSDoc comments (#43854)
1 parent 046c65a commit 58c5412

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/compiler/scanner.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,12 @@ namespace ts {
554554
}
555555

556556
/* @internal */
557-
export function skipTrivia(text: string, pos: number, stopAfterLineBreak?: boolean, stopAtComments = false): number {
557+
export function skipTrivia(text: string, pos: number, stopAfterLineBreak?: boolean, stopAtComments?: boolean, inJSDoc?: boolean): number {
558558
if (positionIsSynthesized(pos)) {
559559
return pos;
560560
}
561561

562+
let canConsumeStar = false;
562563
// Keep in sync with couldStartTrivia
563564
while (true) {
564565
const ch = text.charCodeAt(pos);
@@ -573,6 +574,7 @@ namespace ts {
573574
if (stopAfterLineBreak) {
574575
return pos;
575576
}
577+
canConsumeStar = !!inJSDoc;
576578
continue;
577579
case CharacterCodes.tab:
578580
case CharacterCodes.verticalTab:
@@ -592,6 +594,7 @@ namespace ts {
592594
}
593595
pos++;
594596
}
597+
canConsumeStar = false;
595598
continue;
596599
}
597600
if (text.charCodeAt(pos + 1) === CharacterCodes.asterisk) {
@@ -603,6 +606,7 @@ namespace ts {
603606
}
604607
pos++;
605608
}
609+
canConsumeStar = false;
606610
continue;
607611
}
608612
break;
@@ -613,13 +617,23 @@ namespace ts {
613617
case CharacterCodes.greaterThan:
614618
if (isConflictMarkerTrivia(text, pos)) {
615619
pos = scanConflictMarkerTrivia(text, pos);
620+
canConsumeStar = false;
616621
continue;
617622
}
618623
break;
619624

620625
case CharacterCodes.hash:
621626
if (pos === 0 && isShebangTrivia(text, pos)) {
622627
pos = scanShebangTrivia(text, pos);
628+
canConsumeStar = false;
629+
continue;
630+
}
631+
break;
632+
633+
case CharacterCodes.asterisk:
634+
if (canConsumeStar) {
635+
pos++;
636+
canConsumeStar = false;
623637
continue;
624638
}
625639
break;

src/compiler/utilities.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,12 @@ namespace ts {
478478
return getTokenPosOfNode((<SyntaxList>node)._children[0], sourceFile, includeJsDoc);
479479
}
480480

481-
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos);
481+
return skipTrivia(
482+
(sourceFile || getSourceFileOfNode(node)).text,
483+
node.pos,
484+
/*stopAfterLineBreak*/ false,
485+
/*stopAtComments*/ false,
486+
isInJSDoc(node));
482487
}
483488

484489
export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFileLike): number {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @checkJs: true
5+
6+
// @Filename: index.js
7+
//// /**
8+
//// * @typedef {{
9+
//// * [|foo|]: string;
10+
//// * [|bar|]: number;
11+
//// * }} Foo
12+
//// */
13+
////
14+
//// /** @type {Foo} */
15+
//// const x = {
16+
//// [|foo|]: "",
17+
//// [|bar|]: 42,
18+
//// };
19+
20+
verify.rangesWithSameTextAreDocumentHighlights();

0 commit comments

Comments
 (0)