• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ESLint plugin for JSON(C|5)? files

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

πŸ“› Features

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

  • You can use ESLint to lint JSON.
  • You can apply rules similar to the rules you use for JavaScript to JSON using the "jsonc/auto" rule provided by this plugin.
  • You can choose the appropriate config provided by this plugin depending on whether you are using JSON, JSONC or JSON5.
  • Supports Vue SFC custom blocks such as <i18n>.
    Requirements vue-eslint-parser v7.3.0 and above.
  • Supports ESLint directives. e.g. // eslint-disable-next-line
  • You can check your code in real-time using the ESLint editor integrations.

You can check on the Online DEMO.

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

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

πŸ“– Documentation

See documents.

πŸ’Ώ Installation

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

Requirements

  • ESLint v6.0.0 and above
  • Node.js v12.22.x, v14.17.x, v16.x and above

πŸ“– Usage

Configuration

Use .eslintrc.* file to configure rules. See also: https://eslint.org/docs/user-guide/configuring.

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:

  • plugin:jsonc/base ... Configuration to enable correct JSON parsing.
  • plugin:jsonc/recommended-with-json ... Recommended configuration for JSON.
  • plugin:jsonc/recommended-with-jsonc ... Recommended configuration for JSONC.
  • plugin:jsonc/recommended-with-json5 ... Recommended configuration for JSON5.
  • plugin:jsonc/prettier ... Turn off rules that may conflict with Prettier.
  • plugin:jsonc/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 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",
    },
  ],
  // ...
};

πŸ’» 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"]
}

βœ… Rules

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

JSONC Rules

Rule ID Description Fixable JSON JSONC JSON5
jsonc/auto apply jsonc rules similar to your configured ESLint core rules πŸ”§
jsonc/key-name-casing enforce naming convention to property key names
jsonc/no-bigint-literals disallow BigInt literals ⭐ ⭐ ⭐
jsonc/no-binary-expression disallow binary expression πŸ”§ ⭐ ⭐ ⭐
jsonc/no-binary-numeric-literals disallow binary numeric literals πŸ”§ ⭐ ⭐ ⭐
jsonc/no-comments disallow comments ⭐
jsonc/no-escape-sequence-in-identifier disallow escape sequences in identifiers. πŸ”§ ⭐ ⭐ ⭐
jsonc/no-hexadecimal-numeric-literals disallow hexadecimal numeric literals πŸ”§ ⭐ ⭐
jsonc/no-infinity disallow Infinity ⭐ ⭐
jsonc/no-nan disallow NaN ⭐ ⭐
jsonc/no-number-props disallow number property keys πŸ”§ ⭐ ⭐ ⭐
jsonc/no-numeric-separators disallow numeric separators πŸ”§ ⭐ ⭐ ⭐
jsonc/no-octal-numeric-literals disallow octal numeric literals πŸ”§ ⭐ ⭐ ⭐
jsonc/no-parenthesized disallow parentheses around the expression πŸ”§ ⭐ ⭐ ⭐
jsonc/no-plus-sign disallow plus sign πŸ”§ ⭐ ⭐
jsonc/no-regexp-literals disallow RegExp literals ⭐ ⭐ ⭐
jsonc/no-template-literals disallow template literals πŸ”§ ⭐ ⭐ ⭐
jsonc/no-undefined-value disallow undefined ⭐ ⭐ ⭐
jsonc/no-unicode-codepoint-escapes disallow Unicode code point escape sequences. πŸ”§ ⭐ ⭐ ⭐
jsonc/sort-array-values require array values to be sorted πŸ”§
jsonc/sort-keys require object keys to be sorted πŸ”§
jsonc/valid-json-number disallow invalid number for JSON πŸ”§ ⭐ ⭐
jsonc/vue-custom-block/no-parsing-error disallow parsing errors in Vue custom blocks ⭐ ⭐ ⭐

Extension Rules

Rule ID Description Fixable JSON JSONC JSON5
jsonc/array-bracket-newline enforce line breaks after opening and before closing array brackets πŸ”§
jsonc/array-bracket-spacing disallow or enforce spaces inside of brackets πŸ”§
jsonc/array-element-newline enforce line breaks between array elements πŸ”§
jsonc/comma-dangle require or disallow trailing commas πŸ”§ ⭐
jsonc/comma-style enforce consistent comma style πŸ”§
jsonc/indent enforce consistent indentation πŸ”§
jsonc/key-spacing enforce consistent spacing between keys and values in object literal properties πŸ”§
jsonc/no-dupe-keys disallow duplicate keys in object literals ⭐ ⭐ ⭐
jsonc/no-floating-decimal disallow leading or trailing decimal points in numeric literals πŸ”§ ⭐ ⭐
jsonc/no-irregular-whitespace disallow irregular whitespace
jsonc/no-multi-str disallow multiline strings ⭐ ⭐
jsonc/no-octal-escape disallow octal escape sequences in string literals
jsonc/no-octal disallow legacy octal literals ⭐ ⭐ ⭐
jsonc/no-sparse-arrays disallow sparse arrays ⭐ ⭐ ⭐
jsonc/no-useless-escape disallow unnecessary escape usage ⭐ ⭐ ⭐
jsonc/object-curly-newline enforce consistent line breaks inside braces πŸ”§
jsonc/object-curly-spacing enforce consistent spacing inside braces πŸ”§
jsonc/object-property-newline enforce placing object properties on separate lines πŸ”§
jsonc/quote-props require quotes around object literal property names πŸ”§ ⭐ ⭐
jsonc/quotes enforce use of double or single quotes πŸ”§ ⭐ ⭐
jsonc/space-unary-ops disallow spaces after unary operators πŸ”§ ⭐ ⭐ ⭐

