Include guards on comment kind in pragma extraction (#23313)

This commit is contained in:
Wesley Wigham 2018-04-10 20:50:31 -07:00 committed by GitHub
parent b2e0c4bea6
commit 8a2b4646f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 6 deletions

View file

@ -7603,7 +7603,7 @@ namespace ts {
const tripleSlashXMLCommentStartRegEx = /^\/\/\/\s*<(\S+)\s.*?\/>/im;
const singleLinePragmaRegEx = /^\/\/\/?\s*@(\S+)\s*(.*)\s*$/im;
function extractPragmas(pragmas: PragmaPsuedoMapEntry[], range: CommentRange, text: string) {
const tripleSlash = tripleSlashXMLCommentStartRegEx.exec(text);
const tripleSlash = range.kind === SyntaxKind.SingleLineCommentTrivia && tripleSlashXMLCommentStartRegEx.exec(text);
if (tripleSlash) {
const name = tripleSlash[1].toLowerCase() as keyof PragmaPsuedoMap; // Technically unsafe cast, but we do it so the below check to make it safe typechecks
const pragma = commentPragmas[name] as PragmaDefinition;
@ -7640,15 +7640,17 @@ namespace ts {
return;
}
const singleLine = singleLinePragmaRegEx.exec(text);
const singleLine = range.kind === SyntaxKind.SingleLineCommentTrivia && singleLinePragmaRegEx.exec(text);
if (singleLine) {
return addPragmaForMatch(pragmas, range, PragmaKindFlags.SingleLine, singleLine);
}
const multiLinePragmaRegEx = /\s*@(\S+)\s*(.*)\s*$/gim; // Defined inline since it uses the "g" flag, which keeps a persistent index (for iterating)
let multiLineMatch: RegExpExecArray;
while (multiLineMatch = multiLinePragmaRegEx.exec(text)) {
addPragmaForMatch(pragmas, range, PragmaKindFlags.MultiLine, multiLineMatch);
if (range.kind === SyntaxKind.MultiLineCommentTrivia) {
const multiLinePragmaRegEx = /\s*@(\S+)\s*(.*)\s*$/gim; // Defined inline since it uses the "g" flag, which keeps a persistent index (for iterating)
let multiLineMatch: RegExpExecArray;
while (multiLineMatch = multiLinePragmaRegEx.exec(text)) {
addPragmaForMatch(pragmas, range, PragmaKindFlags.MultiLine, multiLineMatch);
}
}
}

View file

@ -0,0 +1,11 @@
//// [tripleSlashInCommentNotParsed.ts]
/*
/// <reference path="non-existing-file.d.ts" />
*/
void 0;
//// [tripleSlashInCommentNotParsed.js]
/*
/// <reference path="non-existing-file.d.ts" />
*/
void 0;

View file

@ -0,0 +1,6 @@
=== tests/cases/compiler/tripleSlashInCommentNotParsed.ts ===
/*
No type information for this code./// <reference path="non-existing-file.d.ts" />
No type information for this code.*/
No type information for this code.void 0;
No type information for this code.

View file

@ -0,0 +1,8 @@
=== tests/cases/compiler/tripleSlashInCommentNotParsed.ts ===
/*
/// <reference path="non-existing-file.d.ts" />
*/
void 0;
>void 0 : undefined
>0 : 0

View file

@ -0,0 +1,4 @@
/*
/// <reference path="non-existing-file.d.ts" />
*/
void 0;