Commit a5d7178cadb6ae2c34f1cbfc031dd71806dfdcaf

Michael Schmidt 2021-01-17T14:34:24

F#: Fixed comment false positive (#2703) F# has supports `(*)` to convert an operator into a function. This syntax collides with F# comments and produced false positives for comments. The comment pattern has been adjusted to not match `(*)`.

diff --git a/components/prism-fsharp.js b/components/prism-fsharp.js
index 442e689..4b2ac05 100644
--- a/components/prism-fsharp.js
+++ b/components/prism-fsharp.js
@@ -1,7 +1,7 @@
 Prism.languages.fsharp = Prism.languages.extend('clike', {
 	'comment': [
 		{
-			pattern: /(^|[^\\])\(\*[\s\S]*?\*\)/,
+			pattern: /(^|[^\\])\(\*(?!\))[\s\S]*?\*\)/,
 			lookbehind: true
 		},
 		{
diff --git a/components/prism-fsharp.min.js b/components/prism-fsharp.min.js
index c44f1dd..3153afb 100644
--- a/components/prism-fsharp.min.js
+++ b/components/prism-fsharp.min.js
@@ -1 +1 @@
-Prism.languages.fsharp=Prism.languages.extend("clike",{comment:[{pattern:/(^|[^\\])\(\*[\s\S]*?\*\)/,lookbehind:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0}],string:{pattern:/(?:"""[\s\S]*?"""|@"(?:""|[^"])*"|"(?:\\[\s\S]|[^\\"])*")B?|'(?:[^\\']|\\(?:.|\d{3}|x[a-fA-F\d]{2}|u[a-fA-F\d]{4}|U[a-fA-F\d]{8}))'B?/,greedy:!0},"class-name":{pattern:/(\b(?:exception|inherit|interface|new|of|type)\s+|\w\s*:\s*|\s:\??>\s*)[.\w]+\b(?:\s*(?:->|\*)\s*[.\w]+\b)*(?!\s*[:.])/,lookbehind:!0,inside:{operator:/->|\*/,punctuation:/\./}},keyword:/\b(?:let|return|use|yield)(?:!\B|\b)|\b(?:abstract|and|as|assert|base|begin|class|default|delegate|do|done|downcast|downto|elif|else|end|exception|extern|false|finally|for|fun|function|global|if|in|inherit|inline|interface|internal|lazy|match|member|module|mutable|namespace|new|not|null|of|open|or|override|private|public|rec|select|static|struct|then|to|true|try|type|upcast|val|void|when|while|with|asr|land|lor|lsl|lsr|lxor|mod|sig|atomic|break|checked|component|const|constraint|constructor|continue|eager|event|external|fixed|functor|include|method|mixin|object|parallel|process|protected|pure|sealed|tailcall|trait|virtual|volatile)\b/,number:[/\b0x[\da-fA-F]+(?:un|lf|LF)?\b/,/\b0b[01]+(?:y|uy)?\b/,/(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[fm]|e[+-]?\d+)?\b/i,/\b\d+(?:[IlLsy]|u[lsy]?|UL)?\b/],operator:/([<>~&^])\1\1|([*.:<>&])\2|<-|->|[!=:]=|<?\|{1,3}>?|\??(?:<=|>=|<>|[-+*/%=<>])\??|[!?^&]|~[+~-]|:>|:\?>?/}),Prism.languages.insertBefore("fsharp","keyword",{preprocessor:{pattern:/^[^\r\n\S]*#.*/m,alias:"property",inside:{directive:{pattern:/(\s*#)\b(?:else|endif|if|light|line|nowarn)\b/,lookbehind:!0,alias:"keyword"}}}}),Prism.languages.insertBefore("fsharp","punctuation",{"computation-expression":{pattern:/[_a-z]\w*(?=\s*\{)/i,alias:"keyword"}}),Prism.languages.insertBefore("fsharp","string",{annotation:{pattern:/\[<.+?>\]/,inside:{punctuation:/^\[<|>\]$/,"class-name":{pattern:/^\w+$|(^|;\s*)[A-Z]\w*(?=\()/,lookbehind:!0},"annotation-content":{pattern:/[\s\S]+/,inside:Prism.languages.fsharp}}}});
\ No newline at end of file
+Prism.languages.fsharp=Prism.languages.extend("clike",{comment:[{pattern:/(^|[^\\])\(\*(?!\))[\s\S]*?\*\)/,lookbehind:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0}],string:{pattern:/(?:"""[\s\S]*?"""|@"(?:""|[^"])*"|"(?:\\[\s\S]|[^\\"])*")B?|'(?:[^\\']|\\(?:.|\d{3}|x[a-fA-F\d]{2}|u[a-fA-F\d]{4}|U[a-fA-F\d]{8}))'B?/,greedy:!0},"class-name":{pattern:/(\b(?:exception|inherit|interface|new|of|type)\s+|\w\s*:\s*|\s:\??>\s*)[.\w]+\b(?:\s*(?:->|\*)\s*[.\w]+\b)*(?!\s*[:.])/,lookbehind:!0,inside:{operator:/->|\*/,punctuation:/\./}},keyword:/\b(?:let|return|use|yield)(?:!\B|\b)|\b(?:abstract|and|as|assert|base|begin|class|default|delegate|do|done|downcast|downto|elif|else|end|exception|extern|false|finally|for|fun|function|global|if|in|inherit|inline|interface|internal|lazy|match|member|module|mutable|namespace|new|not|null|of|open|or|override|private|public|rec|select|static|struct|then|to|true|try|type|upcast|val|void|when|while|with|asr|land|lor|lsl|lsr|lxor|mod|sig|atomic|break|checked|component|const|constraint|constructor|continue|eager|event|external|fixed|functor|include|method|mixin|object|parallel|process|protected|pure|sealed|tailcall|trait|virtual|volatile)\b/,number:[/\b0x[\da-fA-F]+(?:un|lf|LF)?\b/,/\b0b[01]+(?:y|uy)?\b/,/(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[fm]|e[+-]?\d+)?\b/i,/\b\d+(?:[IlLsy]|u[lsy]?|UL)?\b/],operator:/([<>~&^])\1\1|([*.:<>&])\2|<-|->|[!=:]=|<?\|{1,3}>?|\??(?:<=|>=|<>|[-+*/%=<>])\??|[!?^&]|~[+~-]|:>|:\?>?/}),Prism.languages.insertBefore("fsharp","keyword",{preprocessor:{pattern:/^[^\r\n\S]*#.*/m,alias:"property",inside:{directive:{pattern:/(\s*#)\b(?:else|endif|if|light|line|nowarn)\b/,lookbehind:!0,alias:"keyword"}}}}),Prism.languages.insertBefore("fsharp","punctuation",{"computation-expression":{pattern:/[_a-z]\w*(?=\s*\{)/i,alias:"keyword"}}),Prism.languages.insertBefore("fsharp","string",{annotation:{pattern:/\[<.+?>\]/,inside:{punctuation:/^\[<|>\]$/,"class-name":{pattern:/^\w+$|(^|;\s*)[A-Z]\w*(?=\()/,lookbehind:!0},"annotation-content":{pattern:/[\s\S]+/,inside:Prism.languages.fsharp}}}});
\ No newline at end of file
diff --git a/tests/languages/fsharp/comment_feature.test b/tests/languages/fsharp/comment_feature.test
index ff9170b..4484944 100644
--- a/tests/languages/fsharp/comment_feature.test
+++ b/tests/languages/fsharp/comment_feature.test
@@ -3,14 +3,26 @@
 (* foo
 bar *)
 
+// the next one is not a comment
+(*) (*)
+
 ----------------------------------------------------
 
 [
 	["comment", "// foobar"],
 	["comment", "(**)"],
-	["comment", "(* foo\r\nbar *)"]
+	["comment", "(* foo\r\nbar *)"],
+
+	["comment", "// the next one is not a comment"],
+
+	["punctuation", "("],
+	["operator", "*"],
+	["punctuation", ")"],
+	["punctuation", "("],
+	["operator", "*"],
+	["punctuation", ")"]
 ]
 
 ----------------------------------------------------
 
-Checks for single-line and multi-line comments.
\ No newline at end of file
+Checks for single-line and multi-line comments.
diff --git a/tests/languages/fsharp/issue2696.test b/tests/languages/fsharp/issue2696.test
new file mode 100644
index 0000000..1e7d26f
--- /dev/null
+++ b/tests/languages/fsharp/issue2696.test
@@ -0,0 +1,285 @@
+let score category (dice:Die list) =
+    let iDice = dice |> List.map int |> List.sortDescending
+    let diced = iDice |> List.countBy id |> List.sortByDescending snd
+    let countScore cat = dice |> List.filter (fun d -> d=cat) |> List.length |> (*) (int cat)
+    let isStraight = iDice.[0] - iDice.[4] = 4
+
+    match category  , List.map snd diced  with
+    | Yacht         , [5]         -> 50
+    | Ones          , _           -> countScore Die.One
+    | Twos          , _           -> countScore Die.Two
+    | Threes        , _           -> countScore Die.Three
+    | Fours         , _           -> countScore Die.Four
+    | Fives         , _           -> countScore Die.Five
+    | Sixes         , _           -> countScore Die.Six
+    | FourOfAKind   , [4;1]
+    | FourOfAKind   , [5]         -> iDice |> List.head |> (*) 4
+    | LittleStraight, [1;1;1;1;1] when isStraight && iDice.[0] = 5 -> 30
+    | BigStraight   , [1;1;1;1;1] when isStraight && iDice.[0] = 6 -> 30
+    | FullHouse     , [3;2]
+    | Choice        , _           -> iDice |> List.sum
+    | _             , _           -> 0
+
+----------------------------------------------------
+
+[
+	["keyword", "let"],
+	" score category ",
+	["punctuation", "("],
+	"dice",
+	["punctuation", ":"],
+	["class-name", ["Die"]],
+	" list",
+	["punctuation", ")"],
+	["operator", "="],
+
+	["keyword", "let"],
+	" iDice ",
+	["operator", "="],
+	" dice ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"map int ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"sortDescending\r\n    ",
+
+	["keyword", "let"],
+	" diced ",
+	["operator", "="],
+	" iDice ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"countBy id ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"sortByDescending snd\r\n    ",
+
+	["keyword", "let"],
+	" countScore cat ",
+	["operator", "="],
+	" dice ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"filter ",
+	["punctuation", "("],
+	["keyword", "fun"],
+	" d ",
+	["operator", "->"],
+	" d",
+	["operator", "="],
+	"cat",
+	["punctuation", ")"],
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"length ",
+	["operator", "|>"],
+	["punctuation", "("],
+	["operator", "*"],
+	["punctuation", ")"],
+	["punctuation", "("],
+	"int cat",
+	["punctuation", ")"],
+
+	["keyword", "let"],
+	" isStraight ",
+	["operator", "="],
+	" iDice",
+	["punctuation", "."],
+	["punctuation", "["],
+	["number", "0"],
+	["punctuation", "]"],
+	["operator", "-"],
+	" iDice",
+	["punctuation", "."],
+	["punctuation", "["],
+	["number", "4"],
+	["punctuation", "]"],
+	["operator", "="],
+	["number", "4"],
+
+	["keyword", "match"],
+	" category  ",
+	["punctuation", ","],
+	" List",
+	["punctuation", "."],
+	"map snd diced  ",
+	["keyword", "with"],
+
+	["operator", "|"],
+	" Yacht         ",
+	["punctuation", ","],
+	["punctuation", "["],
+	["number", "5"],
+	["punctuation", "]"],
+	["operator", "->"],
+	["number", "50"],
+
+	["operator", "|"],
+	" Ones          ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" countScore Die",
+	["punctuation", "."],
+	"One\r\n    ",
+
+	["operator", "|"],
+	" Twos          ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" countScore Die",
+	["punctuation", "."],
+	"Two\r\n    ",
+
+	["operator", "|"],
+	" Threes        ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" countScore Die",
+	["punctuation", "."],
+	"Three\r\n    ",
+
+	["operator", "|"],
+	" Fours         ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" countScore Die",
+	["punctuation", "."],
+	"Four\r\n    ",
+
+	["operator", "|"],
+	" Fives         ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" countScore Die",
+	["punctuation", "."],
+	"Five\r\n    ",
+
+	["operator", "|"],
+	" Sixes         ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" countScore Die",
+	["punctuation", "."],
+	"Six\r\n    ",
+
+	["operator", "|"],
+	" FourOfAKind   ",
+	["punctuation", ","],
+	["punctuation", "["],
+	["number", "4"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", "]"],
+
+	["operator", "|"],
+	" FourOfAKind   ",
+	["punctuation", ","],
+	["punctuation", "["],
+	["number", "5"],
+	["punctuation", "]"],
+	["operator", "->"],
+	" iDice ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"head ",
+	["operator", "|>"],
+	["punctuation", "("],
+	["operator", "*"],
+	["punctuation", ")"],
+	["number", "4"],
+
+	["operator", "|"],
+	" LittleStraight",
+	["punctuation", ","],
+	["punctuation", "["],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", "]"],
+	["keyword", "when"],
+	" isStraight ",
+	["operator", "&&"],
+	" iDice",
+	["punctuation", "."],
+	["punctuation", "["],
+	["number", "0"],
+	["punctuation", "]"],
+	["operator", "="],
+	["number", "5"],
+	["operator", "->"],
+	["number", "30"],
+
+	["operator", "|"],
+	" BigStraight   ",
+	["punctuation", ","],
+	["punctuation", "["],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", ";"],
+	["number", "1"],
+	["punctuation", "]"],
+	["keyword", "when"],
+	" isStraight ",
+	["operator", "&&"],
+	" iDice",
+	["punctuation", "."],
+	["punctuation", "["],
+	["number", "0"],
+	["punctuation", "]"],
+	["operator", "="],
+	["number", "6"],
+	["operator", "->"],
+	["number", "30"],
+
+	["operator", "|"],
+	" FullHouse     ",
+	["punctuation", ","],
+	["punctuation", "["],
+	["number", "3"],
+	["punctuation", ";"],
+	["number", "2"],
+	["punctuation", "]"],
+
+	["operator", "|"],
+	" Choice        ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	" iDice ",
+	["operator", "|>"],
+	" List",
+	["punctuation", "."],
+	"sum\r\n    ",
+
+	["operator", "|"],
+	" _             ",
+	["punctuation", ","],
+	" _           ",
+	["operator", "->"],
+	["number", "0"]
+]
\ No newline at end of file