Skip to content

Commit be838a1

Browse files
iFreilichtjoshgoebel
authored andcommitted
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 e87ba14 commit be838a1

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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
Developer Tool:
1011

@@ -14,6 +15,7 @@ Developer Tool:
1415
[Kristian Ekenes]: https://github.com/ekenes
1516
[Aneesh Kulkarni]: https://github.com/aneesh98
1617
[Bruno Meneguele]: https://github.com/bmeneg
18+
[Felix Uhl]: https://github.com/iFreilicht
1719

1820

1921
## Version 11.9.0

src/languages/bash.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export default function(hljs) {
3838
end: /\)/,
3939
contains: [ hljs.BACKSLASH_ESCAPE ]
4040
};
41+
const COMMENT = hljs.inherit(
42+
hljs.COMMENT(),
43+
{
44+
match: [
45+
/(^|\s)/,
46+
/#.*$/
47+
],
48+
scope: {
49+
2: 'comment'
50+
}
51+
}
52+
);
4153
const HERE_DOC = {
4254
begin: /<<-?\s*(?=\w+)/,
4355
starts: { contains: [
@@ -376,7 +388,7 @@ export default function(hljs) {
376388
hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
377389
FUNCTION,
378390
ARITHMETIC,
379-
hljs.HASH_COMMENT_MODE,
391+
COMMENT,
380392
HERE_DOC,
381393
PATH_MODE,
382394
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)