From ed5db6684309cd852907bca35234b0950b75a4cb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 12 Aug 2026 14:19:34 +0000 Subject: [PATCH] test: cover jsLikeAnalyzer computed object-property-key fallback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/unit/unit.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/unit/unit.test.ts b/src/unit/unit.test.ts index 4a8f578..94bea9b 100644 --- a/src/unit/unit.test.ts +++ b/src/unit/unit.test.ts @@ -3917,6 +3917,27 @@ const api = { assert.strictEqual(results[0].complexity, 1); }); + it("should fall back to (anonymous) when an object property key is computed", () => { + // { [computedKey]: function() {} } — the key is a computed_property_name node, + // not a bare property_identifier/identifier, so no name can be inferred from it. + const sourceCode = ` +const computedKey = "getData"; +const api = { + [computedKey]: function() { + if (flag) { return 1; } + } +}; +`; + const results = JavaScriptMetricsAnalyzer.analyzeFile(sourceCode); + assert.strictEqual(results.length, 1, "object method with computed key should be analysed"); + assert.strictEqual( + results[0].name, + "(anonymous)", + "computed property key cannot be used as a name, so it should fall back to (anonymous)" + ); + assert.strictEqual(results[0].complexity, 1); + }); + it("named function_expression in object property should keep its own name", () => { // A named function expression like `{ getData: function inner() {} }` already // has a name node; the improvement should not override it.