Skip to content

Commit 01f049b

Browse files
authored
enh(bash) Less false positives for keywords in arguments (highlightjs#2669)
In bash, option, param, and command names may contain builtins as hyphen-delimited components. As in `foo --set-bar`. (where `set` is a keyword and therefore incorrectly highlighted) In order to handle this, just add `-` to the pattern's set of word characters. Includes a new test for bash tokens containing keywords, which checks hyphens and underscores for a few common cases.
1 parent 621ba9d commit 01f049b

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Big picture:
1818

1919
Language Improvements:
2020

21+
- fix(bash) Fewer false positives for keywords in arguments (#2669) [sirosen][]
2122
- fix(js) Prevent long series of /////// from causing freezes (#2656) [Josh Goebel][]
2223
- enh(csharp) Add `init` and `record` keywords for C# 9.0 (#2660) [Youssef Victor][]
2324
- enh(matlab) Add new R2019b `arguments` keyword and fix `enumeration` keyword (#2619) [Andrew Janke][]
@@ -36,6 +37,7 @@ Language Improvements:
3637
[ezksd]: https://github.com/ezksd
3738
[idleberg]: https://github.com/idleberg
3839
[eytienne]: https://github.com/eytienne
40+
[sirosen]: https://github.com/sirosen
3941

4042

4143
## Version 10.1.1

src/languages/bash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default function(hljs) {
8383
name: 'Bash',
8484
aliases: ['sh', 'zsh'],
8585
keywords: {
86-
$pattern: /\b-?[a-z\._]+\b/,
86+
$pattern: /\b-?[a-z\._-]+\b/,
8787
keyword:
8888
'if then else elif fi for while in do done case esac function',
8989
literal:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<span class="hljs-comment"># a keyword as part of an option</span>
2+
mycmd --disable-foo
3+
4+
<span class="hljs-comment"># a keyword as part of a parameter</span>
5+
some-cmd set-some-setting
6+
some-cmd set_some_setting
7+
8+
<span class="hljs-comment"># a keyword as part of command</span>
9+
check-case foo
10+
check_case foo
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# a keyword as part of an option
2+
mycmd --disable-foo
3+
4+
# a keyword as part of a parameter
5+
some-cmd set-some-setting
6+
some-cmd set_some_setting
7+
8+
# a keyword as part of command
9+
check-case foo
10+
check_case foo

0 commit comments

Comments
 (0)