Skip to content

Commit

Permalink
enh(bash) Less false positives for keywords in arguments (#2669)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sirosen committed Sep 5, 2020
1 parent 621ba9d commit 01f049b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Big picture:

Language Improvements:

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


## Version 10.1.1
Expand Down
2 changes: 1 addition & 1 deletion src/languages/bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function(hljs) {
name: 'Bash',
aliases: ['sh', 'zsh'],
keywords: {
$pattern: /\b-?[a-z\._]+\b/,
$pattern: /\b-?[a-z\._-]+\b/,
keyword:
'if then else elif fi for while in do done case esac function',
literal:
Expand Down
10 changes: 10 additions & 0 deletions test/markup/bash/token-containing-keyword.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<span class="hljs-comment"># a keyword as part of an option</span>
mycmd --disable-foo

<span class="hljs-comment"># a keyword as part of a parameter</span>
some-cmd set-some-setting
some-cmd set_some_setting

<span class="hljs-comment"># a keyword as part of command</span>
check-case foo
check_case foo
10 changes: 10 additions & 0 deletions test/markup/bash/token-containing-keyword.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# a keyword as part of an option
mycmd --disable-foo

# a keyword as part of a parameter
some-cmd set-some-setting
some-cmd set_some_setting

# a keyword as part of command
check-case foo
check_case foo

0 comments on commit 01f049b

Please sign in to comment.