Skip to content

Eslint-plugin: Handle JSON5 format #31336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 6, 2025

Conversation

yatishgoel
Copy link
Contributor

@yatishgoel yatishgoel commented May 1, 2025

Closes #31316

What I did

The eslintPlugin automigration was previously using JSON.parse to read .eslintrc.json files. This caused crashes when users had comments in their configuration (which is valid JSON5 syntax often used in ESLint v8 and earlier).

Following maintainer feedback aiming for comment preservation, this PR fixes the issue by:

  1. Replacing JSON.parse with comment-json.parse() within the configureEslintPlugin function. This correctly handles .eslintrc.json files containing comments or other JSON5 features without errors.
  2. Using comment-json.stringify() to write the modified configuration back to the file.
  3. Adding the comment-json package as a dependency to @storybook/cli.
  4. Updating the relevant unit test (should correctly parse, configure, and preserve comments in JSON5 .eslintrc.json) to verify the behavior.

Note on Comment Preservation: Testing revealed that while comment-json successfully preserves comments located outside of arrays or attached to unmodified object properties, it does not consistently preserve comments located inside an array (like extends) when that array is modified (e.g., by adding plugin:storybook/recommended). The test assertions have been updated to reflect this observed behavior.

This resolves the SyntaxError reported in #31316, allows the migration to proceed smoothly for users with JSON5-formatted ESLint configurations, and preserves the majority of user comments in the configuration file.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Manual testing can be done by running the upgrade command on a project that uses an .eslintrc.json file containing comments:

  1. Set up a sample project using Storybook 8 (or earlier) with an .eslintrc.json file that includes comments (similar to the one in issue [Bug]: Storybook 9 - eslintPlugin automigration crashes on config files with JSON5 format #31316, including comments inside the extends array if possible).
  2. Run npx storybook@latest upgrade using this branch.
  3. Verify that the upgrade process completes without the SyntaxError related to parsing the ESLint config.
  4. Verify that the eslint-plugin-storybook is correctly configured in the extends array in .eslintrc.json.
  5. Verify that comments outside the extends array were preserved, while comments inside it might have been removed.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

@@ -218,7 +220,6 @@ export async function configureEslintPlugin({

eslintConfig.extends = [...existingExtends, 'plugin:storybook/recommended'] as string[];

const eslintFileContents = await readFile(eslintConfigFile, { encoding: 'utf8' });
const spaces = detectIndent(eslintFileContents).amount || 2;
await writeFile(eslintConfigFile, JSON.stringify(eslintConfig, undefined, spaces));
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Using JSON.stringify will strip out any comments and formatting from the original file. Consider using json5.stringify to preserve JSON5 features.

Suggested change
await writeFile(eslintConfigFile, JSON.stringify(eslintConfig, undefined, spaces));
await writeFile(eslintConfigFile, json5.stringify(eslintConfig, undefined, spaces));

@valentinpalkovic
Copy link
Contributor

Hi @yatishgoel,

Thank you for your contribution.

I am wondering, whether we can preserve comments inside of a json5 configuration by transforming comments into namespaced key-value pairs and when stringifying it back to transform it back to a comment. WDYT?

@yatishgoel
Copy link
Contributor Author

Hi @yatishgoel,

Thank you for your contribution.

I am wondering, whether we can preserve comments inside of a json5 configuration by transforming comments into namespaced key-value pairs and when stringifying it back to transform it back to a comment. WDYT?

Hi @valentinpalkovic, thanks for the suggestion! If I’m understanding correctly, you’re proposing that we turn each comment into a temporary, namespaced property on the config object and then convert those properties back into comments when stringifying. That would certainly work to keep comments in place, but it does add a fair bit of custom parsing and post-processing logic.

Would you be open to using a lightweight library like comment-json instead? It handles full round-trip comment support—preserving whitespace, comment placement, and formatting—without the need for us to write and maintain our own encode/decode layer. Let me know what you think!

@valentinpalkovic
Copy link
Contributor

valentinpalkovic commented May 5, 2025

Hi @yatishgoel,
Thank you for your contribution.
I am wondering, whether we can preserve comments inside of a json5 configuration by transforming comments into namespaced key-value pairs and when stringifying it back to transform it back to a comment. WDYT?

Hi @valentinpalkovic, thanks for the suggestion! If I’m understanding correctly, you’re proposing that we turn each comment into a temporary, namespaced property on the config object and then convert those properties back into comments when stringifying. That would certainly work to keep comments in place, but it does add a fair bit of custom parsing and post-processing logic.

Would you be open to using a lightweight library like comment-json instead? It handles full round-trip comment support—preserving whitespace, comment placement, and formatting—without the need for us to write and maintain our own encode/decode layer. Let me know what you think!

Exactly! Using comment-json may indeed be what we are searching for. Are you willing to integrate it into your PR and write some additional tests?

@yatishgoel
Copy link
Contributor Author

Hi @yatishgoel,
Thank you for your contribution.
I am wondering, whether we can preserve comments inside of a json5 configuration by transforming comments into namespaced key-value pairs and when stringifying it back to transform it back to a comment. WDYT?

Hi @valentinpalkovic, thanks for the suggestion! If I’m understanding correctly, you’re proposing that we turn each comment into a temporary, namespaced property on the config object and then convert those properties back into comments when stringifying. That would certainly work to keep comments in place, but it does add a fair bit of custom parsing and post-processing logic.
Would you be open to using a lightweight library like comment-json instead? It handles full round-trip comment support—preserving whitespace, comment placement, and formatting—without the need for us to write and maintain our own encode/decode layer. Let me know what you think!

Exactly! Using comment-json may indeed be what we are searching for. Are you willing to integrate it into your PR and write some additional tests?

Sure

@yatishgoel
Copy link
Contributor Author

Hi @valentinpalkovic,

I've updated the PR to implement that approach:

  • Added comment-json as a dependency.
  • Updated configureEslintPlugin to use commentJson.parse() (correctly configured now to preserve comments) and commentJson.stringify().
  • Updated the unit tests and snapshots to reflect the new behavior.

During testing, I found that while comment-json successfully preserves comments outside of modified arrays (like top-level comments or those attached to other properties like rules), it unfortunately did not preserve comments located inside the extends array when that array was modified by adding the new recommendation. I tried different array modification methods (.push() vs. spread operator), but the result was the same regarding inner-array comments.

The current solution now correctly fixes the original parsing crash and preserves most comments, which is an improvement over stripping them all.

I've updated the PR description to detail these findings accurately.

Could you please take another look when you have a chance?

Thanks!

@valentinpalkovic
Copy link
Contributor

Thank you! Could you push an updated version of the yarn.lock file please?

Copy link

nx-cloud bot commented May 6, 2025

View your CI Pipeline Execution ↗ for commit d50852a.

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 44s View ↗

☁️ Nx Cloud last updated this comment at 2025-05-06 07:46:49 UTC

@ndelangen ndelangen changed the title fix(cli): Handle JSON5 format in eslintrc.json during migration Eslint-plugin: Handle JSON5 format May 6, 2025
@ndelangen ndelangen assigned yannbf and unassigned ndelangen May 6, 2025
@ndelangen
Copy link
Member

@yannbf want to review this? please

@yannbf yannbf merged commit 69bbc76 into storybookjs:next May 6, 2025
55 of 57 checks passed
@github-actions github-actions bot mentioned this pull request May 6, 2025
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Storybook 9 - eslintPlugin automigration crashes on config files with JSON5 format
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy