Home

Awesome

Introduction

eslint-plugin-jsonc is ESLint plugin for JSON, JSONC and JSON5 files.

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status Coverage Status

:name_badge: Features

This ESLint plugin provides linting rules relate to better ways to help you avoid problems when using JSON, JSONC and JSON5.

You can check on the Online DEMO.

:question: Why is it ESLint plugin?

ESLint is a great linter for JavaScript.
Since JSON is a subset of JavaScript, the same parser and rules can be applied to JSON.
Also, JSONC and JSON5, which are variants of JSON, are more similar to JavaScript than JSON. Applying a JavaScript linter to JSON is more rational than creating a JSON-specific linter.

How does eslint-plugin-jsonc work?

This plugin parses .json with its own parser, but this parser just converts AST parsed by acorn (It is used internally by the ESLint standard parser) into AST with another name. However, ASTs that do not exist in JSON and the superset of JSON syntaxes are reported as parsing errors. By converting the AST to another name, we prevent false positives from ESLint core rules.
Moreover, You can do the same linting using the extended rules of the ESLint core rules provided by this plugin.

The parser package used by this plugin is jsonc-eslint-parser.

:question: How is it different from other JSON plugins?

Plugins that do not use AST

e.g. eslint-plugin-json

These plugins use the processor to parse and return the results independently, without providing the ESLint engine with AST and source code text.

Plugins don't provide AST, so you can't use directive comments (e.g. /* eslint-disable */).
Plugins don't provide source code text, so you can't use it with plugins and rules that use text (e.g. eslint-plugin-prettier, eol-last).
Also, most plugins don't support JSON5.

eslint-plugin-jsonc works by providing AST and source code text to ESLint.

Plugins that use the same AST as JavaScript

e.g. eslint-plugin-json-files, eslint-plugin-json-es

These plugins use the same AST as JavaScript for linting.

Since the plugin uses the same AST as JavaScript, it may not report syntax that is not available in JSON (e.g. 1 + 1, (42)). Also, ESLint core rules and other plugin rules can false positives (e.g. quote-props rule reports quote on keys), which can complicate the your configuration.

The AST used by eslint-plugin-jsonc is similar to JavaScript AST, but with a different node name. This will prevent false positives. This means that it can be easily used in combination with other plugins.

<!--DOCS_IGNORE_START-->

:book: Documentation

See documents.

:cd: Installation

npm install --save-dev eslint eslint-plugin-jsonc

Requirements

<!--DOCS_IGNORE_END-->

:book: Usage

<!--USAGE_SECTION_START--> <!--USAGE_GUIDE_START-->

Configuration

New Config (eslint.config.js)

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

Example eslint.config.js:

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:

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 to get the rules that this plugin provides.

Legacy Config (.eslintrc)

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

Example .eslintrc.js:

module.exports = {
  extends: [
    // add more generic rulesets here, such as:
    // 'eslint:recommended',
    "plugin:jsonc/recommended-with-jsonc",
  ],
  rules: {
    // override/add rules settings here, such as:
    // 'jsonc/rule-name': 'error'
  },
};

This plugin provides configs:

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 to get the rules that this plugin provides.

Parser Configuration

If you have already specified a parser in your .eslintrc, you will also need to manually configure the parser for JSON files (your parser config takes priority over that defined by extends shared configs).

For example, if you are using the "@babel/eslint-parser", configure it as follows:

module.exports = {
  // ...
  extends: ["plugin:jsonc/recommended-with-jsonc"],
  // ...
  parser: "@babel/eslint-parser",
  // Add an `overrides` section to add a parser configuration for json.
  overrides: [
    {
      files: ["*.json", "*.json5", "*.jsonc"],
      parser: "jsonc-eslint-parser",
    },
  ],
  // ...
};

Experimental support for @eslint/json

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

Configure it as follows:

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.

:computer: Editor Integrations

Visual Studio Code

Use the dbaeumer.vscode-eslint extension that Microsoft provides officially.

You have to configure the eslint.validate option of the extension to check .json files, because the extension targets only *.js or *.jsx files by default.

Example .vscode/settings.json:

{
  "eslint.validate": ["javascript", "javascriptreact", "json", "jsonc", "json5"]
}
<!--USAGE_GUIDE_END--> <!--USAGE_SECTION_END-->

:white_check_mark: Rules

<!--RULES_SECTION_START-->

The --fix option on the command line automatically fixes problems reported by rules which have a wrench :wrench: below.
The rules with the following star :star: are included in the config.

