🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis
Socket
Book a DemoInstallSign in
Socket

eslint-plugin-jsonc

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-jsonc

ESLint plugin for JSON, JSONC and JSON5 files.

Source
npmnpm
Version
0.7.0
Version published
Weekly downloads
1.2M
0.6%
Maintainers
1
Weekly downloads
 
Created
Source

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 apply rules similar to the rules you use for JavaScript to JSON using the shareable config "plugin:jsonc/auto-config" provided by this plugin.
  • You can choose the appropriate config provided by this plugin depending on whether you are using JSON, JSONC or 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 espree (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.

:book: Documentation

See documents.

:cd: Installation

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

Requirements

  • ESLint v6.0.0 and above
  • Node.js v8.10.0 and above

:book: 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/auto-config ... Automatically apply jsonc rules similar to your configured ESLint core rules to JSON.

See the rule list to get the rules that this plugin provides.

Running ESLint from the command line

If you want to run eslint from the command line, make sure you include the .json extension using the --ext option or a glob pattern, because ESLint targets only .js files by default.

Examples:

eslint --ext .js,.json src
eslint "src/**/*.{js,json}"

: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"
  ]
}

:white_check_mark: Rules

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.

JSONC Rules

Rule IDDescriptionFixableJSONJSONCJSON5
jsonc/no-bigint-literalsdisallow BigInt literals:star::star::star:
jsonc/no-commentsdisallow comments:star:
jsonc/no-number-propsdisallow number property keys:wrench::star::star::star:
jsonc/no-numeric-separatorsdisallow numeric separators:wrench::star::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/valid-json-numberdisallow invalid number for JSON:wrench::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-multi-strdisallow multiline strings:star::star:
jsonc/no-octal-escapedisallow octal escape sequences in string literals
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/sort-keysrequire object keys to be sorted:wrench:
jsonc/space-unary-opsdisallow spaces after unary operators:wrench::star::star::star:

:beers: 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.

:lock: License

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

Keywords

eslint

FAQs

Package last updated on 18 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts