-
Notifications
You must be signed in to change notification settings - Fork 463
[code-simplifier] simplify: extract traverseObjectTree and hasLabelIntentMetadata helpers #47908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,50 +94,63 @@ function resolveFirewallAuditLogPath(auditJsonlPathOverride) { | |
| } | ||
|
|
||
| /** | ||
| * Depth-first traversal of a nested object, calling visitor for each [key, value] pair. | ||
| * Traversal stops early if visitor returns true. | ||
| * | ||
| * @param {unknown} entry | ||
| * @returns {string} | ||
| * @param {(key: string, value: unknown) => boolean | void} visitor - return true to stop early | ||
| * @returns {boolean} true if visitor stopped traversal early | ||
| */ | ||
| function parseMaxAICreditsFromAuditEntry(entry) { | ||
| if (!entry || typeof entry !== "object") return ""; | ||
| function traverseObjectTree(entry, visitor) { | ||
| if (!entry || typeof entry !== "object") return false; | ||
| const stack = [entry]; | ||
| while (stack.length > 0) { | ||
| const node = stack.pop(); | ||
| if (!node || typeof node !== "object") continue; | ||
| for (const [key, value] of Object.entries(node)) { | ||
| if (MAX_AI_CREDITS_FIELDS.has(key)) { | ||
| const parsed = parsePositiveNumberString(value); | ||
| if (parsed) return parsed; | ||
| } | ||
| if (visitor(key, value) === true) return true; | ||
| if (value && typeof value === "object") stack.push(value); | ||
| } | ||
| } | ||
| return ""; | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param {unknown} entry | ||
| * @returns {string} | ||
| */ | ||
| function parseMaxAICreditsFromAuditEntry(entry) { | ||
| let result = ""; | ||
| traverseObjectTree(entry, (key, value) => { | ||
| if (MAX_AI_CREDITS_FIELDS.has(key)) { | ||
| const parsed = parsePositiveNumberString(value); | ||
| if (parsed) { | ||
| result = parsed; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * @param {unknown} entry | ||
| * @returns {{ aiCredits: string, rateLimitError: boolean }} | ||
| */ | ||
| function parseAICreditsErrorInfoFromAuditEntry(entry) { | ||
| if (!entry || typeof entry !== "object") return { aiCredits: "", rateLimitError: false }; | ||
| const stack = [entry]; | ||
| let aiCredits = ""; | ||
| let rateLimitError = false; | ||
| while (stack.length > 0) { | ||
| const node = stack.pop(); | ||
| if (!node || typeof node !== "object") continue; | ||
| for (const [key, value] of Object.entries(node)) { | ||
| if (AI_CREDITS_FIELDS.has(key)) { | ||
| const parsed = parsePositiveNumberString(value); | ||
| if (parsed) aiCredits = parsed; | ||
| } | ||
| if (AI_CREDITS_RATE_LIMIT_ERROR_FIELDS.has(key) && isTrueLike(value)) rateLimitError = true; | ||
| if (AI_CREDITS_RATE_LIMIT_TEXT_FIELDS.has(key) && typeof value === "string") { | ||
| if (AI_CREDITS_RATE_LIMIT_PATTERNS.some(pattern => pattern.test(value))) rateLimitError = true; | ||
| } | ||
| if (value && typeof value === "object") stack.push(value); | ||
| traverseObjectTree(entry, (key, value) => { | ||
| if (AI_CREDITS_FIELDS.has(key)) { | ||
| const parsed = parsePositiveNumberString(value); | ||
| if (parsed) aiCredits = parsed; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fragile early-stop idiom: 💡 Suggested fixUse an explicit callback instead: function parseUnknownModelAICreditsFromAuditEntry(entry) {
return traverseObjectTree(entry, (_key, value) => {
if (value === UNKNOWN_MODEL_AI_CREDITS_TYPE) return true;
});
}When equality fails,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 7c4aa71 with the same explicit callback change in |
||
| } | ||
| } | ||
| if (AI_CREDITS_RATE_LIMIT_ERROR_FIELDS.has(key) && isTrueLike(value)) rateLimitError = true; | ||
| if (AI_CREDITS_RATE_LIMIT_TEXT_FIELDS.has(key) && typeof value === "string") { | ||
| if (AI_CREDITS_RATE_LIMIT_PATTERNS.some(pattern => pattern.test(value))) rateLimitError = true; | ||
| } | ||
| }); | ||
| return { aiCredits, rateLimitError }; | ||
| } | ||
|
|
||
|
|
@@ -257,17 +270,10 @@ function parseMaxAICreditsExceededFromAuditLog(auditJsonlPathOverride) { | |
| * @returns {boolean} | ||
| */ | ||
| function parseUnknownModelAICreditsFromAuditEntry(entry) { | ||
| if (!entry || typeof entry !== "object") return false; | ||
| const stack = [entry]; | ||
| while (stack.length > 0) { | ||
| const node = stack.pop(); | ||
| if (!node || typeof node !== "object") continue; | ||
| for (const [, value] of Object.entries(node)) { | ||
| if (value === UNKNOWN_MODEL_AI_CREDITS_TYPE) return true; | ||
| if (value && typeof value === "object") stack.push(value); | ||
| } | ||
| } | ||
| return false; | ||
| return traverseObjectTree(entry, (_key, value) => { | ||
| if (value === UNKNOWN_MODEL_AI_CREDITS_TYPE) return true; | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] The 💡 Clearer alternativesThe visitor contract requires returning Consider: return traverseObjectTree(entry, (_key, value) => {
if (value === UNKNOWN_MODEL_AI_CREDITS_TYPE) return true;
});or explicitly ternary: return traverseObjectTree(entry, (_key, value) =>
value === UNKNOWN_MODEL_AI_CREDITS_TYPE ? true : undefined
);Either form makes the early-exit signal explicit and avoids the @copilot please address this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in 7c4aa71. I replaced the |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/codebase-design]
hasLabelIntentMetadata(existing ?? null)— the?? nullcoercion is unnecessary since the function already guards withspec &&.💡 Simplification
hasLabelIntentMetadatastarts withspec && (...), so passingundefinedis handled identically tonull. The?? nulladds noise without safety benefit.This aligns with the third call site at line 225 which passes
labelSpecdirectly without coercion.@copilot please address this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 7c4aa71. I removed the
?? nullcall-site coercion and adjusted the helper JSDoc to acceptundefined, preserving behavior while keeping the call clean.