8000 feat(pkg): add option packageIgnoreSort by Shinigami92 · Pull Request #472 · un-ts/prettier · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(pkg): add option packageIgnoreSort #472

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

Merged
merged 3 commits into from
Jun 7, 2025
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
5 changes: 5 additions & 0 deletions .changeset/full-frogs-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-pkg": minor
---

feat(pkg): add option `packageIgnoreSort`
23 changes: 18 additions & 5 deletions packages/pkg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { files } from './rules/files.js'
import { object } from './rules/object.js'
import { dependencyNames, sort } from './rules/sort.js'
import type {
FormatOptions,
ObjectExpression,
ObjectProperty,
FormatOptions,
} from './types.js'

const PKG_REG = /[/\\]?package\.json$/
Expand All @@ -25,11 +25,15 @@ const {
json: { parse },
} = babelParser.parsers

const DEFAULT_SORTS = ['engines', 'devEngines', 'scripts', ...dependencyNames]

const format = (properties: ObjectProperty[], options: FormatOptions) => {
let props = ['engines', 'devEngines', 'scripts', ...dependencyNames].reduce(
(acc, item) => object(acc, item),
sort(properties, options),
)
const { packageIgnoreSort } = options
let props = (
packageIgnoreSort?.length
? DEFAULT_SORTS.filter(item => !packageIgnoreSort.includes(item))
: DEFAULT_SORTS
).reduce((acc, item) => object(acc, item), sort(properties, options))
props = files(props)
return props
}
Expand Down Expand Up @@ -62,5 +66,14 @@ export default {
description:
'An array of property names to sort the package.json properties by.',
},
packageIgnoreSort: {
since: '0.21.0',
category: 'Package',
type: 'string',
array: true,
default: [{ value: [] }],
description:
'An array of property names to ignore when sorting the package.json properties.',
},
},
} as Plugin
1 change: 1 addition & 0 deletions packages/pkg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export type { StringLiteral } from '@babel/types'

export interface FormatOptions {
packageSortOrder?: string[]
packageIgnoreSort?: string[]
}
53 changes: 53 additions & 0 deletions packages/pkg/test/__snapshots__/ignore-sort.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`ignore-sort 1`] = `
"{
"name": "prettier-plugin-pkg",
"version": "0.0.0",
"description": "An opinionated package.json formatter plugin for Prettier",
"repository": "git@github.com/un-ts/prettier.git",
"homepage": "https://github.com/un-ts/prettier/tree/master/packages/pkg",
"author": "JounQin <admin@1stg.me> (https://www.1stG.me)",
"license": "MPL-2.0",
"engines": {
"node": ">= 8.0.0"
},
"main": "lib/cjs",
"module": "lib/esm",
"es2015": "lib/es2015",
"files": [
"lib",
"rules"
],
"keywords": [
"package",
"package.json",
"pkg",
"plugin",
"prettier",
"prettier-plugin"
],
"peerDependencies": {
"prettier": "^1.18.2"
},
"resolutions": {
"imagemin-gifsicle": "^6.0.1",
"@babel/core": "^7.9.0",
"typescript": "^3.8.3",
"prettier": "^2.0.4",
"@babel/preset-env": "^7.12.1",
"eslint-plugin-prettier": "^3.1.2"
},
"commitlint": {
"extends": [
"@1stg"
]
},
"eslintIgnore": [
"CHANGELOG.md",
"lib",
"!.*.js"
]
}
"
`;
17 changes: 17 additions & 0 deletions packages/pkg/test/ignore-sort.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { format } from 'prettier'

import pkg1 from './fixtures/fixture1.json'

import PkgPlugin from 'prettier-plugin-pkg'

test('ignore-sort', async () => {
const input = JSON.stringify(pkg1, null, 2)
const output = await format(input, {
filepath: 'package.json',
parser: 'json-stringify',
plugins: [PkgPlugin],
packageIgnoreSort: ['resolutions'],
})

expect(output).toMatchSnapshot()
})
Loading
0