From bf1cfb78788b665cc38809ad3e38c7ee846fe2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Wed, 15 Apr 2020 17:21:55 +0200 Subject: [PATCH] ESLint Plugin: Add the recommended Prettier config to enforce WP coding styles (#21602) * ESLint Config: Add the recommended Prettier config to enforce WordPress coding styles * Update CHANGELOG.md --- .prettierrc.js | 2 +- package-lock.json | 1 + packages/eslint-plugin/CHANGELOG.md | 3 ++- packages/eslint-plugin/configs/recommended.js | 8 ++++++++ packages/eslint-plugin/package.json | 1 + packages/scripts/config/.eslintrc.js | 18 ------------------ packages/scripts/scripts/format-js.js | 6 ++++-- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/.prettierrc.js b/.prettierrc.js index 0605f83bf9078..51b8aeb41505a 100644 --- a/.prettierrc.js +++ b/.prettierrc.js @@ -1,3 +1,3 @@ // Import the default config file and expose it in the project root. // Useful for editor integrations. -module.exports = require( '@wordpress/scripts/config/.prettierrc.js' ); +module.exports = require( '@wordpress/prettier-config' ); diff --git a/package-lock.json b/package-lock.json index afb2ad34edd75..81f363b22be72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11226,6 +11226,7 @@ "version": "file:packages/eslint-plugin", "dev": true, "requires": { + "@wordpress/prettier-config": "file:packages/prettier-config", "babel-eslint": "^10.1.0", "eslint-config-prettier": "^6.10.1", "eslint-plugin-jest": "^23.8.2", diff --git a/packages/eslint-plugin/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md index 7e158d2c2d2cb..63757ee8c5956 100644 --- a/packages/eslint-plugin/CHANGELOG.md +++ b/packages/eslint-plugin/CHANGELOG.md @@ -3,6 +3,7 @@ ### Bug Fixes - Fixes an error caused by missing `utils` directory from published package ([#21609](https://github.com/WordPress/gutenberg/pull/21609)). +- Added the recommended `Prettier` config that enforces WordPress coding style guidelines ([#21602](https://github.com/WordPress/gutenberg/pull/21602)). ## 5.0.0 (2020-04-15) @@ -25,7 +26,7 @@ - New Rule: [`@wordpress/i18n-ellipsis`](https://github.com/WordPress/gutenberg/blob/master/packages/eslint-plugin/docs/rules/i18n-ellipsis.md) - The bundled `eslint-plugin-react` dependency has been updated from requiring `^7.14.3` to requiring `^7.19.0` ([#21424](https://github.com/WordPress/gutenberg/pull/21424)). -### Bug Fix +### Bug Fixes - The `@wordpress/valid-sprintf` rule now detects usage of `sprintf` via `i18n.sprintf` (e.g. when using `import * as i18n from '@wordpress/i18n'`). - `@wordpress/no-unused-vars-before-return` will correctly consider other unused variables after encountering an instance of an `excludePattern` option exception. diff --git a/packages/eslint-plugin/configs/recommended.js b/packages/eslint-plugin/configs/recommended.js index 9e1659685a85a..bb4fcb4f24fee 100644 --- a/packages/eslint-plugin/configs/recommended.js +++ b/packages/eslint-plugin/configs/recommended.js @@ -1,7 +1,15 @@ +/** + * WordPress dependencies + */ +const defaultPrettierConfig = require( '@wordpress/prettier-config' ); + module.exports = { extends: [ require.resolve( './recommended-with-formatting.js' ), 'plugin:prettier/recommended', 'prettier/react', ], + rules: { + 'prettier/prettier': [ 'error', defaultPrettierConfig ], + }, }; diff --git a/packages/eslint-plugin/package.json b/packages/eslint-plugin/package.json index e73df50366703..f70186a4a2640 100644 --- a/packages/eslint-plugin/package.json +++ b/packages/eslint-plugin/package.json @@ -25,6 +25,7 @@ ], "main": "index.js", "dependencies": { + "@wordpress/prettier-config": "file:../prettier-config", "babel-eslint": "^10.1.0", "eslint-config-prettier": "^6.10.1", "eslint-plugin-jest": "^23.8.2", diff --git a/packages/scripts/config/.eslintrc.js b/packages/scripts/config/.eslintrc.js index b3bb9bce2d2d3..68e37cf71d9cc 100644 --- a/packages/scripts/config/.eslintrc.js +++ b/packages/scripts/config/.eslintrc.js @@ -1,9 +1,3 @@ -/** - * Internal dependencies - */ -const defaultPrettierConfig = require( './.prettierrc' ); -const { hasPrettierConfig } = require( '../utils' ); - const eslintConfig = { root: true, extends: [ @@ -17,16 +11,4 @@ const eslintConfig = { ], }; -if ( ! hasPrettierConfig() ) { - eslintConfig.rules = { - 'prettier/prettier': [ - 'error', - defaultPrettierConfig, - { - usePrettierrc: false, - }, - ], - }; -} - module.exports = eslintConfig; diff --git a/packages/scripts/scripts/format-js.js b/packages/scripts/scripts/format-js.js index 29b2a78ab1fb9..60b75a12d82bc 100644 --- a/packages/scripts/scripts/format-js.js +++ b/packages/scripts/scripts/format-js.js @@ -72,9 +72,11 @@ if ( ! checkResult.success ) { // needed for config, otherwise pass in args to default config in packages // See: https://prettier.io/docs/en/configuration.html let configArgs = []; -// TODO: once setup, use @wordpress/prettier-config if ( ! hasPrettierConfig() ) { - configArgs = [ '--config', fromConfigRoot( '.prettierrc.js' ) ]; + configArgs = [ + '--config', + require.resolve( '@wordpress/prettier-config' ), + ]; } // If `--ignore-path` is not explicitly specified, use the project's or global .eslintignore