diff --git a/.prettierrc.yml b/.prettierrc.yml index 6ff2e4e3..3104934f 100644 --- a/.prettierrc.yml +++ b/.prettierrc.yml @@ -1,8 +1,5 @@ $schema: http://json.schemastore.org/prettierrc -plugins: - - prettier-plugin-packagejson - trailingComma: all overrides: diff --git a/knip.jsonc b/knip.jsonc index dca8dfac..9cb713c9 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -38,7 +38,6 @@ "eslint-plugin-yml", "eslint-processor-vue-blocks", "jsonc-eslint-parser", - "prettier-plugin-packagejson", "prettier", "toml-eslint-parser", "tsc-files", diff --git a/package.json b/package.json index 5ce060d5..4d1ee337 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,9 @@ "name": "@rebeccastevens/eslint-config", "version": "0.0.0-development", "description": "My ESLint shareable config.", - "keywords": ["eslint config"], + "keywords": [ + "eslint config" + ], "homepage": "https://github.com/RebeccaStevens/eslint-config-rebeccastevens#readme", "bugs": { "url": "https://github.com/RebeccaStevens/eslint-config-rebeccastevens/issues" @@ -25,7 +27,11 @@ "require": "./dist/index.cjs", "import": "./dist/index.mjs" }, - "files": ["dist/", "package.json", "LICENSE"], + "files": [ + "dist/", + "package.json", + "LICENSE" + ], "scripts": { "build": "pnpm run build:node", "build:node": "rimraf dist && pnpm run typegen && rollup -c rollup.config.ts --configPlugin rollup-plugin-ts", @@ -80,6 +86,7 @@ "eslint": "9.8.0", "eslint-config-prettier": "9.1.0", "eslint-flat-config-utils": "0.3.0", + "eslint-formatting-reporter": "0.0.0", "eslint-import-resolver-typescript": "3.6.1", "eslint-merge-processors": "0.1.0", "eslint-plugin-eslint-comments": "3.2.0", @@ -114,11 +121,11 @@ "markdownlint": "0.34.0", "markdownlint-cli": "0.41.0", "prettier": "3.3.3", - "prettier-plugin-packagejson": "2.5.1", "rimraf": "6.0.1", "rollup": "4.20.0", "rollup-plugin-ts": "3.4.5", "semantic-release": "24.0.0", + "sort-package-json": "2.10.0", "toml-eslint-parser": "0.10.0", "tsc-files": "1.1.4", "tsx": "4.16.5", @@ -135,6 +142,7 @@ "eslint": "*", "eslint-config-prettier": "*", "eslint-flat-config-utils": "*", + "eslint-formatting-reporter": "*", "eslint-import-resolver-typescript": "*", "eslint-merge-processors": "*", "eslint-plugin-eslint-comments": "*", @@ -159,7 +167,7 @@ "eslint-processor-vue-blocks": "*", "jsonc-eslint-parser": "*", "prettier": "*", - "prettier-plugin-packagejson": "*", + "sort-package-json": "*", "toml-eslint-parser": "*", "vue-eslint-parser": "*", "yaml-eslint-parser": "*" @@ -189,6 +197,9 @@ "eslint-flat-config-utils": { "optional": true }, + "eslint-formatting-reporter": { + "optional": true + }, "eslint-import-resolver-typescript": { "optional": true }, @@ -270,7 +281,7 @@ "prettier": { "optional": true }, - "prettier-plugin-packagejson": { + "sort-package-json": { "optional": true }, "toml-eslint-parser": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d2330d88..f5abc668 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -102,6 +102,9 @@ importers: eslint-flat-config-utils: specifier: 0.3.0 version: 0.3.0 + eslint-formatting-reporter: + specifier: 0.0.0 + version: 0.0.0(eslint@9.8.0) eslint-import-resolver-typescript: specifier: 3.6.1 version: 3.6.1(@typescript-eslint/parser@8.0.1(eslint@9.8.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@9.8.0) @@ -204,9 +207,6 @@ importers: prettier: specifier: 3.3.3 version: 3.3.3 - prettier-plugin-packagejson: - specifier: 2.5.1 - version: 2.5.1(prettier@3.3.3) rimraf: specifier: 6.0.1 version: 6.0.1 @@ -219,6 +219,9 @@ importers: semantic-release: specifier: 24.0.0 version: 24.0.0(typescript@5.4.5) + sort-package-json: + specifier: 2.10.0 + version: 2.10.0 toml-eslint-parser: specifier: 0.10.0 version: 0.10.0 @@ -3575,14 +3578,6 @@ packages: resolution: {integrity: sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==} engines: {node: '>=6.0.0'} - prettier-plugin-packagejson@2.5.1: - resolution: {integrity: sha512-6i4PW1KxEA+VrokYNGeI/q8qQX3u5DNBc7eLr9GX4OrvWr9DMls1lhbuNopkKG7Li9rTNxerWnYQyjxoUO4ROA==} - peerDependencies: - prettier: '>= 1.16.0' - peerDependenciesMeta: - prettier: - optional: true - prettier@3.3.3: resolution: {integrity: sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==} engines: {node: '>=14'} @@ -8116,13 +8111,6 @@ snapshots: dependencies: fast-diff: 1.3.0 - prettier-plugin-packagejson@2.5.1(prettier@3.3.3): - dependencies: - sort-package-json: 2.10.0 - synckit: 0.9.1 - optionalDependencies: - prettier: 3.3.3 - prettier@3.3.3: {} pretty-ms@9.0.0: diff --git a/src/configs/formatters.ts b/src/configs/formatters.ts index 15bd754b..ef4fca62 100644 --- a/src/configs/formatters.ts +++ b/src/configs/formatters.ts @@ -62,12 +62,19 @@ export async function formatters( options.prettierOptions ?? {}, ); - const [pluginFormat, configPrettier] = (await loadPackages([ + const [pluginFormat, configPrettier, sortPackageJson, formattingReporter] = (await loadPackages([ "eslint-plugin-format", "eslint-config-prettier", + "sort-package-json", + "eslint-formatting-reporter", "prettier", - "prettier-plugin-packagejson", - ])) as [ESLint.Plugin, ESLint.ConfigData, unknown, unknown]; + ])) as [ + ESLint.Plugin, + ESLint.ConfigData, + (typeof import("sort-package-json"))["default"], + typeof import("eslint-formatting-reporter"), + unknown, + ]; const turnOffRulesForPrettier = { ...Object.fromEntries(Object.entries(configPrettier.rules ?? {}).filter(([, value]) => value === "off")), @@ -391,6 +398,7 @@ export async function formatters( { name: "rs:formatter:json", files: [GLOB_JSON], + ignores: ["**/package.json"], languageOptions: { parser: parserPlain, }, @@ -440,21 +448,61 @@ export async function formatters( }, }, { - name: "rs:formatter:json", + name: "rs:formatter:packagejson", files: ["**/package.json"], languageOptions: { parser: parserPlain, }, - rules: { - ...turnOffRulesForPrettier, - "format/prettier": [ - "error", - { - ...prettierOptions, - parser: "json", - plugins: ["prettier-plugin-packagejson"], + plugins: { + "package-json": { + meta: { + name: "rs:formatter:packagejson", }, - ], + rules: { + sort: { + meta: { + type: "layout", + fixable: "whitespace", + messages: formattingReporter.messages, + schema: [ + { + type: "object", + properties: { + parser: { + type: "string", + required: true, + }, + }, + additionalProperties: true, + }, + ], + }, + create(context) { + return { + Program() { + const sourceCode = context.sourceCode.text; + try { + const formatted = sortPackageJson(sourceCode); + formattingReporter.reportDifferences(context, sourceCode, formatted); + } catch (error) { + console.error(error); + context.report({ + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 0 }, + }, + message: "Failed to format package.json", + }); + } + }, + }; + }, + }, + }, + } satisfies ESLint.Plugin, + }, + rules: { + "package-json/sort": "error", }, }, );