Skip to content

Commit

Permalink
Merge ba76591 into 531fddb
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Mar 18, 2024
2 parents 531fddb + ba76591 commit 6baf671
Show file tree
Hide file tree
Showing 20 changed files with 439 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-countries-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": minor
---

Add support for flat config
6 changes: 3 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": ["dbaeumer.vscode-eslint"],
},
},
"extensions": ["dbaeumer.vscode-eslint"]
}
}

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
Expand Down
9 changes: 9 additions & 0 deletions .eslintrc.for-vscode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
extends: [require.resolve("./.eslintrc.js")],
overrides: [
{
files: ["tests/lib/rules/*"],
extends: ["plugin:eslint-rule-tester/recommended-legacy"],
},
],
};
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ module.exports = {
project: "./tsconfig.json",
},
},
{
files: ["*.md/**", "**/*.md/**"],
rules: {
"n/no-missing-import": "off",
},
},
{
files: ["scripts/**/*.ts", "tests/**/*.ts", "tests-integrations/**/*.ts"],
rules: {
Expand All @@ -113,5 +119,16 @@ module.exports = {
"@typescript-eslint/no-misused-promises": "off",
},
},
{
files: ["docs/.vitepress/**/*.*"],
rules: {
"eslint-plugin/require-meta-docs-description": "off",
"eslint-plugin/require-meta-docs-url": "off",
"eslint-plugin/require-meta-type": "off",
"eslint-plugin/prefer-message-ids": "off",
"eslint-plugin/prefer-object-rule": "off",
"eslint-plugin/require-meta-schema": "off",
},
},
],
};
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"markdown",
"yaml"
],
"eslint.options": {
"overrideConfigFile": "./.eslintrc.for-vscode.js"
},
"typescript.validate.enable": true,
"javascript.validate.enable": false,
"vetur.validation.script": false,
Expand Down
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,43 @@ npm install --save-dev eslint eslint-plugin-jsonc

### Configuration

