From 00d37268e8fd1e52fd90f1616d8a21802dd83b0a Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 23 Aug 2019 12:55:10 -0700 Subject: [PATCH] Make triple-slash comment classification more restrictive It was overly permissive and ended up making a mess of C#-style comments: `/// Text` Now it checks the element name. Attribute names remain unchecked. --- src/services/classifier.ts | 12 ++++++++---- .../syntacticClassificationsTripleSlash17.ts | 7 +++++++ .../syntacticClassificationsTripleSlash18.ts | 7 +++++++ .../syntacticClassificationsTripleSlash4.ts | 3 +-- 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 tests/cases/fourslash/syntacticClassificationsTripleSlash17.ts create mode 100644 tests/cases/fourslash/syntacticClassificationsTripleSlash18.ts diff --git a/src/services/classifier.ts b/src/services/classifier.ts index 1e1309a2eac..334417ef549 100644 --- a/src/services/classifier.ts +++ b/src/services/classifier.ts @@ -771,6 +771,14 @@ namespace ts { return false; } + // Limiting classification to exactly the elements and attributes + // defined in `ts.commentPragmas` would be excessive, but we can avoid + // some obvious false positives (e.g. in XML-like doc comments) by + // checking the element name. + if (!match[3] || !(commentPragmas as any)[match[3]]) { + return false; + } + let pos = start; pushCommentRange(pos, match[1].length); // /// @@ -779,10 +787,6 @@ namespace ts { pushClassification(pos, match[2].length, ClassificationType.punctuation); // < pos += match[2].length; - if (!match[3]) { - return true; - } - pushClassification(pos, match[3].length, ClassificationType.jsxSelfClosingTagName); // element name pos += match[3].length; diff --git a/tests/cases/fourslash/syntacticClassificationsTripleSlash17.ts b/tests/cases/fourslash/syntacticClassificationsTripleSlash17.ts new file mode 100644 index 00000000000..b95cdd6b6c0 --- /dev/null +++ b/tests/cases/fourslash/syntacticClassificationsTripleSlash17.ts @@ -0,0 +1,7 @@ +/// + +//// /// Text + +var c = classification; +verify.syntacticClassificationsAre( + c.comment("/// Text")); \ No newline at end of file diff --git a/tests/cases/fourslash/syntacticClassificationsTripleSlash18.ts b/tests/cases/fourslash/syntacticClassificationsTripleSlash18.ts new file mode 100644 index 00000000000..148b17f6e45 --- /dev/null +++ b/tests/cases/fourslash/syntacticClassificationsTripleSlash18.ts @@ -0,0 +1,7 @@ +/// + +//// /// Text + +var c = classification; +verify.syntacticClassificationsAre( + c.comment("/// Text")); \ No newline at end of file diff --git a/tests/cases/fourslash/syntacticClassificationsTripleSlash4.ts b/tests/cases/fourslash/syntacticClassificationsTripleSlash4.ts index e656c811377..d407bcee503 100644 --- a/tests/cases/fourslash/syntacticClassificationsTripleSlash4.ts +++ b/tests/cases/fourslash/syntacticClassificationsTripleSlash4.ts @@ -4,5 +4,4 @@ var c = classification; verify.syntacticClassificationsAre( - c.comment("/// "), - c.punctuation("<")); \ No newline at end of file + c.comment("/// <")); // Don't classify until we recognize the element name \ No newline at end of file