Skip to content

Commit

Permalink
fix(prettier-config): call preprocess() if it exists (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown authored Mar 26, 2024
1 parent bf57d93 commit 0ce31e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-feet-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bfra.me/prettier-config": patch
---

Call `preprocess()` on the `json-stringify` parser if it's defined
8 changes: 6 additions & 2 deletions packages/prettier-config/prettier.config.js

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

9 changes: 7 additions & 2 deletions packages/prettier-config/prettier.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {Config, ParserOptions} from 'prettier'
import {parsers} from 'prettier/plugins/babel'
import {format, type Options as PrettierPackageJsonOptions} from 'prettier-package-json'

const jsonStringifyParser = parsers['json-stringify']

/**
* Shared Prettier configuration for bfra.me projects.
*/
Expand Down Expand Up @@ -71,11 +73,14 @@ const config: Config = {
{
parsers: {
'json-stringify': {
...parsers['json-stringify'],
...jsonStringifyParser,

preprocess(text: string, options: ParserOptions) {
if (jsonStringifyParser.preprocess) {
text = jsonStringifyParser.preprocess(text, options)
}
if (/package.*json$/u.test(options.filepath)) {
return format(JSON.parse(text), {
text = format(JSON.parse(text), {
tabWidth: options.tabWidth,
useTabs: options.useTabs === true,
...((options['prettier-package-json'] ??
Expand Down

0 comments on commit 0ce31e4

Please sign in to comment.