Skip to content

Commit

Permalink
feat(eslint-config)!: update prettier config to use requireOf and d…
Browse files Browse the repository at this point in the history
…isable for Markdown code blocks (#802)
  • Loading branch information
marcusrbrown authored Dec 25, 2024
1 parent d19124f commit d6201d2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/popular-chairs-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/eslint-config": minor
---

Update prettier config to use `requireOf` and disable for Markdown code blocks.

2 changes: 1 addition & 1 deletion packages/eslint-config/src/config.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions packages/eslint-config/src/configs/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ import {interopDefault} from '../plugins'
*/
export type MarkdownOptions = Flatten<OptionsFiles & OptionsOverrides & OptionsPrettier>

export const mdFiles = [`*.md`].flatMap(p => [p, `**/${p}`])

export const codeInMdFiles = mdFiles.flatMap(p => [
`${p}/*.js`,
`${p}/*.jsx`,
`${p}/*.cjs`,
`${p}/*.mjs`,
`${p}/*.ts`,
`${p}/*.tsx`,
`${p}/*.cts`,
`${p}/*.mts`,
])

export const extInMdFiles = [
...codeInMdFiles,
...mdFiles.flatMap(p => [
`${p}/*.json`,
`${p}/*.json5`,
`${p}/*.jsonc`,
`${p}/*.toml`,
`${p}/*.yml`,
`${p}/*.yaml`,
`${p}/*.vue`,
`${p}/*.svelte`,
`${p}/*.astro`,
]),
]

/**
* Configures the ESLint rules for Markdown files.
* @param options - The configuration options for Markdown files.
Expand Down
50 changes: 33 additions & 17 deletions packages/eslint-config/src/configs/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type {Config} from '../config'
import type {Flatten, OptionsIsInEditor, OptionsOverrides} from '../options'
import {interopDefault} from '../plugins'
import {requireOf} from '../require-of'
import {fallback} from './fallback'
import {extInMdFiles} from './markdown'

/**
* Represents the options for the ESLint Prettier configuration.
Expand All @@ -14,25 +17,38 @@ export type PrettierOptions = Flatten<OptionsIsInEditor & OptionsOverrides>
*/
export async function prettier(options: PrettierOptions = {}): Promise<Config[]> {
const {isInEditor, overrides} = options
const [configPrettier, pluginPrettier] = await Promise.all([
interopDefault(import('eslint-config-prettier')),
interopDefault(import('eslint-plugin-prettier')),
])
return requireOf(
['eslint-config-prettier', 'eslint-plugin-prettier', 'prettier'],
async () => {
const [configPrettier, pluginPrettier] = await Promise.all([
interopDefault(import('eslint-config-prettier')),
interopDefault(import('eslint-plugin-prettier')),
])

return [
{
name: '@bfra.me/prettier',
plugins: {
prettier: pluginPrettier,
},
rules: {
...configPrettier.rules,
...(pluginPrettier.configs?.recommended as Config).rules,
return [
{
name: '@bfra.me/prettier',
plugins: {
prettier: pluginPrettier,
},
rules: {
...configPrettier.rules,
...(pluginPrettier.configs?.recommended as Config).rules,

...(isInEditor ? {} : {'prettier/prettier': 'error'}),
...(isInEditor ? {} : {'prettier/prettier': 'error'}),

...overrides,
},
...overrides,
},
},
{
name: '@bfra.me/prettier/overrides',
files: extInMdFiles,
rules: {
'prettier/prettier': 'off',
},
},
]
},
]
fallback,
)
}

0 comments on commit d6201d2

Please sign in to comment.