-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Eslint-plugin: Handle JSON5 format #31336
Conversation
There was a problem hiding this 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
code/core/src/cli/eslintPlugin.ts
Outdated
@@ -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)); |
There was a problem hiding this comment.
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.
await writeFile(eslintConfigFile, JSON.stringify(eslintConfig, undefined, spaces)); | |
await writeFile(eslintConfigFile, json5.stringify(eslintConfig, undefined, spaces)); |
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 |
Exactly! Using |
Sure |
I've updated the PR to implement that approach:
During testing, I found that while 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! |
Thank you! Could you push an updated version of the yarn.lock file please? |
View your CI Pipeline Execution ↗ for commit d50852a.
☁️ Nx Cloud last updated this comment at |
@yannbf want to review this? please |
Closes #31316
What I did
The
eslintPlugin
automigration was previously usingJSON.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:
JSON.parse
withcomment-json.parse()
within theconfigureEslintPlugin
function. This correctly handles.eslintrc.json
files containing comments or other JSON5 features without errors.comment-json.stringify()
to write the modified configuration back to the file.comment-json
package as a dependency to@storybook/cli
.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 (likeextends
) when that array is modified (e.g., by addingplugin: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:
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:.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 theextends
array if possible).npx storybook@latest upgrade
using this branch.SyntaxError
related to parsing the ESLint config.eslint-plugin-storybook
is correctly configured in theextends
array in.eslintrc.json
.extends
array were preserved, while comments inside it might have been removed.Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/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>