From be838a1ced9cce3a1caeaabe9f8cd34570801b92 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Thu, 2 Nov 2023 23:04:50 +0100 Subject: [PATCH] fix(bash): Comments false positives In POSIX shells, the `#` needs to be after a whitespace or start-of-line to delimit a comment. Compare: ```sh $ echo asdf#qwert asdf#qwert $ echo asdf #qwert asdf ``` I checked a few other languages as well that use #-delimited comments, but it seems this behaviour is unique to bash and other POSIX shells. Fixes #3917 --- CHANGES.md | 2 ++ src/languages/bash.js | 14 +++++++++++++- test/markup/bash/not-comments.expect.txt | 3 +++ test/markup/bash/not-comments.txt | 3 +++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/markup/bash/not-comments.expect.txt create mode 100644 test/markup/bash/not-comments.txt diff --git a/CHANGES.md b/CHANGES.md index 9157f67300..ac9c8d48c9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ Core Grammars: - fix(css) fix overly greedy pseudo class matching [Bradley Mackey][] - enh(arcade) updated to ArcGIS Arcade version 1.24 [Kristian Ekenes][] - fix(yaml) fix for yaml with keys having brackets highlighted incorrectly [Aneesh Kulkarni][] +- fix(bash) fix # within token being detected as the start of a comment [Felix Uhl][] Developer Tool: @@ -14,6 +15,7 @@ Developer Tool: [Kristian Ekenes]: https://github.com/ekenes [Aneesh Kulkarni]: https://github.com/aneesh98 [Bruno Meneguele]: https://github.com/bmeneg +[Felix Uhl]: https://github.com/iFreilicht ## Version 11.9.0 diff --git a/src/languages/bash.js b/src/languages/bash.js index 17295c7f54..c12c3c18bc 100644 --- a/src/languages/bash.js +++ b/src/languages/bash.js @@ -38,6 +38,18 @@ export default function(hljs) { end: /\)/, contains: [ hljs.BACKSLASH_ESCAPE ] }; + const COMMENT = hljs.inherit( + hljs.COMMENT(), + { + match: [ + /(^|\s)/, + /#.*$/ + ], + scope: { + 2: 'comment' + } + } + ); const HERE_DOC = { begin: /<<-?\s*(?=\w+)/, starts: { contains: [ @@ -376,7 +388,7 @@ export default function(hljs) { hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang FUNCTION, ARITHMETIC, - hljs.HASH_COMMENT_MODE, + COMMENT, HERE_DOC, PATH_MODE, QUOTE_STRING, diff --git a/test/markup/bash/not-comments.expect.txt b/test/markup/bash/not-comments.expect.txt new file mode 100644 index 0000000000..ab12864283 --- /dev/null +++ b/test/markup/bash/not-comments.expect.txt @@ -0,0 +1,3 @@ +echo asdf#qwert yuiop + +echo asdf #qwert yuiop diff --git a/test/markup/bash/not-comments.txt b/test/markup/bash/not-comments.txt new file mode 100644 index 0000000000..b8f6f88383 --- /dev/null +++ b/test/markup/bash/not-comments.txt @@ -0,0 +1,3 @@ +echo asdf#qwert yuiop + +echo asdf #qwert yuiop