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

Scripts: Ensure the default Prettier config is used with lint-js when needed #20071

Merged
merged 2 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Master

### Bug Fixes

- Ensure the default Prettier config is used in the `lint-js` script when no Prettier config is found in the project ([#20071](https://github.com/WordPress/gutenberg/pull/20071)).

## 7.0.0 (2020-02-04)

### Breaking Changes
Expand Down
22 changes: 21 additions & 1 deletion packages/scripts/config/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module.exports = {
/**
* Internal dependencies
*/
const defaultPrettierConfig = require( './.prettierrc' );
const { hasPrettierConfig } = require( '../utils' );

const eslintConfig = {
root: true,
extends: [
'plugin:@wordpress/eslint-plugin/recommended',
Expand All @@ -10,3 +16,17 @@ module.exports = {
'plugin:@wordpress/eslint-plugin/test-unit',
],
};

if ( ! hasPrettierConfig() ) {
eslintConfig.rules = {
'prettier/prettier': [
'error',
defaultPrettierConfig,
{
usePrettierrc: false,
},
],
};
}

module.exports = eslintConfig;
14 changes: 2 additions & 12 deletions packages/scripts/scripts/format-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
getArgFromCLI,
getFileArgsFromCLI,
hasArgInCLI,
hasPackageProp,
hasPrettierConfig,
hasProjectFile,
} = require( '../utils' );

Expand Down Expand Up @@ -72,18 +72,8 @@ 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 = [];
const hasProjectPrettierConfig =
hasProjectFile( '.prettierrc.js' ) ||
hasProjectFile( '.prettierrc.json' ) ||
hasProjectFile( '.prettierrc.toml' ) ||
hasProjectFile( '.prettierrc.yaml' ) ||
hasProjectFile( '.prettierrc.yml' ) ||
hasProjectFile( 'prettier.config.js' ) ||
hasProjectFile( '.prettierrc' ) ||
hasPackageProp( 'prettier' );

// TODO: once setup, use @wordpress/prettier-config
if ( ! hasProjectPrettierConfig ) {
if ( ! hasPrettierConfig() ) {
configArgs = [ '--config', fromConfigRoot( '.prettierrc.js' ) ];
}

Expand Down
11 changes: 11 additions & 0 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ const hasJestConfig = () =>
hasProjectFile( 'jest.config.json' ) ||
hasPackageProp( 'jest' );

const hasPrettierConfig = () =>
hasProjectFile( '.prettierrc.js' ) ||
hasProjectFile( '.prettierrc.json' ) ||
hasProjectFile( '.prettierrc.toml' ) ||
hasProjectFile( '.prettierrc.yaml' ) ||
hasProjectFile( '.prettierrc.yml' ) ||
hasProjectFile( 'prettier.config.js' ) ||
hasProjectFile( '.prettierrc' ) ||
hasPackageProp( 'prettier' );

const hasWebpackConfig = () =>
hasArgInCLI( '--config' ) ||
hasProjectFile( 'webpack.config.js' ) ||
Expand Down Expand Up @@ -98,4 +108,5 @@ module.exports = {
getWebpackArgs,
hasBabelConfig,
hasJestConfig,
hasPrettierConfig,
};
8 changes: 7 additions & 1 deletion packages/scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const {
hasFileArgInCLI,
spawnScript,
} = require( './cli' );
const { getWebpackArgs, hasBabelConfig, hasJestConfig } = require( './config' );
const {
getWebpackArgs,
hasBabelConfig,
hasJestConfig,
hasPrettierConfig,
} = require( './config' );
const {
buildWordPress,
downloadWordPressZip,
Expand All @@ -33,6 +38,7 @@ module.exports = {
hasFileArgInCLI,
hasJestConfig,
hasPackageProp,
hasPrettierConfig,
hasProjectFile,
downloadWordPressZip,
mergeYAMLConfigs,
Expand Down