Skip to content

Commit 451db46

Browse files
authored
Add support for flat config (#311)
1 parent 531fddb commit 451db46

20 files changed

+439
-18
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-jsonc": minor
3+
---
4+
5+
Add support for flat config

.devcontainer/devcontainer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
// Configure tool-specific properties.
1818
"customizations": {
1919
"vscode": {
20-
"extensions": ["dbaeumer.vscode-eslint"],
21-
},
22-
},
20+
"extensions": ["dbaeumer.vscode-eslint"]
21+
}
22+
}
2323

2424
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
2525
// "remoteUser": "root"

.eslintrc.for-vscode.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
extends: [require.resolve("./.eslintrc.js")],
3+
overrides: [
4+
{
5+
files: ["tests/lib/rules/*"],
6+
extends: ["plugin:eslint-rule-tester/recommended-legacy"],
7+
},
8+
],
9+
};

.eslintrc.js

+17
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ module.exports = {
104104
project: "./tsconfig.json",
105105
},
106106
},
107+
{
108+
files: ["*.md/**", "**/*.md/**"],
109+
rules: {
110+
"n/no-missing-import": "off",
111+
},
112+
},
107113
{
108114
files: ["scripts/**/*.ts", "tests/**/*.ts", "tests-integrations/**/*.ts"],
109115
rules: {
@@ -113,5 +119,16 @@ module.exports = {
113119
"@typescript-eslint/no-misused-promises": "off",
114120
},
115121
},
122+
{
123+
files: ["docs/.vitepress/**/*.*"],
124+
rules: {
125+
"eslint-plugin/require-meta-docs-description": "off",
126+
"eslint-plugin/require-meta-docs-url": "off",
127+
"eslint-plugin/require-meta-type": "off",
128+
"eslint-plugin/prefer-message-ids": "off",
129+
"eslint-plugin/prefer-object-rule": "off",
130+
"eslint-plugin/require-meta-schema": "off",
131+
},
132+
},
116133
],
117134
};

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"markdown",
1010
"yaml"
1111
],
12+
"eslint.options": {
13+
"overrideConfigFile": "./.eslintrc.for-vscode.js"
14+
},
1215
"typescript.validate.enable": true,
1316
"javascript.validate.enable": false,
1417
"vetur.validation.script": false,

README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,43 @@ npm install --save-dev eslint eslint-plugin-jsonc
8989

9090
### Configuration
9191

92-
Use `.eslintrc.*` file to configure rules. See also: [https://eslint.org/docs/user-guide/configuring](https://eslint.org/docs/user-guide/configuring).
92+
#### New (ESLint>=v9) Config (Flat Config)
93+
94+
Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.
95+
96+
Example **eslint.config.js**:
97+
98+
```mjs
99+
import eslintPluginJsonc from 'eslint-plugin-jsonc';
100+
export default [
101+
// add more generic rule sets here, such as:
102+
// js.configs.recommended,
103+
...eslintPluginJsonc.configs['flat/recommended-with-jsonc'],
104+
{
105+
rules: {
106+
// override/add rules settings here, such as:
107+
// 'jsonc/rule-name': 'error'
108+
}
109+
}
110+
];
111+
```
112+
113+
This plugin provides configs:
114+
115+
- `*.configs['flat/base']` ... Configuration to enable correct JSON parsing.
116+
- `*.configs['flat/recommended-with-json']` ... Recommended configuration for JSON.
117+
- `*.configs['flat/recommended-with-jsonc']` ... Recommended configuration for JSONC.
118+
- `*.configs['flat/recommended-with-json5']` ... Recommended configuration for JSON5.
119+
- `*.configs['flat/prettier']` ... Turn off rules that may conflict with [Prettier](https://prettier.io/).
120+
- `*.configs['flat/all']` ... Enables all rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk.
121+
122+
This plugin will parse `.json`, `.jsonc` and `.json5` by default using the configuration provided by the plugin (unless you already have a parser configured - see below).
123+
124+
See [the rule list](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/) to get the `rules` that this plugin provides.
125+
126+
#### Legacy Config (ESLint<v9)
127+
128+
Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/>.
93129

94130
Example **.eslintrc.js**:
95131

docs/user-guide/index.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,43 @@ npm install --save-dev eslint eslint-plugin-jsonc
1818

1919
### Configuration
2020

21-
Use `.eslintrc.*` file to configure rules. See also: [https://eslint.org/docs/user-guide/configuring](https://eslint.org/docs/user-guide/configuring).
21+
#### New (ESLint>=v9) Config (Flat Config)
22+
23+
Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.
24+
25+
Example **eslint.config.js**:
26+
27+
```mjs
28+
import eslintPluginJsonc from 'eslint-plugin-jsonc';
29+
export default [
30+
// add more generic rule sets here, such as:
31+
// js.configs.recommended,
32+
...eslintPluginJsonc.configs['flat/recommended-with-jsonc'],
33+
{
34+
rules: {
35+
// override/add rules settings here, such as:
36+
// 'jsonc/rule-name': 'error'
37+
}
38+
}
39+
];
40+
```
41+
42+
This plugin provides configs:
43+
44+
- `*.configs['flat/base']` ... Configuration to enable correct JSON parsing.
45+
- `*.configs['flat/recommended-with-json']` ... Recommended configuration for JSON.
46+
- `*.configs['flat/recommended-with-jsonc']` ... Recommended configuration for JSONC.
47+
- `*.configs['flat/recommended-with-json5']` ... Recommended configuration for JSON5.
48+
- `*.configs['flat/prettier']` ... Turn off rules that may conflict with [Prettier](https://prettier.io/).
49+
- `*.configs['flat/all']` ... Enables all rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk.
50+
51+
This plugin will parse `.json`, `.jsonc` and `.json5` by default using the configuration provided by the plugin (unless you already have a parser configured - see below).
52+
53+
See [the rule list](../rules/index.md) to get the `rules` that this plugin provides.
54+
55+
#### Legacy Config (ESLint<v9)
56+
57+
Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/>.
2258

2359
Example **.eslintrc.js**:
2460

lib/configs/flat/all.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { rules } from "../../utils/rules";
2+
import base from "./base";
3+
const all: Record<string, string> = {};
4+
for (const rule of rules) {
5+
if (rule.meta.docs.ruleId === "jsonc/sort-array-values") continue;
6+
all[rule.meta.docs.ruleId] = "error";
7+
}
8+
9+
export default [
10+
...base,
11+
{
12+
rules: {
13+
...all,
14+
},
15+
},
16+
];

lib/configs/flat/base.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { ESLint } from "eslint";
2+
import * as parser from "jsonc-eslint-parser";
3+
export default [
4+
{
5+
files: [
6+
"*.json",
7+
"**/*.json",
8+
"*.json5",
9+
"**/*.json5",
10+
"*.jsonc",
11+
"**/*.jsonc",
12+
],
13+
plugins: {
14+
get jsonc(): ESLint.Plugin {
15+
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
16+
return require("../../index");
17+
},
18+
},
19+
languageOptions: {
20+
parser,
21+
},
22+
rules: {
23+
// ESLint core rules known to cause problems with JSON.
24+
strict: "off",
25+
"no-unused-expressions": "off",
26+
"no-unused-vars": "off",
27+
},
28+
},
29+
];

lib/configs/flat/prettier.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "npm run update"
4+
import base from "./base";
5+
export default [
6+
...base,
7+
{
8+
rules: {
9+
// eslint-plugin-jsonc rules
10+
"jsonc/array-bracket-newline": "off",
11+
"jsonc/array-bracket-spacing": "off",
12+
"jsonc/array-element-newline": "off",
13+
"jsonc/comma-dangle": "off",
14+
"jsonc/comma-style": "off",
15+
"jsonc/indent": "off",
16+
"jsonc/key-spacing": "off",
17+
"jsonc/no-floating-decimal": "off",
18+
"jsonc/object-curly-newline": "off",
19+
"jsonc/object-curly-spacing": "off",
20+
"jsonc/object-property-newline": "off",
21+
"jsonc/quote-props": "off",
22+
"jsonc/quotes": "off",
23+
"jsonc/space-unary-ops": "off",
24+
},
25+
},
26+
];
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "npm run update"
4+
import base from "./base";
5+
export default [
6+
...base,
7+
{
8+
rules: {
9+
// eslint-plugin-jsonc rules
10+
"jsonc/comma-dangle": "error",
11+
"jsonc/no-bigint-literals": "error",
12+
"jsonc/no-binary-expression": "error",
13+
"jsonc/no-binary-numeric-literals": "error",
14+
"jsonc/no-comments": "error",
15+
"jsonc/no-dupe-keys": "error",
16+
"jsonc/no-escape-sequence-in-identifier": "error",
17+
"jsonc/no-floating-decimal": "error",
18+
"jsonc/no-hexadecimal-numeric-literals": "error",
19+
"jsonc/no-infinity": "error",
20+
"jsonc/no-multi-str": "error",
21+
"jsonc/no-nan": "error",
22+
"jsonc/no-number-props": "error",
23+
"jsonc/no-numeric-separators": "error",
24+
"jsonc/no-octal-numeric-literals": "error",
25+
"jsonc/no-octal": "error",
26+
"jsonc/no-parenthesized": "error",
27+
"jsonc/no-plus-sign": "error",
28+
"jsonc/no-regexp-literals": "error",
29+
"jsonc/no-sparse-arrays": "error",
30+
"jsonc/no-template-literals": "error",
31+
"jsonc/no-undefined-value": "error",
32+
"jsonc/no-unicode-codepoint-escapes": "error",
33+
"jsonc/no-useless-escape": "error",
34+
"jsonc/quote-props": "error",
35+
"jsonc/quotes": "error",
36+
"jsonc/space-unary-ops": "error",
37+
"jsonc/valid-json-number": "error",
38+
"jsonc/vue-custom-block/no-parsing-error": "error",
39+
},
40+
},
41+
];
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "npm run update"
4+
import base from "./base";
5+
export default [
6+
...base,
7+
{
8+
rules: {
9+
// eslint-plugin-jsonc rules
10+
"jsonc/no-bigint-literals": "error",
11+
"jsonc/no-binary-expression": "error",
12+
"jsonc/no-binary-numeric-literals": "error",
13+
"jsonc/no-dupe-keys": "error",
14+
"jsonc/no-escape-sequence-in-identifier": "error",
15+
"jsonc/no-number-props": "error",
16+
"jsonc/no-numeric-separators": "error",
17+
"jsonc/no-octal-numeric-literals": "error",
18+
"jsonc/no-octal": "error",
19+
"jsonc/no-parenthesized": "error",
20+
"jsonc/no-regexp-literals": "error",
21+
"jsonc/no-sparse-arrays": "error",
22+
"jsonc/no-template-literals": "error",
23+
"jsonc/no-undefined-value": "error",
24+
"jsonc/no-unicode-codepoint-escapes": "error",
25+
"jsonc/no-useless-escape": "error",
26+
"jsonc/space-unary-ops": "error",
27+
"jsonc/vue-custom-block/no-parsing-error": "error",
28+
},
29+
},
30+
];
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// IMPORTANT!
2+
// This file has been automatically generated,
3+
// in order to update its content execute "npm run update"
4+
import base from "./base";
5+
export default [
6+
...base,
7+
{
8+
rules: {
9+
// eslint-plugin-jsonc rules
10+
"jsonc/no-bigint-literals": "error",
11+
"jsonc/no-binary-expression": "error",
12+
"jsonc/no-binary-numeric-literals": "error",
13+
"jsonc/no-dupe-keys": "error",
14+
"jsonc/no-escape-sequence-in-identifier": "error",
15+
"jsonc/no-floating-decimal": "error",
16+
"jsonc/no-hexadecimal-numeric-literals": "error",
17+
"jsonc/no-infinity": "error",
18+
"jsonc/no-multi-str": "error",
19+
"jsonc/no-nan": "error",
20+
"jsonc/no-number-props": "error",
21+
"jsonc/no-numeric-separators": "error",
22+
"jsonc/no-octal-numeric-literals": "error",
23+
"jsonc/no-octal": "error",
24+
"jsonc/no-parenthesized": "error",
25+
"jsonc/no-plus-sign": "error",
26+
"jsonc/no-regexp-literals": "error",
27+
"jsonc/no-sparse-arrays": "error",
28+
"jsonc/no-template-literals": "error",
29+
"jsonc/no-undefined-value": "error",
30+
"jsonc/no-unicode-codepoint-escapes": "error",
31+
"jsonc/no-useless-escape": "error",
32+
"jsonc/quote-props": "error",
33+
"jsonc/quotes": "error",
34+
"jsonc/space-unary-ops": "error",
35+
"jsonc/valid-json-number": "error",
36+
"jsonc/vue-custom-block/no-parsing-error": "error",
37+
},
38+
},
39+
];

lib/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import recommendedWithJsonc from "./configs/recommended-with-jsonc";
77
import recommendedWithJson5 from "./configs/recommended-with-json5";
88
import prettier from "./configs/prettier";
99
import all from "./configs/all";
10+
import flatBase from "./configs/base";
11+
import flatRecommendedWithJson from "./configs/flat/recommended-with-json";
12+
import flatRecommendedWithJsonc from "./configs/flat/recommended-with-jsonc";
13+
import flatRecommendedWithJson5 from "./configs/flat/recommended-with-json5";
14+
import flatPrettier from "./configs/flat/prettier";
15+
import flatAll from "./configs/all";
1016
import * as meta from "./meta";
1117

1218
// backward compatibility
@@ -26,6 +32,12 @@ const configs = {
2632
"recommended-with-json5": recommendedWithJson5,
2733
prettier,
2834
all,
35+
"flat/base": flatBase,
36+
"flat/recommended-with-json": flatRecommendedWithJson,
37+
"flat/recommended-with-jsonc": flatRecommendedWithJsonc,
38+
"flat/recommended-with-json5": flatRecommendedWithJson5,
39+
"flat/prettier": flatPrettier,
40+
"flat/all": flatAll,
2941
};
3042

3143
const rules = ruleList.reduce(

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"homepage": "https://ota-meshi.github.io/eslint-plugin-jsonc/",
6868
"dependencies": {
6969
"@eslint-community/eslint-utils": "^4.2.0",
70-
"eslint-compat-utils": "^0.4.0",
70+
"eslint-compat-utils": "^0.5.0",
7171
"espree": "^9.6.1",
7272
"graphemer": "^1.4.0",
7373
"jsonc-eslint-parser": "^2.0.4",
@@ -99,6 +99,7 @@
9999
"eslint-config-prettier": "^9.0.0",
100100
"eslint-plugin-eslint-comments": "^3.2.0",
101101
"eslint-plugin-eslint-plugin": "^5.0.0",
102+
"eslint-plugin-eslint-rule-tester": "^0.5.1",
102103
"eslint-plugin-json-schema-validator": "^4.6.1",
103104
"eslint-plugin-jsonc": "^2.0.0",
104105
"eslint-plugin-markdown": "^3.0.0",

0 commit comments

Comments
 (0)