From 14d260754b15ed034fe42b01a6460df220fb8782 Mon Sep 17 00:00:00 2001 From: Yuns Date: Sat, 24 Aug 2024 08:57:17 +0800 Subject: [PATCH] feat: support prettierConfigs with internal default options --- src/configs/formatters.ts | 12 +++++++++++- src/types.ts | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/configs/formatters.ts b/src/configs/formatters.ts index 76cfdd81fb..f7c60273df 100644 --- a/src/configs/formatters.ts +++ b/src/configs/formatters.ts @@ -53,7 +53,7 @@ export async function formatters( options.prettierOptions || {}, ) - const prettierXmlOptions = { + const prettierXmlOptions: VendoredPrettierOptions = { xmlQuoteAttributes: 'double', xmlSelfClosingSpace: true, xmlSortAttributesByKey: false, @@ -289,5 +289,15 @@ export async function formatters( }) } + if (options.prettierConfigs) { + const prettierConfigs = options.prettierConfigs({ + defaultOptions: prettierOptions, + defaultXmlOptions: prettierXmlOptions, + }) + prettierConfigs.forEach((item) => { + configs.push(item) + }) + } + return configs } diff --git a/src/types.ts b/src/types.ts index 18646d6801..c649493f52 100644 --- a/src/types.ts +++ b/src/types.ts @@ -50,6 +50,11 @@ export type OptionsTypescript = (OptionsTypeScriptWithTypes & OptionsOverrides) | (OptionsTypeScriptParserOptions & OptionsOverrides) +export interface PrettierConfigsOptions { + defaultOptions: VendoredPrettierOptions + defaultXmlOptions: VendoredPrettierOptions +} + export interface OptionsFormatters { /** * Enable formatting support for CSS, Less, Sass, and SCSS. @@ -93,6 +98,11 @@ export interface OptionsFormatters { */ prettierOptions?: VendoredPrettierOptions + /** + * Custom configs let you have different configuration for certain file extensions and specific files. + */ + prettierConfigs?: (options: PrettierConfigsOptions) => TypedFlatConfigItem[] + /** * Custom options for dprint. *