Skip to content

integrate strip-json-comments for JSON configuration parsing#214

Merged
yamadashy merged 5 commits intoyamadashy:mainfrom
ivanionut:main
Dec 29, 2024
Merged

integrate strip-json-comments for JSON configuration parsing#214
yamadashy merged 5 commits intoyamadashy:mainfrom
ivanionut:main

Conversation

@ivanionut
Copy link
Contributor

Integrated strip-json-comments to clean JSON configuration files by removing comments before parsing.

Checklist

  • Run npm run test:
stdout | tests/cli/actions/initAction.test.ts > initAction > createConfigFile > should create a new local config file when one does not exist
configPath /test/dir/repomix.config.json

 ✓ tests/cli/actions/migrationAction.test.ts (5)
 ✓ tests/core/file/permissionCheck.test.ts (13)
 ✓ tests/core/packager.test.ts (5)
 ✓ tests/cli/actions/initAction.test.ts (11)
 ✓ tests/config/configSchema.test.ts (14)
 ✓ tests/cli/cliRun.test.ts (9)
 ✓ tests/core/file/fileProcess.test.ts (8)
 ✓ tests/core/tokenCount/tokenCount.test.ts (12)
     ⠙ should correctly pack simple xml style
 ✓ tests/cli/cliPrint.test.ts (8)
 ✓ tests/cli/cliRun.test.ts (9)
 ✓ tests/config/configLoad.test.ts (7)
 ✓ tests/config/configSchema.test.ts (14)
 ✓ tests/config/globalDirectory.test.ts (7)
 ✓ tests/core/packager.test.ts (5)
 ✓ tests/integration-tests/packager.test.ts (2)
 ✓ tests/shared/logger.test.ts (12)
 ✓ tests/cli/actions/defaultAction.test.ts (7)
 ✓ tests/cli/actions/initAction.test.ts (11)
 ✓ tests/cli/actions/migrationAction.test.ts (5)
 ✓ tests/cli/actions/remoteAction.test.ts (6)
 ✓ tests/cli/actions/versionAction.test.ts (1)
 ✓ tests/core/file/fileCollect.test.ts (3)
 ✓ tests/core/file/fileManipulate.test.ts (40)
 ✓ tests/core/file/filePathSort.test.ts (7)
 ✓ tests/core/file/fileProcess.test.ts (8)
 ✓ tests/core/file/fileSearch.test.ts (12)
 ✓ tests/core/file/gitCommand.test.ts (6)
 ✓ tests/core/file/packageJsonParse.test.ts (2)
 ✓ tests/core/file/permissionCheck.test.ts (13)
 ✓ tests/core/output/outputGenerate.test.ts (1)
 ✓ tests/core/security/securityCheck.test.ts (2)
 ✓ tests/core/tokenCount/tokenCount.test.ts (12)
 ✓ tests/core/output/outputStyles/markdownStyle.test.ts (17)
 ✓ tests/core/output/outputStyles/plainStyle.test.ts (1)
 ✓ tests/core/output/outputStyles/xmlStyle.test.ts (1)

 Test Files  27 passed (27)
      Tests  219 passed (219)
   Start at  18:59:34
   Duration  1.10s (transform 391ms, setup 0ms, collect 1.73s, tests 519ms, environment 4ms, prepare 1.08s)
  • Run npm run lint:
> repomix@0.2.6 lint
> npm run lint-biome && npm run lint-ts && npm run lint-secretlint


> repomix@0.2.6 lint-biome
> biome check --write

Checked 77 files in 19ms. No fixes applied.

> repomix@0.2.6 lint-ts
> tsc --noEmit


> repomix@0.2.6 lint-secretlint
> secretlint "**/*" --secretlintignore .gitignore

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 22, 2024

📝 Walkthrough

Walkthrough

The pull request introduces a new dependency strip-json-comments to the project and modifies the configuration loading mechanism in src/config/configLoad.ts. The change allows JSON configuration files to include comments by preprocessing the file content using the stripJsonComments function before parsing. Additionally, a new parser configuration is added in biome.json to permit comments, and a comment is included in repomix.config.json for documentation purposes. The README.md is updated to reflect these changes, including a transition to JSON5 format for configuration files.

Changes

File Change Summary
package.json Added new dependency strip-json-comments@^5.0.1
src/config/configLoad.ts Added import for stripJsonComments and modified JSON parsing to remove comments before validation
biome.json Added new configuration: "json": { "parser": { "allowComments": true } }
repomix.config.json Added comment in the ignore section for documentation purposes
README.md Updated to reflect name change to "Repomix" and configuration format change to JSON5, including detailed explanations of options

Possibly related PRs

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/config/configLoad.ts (1)

71-71: Consider enhancing error handling for comment stripping.

While the implementation is correct, we could improve error handling by catching potential errors from stripJsonComments.

Consider this enhancement:

-    const config = JSON.parse(stripJsonComments(fileContent));
+    let strippedContent;
+    try {
+      strippedContent = stripJsonComments(fileContent);
+    } catch (error) {
+      throw new RepomixError(`Error stripping comments from config file ${filePath}: ${error.message}`);
+    }
+    const config = JSON.parse(strippedContent);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b263c87 and 797221a.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • package.json (1 hunks)
  • src/config/configLoad.ts (2 hunks)