<!--RULES_TABLE_START-->

JSONC Rules

Rule IDDescriptionFixableJSONJSONCJSON5
jsonc/autoapply jsonc rules similar to your configured ESLint core rules:wrench:
jsonc/key-name-casingenforce naming convention to property key names
jsonc/no-bigint-literalsdisallow BigInt literals:star::star::star:
jsonc/no-binary-expressiondisallow binary expression:wrench::star::star::star:
jsonc/no-binary-numeric-literalsdisallow binary numeric literals:wrench::star::star::star:
jsonc/no-commentsdisallow comments:star:
jsonc/no-escape-sequence-in-identifierdisallow escape sequences in identifiers.:wrench::star::star::star:
jsonc/no-hexadecimal-numeric-literalsdisallow hexadecimal numeric literals:wrench::star::star:
jsonc/no-infinitydisallow Infinity:star::star:
jsonc/no-nandisallow NaN:star::star:
jsonc/no-number-propsdisallow number property keys:wrench::star::star::star:
jsonc/no-numeric-separatorsdisallow numeric separators:wrench::star::star::star:
jsonc/no-octal-numeric-literalsdisallow octal numeric literals:wrench::star::star::star:
jsonc/no-parenthesizeddisallow parentheses around the expression:wrench::star::star::star:
jsonc/no-plus-signdisallow plus sign:wrench::star::star:
jsonc/no-regexp-literalsdisallow RegExp literals:star::star::star:
jsonc/no-template-literalsdisallow template literals:wrench::star::star::star:
jsonc/no-undefined-valuedisallow undefined:star::star::star:
jsonc/no-unicode-codepoint-escapesdisallow Unicode code point escape sequences.:wrench::star::star::star:
jsonc/sort-array-valuesrequire array values to be sorted:wrench:
jsonc/sort-keysrequire object keys to be sorted:wrench:
jsonc/valid-json-numberdisallow invalid number for JSON:wrench::star::star:
jsonc/vue-custom-block/no-parsing-errordisallow parsing errors in Vue custom blocks:star::star::star:

Extension Rules

Rule IDDescriptionFixableJSONJSONCJSON5
jsonc/array-bracket-newlineenforce line breaks after opening and before closing array brackets:wrench:
jsonc/array-bracket-spacingdisallow or enforce spaces inside of brackets:wrench:
jsonc/array-element-newlineenforce line breaks between array elements:wrench:
jsonc/comma-danglerequire or disallow trailing commas:wrench::star:
jsonc/comma-styleenforce consistent comma style:wrench:
jsonc/indentenforce consistent indentation:wrench:
jsonc/key-spacingenforce consistent spacing between keys and values in object literal properties:wrench:
jsonc/no-dupe-keysdisallow duplicate keys in object literals:star::star::star:
jsonc/no-floating-decimaldisallow leading or trailing decimal points in numeric literals:wrench::star::star:
jsonc/no-irregular-whitespacedisallow irregular whitespace
jsonc/no-multi-strdisallow multiline strings:star::star:
jsonc/no-octal-escapedisallow octal escape sequences in string literals
jsonc/no-octaldisallow legacy octal literals:star::star::star:
jsonc/no-sparse-arraysdisallow sparse arrays:star::star::star:
jsonc/no-useless-escapedisallow unnecessary escape usage:star::star::star:
jsonc/object-curly-newlineenforce consistent line breaks inside braces:wrench:
jsonc/object-curly-spacingenforce consistent spacing inside braces:wrench:
jsonc/object-property-newlineenforce placing object properties on separate lines:wrench:
jsonc/quote-propsrequire quotes around object literal property names:wrench::star::star:
jsonc/quotesenforce use of double or single quotes:wrench::star::star:
jsonc/space-unary-opsdisallow spaces after unary operators:wrench::star::star::star:
<!--RULES_TABLE_END--> <!--RULES_SECTION_END-->

:rocket: To Do More Verification

Verify using JSON Schema

You can verify using JSON Schema by checking and installing eslint-plugin-json-schema-validator.

Verify the Vue I18n message resource files

You can verify the message files by checking and installing @intlify/eslint-plugin-vue-i18n.

<!--DOCS_IGNORE_START-->

:traffic_light: Semantic Versioning Policy

eslint-plugin-jsonc follows Semantic Versioning and ESLint's Semantic Versioning Policy.

:beers: Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

Development Tools

<!--DOCS_IGNORE_END-->

:couple: Related Packages

:lock: License

See the LICENSE file for license rights and limitations (MIT).