Skip to content
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

Allow ignoring certain paths from sorting #211

Open
JoshuaKGoldberg opened this issue Feb 7, 2023 · 3 comments
Open

Allow ignoring certain paths from sorting #211

JoshuaKGoldberg opened this issue Feb 7, 2023 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Feb 7, 2023

👋 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: typescript-eslint/typescript-eslint#6017

@ota-meshi
Copy link
Owner

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" }
        }
    ]

DEMO

@JoshuaKGoldberg
Copy link
Contributor Author

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.

@ota-meshi
Copy link
Owner

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" } ]

@ota-meshi ota-meshi added the enhancement New feature or request label Feb 9, 2023
@ota-meshi ota-meshi added the help wanted Extra attention is needed label May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants