Skip to content
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: Fix recommended preset when prettier is not installed#40634

Merged
merged 2 commits into from
Apr 27, 2022

Conversation

chriszarate
Copy link
Contributor

What?

This change fixes recommended ESLint preset when prettier is not installed.

Why?

The prettier package is intended to be an optional peer dependency, but is incorrectly required when using the @wordpress/eslint-plugin/recommended preset. Failure to install prettier results in an error when using that preset:

Oops! Something went wrong! :(

ESLint: 8.14.0

Error: Failed to load plugin '@wordpress/eslint-plugin' declared in '.eslintrc.json': Cannot find module 'prettier/package.json'
Require stack:
- /[...]/eslint-config-wpvip/node_modules/@wordpress/prettier-config/lib/index.js
- /[...]/eslint-config-wpvip/node_modules/@wordpress/eslint-plugin/configs/recommended.js
- /[...]/eslint-config-wpvip/node_modules/requireindex/index.js
- /[...]/eslint-config-wpvip/node_modules/@wordpress/eslint-plugin/configs/index.js
- /[...]/eslint-config-wpvip/node_modules/@wordpress/eslint-plugin/index.js
- /[...]/eslint-config-wpvip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs

The prettier rulesets should be added only when the user has installed prettier. However, the @worpress/prettier-config package eagerly loads prettier's package.json, leading to an error when it is not installed.

How?

This change moves the require of @worpress/prettier-config inside the relevant if block, so that it is only loaded when prettier is confirmed to be installed.

Testing Instructions

On any JavaScript project, use the following .eslintrc.json without prettier installed:

{
	"extends": [
		"plugin:@wordpress/eslint-plugin/recommended"
	]
}

The `prettier` package is incorrectly mandatory when using the `@wordpress/eslint-plugin/recommended` preset. Failure to install it results in an error.

`prettier` is intended to be an optional peer dependency, with related rulesets added only when the user has installed `prettier`. However, the `@worpress/prettier-config` package eagerly loads `prettier`'s `package.json`, leading to an error when it is not installed.

This change moves the require of `@worpress/prettier-config` inside the relevant `if` block, so that it is only loaded when `prettier` is confirmed to be installed.
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Apr 26, 2022
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @chriszarate! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

That makes a lot of sense to move this import 👍🏻

Thank you for the report and a fix for the issue 💯

@gziolo gziolo changed the title Fix recommended ESLint preset when prettier is not installed ESLint Plugin: Fix recommended preset when prettier is not installed Apr 27, 2022
@gziolo gziolo added [Package] ESLint plugin /packages/eslint-plugin [Type] Bug An existing feature does not function as intended labels Apr 27, 2022
@gziolo gziolo merged commit fd1c672 into WordPress:trunk Apr 27, 2022
@github-actions
Copy link

Congratulations on your first merged pull request, @chriszarate! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts:

https://profiles.wordpress.org/me/profile/edit/

And if you don't have a WordPress.org account, you can create one on this page:

https://login.wordpress.org/register

Kudos!

@github-actions github-actions bot added this to the Gutenberg 13.2 milestone Apr 27, 2022
@chriszarate chriszarate deleted the patch-1 branch April 27, 2022 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] ESLint plugin /packages/eslint-plugin [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants