Skip to content

Commit

Permalink
Bash: Fixed single-quoted strings (#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Mar 17, 2021
1 parent d298d46 commit e5cfdb4
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 119 deletions.
17 changes: 16 additions & 1 deletion components/prism-bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,25 @@
},
// “Normal” string
{
pattern: /(^|[^\\](?:\\\\)*)(["'])(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|(?!\2)[^\\`$])*\2/,
// https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
pattern: /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/,
lookbehind: true,
greedy: true,
inside: insideString
},
{
// https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html
pattern: /(^|[^$\\])'[^']*'/,
lookbehind: true,
greedy: true
},
{
// https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
pattern: /\$'(?:[^'\\]|\\[\s\S])*'/,
greedy: true,
inside: {
'entity': insideString.entity
}
}
],
'environment': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-bash.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions components/prism-shell-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

var strings = [
// normal string
// 1 capturing group
/(["'])(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|(?!\1)[^\\`$])*\1/.source,
/"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/.source,
/'[^']*'/.source,
/\$'(?:[^'\\]|\\[\s\S])*'/.source,

// here doc
// 2 capturing groups
/<<-?\s*(["']?)(\w+)\2\s[\s\S]*?[\r\n]\3/.source
/<<-?\s*(["']?)(\w+)\1\s[\s\S]*?[\r\n]\2/.source
].join('|');

Prism.languages['shell-session'] = {
'command': {
pattern: RegExp(/^(?:[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+(?::[^\0-\x1F$#*?"<>:;|]+)?|[^\0-\x1F$#*?"<>:;|]+)?[$#](?:[^\\\r\n'"<]|\\.|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }), 'm'),
pattern: RegExp(/^(?:[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+(?::[^\0-\x1F$#*?"<>:;|]+)?|[^\0-\x1F$#*?"<>:;|]+)?[$#](?:[^\\\r\n'"<$]|\\.|\$(?!')|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }), 'm'),
greedy: true,
inside: {
'info': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-shell-session.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/languages/bash/assign-left_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ foo+=('xyz')
["assign-left", ["foo"]],
["operator", ["="]],
["number", "12"],

["assign-left", ["bar"]],
["operator", ["+="]],
["string", ["'xyz'"]],
["string", "'xyz'"],

["assign-left", ["foo"]],
["operator", ["+="]],
["punctuation", "("],
["string", ["'xyz'"]],
["string", "'xyz'"],
["punctuation", ")"]
]

Expand Down
65 changes: 52 additions & 13 deletions tests/languages/bash/entities_in_strings_feature.test
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
'1\a2\b3\c4\e5\f6\n7\r8\t9\v'
'1234\056789'
'abc\xdef'
'123\456789'
'\uABCDEFG'
$'1\a2\b3\c4\e5\f6\n7\r8\t9\v'
$'1234\056789'
$'123\456789'
"abc\xdef"
"\uABCDEFG"
"a\"b"

'1\a2\b3\c4\e5\f6\n7\r8\t9\v'

----------------------------------------------------

[
["string", [
"'1", ["entity", "\\a"], "2", ["entity", "\\b"], "3", ["entity", "\\c"],
"4", ["entity", "\\e"], "5", ["entity", "\\f"], "6", ["entity", "\\n"],
"7", ["entity", "\\r"], "8", ["entity", "\\t"], "9", ["entity", "\\v"],
"$'1",
["entity", "\\a"],
"2",
["entity", "\\b"],
"3",
["entity", "\\c"],
"4",
["entity", "\\e"],
"5",
["entity", "\\f"],
"6",
["entity", "\\n"],
"7",
["entity", "\\r"],
"8",
["entity", "\\t"],
"9",
["entity", "\\v"],
"'"
]],
["string", ["'1234", ["entity", "\\056"], "789'"]],
["string", ["'abc", ["entity", "\\xde"], "f'"]],
["string", ["'123", ["entity", "\\456"], "789'"]],
["string", ["'", ["entity", "\\uABCD"], "EFG'"]],
["string", ["\"a", ["entity", "\\\""], "b\""]]
["string", [
"$'1234",
["entity", "\\056"],
"789'"
]],
["string", [
"$'123",
["entity", "\\456"],
"789'"
]],
["string", [
"\"abc",
["entity", "\\xde"],
"f\""
]],
["string", [
"\"",
["entity", "\\uABCD"],
"EFG\""
]],
["string", [
"\"a",
["entity", "\\\""],
"b\""
]],

["string", "'1\\a2\\b3\\c4\\e5\\f6\\n7\\r8\\t9\\v'"]
]

----------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions tests/languages/bash/issue2436.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
echo $'module.exports = {\n extends: [\n // add more generic rulesets here, such as:\n // 'eslint:recommended',\n "plugin:vue/vue3-recommended",\n "prettier",\n "prettier/vue",\n ],\n rules: {\n // override/add rules settings here, such as:\n // 'vue/no-unused-vars': 'error'\n },\n};' > .eslintrc.js

----------------------------------------------------

[
["builtin", "echo"],
["string", [
"$'module.exports = {",
["entity", "\\n"],
" extends: [",
["entity", "\\n"],
" // add more generic rulesets here, such as:",
["entity", "\\n"],
" // '"
]],
"eslint:recommended",
["string", "',\\n \"plugin:vue/vue3-recommended\",\\n \"prettier\",\\n \"prettier/vue\",\\n ],\\n rules: {\\n // override/add rules settings here, such as:\\n // '"],
"vue/no-unused-vars",
["string", "': '"],
"error",
["string", "'\\n },\\n};'"],
["operator", [">"]],
" .eslintrc.js"
]
Loading

0 comments on commit e5cfdb4

Please sign in to comment.