Skip to content

Commit 9609244

Browse files
committed
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
1 parent 3d5d07e commit 9609244

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ Core Grammars:
55
- fix(css) fix overly greedy pseudo class matching [Bradley Mackey][]
66
- enh(arcade) updated to ArcGIS Arcade version 1.24 [Kristian Ekenes][]
77
- fix(yaml) fix for yaml with keys having brackets highlighted incorrectly [Aneesh Kulkarni][]
8+
- fix(bash) fix # within token being detected as the start of a comment [Felix Uhl][]
89

910
[Bradley Mackey]: https://github.com/bradleymackey
1011
[Kristian Ekenes]: https://github.com/ekenes
1112
[Aneesh Kulkarni]: https://github.com/aneesh98
1213
[Bruno Meneguele]: https://github.com/bmeneg
14+
[Felix Uhl]: https://github.com/iFreilicht
1315

1416

1517
## Version 11.9.0

src/languages/bash.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ export default function(hljs) {
3838
end: /\)/,
3939
contains: [ hljs.BACKSLASH_ESCAPE ]
4040
};
41+
const COMMENT = hljs.inherit(hljs.HASH_COMMENT_MODE, {
42+
begin: undefined,
43+
end: undefined,
44+
match: [
45+
/(^|\s)(?=#)/,
46+
'#.*$'
47+
],
48+
scope: {
49+
2: 'comment'
50+
},
51+
});
4152
const HERE_DOC = {
4253
begin: /<<-?\s*(?=\w+)/,
4354
starts: { contains: [
@@ -376,7 +387,7 @@ export default function(hljs) {
376387
hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
377388
FUNCTION,
378389
ARITHMETIC,
379-
hljs.HASH_COMMENT_MODE,
390+
COMMENT,
380391
HERE_DOC,
381392
PATH_MODE,
382393
QUOTE_STRING,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span class="hljs-built_in">echo</span> asdf#qwert yuiop
2+
3+
<span class="hljs-built_in">echo</span> asdf <span class="hljs-comment">#qwert yuiop</span>

test/markup/bash/not-comments.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo asdf#qwert yuiop
2+
3+
echo asdf #qwert yuiop

0 commit comments

Comments
 (0)