eslint-plugin-jsonc
eslint-plugin-jsonc copied to clipboard
Allow ignoring certain paths from sorting
👋 I'm a big fan of this plugin and use it in http://github.com/JoshuaKGoldberg/template-typescript-node-package to sort package.json
contents. Thanks for making it!
Turns out sometimes package.json
content ordering does matter. Per https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing, the types
resolution under "exports"
> "."
must come first:
// package.json
{
"exports": {
".": {
// Entry-point for TypeScript resolution - must occur first!
"types": "./types/index.d.ts",
// Entry-point for `import "my-package"` in ESM
"import": "./esm/index.js",
// Entry-point for `require("my-package") in CJS
"require": "./commonjs/index.cjs",
},
}
}
Would you be open to a config option allowing to exclude certain object property paths? Maybe:
module.exports = {
overrides: [
{
files: "*.json",
parser: "jsonc-eslint-parser",
rules: {
"jsonc/sort-keys": [
"error",
{
ignore: ["exports", "."],
},
],
},
},
],
};
...I'm not confident in that ignore: string[]
format. But can't think of a better option right now. 🤔
Potentially relevant: https://github.com/typescript-eslint/typescript-eslint/discussions/6017
Thank you for posting issue!
I think setting the rule options like this might work:
"jsonc/sort-keys": ["error",
{
"pathPattern": "^exports(?:\\[[^\\]]+\\]|\\.[^.]+)$",
"order": [
"type",
"import",
"require"
]
},
{
"pathPattern": ".*",
"order": { "type": "asc" }
}
]
Thanks for the quick response! That's close and probably usable for my use case. But it still enforces an ordering. Is there a way to ignore that particular pathPattern
altogether? It'd be nice to not have to specify the order, in case there are other types of exports
defined.
There is currently no way to explicitly set it to be ignored.
I think it's probably a good idea to add a order: { type: "ignore" }
option to the rule to achieve that.
The current rule ignores ordering for keys that are not defined, so by using a pattern that doesn't match anything, you can also use a hack like this to ignore ordering: "order": [ { "keyPattern": "(?=a)b" } ]