Skip to content

Commit

Permalink
Improve loading of Prettier plugins and types (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown authored Aug 23, 2024
1 parent 88253a9 commit 3d6ffbc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .changeset/sweet-chairs-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/prettier-plugins": minor
---

Export fully-qualified Prettier `Plugin` and improve typings

6 changes: 6 additions & 0 deletions .changeset/twelve-moose-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/prettier-config": minor
---

Resolve installed Prettier plugins to `Plugin` objects; improve typings

3 changes: 2 additions & 1 deletion packages/prettier-config/120-proof.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import prettierConfig from '@bfra.me/prettier-config'
import type {Config} from 'prettier'

/**
* Shared Prettier configuration for bfra.me projects with `printWidth` set to 120 characters.
*/
const config = {
const config: Config = {
...prettierConfig,
printWidth: 120,
}
Expand Down
26 changes: 26 additions & 0 deletions packages/prettier-config/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {Plugin} from 'prettier'

export type InteropDefault<T> = T extends {default: infer U} ? U : T

function interopDefault<T>(m: T): InteropDefault<T> {
return (m as any).default || m
}

import {default as pluginPackageJson} from '@bfra.me/prettier-plugins/package-json'
const resolvedPlugins: Record<string, Plugin> = {
'@bfra.me/prettier-plugins/package-json': interopDefault(pluginPackageJson),
}

export const resolve = <T extends Plugin>(
resolver: (id: string) => string,
plugin: string,
): string | T => {
try {
if (resolvedPlugins[plugin]) {
return resolvedPlugins[plugin] as unknown as T
}
return resolver(plugin)
} finally {
return plugin
}
}
62 changes: 35 additions & 27 deletions packages/prettier-config/prettier.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {createRequire} from 'module'
import {resolve} from './plugins.js'
import type {Config} from 'prettier'

const {resolve} = createRequire(import.meta.url)
import {createRequire} from 'module'

const require = createRequire(import.meta.url)
const resolvePlugin = resolve.bind(null, require.resolve)

/**
* Shared Prettier configuration for bfra.me projects.
Expand All @@ -15,26 +18,6 @@ const config: Config = {
singleQuote: true,

overrides: [
// VS Code configuration files:
// Use 4 spaces for indentation to match the default VS Code settings.
{
files: ['.vscode/*.json', '.devcontainer/**/devcontainer*.json'],
options: {
tabWidth: 4,
},
},

// Markdown:
// - Disable single quotes for Markdown files.
// - Disable `proseWrap` to avoid wrapping prose.
{
files: ['*.md'],
options: {
proseWrap: 'never',
singleQuote: false,
},
},

// Adapted from https://github.com/sxzz/prettier-config/blob/1e5cc3021e5816aceebe0b90af1d530239442ecf/index.js.
// Require a pragma for paths typically not under version control or written by build tools.
{
Expand All @@ -44,32 +27,57 @@ const config: Config = {
'**/lib/**',
'**/coverage/**',
'**/out/**',
'**/temp/**',
'**/.idea/**',
'**/.next/**',
'**/.nuxt/**',
'**/.output/**',
'**/.vercel/**',
'**/.vitepress/cache/**',
'**/.vite-inspect/**',
'**/__snapshots__/**',

'**/auto-import?(s).d.ts',
'**/.changeset/*.md',
'**/CHANGELOG*.md',
'**/changelog*.md',
'**/components.d.ts',
'**/devcontainer-lock.json',
'**/LICENSE*',
'**/license*',
'**/*.min.*',

'package-lock.json',
'pnpm-lock.yaml',
'yarn.lock',
'**/package-lock.json',
'**/pnpm-lock.yaml',
'**/typed-router.d.ts',
'**/yarn.lock',
],
options: {
requirePragma: true,
},
},

// VS Code configuration files:
// Use 4 spaces for indentation to match the default VS Code settings.
{
files: ['.vscode/*.json', '.devcontainer/**/devcontainer*.json'],
options: {
tabWidth: 4,
},
},

// Markdown:
// - Disable single quotes for Markdown files.
// - Disable `proseWrap` to avoid wrapping prose.
{
files: ['*.md'],
options: {
proseWrap: 'never',
singleQuote: false,
},
},
],

plugins: ['@bfra.me/prettier-plugins/package-json'].map(plugin => resolve(plugin)),
plugins: ['@bfra.me/prettier-plugins/package-json'].map(resolvePlugin),
}

export default config
16 changes: 11 additions & 5 deletions packages/prettier-plugins/src/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type {Parser, ParserOptions, SupportOption} from 'prettier'
import type {Parser, ParserOptions, Plugin, SupportOption} from 'prettier'
import prettier from 'prettier'
import {parsers as babelParsers} from 'prettier/plugins/babel'
import {sortPackageJson} from 'sort-package-json'

type SortPackageJsonOptions = NonNullable<Parameters<typeof sortPackageJson>[1]>
export type SortPackageJsonOptions = NonNullable<Parameters<typeof sortPackageJson>[1]>

export type PrettierPackageJsonOptions = {
/** Custom ordering array or comparator function. */
sortPackageJsonSortOrder: SortPackageJsonOptions['sortOrder']
sortPackageJsonSortOrder?: SortPackageJsonOptions['sortOrder']
}

export type PrettierOptions = ParserOptions & PrettierPackageJsonOptions
Expand All @@ -17,7 +17,7 @@ export const options = {
category: 'Format',
type: 'string',
description: 'Custom ordering array.',
default: [{value: []}],
default: [{value: [] as string[]}],
array: true,
},
} satisfies Record<keyof PrettierPackageJsonOptions, SupportOption>
Expand All @@ -28,7 +28,6 @@ export const parsers = {
'json-stringify': {
...parser,

// @ts-expect-error - options
async parse(text: string, options: PrettierOptions) {
const {filepath} = options
if (/package.*json$/u.test(filepath)) {
Expand All @@ -50,3 +49,10 @@ export const parsers = {
},
},
} satisfies Record<string, Parser>

const PackageJsonPlugin: Plugin<string> = {
parsers,
options,
}

export default PackageJsonPlugin

0 comments on commit 3d6ffbc

Please sign in to comment.