Use `.eslintrc.*` file to configure rules. See also: [https://eslint.org/docs/user-guide/configuring](https://eslint.org/docs/user-guide/configuring).
#### New (ESLint>=v9) Config (Flat Config)

Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.

Example **eslint.config.js**:

```mjs
import eslintPluginJsonc from 'eslint-plugin-jsonc';
export default [
// add more generic rule sets here, such as:
// js.configs.recommended,
...eslintPluginJsonc.configs['flat/recommended-with-jsonc'],
{
rules: {
// override/add rules settings here, such as:
// 'jsonc/rule-name': 'error'
}
}
];
```

This plugin provides configs:

- `*.configs['flat/base']` ... Configuration to enable correct JSON parsing.
- `*.configs['flat/recommended-with-json']` ... Recommended configuration for JSON.
- `*.configs['flat/recommended-with-jsonc']` ... Recommended configuration for JSONC.
- `*.configs['flat/recommended-with-json5']` ... Recommended configuration for JSON5.
- `*.configs['flat/prettier']` ... Turn off rules that may conflict with [Prettier](https://prettier.io/).
- `*.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.

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).

See [the rule list](https://ota-meshi.github.io/eslint-plugin-jsonc/rules/) to get the `rules` that this plugin provides.

#### Legacy Config (ESLint<v9)

Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/>.

Example **.eslintrc.js**:

Expand Down
38 changes: 37 additions & 1 deletion docs/user-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,43 @@ npm install --save-dev eslint eslint-plugin-jsonc

### Configuration

Use `.eslintrc.*` file to configure rules. See also: [https://eslint.org/docs/user-guide/configuring](https://eslint.org/docs/user-guide/configuring).
#### New (ESLint>=v9) Config (Flat Config)

Use `eslint.config.js` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/configuration-files-new>.

Example **eslint.config.js**:

```mjs
import eslintPluginJsonc from 'eslint-plugin-jsonc';
export default [
// add more generic rule sets here, such as:
// js.configs.recommended,
...eslintPluginJsonc.configs['flat/recommended-with-jsonc'],
{
rules: {
// override/add rules settings here, such as:
// 'jsonc/rule-name': 'error'
}
}
];
```

This plugin provides configs:

- `*.configs['flat/base']` ... Configuration to enable correct JSON parsing.
- `*.configs['flat/recommended-with-json']` ... Recommended configuration for JSON.
- `*.configs['flat/recommended-with-jsonc']` ... Recommended configuration for JSONC.
- `*.configs['flat/recommended-with-json5']` ... Recommended configuration for JSON5.
- `*.configs['flat/prettier']` ... Turn off rules that may conflict with [Prettier](https://prettier.io/).
- `*.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.

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).

See [the rule list](../rules/index.md) to get the `rules` that this plugin provides.

#### Legacy Config (ESLint<v9)

Use `.eslintrc.*` file to configure rules. See also: <https://eslint.org/docs/latest/use/configure/>.

Example **.eslintrc.js**:

Expand Down
16 changes: 16 additions & 0 deletions lib/configs/flat/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { rules } from "../../utils/rules";
import base from "./base";
const all: Record<string, string> = {};
for (const rule of rules) {
if (rule.meta.docs.ruleId === "jsonc/sort-array-values") continue;
all[rule.meta.docs.ruleId] = "error";
}

export default [
...base,
{
rules: {
...all,
},
},
];
29 changes: 29 additions & 0 deletions lib/configs/flat/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ESLint } from "eslint";
import * as parser from "jsonc-eslint-parser";
export default [
{
files: [
"*.json",
"**/*.json",
"*.json5",
"**/*.json5",
"*.jsonc",
"**/*.jsonc",
],
plugins: {
get jsonc(): ESLint.Plugin {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
return require("../../index");
},
},
languageOptions: {
parser,
},
rules: {
// ESLint core rules known to cause problems with JSON.
strict: "off",
"no-unused-expressions": "off",
"no-unused-vars": "off",
},
},
];
26 changes: 26 additions & 0 deletions lib/configs/flat/prettier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import base from "./base";
export default [
...base,
{
rules: {
// eslint-plugin-jsonc rules
"jsonc/array-bracket-newline": "off",
"jsonc/array-bracket-spacing": "off",
"jsonc/array-element-newline": "off",
"jsonc/comma-dangle": "off",
"jsonc/comma-style": "off",
"jsonc/indent": "off",
"jsonc/key-spacing": "off",
"jsonc/no-floating-decimal": "off",
"jsonc/object-curly-newline": "off",
"jsonc/object-curly-spacing": "off",
"jsonc/object-property-newline": "off",
"jsonc/quote-props": "off",
"jsonc/quotes": "off",
"jsonc/space-unary-ops": "off",
},
},
];
41 changes: 41 additions & 0 deletions lib/configs/flat/recommended-with-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import base from "./base";
export default [
...base,
{
rules: {
// eslint-plugin-jsonc rules
"jsonc/comma-dangle": "error",
"jsonc/no-bigint-literals": "error",
"jsonc/no-binary-expression": "error",
"jsonc/no-binary-numeric-literals": "error",
"jsonc/no-comments": "error",
"jsonc/no-dupe-keys": "error",
"jsonc/no-escape-sequence-in-identifier": "error",
"jsonc/no-floating-decimal": "error",
"jsonc/no-hexadecimal-numeric-literals": "error",
"jsonc/no-infinity": "error",
"jsonc/no-multi-str": "error",
"jsonc/no-nan": "error",
"jsonc/no-number-props": "error",
"jsonc/no-numeric-separators": "error",
"jsonc/no-octal-numeric-literals": "error",
"jsonc/no-octal": "error",
"jsonc/no-parenthesized": "error",
"jsonc/no-plus-sign": "error",
"jsonc/no-regexp-literals": "error",
"jsonc/no-sparse-arrays": "error",
"jsonc/no-template-literals": "error",
"jsonc/no-undefined-value": "error",
"jsonc/no-unicode-codepoint-escapes": "error",
"jsonc/no-useless-escape": "error",
"jsonc/quote-props": "error",
"jsonc/quotes": "error",
"jsonc/space-unary-ops": "error",
"jsonc/valid-json-number": "error",
"jsonc/vue-custom-block/no-parsing-error": "error",
},
},
];
30 changes: 30 additions & 0 deletions lib/configs/flat/recommended-with-json5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import base from "./base";
export default [
...base,
{
rules: {
// eslint-plugin-jsonc rules
"jsonc/no-bigint-literals": "error",
"jsonc/no-binary-expression": "error",
"jsonc/no-binary-numeric-literals": "error",
"jsonc/no-dupe-keys": "error",
"jsonc/no-escape-sequence-in-identifier": "error",
"jsonc/no-number-props": "error",
"jsonc/no-numeric-separators": "error",
"jsonc/no-octal-numeric-literals": "error",
"jsonc/no-octal": "error",
"jsonc/no-parenthesized": "error",
"jsonc/no-regexp-literals": "error",
"jsonc/no-sparse-arrays": "error",
"jsonc/no-template-literals": "error",
"jsonc/no-undefined-value": "error",
"jsonc/no-unicode-codepoint-escapes": "error",
"jsonc/no-useless-escape": "error",
"jsonc/space-unary-ops": "error",
"jsonc/vue-custom-block/no-parsing-error": "error",
},
},
];
39 changes: 39 additions & 0 deletions lib/configs/flat/recommended-with-jsonc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "npm run update"
import base from "./base";
export default [
...base,
{
rules: {
// eslint-plugin-jsonc rules
"jsonc/no-bigint-literals": "error",
"jsonc/no-binary-expression": "error",
"jsonc/no-binary-numeric-literals": "error",
"jsonc/no-dupe-keys": "error",
"jsonc/no-escape-sequence-in-identifier": "error",
"jsonc/no-floating-decimal": "error",
"jsonc/no-hexadecimal-numeric-literals": "error",
"jsonc/no-infinity": "error",
"jsonc/no-multi-str": "error",
"jsonc/no-nan": "error",
"jsonc/no-number-props": "error",
"jsonc/no-numeric-separators": "error",
"jsonc/no-octal-numeric-literals": "error",
"jsonc/no-octal": "error",
"jsonc/no-parenthesized": "error",
"jsonc/no-plus-sign": "error",
"jsonc/no-regexp-literals": "error",
"jsonc/no-sparse-arrays": "error",
"jsonc/no-template-literals": "error",
"jsonc/no-undefined-value": "error",
"jsonc/no-unicode-codepoint-escapes": "error",
"jsonc/no-useless-escape": "error",
"jsonc/quote-props": "error",
"jsonc/quotes": "error",
"jsonc/space-unary-ops": "error",
"jsonc/valid-json-number": "error",
"jsonc/vue-custom-block/no-parsing-error": "error",
},
},
];
12 changes: 12 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import recommendedWithJsonc from "./configs/recommended-with-jsonc";
import recommendedWithJson5 from "./configs/recommended-with-json5";
import prettier from "./configs/prettier";
import all from "./configs/all";
import flatBase from "./configs/base";
import flatRecommendedWithJson from "./configs/flat/recommended-with-json";
import flatRecommendedWithJsonc from "./configs/flat/recommended-with-jsonc";
import flatRecommendedWithJson5 from "./configs/flat/recommended-with-json5";
import flatPrettier from "./configs/flat/prettier";
import flatAll from "./configs/all";
import * as meta from "./meta";

