Skip to content

Commit e5cfdb4

Browse files
Bash: Fixed single-quoted strings (#2792)
1 parent d298d46 commit e5cfdb4

9 files changed

+132
-119
lines changed

components/prism-bash.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,25 @@
133133
},
134134
// “Normal” string
135135
{
136-
pattern: /(^|[^\\](?:\\\\)*)(["'])(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|(?!\2)[^\\`$])*\2/,
136+
// https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
137+
pattern: /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/,
137138
lookbehind: true,
138139
greedy: true,
139140
inside: insideString
141+
},
142+
{
143+
// https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html
144+
pattern: /(^|[^$\\])'[^']*'/,
145+
lookbehind: true,
146+
greedy: true
147+
},
148+
{
149+
// https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
150+
pattern: /\$'(?:[^'\\]|\\[\s\S])*'/,
151+
greedy: true,
152+
inside: {
153+
'entity': insideString.entity
154+
}
140155
}
141156
],
142157
'environment': {

components/prism-bash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-shell-session.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

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

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

1617
Prism.languages['shell-session'] = {
1718
'command': {
18-
pattern: RegExp(/^(?:[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+(?::[^\0-\x1F$#*?"<>:;|]+)?|[^\0-\x1F$#*?"<>:;|]+)?[$#](?:[^\\\r\n'"<]|\\.|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }), 'm'),
19+
pattern: RegExp(/^(?:[^\s@:$#*!/\\]+@[^\s@:$#*!/\\]+(?::[^\0-\x1F$#*?"<>:;|]+)?|[^\0-\x1F$#*?"<>:;|]+)?[$#](?:[^\\\r\n'"<$]|\\.|\$(?!')|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }), 'm'),
1920
greedy: true,
2021
inside: {
2122
'info': {

components/prism-shell-session.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/bash/assign-left_feature.test

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ foo+=('xyz')
88
["assign-left", ["foo"]],
99
["operator", ["="]],
1010
["number", "12"],
11+
1112
["assign-left", ["bar"]],
1213
["operator", ["+="]],
13-
["string", ["'xyz'"]],
14+
["string", "'xyz'"],
15+
1416
["assign-left", ["foo"]],
1517
["operator", ["+="]],
1618
["punctuation", "("],
17-
["string", ["'xyz'"]],
19+
["string", "'xyz'"],
1820
["punctuation", ")"]
1921
]
2022

tests/languages/bash/entities_in_strings_feature.test

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
1-
'1\a2\b3\c4\e5\f6\n7\r8\t9\v'
2-
'1234\056789'
3-
'abc\xdef'
4-
'123\456789'
5-
'\uABCDEFG'
1+
$'1\a2\b3\c4\e5\f6\n7\r8\t9\v'
2+
$'1234\056789'
3+
$'123\456789'
4+
"abc\xdef"
5+
"\uABCDEFG"
66
"a\"b"
77

8+
'1\a2\b3\c4\e5\f6\n7\r8\t9\v'
9+
810
----------------------------------------------------
911

1012
[
1113
["string", [
12-
"'1", ["entity", "\\a"], "2", ["entity", "\\b"], "3", ["entity", "\\c"],
13-
"4", ["entity", "\\e"], "5", ["entity", "\\f"], "6", ["entity", "\\n"],
14-
"7", ["entity", "\\r"], "8", ["entity", "\\t"], "9", ["entity", "\\v"],
14+
"$'1",
15+
["entity", "\\a"],
16+
"2",
17+
["entity", "\\b"],
18+
"3",
19+
["entity", "\\c"],
20+
"4",
21+
["entity", "\\e"],
22+
"5",
23+
["entity", "\\f"],
24+
"6",
25+
["entity", "\\n"],
26+
"7",
27+
["entity", "\\r"],
28+
"8",
29+
["entity", "\\t"],
30+
"9",
31+
["entity", "\\v"],
1532
"'"
1633
]],
17-
["string", ["'1234", ["entity", "\\056"], "789'"]],
18-
["string", ["'abc", ["entity", "\\xde"], "f'"]],
19-
["string", ["'123", ["entity", "\\456"], "789'"]],
20-
["string", ["'", ["entity", "\\uABCD"], "EFG'"]],
21-
["string", ["\"a", ["entity", "\\\""], "b\""]]
34+
["string", [
35+
"$'1234",
36+
["entity", "\\056"],
37+
"789'"
38+
]],
39+
["string", [
40+
"$'123",
41+
["entity", "\\456"],
42+
"789'"
43+
]],
44+
["string", [
45+
"\"abc",
46+
["entity", "\\xde"],
47+
"f\""
48+
]],
49+
["string", [
50+
"\"",
51+
["entity", "\\uABCD"],
52+
"EFG\""
53+
]],
54+
["string", [
55+
"\"a",
56+
["entity", "\\\""],
57+
"b\""
58+
]],
59+
60+
["string", "'1\\a2\\b3\\c4\\e5\\f6\\n7\\r8\\t9\\v'"]
2261
]
2362

2463
----------------------------------------------------

tests/languages/bash/issue2436.test

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
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
2+
3+
----------------------------------------------------
4+
5+
[
6+
["builtin", "echo"],
7+
["string", [
8+
"$'module.exports = {",
9+
["entity", "\\n"],
10+
" extends: [",
11+
["entity", "\\n"],
12+
" // add more generic rulesets here, such as:",
13+
["entity", "\\n"],
14+
" // '"
15+
]],
16+
"eslint:recommended",
17+
["string", "',\\n \"plugin:vue/vue3-recommended\",\\n \"prettier\",\\n \"prettier/vue\",\\n ],\\n rules: {\\n // override/add rules settings here, such as:\\n // '"],
18+
"vue/no-unused-vars",
19+
["string", "': '"],
20+
"error",
21+
["string", "'\\n },\\n};'"],
22+
["operator", [">"]],
23+
" .eslintrc.js"
24+
]

0 commit comments

Comments
 (0)