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

feat: support flat config #161

Merged
merged 24 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 20 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
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {
},
},
{
files: "**/*.md/*.ts",
files: ["**/*.md/*.ts", "**/*.md/*.js"],
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
rules: {
"n/no-missing-import": [
"error",
Expand Down
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ npm install eslint eslint-plugin-package-json jsonc-eslint-parser --save-dev

## Usage

### Flat Config

JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
In your ESLint configuration file:

```js
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
import packageJson from "eslint-plugin-package-json/configs/recommended";

export default [
// your other ESLint configurations
packageJson,
];
```

If you want to override the recommended rules:

```js
import packageJson from "eslint-plugin-package-json/configs/recommended";

export default [
// your other ESLint configurations
{
...packageJson,
rules: {
...packageJson.rules,
"package-json/valid-package-def": "off",
},
},
];
```

JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
### Legacy Config

Add an override to your ESLint configuration file that specifies this plugin, [`jsonc-eslint-parser`](https://github.com/ota-meshi/jsonc-eslint-parser), and its recommended rules for your `package.json` file:

```js
Expand Down Expand Up @@ -75,7 +107,7 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord
💼 Configurations enabled in.\
✅ Set in the `recommended` configuration.\
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

| Name                        | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------- | :- | :- | :- |
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/unique-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

💼 This rule is enabled in the ✅ `recommended` config.

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

<!-- end auto-generated rule header -->
2 changes: 1 addition & 1 deletion docs/rules/valid-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

💼 This rule is enabled in the ✅ `recommended` config.

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/valid-repository-directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

💼 This rule is enabled in the ✅ `recommended` config.

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/valid-version.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

💼 This rule is enabled in the ✅ `recommended` config.

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

<!-- end auto-generated rule header -->

Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
},
"import": "./lib/index.mjs",
"require": "./lib/index.js"
},
"./configs/recommended": {
"types": {
"import": "./lib/configs/recommended.d.mts",
"require": "./lib/configs/recommended.d.ts"
},
"import": "./lib/configs/recommended.mjs",
"require": "./lib/configs/recommended.js"
}
},
"main": "./lib/index.js",
Expand Down
18 changes: 18 additions & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as parserJsonc from "jsonc-eslint-parser";

import { plugin, recommendedRuleSettings } from "../plugin.js";

const recommended = {
files: ["**/package.json"],
languageOptions: {
parser: parserJsonc,
},
plugins: {
get "package-json"() {
return plugin;
},
},
rules: recommendedRuleSettings,
};

export default recommended;

Check warning on line 18 in src/configs/recommended.ts

View check run for this annotation

Codecov / codecov/patch

src/configs/recommended.ts#L1-L18

Added lines #L1 - L18 were not covered by tests
30 changes: 4 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import { rule as orderProperties } from "./rules/order-properties.js";
import { rule as preferRepositoryShorthand } from "./rules/prefer-repository-shorthand.js";
import { rule as sortCollections } from "./rules/sort-collections.js";
import { rule as uniqueDependencies } from "./rules/unique-dependencies.js";
import { rule as validLocalDependency } from "./rules/valid-local-dependency.js";
import { rule as validName } from "./rules/valid-name.js";
import { rule as validPackageDef } from "./rules/valid-package-def.js";
import { rule as validRepositoryDirectory } from "./rules/valid-repository-directory.js";
import { rule as validVersion } from "./rules/valid-version.js";

export const rules = {
"order-properties": orderProperties,
"prefer-repository-shorthand": preferRepositoryShorthand,
"sort-collections": sortCollections,
"unique-dependencies": uniqueDependencies,
"valid-local-dependency": validLocalDependency,
"valid-name": validName,
"valid-package-def": validPackageDef,
"valid-repository-directory": validRepositoryDirectory,
"valid-version": validVersion,
};
import { plugin, recommendedRuleSettings } from "./plugin.js";

export const rules = plugin.rules;
export const configs = {
recommended: {
rules: Object.fromEntries(
Object.entries(rules)
.filter(([, rule]) => rule.meta.docs?.recommended)
.map(([name]) => ["package-json/" + name, "error" as const]),
),
plugins: ["package-json"],
rules: recommendedRuleSettings,
},
};
44 changes: 44 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createRequire } from "node:module";

import { rule as orderProperties } from "./rules/order-properties.js";
import { rule as preferRepositoryShorthand } from "./rules/prefer-repository-shorthand.js";
import { rule as sortCollections } from "./rules/sort-collections.js";
import { rule as uniqueDependencies } from "./rules/unique-dependencies.js";
import { rule as validLocalDependency } from "./rules/valid-local-dependency.js";
import { rule as validName } from "./rules/valid-name.js";
import { rule as validPackageDef } from "./rules/valid-package-def.js";
import { rule as validRepositoryDirectory } from "./rules/valid-repository-directory.js";
import { rule as validVersion } from "./rules/valid-version.js";

const require = createRequire(import.meta.url || __filename);

const { name, version } = require("../package.json") as {
name: string;
version: string;
};

const allRules = {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
"order-properties": orderProperties,
"prefer-repository-shorthand": preferRepositoryShorthand,
"sort-collections": sortCollections,
"unique-dependencies": uniqueDependencies,
"valid-local-dependency": validLocalDependency,
"valid-name": validName,
"valid-package-def": validPackageDef,
"valid-repository-directory": validRepositoryDirectory,
"valid-version": validVersion,
};

export const plugin = {
meta: {
name,
version,
},
rules: allRules,
};

export const recommendedRuleSettings = Object.fromEntries(
Object.entries(allRules)
.filter(([, rule]) => rule.meta.docs?.recommended)
.map(([name]) => ["package-json/" + name, "error" as const]),
);
Loading