Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ESLint plugin for JSON(C|5)? files

License

NotificationsYou must be signed in to change notification settings

ota-meshi/eslint-plugin-jsonc

Repository files navigation

eslint-plugin-jsonc is ESLint plugin forJSON,JSONC andJSON5 files.

NPM licenseNPM versionNPM downloadsNPM downloadsNPM downloadsNPM downloadsNPM downloadsBuild StatusCoverage Status

📛 Features

This ESLint plugin provides linting rules relate to better ways to help you avoid problems when usingJSON,JSONC andJSON5.

  • You can use ESLint to lintJSON.
  • 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 usingJSON,JSONC orJSON5.
  • SupportsVue SFC custom blocks such as<i18n>.
    Requirementsvue-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 theOnline DEMO.

❓ Why is it ESLint plugin?

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

How doeseslint-plugin-jsonc work?

This plugin parses.json with its own parser, but this parser just converts AST parsed byacorn (It is used internally by the ESLint standard parser) into AST with another name. However, ASTs that do not exist inJSON 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 isjsonc-eslint-parser.

❓ How is it different from other JSON plugins?

They work similarly, but@eslint/json is an ESLint JSON language plugin, buteslint-plugin-jsonc is an ESLint plugin.

  • eslint-plugin-jsonc was created 4 years before@eslint/json and it has more rules than@eslint/json.
  • The parser used byeslint-plugin-jsonc accepts more non-standard JSON syntax than@eslint/json.eslint-plugin-jsonc has rules that can auto-fixed these non-standard syntax to standard syntax.
  • eslint-plugin-jsonc can also be used together with@eslint/json.

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 byeslint-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

Seedocuments.

💿 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

New Config (eslint.config.js)

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

Exampleeslint.config.js:

importeslintPluginJsoncfrom'eslint-plugin-jsonc';exportdefault[// 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 withPrettier.
  • *.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).

Seethe rule list to get therules 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:

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

Seethe rule list to get therules 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 byextends 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:

importjsonfrom"@eslint/json";importjsoncfrom'eslint-plugin-jsonc';exportdefault[{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.

💻 Editor Integrations

Visual Studio Code

Use thedbaeumer.vscode-eslint extension that Microsoft provides officially.

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

Extension Rules

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

🚀 To Do More Verification

Verify using JSON Schema

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

Verify theVue 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 followsSemantic Versioning andESLint'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 theLICENSE file for license rights and limitations (MIT).

About

ESLint plugin for JSON(C|5)? files

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors15


[8]ページ先頭

©2009-2025 Movatter.jp