From 071be842f0bd58de4863cdf2ab86d60f49912abf Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 13 Nov 2024 13:37:44 -0500 Subject: [PATCH] Merge commit from fork * Fix runaway regex whitespace matching * Include negative lookbehind * Update packages/plugin-kit/tests/config-comment-parser.test.js Co-authored-by: Milos Djermanovic * Update packages/plugin-kit/tests/config-comment-parser.test.js Co-authored-by: Milos Djermanovic * Update packages/plugin-kit/tests/config-comment-parser.test.js Co-authored-by: Milos Djermanovic * Update packages/plugin-kit/tests/config-comment-parser.test.js Co-authored-by: Milos Djermanovic * Update packages/plugin-kit/src/config-comment-parser.js Co-authored-by: Milos Djermanovic --------- Co-authored-by: Milos Djermanovic --- .../plugin-kit/src/config-comment-parser.js | 4 +- .../tests/config-comment-parser.test.js | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/plugin-kit/src/config-comment-parser.js b/packages/plugin-kit/src/config-comment-parser.js index f22f97a9..f9a28223 100644 --- a/packages/plugin-kit/src/config-comment-parser.js +++ b/packages/plugin-kit/src/config-comment-parser.js @@ -99,7 +99,9 @@ export class ConfigCommentParser { const items = /** @type {StringConfig} */ ({}); // Collapse whitespace around `:` and `,` to make parsing easier - const trimmedString = string.replace(/\s*([:,])\s*/gu, "$1"); + const trimmedString = string + .trim() + .replace(/(? { if (!name) { diff --git a/packages/plugin-kit/tests/config-comment-parser.test.js b/packages/plugin-kit/tests/config-comment-parser.test.js index e9895dcd..549ee8ac 100644 --- a/packages/plugin-kit/tests/config-comment-parser.test.js +++ b/packages/plugin-kit/tests/config-comment-parser.test.js @@ -74,6 +74,55 @@ describe("ConfigCommentParser", () => { b: null, }); }); + + it("should return an empty object for an empty string", () => { + const code = ""; + const result = commentParser.parseStringConfig(code, comment); + + assert.deepStrictEqual(result, {}); + }); + + it("should parse string config with one item, no value, and leading whitespace", () => { + const code = `${" ".repeat(100000)}a`; + const result = commentParser.parseStringConfig(code, comment); + + assert.deepStrictEqual(result, { + a: null, + }); + }); + + it("should parse string config with one item, no value, and trailing whitespace", () => { + const code = `a${" ".repeat(100000)}`; + const result = commentParser.parseStringConfig(code, comment); + + assert.deepStrictEqual(result, { + a: null, + }); + }); + + it("should parse string config with two items, no values, and whitespace in the middle", () => { + const code = `a${" ".repeat(100000)}b`; + const result = commentParser.parseStringConfig(code, comment); + + assert.deepStrictEqual(result, { + a: null, + b: null, + }); + }); + + it("should parse string config with multiple items with values separated by commas and lots of whitespace", () => { + const whitespace = " ".repeat(100000); + const code = `a: 1${whitespace},${whitespace}b: 2${whitespace},${whitespace}c: 3${whitespace},${whitespace}d: 4`; + const result = commentParser.parseStringConfig(code, comment); + + assert.deepStrictEqual(result, { + a: "1", + b: "2", + c: "3", + d: "4", + }); + }); + }); describe("parseListConfig", () => {