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
25 changes: 22 additions & 3 deletions shell/plugins/menu/MenuModel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
function stripJsonc(raw) {
return String(raw || "")
.replace(/^\s*\/\/[^\n]*(\n|$)/gm, "")
.replace(/,(\s*[}\]])/g, "$1")
var s = String(raw || "").replace(/^\s*\/\/[^\n]*(\n|$)/gm, "");
var out = "";
var inString = false;
var escape = false;
for (var i = 0; i < s.length; i++) {
var c = s[i];
if (escape) {
escape = false;
} else if (c === '"') {
inString = !inString;
} else if (c === '\\' && inString) {
escape = true;
}

if (!inString && c === ',') {
var j = i + 1;
while (j < s.length && /\s/.test(s[j])) j++;
if (j < s.length && (s[j] === '}' || s[j] === ']')) continue;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}
out += c;
}
return out;
}

function normalizeAliases(value) {
Expand Down
8 changes: 8 additions & 0 deletions test/shell.d/menu-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const parsed = menu.parseMenuJsonc(`
`)

assertEqual(parsed.length, 3, 'menu parses JSONC with comments and trailing commas')

// String-aware trailing commas (#13250)
const withCommasInStrings = menu.parseMenuJsonc('{\n "b": { "label": "x, ]y", "action": "mv f{.bak,}" },\n}')
assertEqual(withCommasInStrings[0].label, 'x, ]y', 'menu keeps a comma before ] inside a label')
assertEqual(withCommasInStrings[0].action, 'mv f{.bak,}', 'menu keeps a comma before } inside an action')
assertEqual(menu.parseMenuJsonc('{"a": {"label": "q\\\\"},}')[0].label, 'q\\', 'menu ends a string at a quote after an escaped backslash before a trailing comma')
assertEqual(menu.parseMenuJsonc('{"a": {"label": "say \\"hi, }"}}')[0].label, 'say "hi, }', 'menu keeps a comma before } after an escaped quote inside a label')

assertDeepEqual(
parsed.find(item => item.id === 'style.theme'),
{
Expand Down