From bdc42a95a8e070baddc3f5543e8369f25b581d8b Mon Sep 17 00:00:00 2001 From: William Harrison <87287585+wdhdev@users.noreply.github.com> Date: Fri, 11 Apr 2025 11:23:10 +0800 Subject: [PATCH] chore(ci): fix duplicate keys test --- tests/json.test.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/json.test.js b/tests/json.test.js index 1b40b75d5..2ebd3bace 100644 --- a/tests/json.test.js +++ b/tests/json.test.js @@ -36,20 +36,30 @@ const domainsPath = path.resolve("domains"); const files = fs.readdirSync(domainsPath); function findDuplicateKeys(jsonString) { - const keyPattern = /"([^"]+)"(?=\s*:)/g; - const keys = []; - let match; + const duplicateKeys = []; - while ((match = keyPattern.exec(jsonString)) !== null) { - keys.push(match[1]); - } + const stack = []; - const keyCount = {}; - keys.forEach((key) => { - keyCount[key] = (keyCount[key] || 0) + 1; + const obj = JSON.parse(jsonString, (key, value) => { + if (typeof key === "string") { + const current = stack[stack.length - 1]; + if (current) { + if (current[key]) { + duplicateKeys.push(key); + } else { + current[key] = true; + } + } + } + + if (typeof value === "object" && value !== null) { + stack.push({}); + } + + return value; }); - return Object.keys(keyCount).filter((key) => keyCount[key] > 1); + return [...new Set(duplicateKeys)]; } async function validateFields(t, obj, fields, file, prefix = "") {