From 59594a6e0f6736d5151d04cf87cba721bc68aa47 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:57:18 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20test:=20make=20jsFunc=20skip=20t?= =?UTF-8?q?he=20parameter=20list=20before=20brace-matching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jsFunc brace-matched a page function from the first `{` after its signature. A default parameter such as `opts = {}` closes that brace at once, so the slice stopped at `function costSortTh(table, key, label, opts = {}` and any node harness whose closure reached it failed with a SyntaxError pointing at the next function. Scan the parameter list first (balanced, string-aware) and start brace-matching at the body. Pin the extractor on every top-level function in the shipped page so a new signature shape fails here, naming the function, rather than as an opaque node error in whichever harness happens to reach it first. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/dashboard/acmm_per_repo_ui_test.go | 48 ++++++++- .../dashboard/js_func_default_param_test.go | 102 ++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/pkg/dashboard/js_func_default_param_test.go diff --git a/src/pkg/dashboard/acmm_per_repo_ui_test.go b/src/pkg/dashboard/acmm_per_repo_ui_test.go index 6d4e24986a..7f92f3f63a 100644 --- a/src/pkg/dashboard/acmm_per_repo_ui_test.go +++ b/src/pkg/dashboard/acmm_per_repo_ui_test.go @@ -38,7 +38,7 @@ func jsFunc(t *testing.T, html, name string) string { declStart -= len("async ") } depth := 0 - for i := strings.Index(html[start:], "{") + start; i < len(html); i++ { + for i := jsBodyStart(html, start); i < len(html); i++ { switch html[i] { case '{': depth++ @@ -63,6 +63,52 @@ function formatNumber(value, locale, options) { try { return new Intl.NumberForm return "" } +// jsBodyStart returns the index of the opening brace of the function body +// whose signature starts at sig. It skips the parameter list first, so a +// default parameter such as `opts = {}` is not mistaken for the body: brace- +// matching from the first `{` after the signature would otherwise return the +// truncated `function f(a, opts = {}` and break every node harness that pulls +// such a function into its closure. Parentheses, brackets and braces inside +// the parameter list are balanced; string literals in defaults are skipped. +// Falls back to the first `{` when no parameter list closes, so a signature +// this scanner does not understand degrades to the previous behaviour rather +// than failing. +func jsBodyStart(html string, sig int) int { + open := strings.IndexByte(html[sig:], '(') + if open < 0 { + return strings.IndexByte(html[sig:], '{') + sig + } + depth := 0 + var quote byte + for i := sig + open; i < len(html); i++ { + c := html[i] + if quote != 0 { + if c == '\\' { + i++ + } else if c == quote { + quote = 0 + } + continue + } + switch c { + case '\'', '"', '`': + quote = c + case '(', '[', '{': + depth++ + case ')', ']', '}': + depth-- + if depth == 0 { + body := strings.IndexByte(html[i:], '{') + if body < 0 { + return len(html) + } + return i + body + } + } + } + return strings.IndexByte(html[sig:], '{') + sig +} + // TestACMMRepoRankingOrdersWeakestFirst runs the panel's own ranking helpers // against a fixture whose correct order is known by construction. // diff --git a/src/pkg/dashboard/js_func_default_param_test.go b/src/pkg/dashboard/js_func_default_param_test.go new file mode 100644 index 0000000000..4ac275e4a5 --- /dev/null +++ b/src/pkg/dashboard/js_func_default_param_test.go @@ -0,0 +1,102 @@ +package dashboard + +import ( + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "testing" +) + +// jsBodyOpenRe matches the `) {` (or minified `){`) that opens a function body. +var jsBodyOpenRe = regexp.MustCompile(`\)\s*\{`) + +// jsFunc brace-matched from the first `{` after the signature. A default +// parameter such as `opts = {}` closes that brace immediately, so the slice +// was the truncated `function costSortTh(table, key, label, opts = {}` and the +// node harness that pulled it in (your-turn → render → renderStageTracker → +// … → connect → renderCost → costSortTh) died with "SyntaxError: Unexpected +// token 'function'" pointing at the NEXT function in the closure, not this +// one. These tests pin the extractor on the shapes the page actually uses. +func TestJsFuncSkipsDefaultParameterBraces(t *testing.T) { + cases := []struct { + name string + html string + want string + }{ + { + name: "object default", + html: " function f(a, opts = {}) {\n return a;\n }\n function g() { return 1; }\n", + want: "function f(a, opts = {}) {\n return a;\n }", + }, + { + name: "async with object default", + html: " async function f(opts = { x: 1 }) {\n return opts.x;\n }\n", + want: "async function f(opts = { x: 1 }) {\n return opts.x;\n }", + }, + { + name: "string default containing braces and parens", + html: " function f(sep = '})(') {\n return sep;\n }\n", + want: "function f(sep = '})(') {\n return sep;\n }", + }, + { + name: "array default and arrow default", + html: " function f(xs = [], cb = (v) => ({ v })) {\n return cb(xs);\n }\n", + want: "function f(xs = [], cb = (v) => ({ v })) {\n return cb(xs);\n }", + }, + { + name: "plain parameters unchanged", + html: " function f(a, b) {\n if (a) { return b; }\n return a;\n }\n", + want: "function f(a, b) {\n if (a) { return b; }\n return a;\n }", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := jsFunc(t, tc.html, "f"); got != tc.want { + t.Fatalf("jsFunc sliced\n%q\nwant\n%q", got, tc.want) + } + }) + } +} + +// Every top-level function in the shipped page must slice to a complete +// declaration. The check is structural — the slice ends on the body's closing +// brace, so it must contain the `) {` that opens it — and runs on the real +// page so a new signature shape the extractor cannot read fails here, with +// the function named, instead of as an opaque SyntaxError in whichever node +// harness happens to reach it first. +func TestJsFuncSlicesEveryPageFunctionToItsBody(t *testing.T) { + html := indexHTML(t) + var bad []string + for _, m := range jsDeclRe.FindAllStringSubmatch(html, -1) { + src := jsFunc(t, html, m[1]) + if !jsBodyOpenRe.MatchString(src) || !strings.HasSuffix(src, "}") { + bad = append(bad, m[1]) + } + } + if len(bad) > 0 { + t.Fatalf("%d page functions slice without reaching their body (default-parameter braces?): %s", len(bad), strings.Join(bad, ", ")) + } +} + +// costSortTh is the shipped function with the `opts = {}` signature; its +// slice must be a program node accepts, which is what the your-turn harness +// needs when the cost panel is in its closure. +func TestJsFuncCostSortThSliceIsValidJavaScript(t *testing.T) { + src := jsFunc(t, indexHTML(t), "costSortTh") + if !strings.Contains(src, "return `