🔇 Additional comments (2)
package.json (1)

72-73: LGTM! Dependency addition looks good.

The strip-json-comments package is correctly added as a production dependency with an appropriate version constraint.

src/config/configLoad.ts (1)

3-3: LGTM! Clean import statement.

The import is properly placed and uses the correct import style for the package.

@rockmandash
Copy link

OMG I need this!!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
biome.json (1)

40-43: Consider documenting this configuration change.

Since this is a significant change in how JSON files are processed, it would be helpful to document this capability in the project's documentation.

Consider adding a comment in the README or documentation about JSON files supporting comments, along with examples of valid comment syntax. I can help draft this documentation if needed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 797221a and a7b8dd0.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (4)
  • biome.json (1 hunks)
  • package.json (1 hunks)
  • repomix.config.json (1 hunks)
  • src/config/configLoad.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • repomix.config.json
🚧 Files skipped from review as they are similar to previous changes (2)
  • package.json
  • src/config/configLoad.ts
🔇 Additional comments (1)
biome.json (1)

40-43: LGTM! Verify impact on included JSON files.

The JSON parser configuration is correctly structured and aligns with the PR's objective of supporting commented JSON configurations.

Let's verify the impact on all JSON files in the project:

✅ Verification successful

Enabling JSON comments is safe and already in use

The verification shows that:

  1. There are 8 JSON files in the project that will be processed by Biome
  2. Comments are already being used in configuration files:
    • repomix.config.json has line comments
    • tsconfig.json and tsconfig.build.json contain include patterns (which are not actually comments, these were false positives)

Let's verify if there are any multi-line comments as well, with a more precise pattern:


Enabling JSON comments is safe and already in use

The verification confirms that:

  • Comments are already being used in repomix.config.json with single-line comments
  • No multi-line comments are currently used (the matches in tsconfig files were false positives)
  • The configuration change aligns with existing usage in the codebase
  • This change will support the current commenting practice and allow for more flexible JSON configuration files
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check all JSON files that might be affected by the new parser configuration

# Find all JSON files mentioned in the include section
echo "Checking JSON files that will be processed by Biome:"
fd -e json --exec echo "Found JSON file: {}"

# Look for existing comments in JSON files
echo -e "\nChecking for existing comments in JSON files:"
rg -U '(?m)^\s*//|/\*[\s\S]*?\*/' --type json

Length of output: 893


Script:

#!/bin/bash
# Check specifically for JSON comments with a more precise pattern
echo "Checking for single-line comments (//):"
rg '^\s*//.*$' --type json

echo -e "\nChecking for multi-line comments (/* */):"
rg '/\*(?s).*?\*/' --type json

Length of output: 417

@codecov
Copy link

codecov bot commented Dec 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.01%. Comparing base (28e3a76) to head (a7b8dd0).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #214   +/-   ##
=======================================
  Coverage   92.01%   92.01%           
=======================================
  Files          44       44           
  Lines        2041     2041           
  Branches      450      450           
=======================================
  Hits         1878     1878           
  Misses        163      163           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
README.md (2)

375-375: Enhance the JSON5 example and improve syntax highlighting

Consider these improvements for better documentation:

  1. Use json instead of json5 for wider syntax highlighting support
  2. Add more JSON5 features to showcase its advantages, such as:
    • Trailing commas
    • Single quotes
    • Multi-line strings

Example enhancement:

-```json5
+```json
 {
   "output": {
-    "filePath": "repomix-output.xml",
+    "filePath": 'repomix-output.xml',
     "style": "xml",
-    "headerText": "Custom header information for the packed file.",
+    "headerText": "Custom header information \
+                  for the packed file.",
     "removeComments": false,
     "removeEmptyLines": false,
     "showLineNumbers": false,
     "copyToClipboard": true,
     "topFilesLength": 5,
-    "includeEmptyDirectories": false
+    "includeEmptyDirectories": false,  // trailing comma
   },

392-392: Enhance the comment with more context

The comment about .repomixignore could be more informative by explaining the relationship between configuration-based patterns and .repomixignore.

Example enhancement:

-    // Patterns can also be specified in .repomixignore
+    // These patterns complement those in .repomixignore file.
+    // See "Ignore Patterns" section for priority order.
     "customPatterns": ["additional-folder", "**/*.log"]
tests/config/configLoad.test.ts (1)

87-99: Consider adding more comment edge cases
Testing single-line and multi-line comments here is excellent. To further increase reliability, you could include edge cases such as comments at the end of lines or empty lines containing only comments.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7b8dd0 and f71d090.

📒 Files selected for processing (2)
  • README.md (2 hunks)
  • tests/config/configLoad.test.ts (1 hunks)
🔇 Additional comments (2)
tests/config/configLoad.test.ts (2)

86-86: Good descriptive test name for comment-based parsing
This test's name precisely clarifies its purpose, aligning with the PR's objective of supporting JSON files containing comments.


100-107: Test ensures correctness of final parsed structure
Confirming that the returned object matches the expected result validates that comments are properly stripped out and do not interfere with JSON parsing. This test effectively verifies the integration of the strip-json-comments dependency.

@yamadashy
Copy link
Owner

Hi, @ivanionut !
Thanks for the implementation!

I've added some tests and README updates - the changes look great.
Merging now.

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.

3 participants