ESLint JSON Compatibility Utilities

0.2.3 · active · verified Sun Apr 19

eslint-json-compat-utils is a specialized utility designed to bridge the gap between ESLint rules written for `jsonc-eslint-parser` and those compatible with `@eslint/json`. It provides conversion functions that adapt rule objects, plugin objects, and `create` functions, enabling developers to reuse existing logic across different JSON AST parsers in ESLint. The current stable version is 0.2.3, with releases occurring periodically to address bugs and add minor features, as seen in recent patch updates. Its key differentiator is its focused purpose on AST transformation for JSON linting rules, specifically to ensure compatibility when transitioning between or supporting both `jsonc-eslint-parser` (which supports JSONC, JSON5) and `@eslint/json` (standard JSON).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `toCompatRule` to convert an existing ESLint rule, originally targeting `jsonc-eslint-parser` AST nodes, into a version compatible with `@eslint/json`.

import { toCompatRule } from 'eslint-json-compat-utils';

// Example of a rule originally designed for jsonc-eslint-parser
const originalRule = {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Ensure all array elements are strings',
      category: 'Possible Errors',
      recommended: false,
    },
    schema: [],
  },
  create(context) {
    return {
      // This visitor key is specific to jsonc-eslint-parser's AST
      JSONArrayExpression(node) {
        for (const element of node.elements) {
          if (element.type !== 'Literal' || typeof element.value !== 'string') {
            context.report({
              node: element,
              message: 'Array element must be a string.',
            });
          }
        }
      },
    };
  },
};

// Convert the rule for compatibility with @eslint/json
export default toCompatRule(originalRule);

// To run this in an ESLint setup, ensure your .eslintrc.js has:
// parser: '@eslint/json',
// parserOptions: { ecmaVersion: 2020 },
// rules: { 'your-plugin/your-rule-name': 'error' }

view raw JSON →