Skip to content

Commit

Permalink
ESLint Plugin: Fix recommended preset when prettier is not instal…
Browse files Browse the repository at this point in the history
…led (#40634)

* Fix `recommended` ESLint preset when `prettier` is not installed

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.

* Update CHANGELOG.md

Co-authored-by: Greg Ziółkowski <[email protected]>
  • Loading branch information
chriszarate and gziolo authored Apr 27, 2022
1 parent d4766aa commit fd1c672
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- Fix the `recommended` preset when Prettier is not installed ([#40634](https://github.com/WordPress/gutenberg/pull/40634)).

## 12.0.0 (2022-04-08)

### Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const { cosmiconfigSync } = require( 'cosmiconfig' );
/**
* WordPress dependencies
*/
const defaultPrettierConfig = require( '@wordpress/prettier-config' );

/**
* Internal dependencies
Expand All @@ -22,6 +21,7 @@ if ( isPackageInstalled( 'prettier' ) ) {

const { config: localPrettierConfig } =
cosmiconfigSync( 'prettier' ).search() || {};
const defaultPrettierConfig = require( '@wordpress/prettier-config' );
const prettierConfig = { ...defaultPrettierConfig, ...localPrettierConfig };
config.rules = {
'prettier/prettier': [ 'error', prettierConfig ],
Expand Down

0 comments on commit fd1c672

Please sign in to comment.