Skip to content

Commit 8a2b464

Browse files
authored
Include guards on comment kind in pragma extraction (#23313)
1 parent b2e0c4b commit 8a2b464

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

src/compiler/parser.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7603,7 +7603,7 @@ namespace ts {
76037603
const tripleSlashXMLCommentStartRegEx = /^\/\/\/\s*<(\S+)\s.*?\/>/im;
76047604
const singleLinePragmaRegEx = /^\/\/\/?\s*@(\S+)\s*(.*)\s*$/im;
76057605
function extractPragmas(pragmas: PragmaPsuedoMapEntry[], range: CommentRange, text: string) {
7606-
const tripleSlash = tripleSlashXMLCommentStartRegEx.exec(text);
7606+
const tripleSlash = range.kind === SyntaxKind.SingleLineCommentTrivia && tripleSlashXMLCommentStartRegEx.exec(text);
76077607
if (tripleSlash) {
76087608
const name = tripleSlash[1].toLowerCase() as keyof PragmaPsuedoMap; // Technically unsafe cast, but we do it so the below check to make it safe typechecks
76097609
const pragma = commentPragmas[name] as PragmaDefinition;
@@ -7640,15 +7640,17 @@ namespace ts {
76407640
return;
76417641
}
76427642

7643-
const singleLine = singleLinePragmaRegEx.exec(text);
7643+
const singleLine = range.kind === SyntaxKind.SingleLineCommentTrivia && singleLinePragmaRegEx.exec(text);
76447644
if (singleLine) {
76457645
return addPragmaForMatch(pragmas, range, PragmaKindFlags.SingleLine, singleLine);
76467646
}
76477647

7648-
const multiLinePragmaRegEx = /\s*@(\S+)\s*(.*)\s*$/gim; // Defined inline since it uses the "g" flag, which keeps a persistent index (for iterating)
7649-
let multiLineMatch: RegExpExecArray;
7650-
while (multiLineMatch = multiLinePragmaRegEx.exec(text)) {
7651-
addPragmaForMatch(pragmas, range, PragmaKindFlags.MultiLine, multiLineMatch);
7648+
if (range.kind === SyntaxKind.MultiLineCommentTrivia) {
7649+
const multiLinePragmaRegEx = /\s*@(\S+)\s*(.*)\s*$/gim; // Defined inline since it uses the "g" flag, which keeps a persistent index (for iterating)
7650+
let multiLineMatch: RegExpExecArray;
7651+
while (multiLineMatch = multiLinePragmaRegEx.exec(text)) {
7652+
addPragmaForMatch(pragmas, range, PragmaKindFlags.MultiLine, multiLineMatch);
7653+
}
76527654
}
76537655
}
76547656

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tripleSlashInCommentNotParsed.ts]
2+
/*
3+
/// <reference path="non-existing-file.d.ts" />
4+
*/
5+
void 0;
6+
7+
//// [tripleSlashInCommentNotParsed.js]
8+
/*
9+
/// <reference path="non-existing-file.d.ts" />
10+
*/
11+
void 0;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/tripleSlashInCommentNotParsed.ts ===
2+
/*
3+
No type information for this code./// <reference path="non-existing-file.d.ts" />
4+
No type information for this code.*/
5+
No type information for this code.void 0;
6+
No type information for this code.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/tripleSlashInCommentNotParsed.ts ===
2+
/*
3+
/// <reference path="non-existing-file.d.ts" />
4+
*/
5+
void 0;
6+
>void 0 : undefined
7+
>0 : 0
8+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
/// <reference path="non-existing-file.d.ts" />
3+
*/
4+
void 0;

0 commit comments

Comments
 (0)