πŸš€ 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.

πŸš₯ Semantic Versioning Policy

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

🍻 Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

Development Tools

  • npm test runs tests and measures coverage.
  • npm run update runs in order to update readme and recommended configuration.

πŸ‘« Related Packages

πŸ”’ License

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

More Repositories

1

eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.
TypeScript
223
star
2

eslint-plugin-astro

ESLint plugin for Astro component
TypeScript
117
star
3

stylelint-config-recommended-vue

The recommended shareable Vue config for Stylelint.
JavaScript
72
star
4

eslint-plugin-es-x

ESLint plugin about ECMAScript syntactic features.
JavaScript
64
star
5

eslint-plugin-json-schema-validator

ESLint plugin that validates data using JSON Schema Validator.
TypeScript
64
star
6

eslint-plugin-yml

This ESLint plugin provides linting rules for YAML.
TypeScript
64
star
7

jsonc-eslint-parser

JSON, JSONC and JSON5 parser for use with ESLint plugins.
TypeScript
54
star
8

astro-eslint-parser

Astro component parser for ESLint
TypeScript
43
star
9

vuepress-plugin-full-text-search2

VuePress v2 plugin that adds full-text search box.
TypeScript
31
star
10

stylelint-config-html

The shareable HTML config for Stylelint.
JavaScript
29
star
11

stylelint-plugin-stylus

stylelint plugin for Stylus
JavaScript
24
star
12

eslint-plugin-css

An ESLint plugin that provides rules to verify CSS definition objects.
TypeScript
19
star
13

eslint-plugin-lodash-template

ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
JavaScript
16
star
14

eslint-plugin-toml

This ESLint plugin provides linting rules for TOML.
TypeScript
15
star
15

stylelint-config-standard-vue

The standard shareable Vue config for Stylelint.
JavaScript
15
star
16

postcss-styl

PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
JavaScript
15
star
17

postcss-html

PostCSS syntax for parsing HTML (and HTML-like)
JavaScript
14
star
18

typescript-eslint-parser-for-extra-files

An experimental ESLint custom parser for Vue, Svelte, and Astro for use with TypeScript. It provides type information in combination with each framework's ESLint custom parser.
TypeScript
13
star
19

eslint-plugin-node-dependencies

ESLint plugin to check Node.js dependencies.
TypeScript
12
star
20

vite-plugin-eslint4b

Vite plugin for running ESLint on browser
TypeScript
9
star
21

vite-plugin-svelte-md

Markdown with Svelte for Vite
TypeScript
9
star
22

toml-eslint-parser

A TOML parser that produces output compatible with ESLint
TypeScript
9
star
23

estree-halstead

Halstead complexity measures for ESTree
TypeScript
8
star
24

eslint-plugin-vue-demo

eslint-plugin-vue Online Playground
Vue
5
star
25

eslint-plugin

ESLint configuration plugin for me.
JavaScript
4
star
26

chromeextension_selectbox_filter

JavaScript
4
star
27

eslint-online-playground

ESLint Online Playground
TypeScript
3
star
28

shine-and-bright

Dark and Light Theme Switch CSS
CSS
3
star
29

mrpm

npm package
JavaScript
3
star
30

data-table-performance-test

Grid/Data Table/Spreadsheet Components Performance Test
JavaScript
2
star
31

yaml-eslint-parser

A YAML parser that produces output compatible with ESLint
TypeScript
2
star
32

eslint-plugin-promise-playground

eslint-plugin-promise Online Playground
Svelte
2
star
33

stylelint4b

stylelint which works in browsers
JavaScript
1
star
34

spring-webflux-x-cheetah-grid-example

Vue
1
star
35

vue-fes-japan-online-2022-slide

Vue Fes Japan Online 2022 発葨スラむド
TypeScript
1
star
36

image-matcher

JavaScript
1
star
37

eslint-config-vue-preset

Provides various shareable configs for `eslint-plugin-vue`.
JavaScript
1
star
38

postcss-calc-ast-parser

Parse the `calc()` function defined in CSS and convert it to AST.
TypeScript
1
star
39

stylelint-demo-poc

TypeScript
1
star
40

vue_components_example_materialize

Vue.js v2γ‚’δ½Ώγ£γŸγ‚³γƒ³γƒγƒΌγƒγƒ³γƒˆδ½œζˆγ‚΅γƒ³γƒ—γƒ«οΌˆMaterializeεˆ©η”¨οΌ‰
JavaScript
1
star