Analysis of commit e85150e681f69d58a07dadcfbe5804dccde749c5
Assignee: @copilot
Summary
Multiple repo policy-check handlers repeat the same package.json JSON parsing + try/catch error-return logic. This is duplicated across 3 files and heavily repeated within npmPackages.ts, increasing maintenance cost and making error handling inconsistent over time.
Duplication Details
Pattern: JSON.parse(readFile(...)) with identical catch { return \Error parsing JSON file: ...`; }`
Impact Analysis
- Maintainability: When package.json parsing requirements change (e.g., improved diagnostics, schema validation, stricter typing), the change must be repeated in many places.
- Bug Risk: Future edits can easily introduce subtle divergence (different error messages/return types, partial handling, inconsistent logging).
- Code Bloat: The same 6–8 line block appears ~25 times in this area alone.
Refactoring Recommendations
-
Extract a shared helper into build-tools/packages/build-cli/src/library/repoPolicyCheck/common.ts
- Example:
tryReadJsonFile(T)(path: string): { ok: true; value: T } | { ok: false; message: string }
- Update handlers to use the helper and return the helper’s message on failure.
- Benefits: Single point of change, consistent error reporting.
-
Add a package.json-focused wrapper
- Example:
tryReadPackageJson(path: string) built on the generic helper.
- Benefits: Keeps handler bodies small and uniform, reduces repeated
as PackageJson casts.
Implementation Checklist
Analysis Metadata
- Detection Method: Serena semantic + targeted pattern search
- Commit:
e85150e681f69d58a07dadcfbe5804dccde749c5
- Analysis Date: 2026-03-30T21:50:25.550Z
Generated by Duplicate Code Detector · ◷
To install this agentic workflow, run
gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@94662b1dee8ce96c876ba9f33b3ab8be32de82a4
Analysis of commit
e85150e681f69d58a07dadcfbe5804dccde749c5Assignee:
@copilotSummary
Multiple repo policy-check handlers repeat the same
package.jsonJSON parsing +try/catcherror-return logic. This is duplicated across 3 files and heavily repeated withinnpmPackages.ts, increasing maintenance cost and making error handling inconsistent over time.Duplication Details
Pattern:
JSON.parse(readFile(...))with identicalcatch { return \Error parsing JSON file: ...`; }`Severity: Medium
Occurrences:
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts: 22JSON.parse(readFile(blocks (21 with the exactError parsing JSON file:message)build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts: 2build-tools/packages/build-cli/src/library/repoPolicyCheck/pnpm.ts: 1Locations (examples):
build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts(lines 776–781)build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts(lines 1857–1862)build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts(lines 770–775)build-tools/packages/build-cli/src/library/repoPolicyCheck/pnpm.ts(lines 23–28)Code Sample (representative):
Impact Analysis
Refactoring Recommendations
Extract a shared helper into
build-tools/packages/build-cli/src/library/repoPolicyCheck/common.tstryReadJsonFile(T)(path: string): { ok: true; value: T } | { ok: false; message: string }Add a package.json-focused wrapper
tryReadPackageJson(path: string)built on the generic helper.as PackageJsoncasts.Implementation Checklist
repoPolicyCheck/common.tsnpmPackages.tsto use helper in each handler/resolverfluidBuildTasks.tsandpnpm.tsto use helperstring | undefinedvs resolver objects)Analysis Metadata
e85150e681f69d58a07dadcfbe5804dccde749c5