-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prettier-plugins): switch to
sort-package-json
(#192)
- Loading branch information
1 parent
7a17932
commit b84fa58
Showing
9 changed files
with
201 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bfra.me/prettier-plugins": minor | ||
--- | ||
|
||
Switch to [`sort-package-json`](https://github.com/keithamus/sort-package-json) in the `package-json` plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@bfra.me/prettier-plugins": patch | ||
"@bfra.me/semantic-release": patch | ||
"@bfra.me/prettier-config": patch | ||
"@bfra.me/tsconfig": patch | ||
--- | ||
|
||
Format `package.json` with latest `@bfra.me/prettier-plugins` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,53 @@ | ||
import type {Parser, ParserOptions} from 'prettier' | ||
import type {Parser, SupportOption} from 'prettier' | ||
import prettier from 'prettier' | ||
import {parsers as babelParsers} from 'prettier/plugins/babel' | ||
import {format, type Options as PrettierPackageJsonOptions} from 'prettier-package-json' | ||
import {sortPackageJson} from 'sort-package-json' | ||
|
||
const jsonStringifyParser = babelParsers['json-stringify'] | ||
type SortPackageJsonOptions = NonNullable<Parameters<typeof sortPackageJson>[1]> | ||
|
||
export const parsers = { | ||
interface PluginOptions { | ||
/** Custom ordering array or comparator function. */ | ||
sortPackageJsonSortOrder: SortPackageJsonOptions['sortOrder'] | ||
} | ||
|
||
declare module 'prettier' { | ||
interface RequiredOptions extends PluginOptions {} | ||
} | ||
|
||
export const options: Record<keyof PluginOptions, SupportOption> = { | ||
sortPackageJsonSortOrder: { | ||
category: 'Format', | ||
type: 'string', | ||
description: 'Custom ordering array.', | ||
default: [{value: []}], | ||
array: true, | ||
}, | ||
} | ||
|
||
const parser = babelParsers['json-stringify'] | ||
|
||
export const parsers: Record<string, Parser> = { | ||
'json-stringify': { | ||
...jsonStringifyParser, | ||
...parser, | ||
|
||
preprocess(text: string, options: ParserOptions) { | ||
if (jsonStringifyParser.preprocess) { | ||
text = jsonStringifyParser.preprocess(text, options) | ||
} | ||
if (/package.*json$/u.test(options.filepath)) { | ||
text = format(JSON.parse(text), { | ||
tabWidth: options.tabWidth, | ||
useTabs: options.useTabs === true, | ||
...((options['prettier-package-json'] ?? {}) as Partial<PrettierPackageJsonOptions>), | ||
}) | ||
async parse(text, options) { | ||
const {filepath} = options | ||
if (/package.*json$/u.test(filepath)) { | ||
// Format the text with prettier to avoid any parsing errors | ||
text = await prettier.format(text, {filepath}) | ||
|
||
if (parser.preprocess) { | ||
text = parser.preprocess(text, options) | ||
} | ||
|
||
const sortOrder = options?.sortPackageJsonSortOrder | ||
text = sortPackageJson( | ||
text, | ||
(sortOrder && sortOrder.length > 0 ? {sortOrder} : {}) as SortPackageJsonOptions, | ||
) | ||
} | ||
return text | ||
|
||
return parser.parse(text, options) | ||
}, | ||
}, | ||
} satisfies Record<string, Parser> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.