🔍 Duplicate Code Detected: PackageJson parse boilerplate in repoPolicyCheck
Analysis of commit 73a892497061d9677f4ebe0fd3394476ad60c98f
Assignee: @copilot
Summary
Multiple repo policy-check handlers repeat the same package.json read/parse/try-catch boilerplate (and closely related follow-up checks). This appears 20+ times and makes it easy for behavior to drift (e.g., inconsistent error return shapes).
Duplication Details
Pattern: Read + parse package.json with identical try/catch
-
Severity: Medium
-
Occurrences: 22+ instances (20 in npmPackages.ts, 2 in fluidBuildTasks.ts)
-
Locations (examples):
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 775–781)
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 872–876)
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 895–899)
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 1175–1179)
build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts (lines 770–775)
build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts (lines 829–834)
-
Code Sample (representative):
let json: PackageJson;
try {
json = JSON.parse(readFile(file)) as PackageJson;
} catch {
return `Error parsing JSON file: \$\{file}`;
}
Impact Analysis
- Maintainability: Changes to parsing (e.g., JSON5 support, better error details, validation) must be made in many places.
- Bug Risk: Some handlers return different error shapes on parse failure (string vs
{ resolved: false, message }), increasing drift risk.
- Code Bloat: The same 6–8 line block repeated 20+ times adds noise and makes the actual policy logic harder to scan.
Refactoring Recommendations
-
Extract a shared helper
- Suggested location:
build-tools/packages/build-cli/src/library/repoPolicyCheck/common.ts
- Example shape:
tryReadPackageJson(file): { ok: true; json: PackageJson } | { ok: false; message: string }
- (Optionally)
readPackageJsonOrThrow + centralized catch-to-message wrapper.
- Benefits: one place to improve error messages/validation and keep return conventions consistent.
-
Standardize parse-error handling across handlers
- Pick one convention for parse failures (e.g., always return a string message) and adapt the few handlers that currently return objects.
Implementation Checklist
Analysis Metadata
- Analyzed Files (focused subset):
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts, build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts
- Detection Method: Serena semantic pattern search (
search_for_pattern) + targeted line inspection
- Commit:
73a892497061d9677f4ebe0fd3394476ad60c98f
- Analysis Date: 2026-04-13T21:56:03.874Z
Generated by Duplicate Code Detector · ◷
To install this agentic workflow, run
gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@94662b1dee8ce96c876ba9f33b3ab8be32de82a4
🔍 Duplicate Code Detected: PackageJson parse boilerplate in repoPolicyCheck
Analysis of commit
73a892497061d9677f4ebe0fd3394476ad60c98fAssignee:
@copilotSummary
Multiple repo policy-check handlers repeat the same
package.jsonread/parse/try-catch boilerplate (and closely related follow-up checks). This appears 20+ times and makes it easy for behavior to drift (e.g., inconsistent error return shapes).Duplication Details
Pattern: Read + parse
package.jsonwith identical try/catchSeverity: Medium
Occurrences: 22+ instances (20 in
npmPackages.ts, 2 influidBuildTasks.ts)Locations (examples):
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts(lines 775–781)build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts(lines 872–876)build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts(lines 895–899)build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts(lines 1175–1179)build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts(lines 770–775)build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts(lines 829–834)Code Sample (representative):
Impact Analysis
{ resolved: false, message }), increasing drift risk.Refactoring Recommendations
Extract a shared helper
build-tools/packages/build-cli/src/library/repoPolicyCheck/common.tstryReadPackageJson(file): { ok: true; json: PackageJson } | { ok: false; message: string }readPackageJsonOrThrow+ centralized catch-to-message wrapper.Standardize parse-error handling across handlers
Implementation Checklist
repoPolicyCheck/common.ts(or similar)npmPackages.tsandfluidBuildTasks.ts{ resolved, message }patterns)Analysis Metadata
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts,build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.tssearch_for_pattern) + targeted line inspection73a892497061d9677f4ebe0fd3394476ad60c98f