jsonc-eslint-parser
JSON, JSONC and JSON5 parser for use with ESLint plugins
Last updated 3 years ago by ota-meshi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jsonc-eslint-parser 
SYNC missed versions from official npm registry.

jsonc-eslint-parser

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

:name_badge: Introduction

JSON, JSONC and JSON5 parser for use with ESLint plugins.

This parser allows us to lint JSON, JSONC and JSON5 files. This parser and the rules of eslint-plugin-jsonc would catch some of the mistakes and code style violations.

See eslint-plugin-jsonc for details.

:cd: Installation

npm i --save-dev jsonc-eslint-parser

:book: Usage (Flat Config)

In your ESLint configuration file, set the parser property:

import * as jsoncParser from "jsonc-eslint-parser";

export default [
  {
    // ...
    // Add the following settings.
    files: ["**/*.json", "**/*.json5"], // Specify the extension or pattern you want to parse as JSON.
    languageOptions: {
      parser: jsoncParser, // Set this parser.
    }
  },
];

:book: Usage (Legacy Config)

In your ESLint configuration file, set the overrides > parser property:

{
  // ...
  // Add the following settings.
  "overrides": [
    {
      "files": ["*.json", "*.json5"], // Specify the extension or pattern you want to parse as JSON.
      "parser": "jsonc-eslint-parser", // Set this parser.
    },
  ],
}

:gear: Configuration

The following additional configuration options are available by specifying them in parserOptions in your ESLint configuration file.

{
  // Additional configuration options
  "parserOptions": {
    "jsonSyntax": "JSON5"
  }
}

parserOptions.jsonSyntax

Set to "JSON", "JSONC" or "JSON5". Select the JSON syntax you are using.
If not specified, all syntaxes that express static values ​​are accepted. For example, template literals without interpolation.

Note : Recommended to loosen the syntax checking by the parser and use check rules of eslint-plugin-jsonc to automatically fix it.

:gear: API

parseJSON(code, options?)

Parses the given JSON source code and returns the AST.

import { parseJSON } from "jsonc-eslint-parser";

const ast = parseJSON('{"key": "value"}', { jsonSyntax: "JSON" });
console.log(ast);

Parameters:

  • code (string): The JSON source code to parse.
  • options (object, optional): Parser options.
    • jsonSyntax ("JSON" | "JSONC" | "JSON5"): The JSON syntax to use.

Returns: JSONProgram - The root AST node.

parseForESLint(code, options?)

Parses the given JSON source code for ESLint. This is the main parser function used by ESLint.

import { parseForESLint } from "jsonc-eslint-parser";

const result = parseForESLint('{"key": "value"}', { jsonSyntax: "JSON" });
console.log(result.ast);
console.log(result.services);
console.log(result.visitorKeys);

Parameters:

  • code (string): The JSON source code to parse.
  • options (object, optional): Parser options (same as parseJSON).

Returns: An object containing:

  • ast: The root AST node.
  • services: An object with helper methods like getStaticJSONValue().
  • visitorKeys: Visitor keys for traversing the AST.

tokenize(code, options?)

Tokenizes the given JSON source code and returns an array of tokens.

import { tokenize } from "jsonc-eslint-parser";

const tokens = tokenize('{"key": "value"}', { jsonSyntax: "JSON" });
console.log(tokens);
// [
//   { type: 'Punctuator', value: '{', range: [0, 1], loc: {...} },
//   { type: 'String', value: '"key"', range: [1, 6], loc: {...} },
//   { type: 'Punctuator', value: ':', range: [6, 7], loc: {...} },
//   { type: 'String', value: '"value"', range: [8, 15], loc: {...} },
//   { type: 'Punctuator', value: '}', range: [15, 16], loc: {...} }
// ]

// Include comments in the result
const tokensWithComments = tokenize('{"key": "value" /* comment */}', {
  jsonSyntax: "JSONC",
  includeComments: true
});

Parameters:

  • code (string): The JSON source code to tokenize.
  • options (object, optional): Parser options.
    • jsonSyntax ("JSON" | "JSONC" | "JSON5"): The JSON syntax to use.
    • includeComments (boolean): If true, comments are included in the result array.

Returns: Token[] or (Token | Comment)[] - An array of tokens, optionally including comments.

Usage for Custom Rules / Plugins

:traffic_light: Semantic Versioning Policy

jsonc-eslint-parser follows Semantic Versioning.

:couple: Related Packages

:lock: License

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

Current Tags

  • 2.3.0                                ...           latest (3 years ago)

22 Versions

  • 2.3.0                                ...           3 years ago
  • 2.2.0                                ...           3 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.4                                ...           4 years ago
  • 2.0.3                                ...           4 years ago
  • 2.0.2                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.4.1                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.1                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.6.2                                ...           5 years ago
  • 0.6.1                                ...           5 years ago
  • 0.6.0                                ...           6 years ago
  • 0.5.2                                ...           6 years ago
  • 0.5.1                                ...           6 years ago
  • 0.5.0                                ...           6 years ago

Copyright 2013 - present © cnpmjs.org | Home |