Skip to content

Commit 04e576c

Browse files
authored
feat: merge prettier plugins with options (#614)
1 parent 2862e4c commit 04e576c

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

src/configs/formatters.ts

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
2-
import type { VendoredPrettierOptions } from '../vender/prettier-types'
2+
import type { VendoredPrettierOptions, VendoredPrettierRuleOptions } from '../vender/prettier-types'
33

44
import { isPackageExists } from 'local-pkg'
55
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_SVG, GLOB_XML } from '../globs'
66

77
import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils'
88
import { StylisticConfigDefaults } from './stylistic'
99

10+
function mergePrettierOptions(
11+
options: VendoredPrettierOptions,
12+
overrides: VendoredPrettierRuleOptions = {},
13+
): VendoredPrettierRuleOptions {
14+
return {
15+
...options,
16+
...overrides,
17+
plugins: [
18+
...(overrides.plugins || []),
19+
...(options.plugins || []),
20+
],
21+
}
22+
}
23+
1024
export async function formatters(
1125
options: OptionsFormatters | true = {},
1226
stylistic: StylisticConfig = {},
@@ -57,7 +71,7 @@ export async function formatters(
5771
options.prettierOptions || {},
5872
)
5973

60-
const prettierXmlOptions = {
74+
const prettierXmlOptions: VendoredPrettierOptions = {
6175
xmlQuoteAttributes: 'double',
6276
xmlSelfClosingSpace: true,
6377
xmlSortAttributesByKey: false,
@@ -95,10 +109,9 @@ export async function formatters(
95109
rules: {
96110
'format/prettier': [
97111
'error',
98-
{
99-
...prettierOptions,
112+
mergePrettierOptions(prettierOptions, {
100113
parser: 'css',
101-
},
114+
}),
102115
],
103116
},
104117
},
@@ -111,10 +124,9 @@ export async function formatters(
111124
rules: {
112125
'format/prettier': [
113126
'error',
114-
{
115-
...prettierOptions,
127+
mergePrettierOptions(prettierOptions, {
116128
parser: 'scss',
117-
},
129+
}),
118130
],
119131
},
120132
},
@@ -127,10 +139,9 @@ export async function formatters(
127139
rules: {
128140
'format/prettier': [
129141
'error',
130-
{
131-
...prettierOptions,
142+
mergePrettierOptions(prettierOptions, {
132143
parser: 'less',
133-
},
144+
}),
134145
],
135146
},
136147
},
@@ -147,10 +158,9 @@ export async function formatters(
147158
rules: {
148159
'format/prettier': [
149160
'error',
150-
{
151-
...prettierOptions,
161+
mergePrettierOptions(prettierOptions, {
152162
parser: 'html',
153-
},
163+
}),
154164
],
155165
},
156166
})
@@ -166,14 +176,12 @@ export async function formatters(
166176
rules: {
167177
'format/prettier': [
168178
'error',
169-
{
170-
...prettierXmlOptions,
171-
...prettierOptions,
179+
mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, {
172180
parser: 'xml',
173181
plugins: [
174182
'@prettier/plugin-xml',
175183
],
176-
},
184+
}),
177185
],
178186
},
179187
})
@@ -188,14 +196,12 @@ export async function formatters(
188196
rules: {
189197
'format/prettier': [
190198
'error',
191-
{
192-
...prettierXmlOptions,
193-
...prettierOptions,
199+
mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, {
194200
parser: 'xml',
195201
plugins: [
196202
'@prettier/plugin-xml',
197203
],
198-
},
204+
}),
199205
],
200206
},
201207
})
@@ -223,11 +229,10 @@ export async function formatters(
223229
[`format/${formater}`]: [
224230
'error',
225231
formater === 'prettier'
226-
? {
227-
...prettierOptions,
228-
embeddedLanguageFormatting: 'off',
229-
parser: 'markdown',
230-
}
232+
? mergePrettierOptions(prettierOptions, {
233+
embeddedLanguageFormatting: 'off',
234+
parser: 'markdown',
235+
})
231236
: {
232237
...dprintOptions,
233238
language: 'markdown',
@@ -246,14 +251,13 @@ export async function formatters(
246251
rules: {
247252
'format/prettier': [
248253
'error',
249-
{
250-
...prettierOptions,
254+
mergePrettierOptions(prettierOptions, {
251255
embeddedLanguageFormatting: 'off',
252256
parser: 'slidev',
253257
plugins: [
254258
'prettier-plugin-slidev',
255259
],
256-
},
260+
}),
257261
],
258262
},
259263
})
@@ -270,13 +274,12 @@ export async function formatters(
270274
rules: {
271275
'format/prettier': [
272276
'error',
273-
{
274-
...prettierOptions,
277+
mergePrettierOptions(prettierOptions, {
275278
parser: 'astro',
276279
plugins: [
277280
'prettier-plugin-astro',
278281
],
279-
},
282+
}),
280283
],
281284
},
282285
})
@@ -306,10 +309,9 @@ export async function formatters(
306309
rules: {
307310
'format/prettier': [
308311
'error',
309-
{
310-
...prettierOptions,
312+
mergePrettierOptions(prettierOptions, {
311313
parser: 'graphql',
312-
},
314+
}),
313315
],
314316
},
315317
})

src/vender/prettier-types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
export type VendoredPrettierOptions = Partial<VendoredPrettierOptionsRequired>
66

7+
export type VendoredPrettierRuleOptions = VendoredPrettierOptions & {
8+
parser?: BuiltInParserName | ExternalParserName
9+
[k: string]: unknown | undefined
10+
}
11+
712
export interface VendoredPrettierOptionsRequired {
813
/**
914
* Specify the line length that the printer will wrap on.
@@ -146,6 +151,8 @@ export type BuiltInParserName =
146151
| 'xml'
147152
| 'yaml'
148153

154+
export type ExternalParserName = 'slidev' | 'astro'
155+
149156
// This utility is here to handle the case where you have an explicit union
150157
// between string literals and the generic string type. It would normally
151158
// resolve out to just the string type, but this generic LiteralUnion maintains

0 commit comments

Comments
 (0)