From e192d3ed4f8a422dd48ac6b24a3f8d7fbadd9024 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 4 Dec 2023 17:53:58 +0100 Subject: [PATCH] feat: add graphql to formatters, provide shorthand for enable all formatters --- README.md | 1 + eslint.config.js | 6 +----- src/cli/constants.ts | 2 +- src/configs/formatters.ts | 31 ++++++++++++++++++++++++++++++- src/factory.ts | 2 +- src/types.ts | 9 ++++++++- test/fixtures.test.ts | 7 +------ 7 files changed, 43 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7826bd6785..09c08a0060 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ Add the following settings to your `.vscode/settings.json`: // Silent the stylistic rules in you IDE, but still auto fix them "eslint.rules.customizations": [ { "rule": "style/*", "severity": "off" }, + { "rule": "format/*", "severity": "off" }, { "rule": "*-indent", "severity": "off" }, { "rule": "*-spacing", "severity": "off" }, { "rule": "*-spaces", "severity": "off" }, diff --git a/eslint.config.js b/eslint.config.js index 6d78959aa9..8d318f501f 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -11,11 +11,7 @@ export default antfu( 'fixtures', '_fixtures', ], - formatters: { - css: true, - html: true, - markdown: true, - }, + formatters: true, }, { files: ['src/**/*.ts'], diff --git a/src/cli/constants.ts b/src/cli/constants.ts index 2bab4392c3..45a4254f00 100644 --- a/src/cli/constants.ts +++ b/src/cli/constants.ts @@ -26,7 +26,7 @@ export const vscodeSettingsString = ` // Silent the stylistic rules in you IDE, but still auto fix them "eslint.rules.customizations": [ { "rule": "style/*", "severity": "off" }, - { "rule": "prettier/*", "severity": "off" }, + { "rule": "format/*", "severity": "off" }, { "rule": "*-indent", "severity": "off" }, { "rule": "*-spacing", "severity": "off" }, { "rule": "*-spaces", "severity": "off" }, diff --git a/src/configs/formatters.ts b/src/configs/formatters.ts index 1bf0c1f54b..8ec219cd5a 100644 --- a/src/configs/formatters.ts +++ b/src/configs/formatters.ts @@ -5,13 +5,23 @@ import type { FlatConfigItem, OptionsFormatters, StylisticConfig } from '../type import { StylisticConfigDefaults } from './stylistic' export async function formatters( - options: OptionsFormatters = {}, + options: OptionsFormatters | true = {}, stylistic: StylisticConfig = {}, ): Promise { await ensurePackages([ 'eslint-plugin-format', ]) + if (options === true) { + options = { + css: true, + graphql: true, + html: true, + markdown: true, + toml: true, + } + } + const { indent, quotes, @@ -170,5 +180,24 @@ export async function formatters( }) } + if (options.graphql) { + configs.push({ + files: ['**/*.graphql'], + languageOptions: { + parser: pluginFormat.parserPlain, + }, + name: 'antfu:formatter:graphql', + rules: { + 'format/prettier': [ + 'error', + { + ...prettierOptions, + parser: 'graphql', + }, + ], + }, + }) + } + return configs } diff --git a/src/factory.ts b/src/factory.ts index 2573e6be1b..cabbb172b0 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -172,7 +172,7 @@ export async function antfu( componentExts, overrides: overrides.markdown, }, - !!options.formatters?.markdown, + options.formatters === true || !!(options.formatters || {})?.markdown, ), ) } diff --git a/src/types.ts b/src/types.ts index 83cda11e6f..7a0ac5266a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -109,6 +109,11 @@ export interface OptionsFormatters { */ markdown?: 'prettier' | 'dprint' | boolean + /** + * Enable formatting support for GraphQL. + */ + graphql?: 'prettier' | boolean + /** * Custom options for Prettier. * @@ -280,9 +285,11 @@ export interface OptionsConfig extends OptionsComponentExts { * Requires installing: * - `eslint-plugin-format` * + * When set to `true`, it will enable all formatters. + * * @default false */ - formatters?: OptionsFormatters + formatters?: boolean | OptionsFormatters /** * Control to disable some rules in editors. diff --git a/test/fixtures.test.ts b/test/fixtures.test.ts index 7acfb75d1c..298e648e4d 100644 --- a/test/fixtures.test.ts +++ b/test/fixtures.test.ts @@ -60,12 +60,7 @@ runWithConfig( { typescript: true, vue: true, - formatters: { - css: true, - html: true, - markdown: true, - toml: 'dprint', - }, + formatters: true, }, )