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

feat: merge prettier plugins with options #614

Merged
merged 1 commit into from
Sep 26, 2024
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
74 changes: 38 additions & 36 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import type { VendoredPrettierOptions, VendoredPrettierRuleOptions } from '../vender/prettier-types'

import { isPackageExists } from 'local-pkg'
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'

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

function mergePrettierOptions(
options: VendoredPrettierOptions,
overrides: VendoredPrettierRuleOptions = {},
): VendoredPrettierRuleOptions {
return {
...options,
...overrides,
plugins: [
...(overrides.plugins || []),
...(options.plugins || []),
],
}
}

export async function formatters(
options: OptionsFormatters | true = {},
stylistic: StylisticConfig = {},
Expand Down Expand Up @@ -57,7 +71,7 @@ export async function formatters(
options.prettierOptions || {},
)

const prettierXmlOptions = {
const prettierXmlOptions: VendoredPrettierOptions = {
xmlQuoteAttributes: 'double',
xmlSelfClosingSpace: true,
xmlSortAttributesByKey: false,
Expand Down Expand Up @@ -95,10 +109,9 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
parser: 'css',
},
}),
],
},
},
Expand All @@ -111,10 +124,9 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
parser: 'scss',
},
}),
],
},
},
Expand All @@ -127,10 +139,9 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
parser: 'less',
},
}),
],
},
},
Expand All @@ -147,10 +158,9 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
parser: 'html',
},
}),
],
},
})
Expand All @@ -166,14 +176,12 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierXmlOptions,
...prettierOptions,
mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, {
parser: 'xml',
plugins: [
'@prettier/plugin-xml',
],
},
}),
],
},
})
Expand All @@ -188,14 +196,12 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierXmlOptions,
...prettierOptions,
mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, {
parser: 'xml',
plugins: [
'@prettier/plugin-xml',
],
},
}),
],
},
})
Expand Down Expand Up @@ -223,11 +229,10 @@ export async function formatters(
[`format/${formater}`]: [
'error',
formater === 'prettier'
? {
...prettierOptions,
embeddedLanguageFormatting: 'off',
parser: 'markdown',
}
? mergePrettierOptions(prettierOptions, {
embeddedLanguageFormatting: 'off',
parser: 'markdown',
})
: {
...dprintOptions,
language: 'markdown',
Expand All @@ -246,14 +251,13 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
embeddedLanguageFormatting: 'off',
parser: 'slidev',
plugins: [
'prettier-plugin-slidev',
],
},
}),
],
},
})
Expand All @@ -270,13 +274,12 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
parser: 'astro',
plugins: [
'prettier-plugin-astro',
],
},
}),
],
},
})
Expand Down Expand Up @@ -306,10 +309,9 @@ export async function formatters(
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
mergePrettierOptions(prettierOptions, {
parser: 'graphql',
},
}),
],
},
})
Expand Down
7 changes: 7 additions & 0 deletions src/vender/prettier-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

export type VendoredPrettierOptions = Partial<VendoredPrettierOptionsRequired>

export type VendoredPrettierRuleOptions = VendoredPrettierOptions & {
parser?: BuiltInParserName | ExternalParserName
[k: string]: unknown | undefined
}

export interface VendoredPrettierOptionsRequired {
/**
* Specify the line length that the printer will wrap on.
Expand Down Expand Up @@ -146,6 +151,8 @@ export type BuiltInParserName =
| 'xml'
| 'yaml'

export type ExternalParserName = 'slidev' | 'astro'

// This utility is here to handle the case where you have an explicit union
// between string literals and the generic string type. It would normally
// resolve out to just the string type, but this generic LiteralUnion maintains
Expand Down