// backward compatibility
Expand All @@ -26,6 +32,12 @@ const configs = {
"recommended-with-json5": recommendedWithJson5,
prettier,
all,
"flat/base": flatBase,
"flat/recommended-with-json": flatRecommendedWithJson,
"flat/recommended-with-jsonc": flatRecommendedWithJsonc,
"flat/recommended-with-json5": flatRecommendedWithJson5,
"flat/prettier": flatPrettier,
"flat/all": flatAll,
};

const rules = ruleList.reduce(
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"homepage": "https://ota-meshi.github.io/eslint-plugin-jsonc/",
"dependencies": {
"@eslint-community/eslint-utils": "^4.2.0",
"eslint-compat-utils": "^0.4.0",
"eslint-compat-utils": "^0.5.0",
"espree": "^9.6.1",
"graphemer": "^1.4.0",
"jsonc-eslint-parser": "^2.0.4",
Expand Down Expand Up @@ -99,6 +99,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-eslint-plugin": "^5.0.0",
"eslint-plugin-eslint-rule-tester": "^0.5.1",
"eslint-plugin-json-schema-validator": "^4.6.1",
"eslint-plugin-jsonc": "^2.0.0",
"eslint-plugin-markdown": "^3.0.0",
Expand Down
Loading

0 comments on commit 6baf671

Please sign in to comment.