From 8a2b4646f01bb1357b4d68eddff0d7f827d23432 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 10 Apr 2018 20:50:31 -0700 Subject: [PATCH] Include guards on comment kind in pragma extraction (#23313) --- src/compiler/parser.ts | 14 ++++++++------ .../reference/tripleSlashInCommentNotParsed.js | 11 +++++++++++ .../tripleSlashInCommentNotParsed.symbols | 6 ++++++ .../reference/tripleSlashInCommentNotParsed.types | 8 ++++++++ .../compiler/tripleSlashInCommentNotParsed.ts | 4 ++++ 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/tripleSlashInCommentNotParsed.js create mode 100644 tests/baselines/reference/tripleSlashInCommentNotParsed.symbols create mode 100644 tests/baselines/reference/tripleSlashInCommentNotParsed.types create mode 100644 tests/cases/compiler/tripleSlashInCommentNotParsed.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0b89c3c313..ef15c57f1d 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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); + } } } diff --git a/tests/baselines/reference/tripleSlashInCommentNotParsed.js b/tests/baselines/reference/tripleSlashInCommentNotParsed.js new file mode 100644 index 0000000000..5b16e26c4f --- /dev/null +++ b/tests/baselines/reference/tripleSlashInCommentNotParsed.js @@ -0,0 +1,11 @@ +//// [tripleSlashInCommentNotParsed.ts] +/* +/// +*/ +void 0; + +//// [tripleSlashInCommentNotParsed.js] +/* +/// +*/ +void 0; diff --git a/tests/baselines/reference/tripleSlashInCommentNotParsed.symbols b/tests/baselines/reference/tripleSlashInCommentNotParsed.symbols new file mode 100644 index 0000000000..3df0d7dfdd --- /dev/null +++ b/tests/baselines/reference/tripleSlashInCommentNotParsed.symbols @@ -0,0 +1,6 @@ +=== tests/cases/compiler/tripleSlashInCommentNotParsed.ts === +/* +No type information for this code./// +No type information for this code.*/ +No type information for this code.void 0; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/tripleSlashInCommentNotParsed.types b/tests/baselines/reference/tripleSlashInCommentNotParsed.types new file mode 100644 index 0000000000..c4390cce54 --- /dev/null +++ b/tests/baselines/reference/tripleSlashInCommentNotParsed.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/tripleSlashInCommentNotParsed.ts === +/* +/// +*/ +void 0; +>void 0 : undefined +>0 : 0 + diff --git a/tests/cases/compiler/tripleSlashInCommentNotParsed.ts b/tests/cases/compiler/tripleSlashInCommentNotParsed.ts new file mode 100644 index 0000000000..34ab70ace3 --- /dev/null +++ b/tests/cases/compiler/tripleSlashInCommentNotParsed.ts @@ -0,0 +1,4 @@ +/* +/// +*/ +void 0; \ No newline at end of file