highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.31k stars 3.52k forks source link

fix(bash): Comments false positives #3918

Closed iFreilicht closed 8 months ago

iFreilicht commented 8 months ago

In POSIX shells, the # needs to be after a whitespace or start-of-line to delimit a comment. Compare:

$ echo asdf#qwert
asdf#qwert
$ echo asdf #qwert
asdf

This is fixed with this PR.

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

Inside the bash mode, HASH_COMMENT_MODE was wrapped in another mode that prevents a match if the # is not after the start of the line or a whitespace character.

Checklist

joshgoebel commented 8 months ago

We'd prefer to do this with a multi-matcher rather than nesting, see an example:

https://github.com/highlightjs/highlight.js/blob/main/src/languages/ruby.js#L247

And you could just use inherit to update the existing rule...

hljs.inherit(HASH_COMMENT_MODE, 
// the changes you want to merge in
{
  match: // ...
}
iFreilicht commented 8 months ago

Okay, done! I had to set begin and end to undefined, they seems to be incompatible with match.

github-actions[bot] commented 8 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

2 files changed

Total change +75 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/languages/bash.min.js | 1.54 KB | 1.58 KB | +37 B | | languages/bash.min.js | 1.55 KB | 1.59 KB | +38 B |
github-actions[bot] commented 8 months ago

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +69 B

View Changes | file | base | pr | diff | | --- | --- | --- | --- | | es/core.min.js | 8.14 KB | 8.13 KB | -2 B | | es/highlight.min.js | 8.14 KB | 8.13 KB | -2 B | | es/languages/bash.min.js | 1.54 KB | 1.58 KB | +37 B | | highlight.min.js | 8.17 KB | 8.17 KB | -2 B | | languages/bash.min.js | 1.55 KB | 1.59 KB | +38 B |