Skip to content

Commit

Permalink
Experimental support for @eslint/json (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Nov 8, 2024
1 parent ce9d5b1 commit 5b1e9ac
Show file tree
Hide file tree
Showing 94 changed files with 1,336 additions and 186 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-berries-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": minor
---

Experimental support for `@eslint/json`
6 changes: 3 additions & 3 deletions .github/workflows/NodeCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- name: Install Packages
run: npm install
- name: Test
run: npm test
run: npm run test:transpile-only
test-with-eslint6:
runs-on: ubuntu-latest
strategy:
Expand All @@ -100,7 +100,7 @@ jobs:
npx rimraf node_modules
npm install
- name: Test
run: npm test
run: npm run test:transpile-only
test-with-eslint7:
runs-on: ubuntu-latest
strategy:
Expand All @@ -118,7 +118,7 @@ jobs:
npx rimraf node_modules
npm install
- name: Test
run: npm test
run: npm run test:transpile-only
test-and-coverage:
runs-on: ubuntu-latest
steps:
Expand Down
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/do

Example **eslint.config.js**:

```mjs
```js
import eslintPluginJsonc from 'eslint-plugin-jsonc';
export default [
// add more generic rule sets here, such as:
Expand Down Expand Up @@ -179,6 +179,55 @@ module.exports = {
};
```

#### **Experimental** support for `@eslint/json`

We've launched experimental support for [`@eslint/json`].

Configure it as follows:

```js
import json from "@eslint/json";
import jsonc from 'eslint-plugin-jsonc';

export default [
{
plugins: {
json,
jsonc
},
},
{
files: ["**/*.json"],
language: "json/json",
rules: {
// 'json/rule-name': 'error',
// 'jsonc/rule-name': 'error'
},
},
{
files: ["**/*.jsonc", ".vscode/*.json"],
language: "json/jsonc",
rules: {
// 'json/rule-name': 'error',
// 'jsonc/rule-name': 'error'
},
},
{
files: ["**/*.json5"],
language: "json/json5",
rules: {
// 'json/rule-name': 'error',
// 'jsonc/rule-name': 'error'
},
},
];
```

However, we're not yet sure how best to make this work.
Please note that we may change behavior without notice.

[`@eslint/json`]: https://github.com/eslint/json

## :computer: Editor Integrations

### Visual Studio Code
Expand Down
3 changes: 1 addition & 2 deletions docs/.vitepress/shim/rules/auto.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getSourceCode } from "eslint-compat-utils";
// eslint-disable-next-line n/no-missing-import -- The file path used is the actual rule path
import { createRule } from "../utils";
export default createRule("auto", {
Expand All @@ -16,7 +15,7 @@ export default createRule("auto", {
type: "suggestion",
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.isJSON) {
return {};
}
Expand Down
51 changes: 50 additions & 1 deletion docs/user-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/do

Example **eslint.config.js**:

```mjs
```js
import eslintPluginJsonc from 'eslint-plugin-jsonc';
export default [
// add more generic rule sets here, such as:
Expand Down Expand Up @@ -108,6 +108,55 @@ module.exports = {
};
```

#### **Experimental** support for `@eslint/json`

We've launched experimental support for [`@eslint/json`].

Configure it as follows:

```js
import json from "@eslint/json";
import jsonc from 'eslint-plugin-jsonc';

export default [
{
plugins: {
json,
jsonc
},
},
{
files: ["**/*.json"],
language: "json/json",
rules: {
// 'json/rule-name': 'error',
// 'jsonc/rule-name': 'error'
},
},
{
files: ["**/*.jsonc", ".vscode/*.json"],
language: "json/jsonc",
rules: {
// 'json/rule-name': 'error',
// 'jsonc/rule-name': 'error'
},
},
{
files: ["**/*.json5"],
language: "json/json5",
rules: {
// 'json/rule-name': 'error',
// 'jsonc/rule-name': 'error'
},
},
];
```

However, we're not yet sure how best to make this work.
Please note that we may change behavior without notice.

[`@eslint/json`]: https://github.com/eslint/json

## :computer: Editor Integrations

### Visual Studio Code
Expand Down
23 changes: 5 additions & 18 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export default [
"@typescript-eslint/no-shadow": "off",
"one-var": "off",
"no-invalid-this": "off",
"new-cap": "off",
"func-style": "off",

"no-restricted-imports": [
"error",
Expand All @@ -84,36 +86,18 @@ export default [
message:
"Please use `eslint-compat-utils` module's `getSourceCode(context)` instead.",
},
{
object: "context",
property: "sourceCode",
message:
"Please use `eslint-compat-utils` module's `getSourceCode(context)` instead.",
},
{
object: "context",
property: "getFilename",
message:
"Please use `eslint-compat-utils` module's `getFilename(context)` instead.",
},
{
object: "context",
property: "filename",
message:
"Please use `eslint-compat-utils` module's `getFilename(context)` instead.",
},
{
object: "context",
property: "getCwd",
message:
"Please use `eslint-compat-utils` module's `getCwd(context)` instead.",
},
{
object: "context",
property: "cwd",
message:
"Please use `eslint-compat-utils` module's `getCwd(context)` instead.",
},
{
object: "context",
property: "getScope",
Expand All @@ -140,6 +124,9 @@ export default [
},
{
files: ["*.md/**", "**/*.md/**"],
languageOptions: {
sourceType: "module",
},
rules: {
"n/no-missing-import": "off",
},
Expand Down
16 changes: 9 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import flatRecommendedWithJson5 from "./configs/flat/recommended-with-json5";
import flatPrettier from "./configs/flat/prettier";
import flatAll from "./configs/flat/all";
import * as meta from "./meta";
import { compatMomoaCreate } from "./utils/compat-momoa";

// backward compatibility
import {
parseForESLint,
parseJSON,
Expand Down Expand Up @@ -52,9 +52,9 @@ export default {
meta,
configs,
rules,
// as parser

// backward compatibility
parseForESLint,
// tools
parseJSON,
traverseNodes,
getStaticJSONValue,
Expand All @@ -63,12 +63,14 @@ export {
meta,
configs,
rules,
// as parser
parseForESLint,
// tools
compatMomoaCreate,
// types
AST,

// backward compatibility
parseForESLint,
parseJSON,
traverseNodes,
getStaticJSONValue,
// types
AST,
};
3 changes: 1 addition & 2 deletions lib/rules/array-bracket-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
import type { AST } from "jsonc-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "eslint-compat-utils";
import { isTokenOnSameLine } from "../utils/eslint-ast-utils";
import type { Token } from "../types";
import { isCommentToken } from "@eslint-community/eslint-utils";
Expand Down Expand Up @@ -57,7 +56,7 @@ export default createRule("array-bracket-newline", {
},
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.isJSON) {
return {};
}
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/array-bracket-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
import type { AST } from "jsonc-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "eslint-compat-utils";
import { isTokenOnSameLine } from "../utils/eslint-ast-utils";
import type { Token } from "../types";

Expand Down Expand Up @@ -49,7 +48,7 @@ export default createRule("array-bracket-spacing", {
},
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.isJSON) {
return {};
}
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/array-element-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
import type { AST } from "jsonc-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "eslint-compat-utils";
import type { Token } from "../types";
import { isCommaToken, isCommentToken } from "@eslint-community/eslint-utils";
import { isTokenOnSameLine } from "../utils/eslint-ast-utils";
Expand Down Expand Up @@ -83,7 +82,7 @@ export default createRule("array-element-newline", {
},
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.isJSON) {
return {};
}
Expand Down
25 changes: 14 additions & 11 deletions lib/rules/auto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getCwd, getFilename, getSourceCode } from "eslint-compat-utils";
import type { RuleListener, RuleModule } from "../types";
import type { BaseRuleListener, RuleModule } from "../types";
import { createRule } from "../utils";
import { getAutoConfig } from "../utils/get-auto-jsonc-rules-config";

Expand All @@ -18,13 +17,12 @@ export default createRule("auto", {
type: "suggestion",
},
create(context, params) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.isJSON) {
if (!context.sourceCode.parserServices.isJSON) {
return {};
}
const autoConfig = getAutoConfig(getCwd(context), getFilename(context));
const autoConfig = getAutoConfig(context.cwd, context.filename);

const visitor: RuleListener = {};
const visitor: BaseRuleListener = {};
for (const ruleId of Object.keys(autoConfig)) {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special rule
const rule: RuleModule = require(
Expand All @@ -45,20 +43,25 @@ export default createRule("auto", {
context.report(options);
},
};
const ruleVisitor = rule.jsoncDefineRule.create(subContext, params);
for (const key of Object.keys(ruleVisitor)) {
const ruleVisitor: BaseRuleListener = rule.jsoncDefineRule.create(
subContext,
params,
);
for (const key of Object.keys(
ruleVisitor,
) as (keyof BaseRuleListener)[]) {
const newVisit = ruleVisitor[key];
const oldVisit = visitor[key];
if (!newVisit) {
continue;
}
if (!oldVisit) {
visitor[key] = ruleVisitor[key];
visitor[key] = ruleVisitor[key] as any;
} else {
visitor[key] = (...args: [never]) => {
visitor[key] = ((...args: [never]) => {
oldVisit(...args);
newVisit(...args);
};
}) as any;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/comma-dangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// MIT License. Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
import type { AST } from "jsonc-eslint-parser";
import { createRule } from "../utils";
import { getSourceCode } from "eslint-compat-utils";
import { isCommaToken } from "@eslint-community/eslint-utils";
import { getNextLocation } from "../utils/eslint-ast-utils";

Expand Down Expand Up @@ -96,7 +95,7 @@ export default createRule("comma-dangle", {
},
},
create(context) {
const sourceCode = getSourceCode(context);
const sourceCode = context.sourceCode;
if (!sourceCode.parserServices.isJSON) {
return {};
}
Expand Down
Loading

0 comments on commit 5b1e9ac

Please sign in to comment.