Skip to content

fix: allow trailing commas when parsing JSONC - #295

Open
naveentehrpariya wants to merge 1 commit into
unjs:mainfrom
naveentehrpariya:fix-jsonc-trailing-commas
Open

naveentehrpariya wants to merge 1 commit into
unjs:mainfrom
naveentehrpariya:fix-jsonc-trailing-commas

Conversation

@naveentehrpariya

@naveentehrpariya naveentehrpariya commented Oct 30, 2026 •

Copy link
Copy Markdown

Problem

confbox 0.3.0 made parseJSONC reject trailing commas unless allowTrailingComma is passed (it strips comments, then hands the text to native JSON.parse). pkg-types calls it without options at three sites, so reading a JSONC file with a trailing comma throws:

// tsconfig.json — accepted by tsc and by VS Code
{
  "compilerOptions": {
    "strict": true,
  },
}
await readTSConfig('tsconfig.json')
// SyntaxError: Expected double-quoted property name in JSON at position 47

This already breaks two of the repo's own tests

On a fresh clone of main, vitest run fails:

  • test/index.test.ts › read package.json (jsonc)
  • test/index.test.ts › reads package.json with comments (JSONC)

Both read test/fixture/jsonc/package.json, which has a trailing comma, so the suite already asserts that trailing commas must parse. It went unnoticed because "test": "pnpm lint && pnpm typecheck" and checks.yml never run vitest.

Bisects to the confbox ^0.2.4 → ^0.3.0 bump: 0.1.8 and 0.2.4 tolerate trailing commas, 0.3.x throws without the opt-in. The confbox default change was intentional and documented as breaking, so the gap is pkg-types not opting in.

Fix

Pass { allowTrailingComma: true } at the three parseJSONC call sites (src/tsconfig/utils.ts, src/packagejson/utils.ts ×2). This matches what pkg-types did before migrating to confbox, and what TypeScript's own tsconfig parser accepts.

After this change vitest run goes from 3 failures to 1. The remaining failure (detects Deno workspace) is pre-existing and unrelated — findWorkspaceDir defaults workspaceFile to "furthest" and the repo root now has its own pnpm-workspace.yaml, so the fixture resolves to the repo root. Happy to open that separately.

tsc --noEmit, oxlint and oxfmt --check are all clean.

Might also be worth running vitest in checks.yml so this class of regression surfaces in CI.

Summary by CodeRabbit

  • Bug Fixes
    • Package metadata and TypeScript configuration files containing trailing commas are now parsed successfully in the applicable fallback parsing paths.
    • Strict JSON parsing and other format-specific parsing behavior remain unchanged.

confbox 0.3.0 made parseJSONC reject trailing commas unless
allowTrailingComma is passed, and pkg-types never opted in, so reading
a JSONC file with a trailing comma throws:

  // tsconfig.json — accepted by tsc and VS Code
  {
    "compilerOptions": {
      "strict": true,
    },
  }

  await readTSConfig('tsconfig.json')
  // SyntaxError: Expected double-quoted property name in JSON

This also broke two of the repo's own tests, which went unnoticed
because the test script only runs lint and typecheck:

  - 'read package.json (jsonc)'
  - 'reads package.json with comments (JSONC)'

Both use test/fixture/jsonc/package.json, which has a trailing comma,
so the suite already asserts trailing commas must parse.

Pass { allowTrailingComma: true } at the three parseJSONC call sites.
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 75ff9100-c3dd-4501-9b2f-23d59cb31af0
Commits

Reviewing files that changed from the base of the PR and between 9be578d and 91feec8.

Files selected for processing: 2
  • src/packagejson/utils.ts
  • src/tsconfig/utils.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

Package configuration JSONC fallbacks and TypeScript configuration parsing now allow trailing commas. Strict JSON parsing and other format-specific parsing paths remain unchanged.

Changes: Configuration parsing

Layer / File(s) Summary
Allow trailing commas in configuration parsing
src/packagejson/utils.ts, src/tsconfig/utils.ts
The JSONC fallbacks in readPackage and readPackageJSON now allow trailing commas. readTSConfig also allows trailing commas.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: pi0

Merge Risk: Minimal · up to 91fee

No actionable merge-blocking risk was identified in this focused parsing change.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: trailing commas are now allowed when the JSONC parser runs. The raw summary confirms that readPackage, readPackageJSON, and readTSConfig now accept trailing commas in …
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: trailing commas are now allowed when the JSONC parser runs. The raw summary confirms that readPackage, readPackageJSON, and readTSConfig now accept trailing commas in their JSONC fallback paths. The title uses the conventional fix: prefix and stays under one line.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 2 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant