Skip to content

Commit 5b1e9ac

Browse files
authored
Experimental support for @eslint/json (#369)
1 parent ce9d5b1 commit 5b1e9ac

File tree

94 files changed

+1336
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1336
-186
lines changed

.changeset/shaggy-berries-knock.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-jsonc": minor
3+
---
4+
5+
Experimental support for `@eslint/json`

.github/workflows/NodeCI.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
- name: Install Packages
8383
run: npm install
8484
- name: Test
85-
run: npm test
85+
run: npm run test:transpile-only
8686
test-with-eslint6:
8787
runs-on: ubuntu-latest
8888
strategy:
@@ -100,7 +100,7 @@ jobs:
100100
npx rimraf node_modules
101101
npm install
102102
- name: Test
103-
run: npm test
103+
run: npm run test:transpile-only
104104
test-with-eslint7:
105105
runs-on: ubuntu-latest
106106
strategy:
@@ -118,7 +118,7 @@ jobs:
118118
npx rimraf node_modules
119119
npm install
120120
- name: Test
121-
run: npm test
121+
run: npm run test:transpile-only
122122
test-and-coverage:
123123
runs-on: ubuntu-latest
124124
steps:

README.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/do
9595

9696
Example **eslint.config.js**:
9797

98-
```mjs
98+
```js
9999
import eslintPluginJsonc from 'eslint-plugin-jsonc';
100100
export default [
101101
// add more generic rule sets here, such as:
@@ -179,6 +179,55 @@ module.exports = {
179179
};
180180
```
181181

182+
#### **Experimental** support for `@eslint/json`
183+
184+
We've launched experimental support for [`@eslint/json`].
185+
186+
Configure it as follows:
187+
188+
```js
189+
import json from "@eslint/json";
190+
import jsonc from 'eslint-plugin-jsonc';
191+
192+
export default [
193+
{
194+
plugins: {
195+
json,
196+
jsonc
197+
},
198+
},
199+
{
200+
files: ["**/*.json"],
201+
language: "json/json",
202+
rules: {
203+
// 'json/rule-name': 'error',
204+
// 'jsonc/rule-name': 'error'
205+
},
206+
},
207+
{
208+
files: ["**/*.jsonc", ".vscode/*.json"],
209+
language: "json/jsonc",
210+
rules: {
211+
// 'json/rule-name': 'error',
212+
// 'jsonc/rule-name': 'error'
213+
},
214+
},
215+
{
216+
files: ["**/*.json5"],
217+
language: "json/json5",
218+
rules: {
219+
// 'json/rule-name': 'error',
220+
// 'jsonc/rule-name': 'error'
221+
},
222+
},
223+
];
224+
```
225+
226+
However, we're not yet sure how best to make this work.
227+
Please note that we may change behavior without notice.
228+
229+
[`@eslint/json`]: https://github.com/eslint/json
230+
182231
## :computer: Editor Integrations
183232

184233
### Visual Studio Code

docs/.vitepress/shim/rules/auto.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getSourceCode } from "eslint-compat-utils";
21
// eslint-disable-next-line n/no-missing-import -- The file path used is the actual rule path
32
import { createRule } from "../utils";
43
export default createRule("auto", {
@@ -16,7 +15,7 @@ export default createRule("auto", {
1615
type: "suggestion",
1716
},
1817
create(context) {
19-
const sourceCode = getSourceCode(context);
18+
const sourceCode = context.sourceCode;
2019
if (!sourceCode.parserServices.isJSON) {
2120
return {};
2221
}

docs/user-guide/index.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/do
2424

2525
Example **eslint.config.js**:
2626

27-
```mjs
27+
```js
2828
import eslintPluginJsonc from 'eslint-plugin-jsonc';
2929
export default [
3030
// add more generic rule sets here, such as:
@@ -108,6 +108,55 @@ module.exports = {
108108
};
109109
```
110110

111+
#### **Experimental** support for `@eslint/json`
112+
113+
We've launched experimental support for [`@eslint/json`].
114+
115+
Configure it as follows:
116+
117+
```js
118+
import json from "@eslint/json";
119+
import jsonc from 'eslint-plugin-jsonc';
120+
121+
export default [
122+
{
123+
plugins: {
124+
json,
125+
jsonc
126+
},
127+
},
128+
{
129+
files: ["**/*.json"],
130+
language: "json/json",
131+
rules: {
132+
// 'json/rule-name': 'error',
133+
// 'jsonc/rule-name': 'error'
134+
},
135+
},
136+
{
137+
files: ["**/*.jsonc", ".vscode/*.json"],
138+
language: "json/jsonc",
139+
rules: {
140+
// 'json/rule-name': 'error',
141+
// 'jsonc/rule-name': 'error'
142+
},
143+
},
144+
{
145+
files: ["**/*.json5"],
146+
language: "json/json5",
147+
rules: {
148+
// 'json/rule-name': 'error',
149+
// 'jsonc/rule-name': 'error'
150+
},
151+
},
152+
];
153+
```
154+
155+
However, we're not yet sure how best to make this work.
156+
Please note that we may change behavior without notice.
157+
158+
[`@eslint/json`]: https://github.com/eslint/json
159+
111160
## :computer: Editor Integrations
112161

113162
### Visual Studio Code

eslint.config.mjs

+5-18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export default [
5959
"@typescript-eslint/no-shadow": "off",
6060
"one-var": "off",
6161
"no-invalid-this": "off",
62+
"new-cap": "off",
63+
"func-style": "off",
6264

6365
"no-restricted-imports": [
6466
"error",
@@ -84,36 +86,18 @@ export default [
8486
message:
8587
"Please use `eslint-compat-utils` module's `getSourceCode(context)` instead.",
8688
},
87-
{
88-
object: "context",
89-
property: "sourceCode",
90-
message:
91-
"Please use `eslint-compat-utils` module's `getSourceCode(context)` instead.",
92-
},
9389
{
9490
object: "context",
9591
property: "getFilename",
9692
message:
9793
"Please use `eslint-compat-utils` module's `getFilename(context)` instead.",
9894
},
99-
{
100-
object: "context",
101-
property: "filename",
102-
message:
103-
"Please use `eslint-compat-utils` module's `getFilename(context)` instead.",
104-
},
10595
{
10696
object: "context",
10797
property: "getCwd",
10898
message:
10999
"Please use `eslint-compat-utils` module's `getCwd(context)` instead.",
110100
},
111-
{
112-
object: "context",
113-
property: "cwd",
114-
message:
115-
"Please use `eslint-compat-utils` module's `getCwd(context)` instead.",
116-
},
117101
{
118102
object: "context",
119103
property: "getScope",
@@ -140,6 +124,9 @@ export default [
140124
},
141125
{
142126
files: ["*.md/**", "**/*.md/**"],
127+
languageOptions: {
128+
sourceType: "module",
129+
},
143130
rules: {
144131
"n/no-missing-import": "off",
145132
},

lib/index.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import flatRecommendedWithJson5 from "./configs/flat/recommended-with-json5";
1414
import flatPrettier from "./configs/flat/prettier";
1515
import flatAll from "./configs/flat/all";
1616
import * as meta from "./meta";
17+
import { compatMomoaCreate } from "./utils/compat-momoa";
1718

18-
// backward compatibility
1919
import {
2020
parseForESLint,
2121
parseJSON,
@@ -52,9 +52,9 @@ export default {
5252
meta,
5353
configs,
5454
rules,
55-
// as parser
55+
56+
// backward compatibility
5657
parseForESLint,
57-
// tools
5858
parseJSON,
5959
traverseNodes,
6060
getStaticJSONValue,
@@ -63,12 +63,14 @@ export {
6363
meta,
6464
configs,
6565
rules,
66-
// as parser
67-
parseForESLint,
6866
// tools
67+
compatMomoaCreate,
68+
// types
69+
AST,
70+
71+
// backward compatibility
72+
parseForESLint,
6973
parseJSON,
7074
traverseNodes,
7175
getStaticJSONValue,
72-
// types
73-
AST,
7476
};

lib/rules/array-bracket-newline.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
33
import type { AST } from "jsonc-eslint-parser";
44
import { createRule } from "../utils";
5-
import { getSourceCode } from "eslint-compat-utils";
65
import { isTokenOnSameLine } from "../utils/eslint-ast-utils";
76
import type { Token } from "../types";
87
import { isCommentToken } from "@eslint-community/eslint-utils";
@@ -57,7 +56,7 @@ export default createRule("array-bracket-newline", {
5756
},
5857
},
5958
create(context) {
60-
const sourceCode = getSourceCode(context);
59+
const sourceCode = context.sourceCode;
6160
if (!sourceCode.parserServices.isJSON) {
6261
return {};
6362
}

lib/rules/array-bracket-spacing.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
33
import type { AST } from "jsonc-eslint-parser";
44
import { createRule } from "../utils";
5-
import { getSourceCode } from "eslint-compat-utils";
65
import { isTokenOnSameLine } from "../utils/eslint-ast-utils";
76
import type { Token } from "../types";
87

@@ -49,7 +48,7 @@ export default createRule("array-bracket-spacing", {
4948
},
5049
},
5150
create(context) {
52-
const sourceCode = getSourceCode(context);
51+
const sourceCode = context.sourceCode;
5352
if (!sourceCode.parserServices.isJSON) {
5453
return {};
5554
}

lib/rules/array-element-newline.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
33
import type { AST } from "jsonc-eslint-parser";
44
import { createRule } from "../utils";
5-
import { getSourceCode } from "eslint-compat-utils";
65
import type { Token } from "../types";
76
import { isCommaToken, isCommentToken } from "@eslint-community/eslint-utils";
87
import { isTokenOnSameLine } from "../utils/eslint-ast-utils";
@@ -83,7 +82,7 @@ export default createRule("array-element-newline", {
8382
},
8483
},
8584
create(context) {
86-
const sourceCode = getSourceCode(context);
85+
const sourceCode = context.sourceCode;
8786
if (!sourceCode.parserServices.isJSON) {
8887
return {};
8988
}

lib/rules/auto.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { getCwd, getFilename, getSourceCode } from "eslint-compat-utils";
2-
import type { RuleListener, RuleModule } from "../types";
1+
import type { BaseRuleListener, RuleModule } from "../types";
32
import { createRule } from "../utils";
43
import { getAutoConfig } from "../utils/get-auto-jsonc-rules-config";
54

@@ -18,13 +17,12 @@ export default createRule("auto", {
1817
type: "suggestion",
1918
},
2019
create(context, params) {
21-
const sourceCode = getSourceCode(context);
22-
if (!sourceCode.parserServices.isJSON) {
20+
if (!context.sourceCode.parserServices.isJSON) {
2321
return {};
2422
}
25-
const autoConfig = getAutoConfig(getCwd(context), getFilename(context));
23+
const autoConfig = getAutoConfig(context.cwd, context.filename);
2624

27-
const visitor: RuleListener = {};
25+
const visitor: BaseRuleListener = {};
2826
for (const ruleId of Object.keys(autoConfig)) {
2927
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special rule
3028
const rule: RuleModule = require(
@@ -45,20 +43,25 @@ export default createRule("auto", {
4543
context.report(options);
4644
},
4745
};
48-
const ruleVisitor = rule.jsoncDefineRule.create(subContext, params);
49-
for (const key of Object.keys(ruleVisitor)) {
46+
const ruleVisitor: BaseRuleListener = rule.jsoncDefineRule.create(
47+
subContext,
48+
params,
49+
);
50+
for (const key of Object.keys(
51+
ruleVisitor,
52+
) as (keyof BaseRuleListener)[]) {
5053
const newVisit = ruleVisitor[key];
5154
const oldVisit = visitor[key];
5255
if (!newVisit) {
5356
continue;
5457
}
5558
if (!oldVisit) {
56-
visitor[key] = ruleVisitor[key];
59+
visitor[key] = ruleVisitor[key] as any;
5760
} else {
58-
visitor[key] = (...args: [never]) => {
61+
visitor[key] = ((...args: [never]) => {
5962
oldVisit(...args);
6063
newVisit(...args);
61-
};
64+
}) as any;
6265
}
6366
}
6467
}

lib/rules/comma-dangle.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
33
import type { AST } from "jsonc-eslint-parser";
44
import { createRule } from "../utils";
5-
import { getSourceCode } from "eslint-compat-utils";
65
import { isCommaToken } from "@eslint-community/eslint-utils";
76
import { getNextLocation } from "../utils/eslint-ast-utils";
87

@@ -96,7 +95,7 @@ export default createRule("comma-dangle", {
9695
},
9796
},
9897
create(context) {
99-
const sourceCode = getSourceCode(context);
98+
const sourceCode = context.sourceCode;
10099
if (!sourceCode.parserServices.isJSON) {
101100
return {};
102101
}

0 commit comments

Comments
 (0)