Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ New Grammars:

Core Grammars:

- fix(bash) keep heredoc continuation commands outside the string span, issue #4377 [puneetdixit200][]
- fix(javascript) correctly highlight 'for await' again [wolfgang42][]
- enh(csp) add missing directives / keywords from MDN (7 more) [Max Liashuk][]
- enh(ada) add new `parallel` keyword, allow `[]` for Ada 2022 [Max Reznik][]
Expand Down Expand Up @@ -55,6 +56,7 @@ CONTRIBUTORS
[te-ing]: https://github.com/te-ing
[Anthony Martin]: https://github.com/anthony-c-martin
[NriotHrreion]: https://github.com/NriotHrreion
[puneetdixit200]: https://github.com/puneetdixit200


## Version 11.11.1
Expand Down
71 changes: 47 additions & 24 deletions src/languages/bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ export default function(hljs) {
}
}
);
const HERE_DOC = {
begin: /<<-?\s*(?=\w+)/,
starts: { contains: [
hljs.END_SAME_AS_BEGIN({
begin: /(\w+)/,
end: /(\w+)/,
className: 'string'
})
] }
};
const QUOTE_STRING = {
className: 'string',
begin: /"/,
Expand Down Expand Up @@ -368,27 +358,60 @@ export default function(hljs) {
"whoami",
"yes"
];
const BASH_KEYWORDS = {
$pattern: /\b[a-z][a-z0-9._-]+\b/,
keyword: KEYWORDS,
literal: LITERALS,
built_in: [
...SHELL_BUILT_INS,
...BASH_BUILT_INS,
// Shell modifiers
"set",
"shopt",
...ZSH_BUILT_INS,
...GNU_CORE_UTILS
]
};
let heredocDelimiter = "";
const HERE_DOC_BODY = {
className: 'string',
begin: /\n/,
end: /^\t*(\w+)$/m,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the requirement for heredoc naming in Bash? Could this be tightened up any further than \w+?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs broadening rather than tightening. Bash takes a word after << and applies quote removal; it is not limited to \w+.

For example, this branch captures only END here and never finds the real terminator, so echo done stays inside the string:

cat <<END-OF | cat
hello
END-OF
echo done

<<'EOF' is valid too, but is not recognized as a heredoc by this matcher. Could we add both as markup cases before settling on the delimiter match?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Add all the cases we know of, then try to get the highlighted correctly - that's the way to do it.

endsParent: true,
'on:end': (m, resp) => {
if (m[1] !== heredocDelimiter) resp.ignoreMatch();
}
};
const HERE_DOC = {
begin: /<<-?\s*(\w+)/,
'on:begin': (m) => { heredocDelimiter = m[1]; },
end: /\n/,
returnEnd: true,
keywords: BASH_KEYWORDS,
contains: [
ARITHMETIC,
COMMENT,
PATH_MODE,
QUOTE_STRING,
ESCAPED_QUOTE,
APOS_STRING,
ESCAPED_APOS,
VAR
Comment on lines +391 to +399

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this is because of the pipe and piping the string into any sequence of bash, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only pipes. The same split is needed for any shell syntax after the delimiter word, for example <<EOF >out or <<EOF && echo queued. The body should start at the following newline; everything before that newline should remain normal Bash.

],
starts: {
contains: [
HERE_DOC_BODY
]
}
};

return {
name: 'Bash',
aliases: [
'sh',
'zsh'
],
keywords: {
$pattern: /\b[a-z][a-z0-9._-]+\b/,
keyword: KEYWORDS,
literal: LITERALS,
built_in: [
...SHELL_BUILT_INS,
...BASH_BUILT_INS,
// Shell modifiers
"set",
"shopt",
...ZSH_BUILT_INS,
...GNU_CORE_UTILS
]
},
keywords: BASH_KEYWORDS,
contains: [
KNOWN_SHEBANG, // to catch known shells and boost relevancy
hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
Expand Down
3 changes: 3 additions & 0 deletions test/markup/bash/heredoc-continuation.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;ok&quot;</span> &lt;&lt;-EOF | <span class="hljs-built_in">cat</span><span class="hljs-string">
Hello
EOF</span>
3 changes: 3 additions & 0 deletions test/markup/bash/heredoc-continuation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "ok" <<-EOF | cat
Hello
EOF
2 changes: 1 addition & 1 deletion test/markup/bash/strings.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SCRIPT_DIR=<span class="hljs-string">&quot;<span class="hljs-subst">$( cd <span
TLS_DIR=<span class="hljs-string">&quot;<span class="hljs-variable">$SCRIPT_DIR</span>/../src/main/resources/tls&quot;</span>
ROOT_DIR=<span class="hljs-string">&quot;<span class="hljs-variable">$SCRIPT_DIR</span>/..&quot;</span>

jshell -s - &lt;&lt; <span class="hljs-string">EOF
jshell -s - &lt;&lt; EOF<span class="hljs-string">
System.out.printf(&quot;Procs: %s%n&quot;, getdata())
EOF</span>

Expand Down