microsoft / microsoft/FluidFramework

Duplicate Code: repeated package.json parse boilerplate in repoPolicyCheck handlers

Open
#27,019 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.9k
Forks
586
Avg merge
1d 15h
Merged PRs (30d)
146

Description

🔍 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

  1. 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.
  2. 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

  • Introduce helper in repoPolicyCheck/common.ts (or similar)
  • Replace repeated try/catch blocks in npmPackages.ts and fluidBuildTasks.ts
  • Ensure handler return types remain correct (string vs { resolved, message } patterns)
  • Run existing build/lint/test for build-cli as appropriate

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts and fluidBuildTasks.ts, inspecting the listed parse blocks and their handler return types. Review repoPolicyCheck/common.ts and run the existing build, lint, and test checks for build-cli. Done means the repeated parsing is centralized, return conventions remain correct, and the affected checks pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.