Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(bash): Comments false positives #3918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

iFreilicht
Copy link
Contributor

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

  • Added markup tests
  • Updated the changelog at CHANGES.md

@joshgoebel
Copy link
Member

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 iFreilicht force-pushed the fix-bash-comments branch 2 times, most recently from 9609244 to 2a3c7e1 Compare November 2, 2023 23:16
@iFreilicht
Copy link
Contributor Author

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

Copy link

github-actions bot commented Nov 3, 2023

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

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 highlightjs#3917
Copy link

github-actions bot commented Nov 3, 2023

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

@joshgoebel joshgoebel merged commit d78749a into highlightjs:main Nov 3, 2023
@iFreilicht iFreilicht deleted the fix-bash-comments branch November 3, 2023 10:20
wolfgang42 added a commit to wolfgang42/highlight.js that referenced this pull request Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positives for comments in bash
2 participants