Skip to content

Commit

Permalink
fix: directly use sort-package-json instead of prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Aug 9, 2024
1 parent e489186 commit 171168a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 40 deletions.
3 changes: 0 additions & 3 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
$schema: http://json.schemastore.org/prettierrc

plugins:
- prettier-plugin-packagejson

trailingComma: all

overrides:
Expand Down
1 change: 0 additions & 1 deletion knip.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"eslint-plugin-yml",
"eslint-processor-vue-blocks",
"jsonc-eslint-parser",
"prettier-plugin-packagejson",
"prettier",
"toml-eslint-parser",
"tsc-files",
Expand Down
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": "*",
Expand All @@ -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": "*"
Expand Down Expand Up @@ -189,6 +197,9 @@
"eslint-flat-config-utils": {
"optional": true
},
"eslint-formatting-reporter": {
"optional": true
},
"eslint-import-resolver-typescript": {
"optional": true
},
Expand Down Expand Up @@ -270,7 +281,7 @@
"prettier": {
"optional": true
},
"prettier-plugin-packagejson": {
"sort-package-json": {
"optional": true
},
"toml-eslint-parser": {
Expand Down
24 changes: 6 additions & 18 deletions pnpm-lock.yaml

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

74 changes: 61 additions & 13 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down Expand Up @@ -391,6 +398,7 @@ export async function formatters(
{
name: "rs:formatter:json",
files: [GLOB_JSON],
ignores: ["**/package.json"],
languageOptions: {
parser: parserPlain,
},
Expand Down Expand Up @@ -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",
},
},
);
Expand Down

0 comments on commit 171168a

Please sign in to comment.