ota-meshi / eslint-plugin-jsonc

ESLint plugin for JSON(C|5)? files
https://ota-meshi.github.io/eslint-plugin-jsonc/
MIT License
180 stars 17 forks source link

Struggling to get plugin to work with typescript parser... #290

Open joeprivettsmart opened 6 months ago

joeprivettsmart commented 6 months ago

Hello, do you have an example of how to set-up the configuration when using this plugin with the typescript parser?

scagood commented 5 months ago

In my config I use:

import jsoncPlugin from 'eslint-plugin-jsonc';
import jsoncParser from 'jsonc-eslint-parser';

import { getJsonFiles } from '../utilities/eslint-files.js';

/**
 * Get ESLint config for imports check
 * @returns {Promise<import('eslint').Linter.FlatConfig[]>}
 */
export async function createEslintJsonConfig() {
    return [
        {
            files: await getJsonFiles(),
            languageOptions: {
                parser: /** @type {*} */ (jsoncParser),
            },
            plugins: {
                jsonc: /** @type {*} */ (jsoncPlugin),
            },
        },
        {
            files: [ '**/*.json' ],
            rules: /** @type {import('eslint').Linter.RulesRecord} */
                (jsoncPlugin.configs['recommended-with-json'].rules),
        },
        {
            files: [ '**/*.jsonc' ],
            rules: /** @type {import('eslint').Linter.RulesRecord} */
                (jsoncPlugin.configs['recommended-with-jsonc'].rules),
        },
        {
            files: [ '**/*.json5' ],
            rules: /** @type {import('eslint').Linter.RulesRecord} */
                (jsoncPlugin.configs['recommended-with-json5'].rules),
        },
    ];
}

You dont need to overlap the ts parser and the jsonc parser :)

reddist commented 2 months ago

@joeprivettsmart This one worked for me:

Exact answer (".eslintrc.js" config):

module.exports = {
  // ...
  overrides: [
    // ...
    {
      files: [ '**/locales/*.json' ],
      parser: "jsonc-eslint-parser",
      extends: [ 'plugin:jsonc/base' ],
      rules: {
        "jsonc/sort-keys": [
          "error",
          {
            pathPattern: ".*",
            order: { type: "asc", natural: true }
          },
        ],
      },
    },
  ],
}

My full ".eslintrc.js" config:

module.exports = {
  root: true,
  env: {
    node: true,
    "vue/setup-compiler-macros": true,
  },
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/typescript/recommended',
    'plugin:prettier/recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  plugins: ['@typescript-eslint', 'perfectionist'],
  parser: "vue-eslint-parser",
  parserOptions: {
    parser: "@typescript-eslint/parser",
    ecmaVersion: 'latest',
    sourceType: "module"
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-control-regex': 0,
    'no-unused-vars': 'off',
    'no-unsafe-optional-chaining': 'off',
    "perfectionist/sort-enums": [ "warn", { type: "natural", order: "asc" } ],
    "perfectionist/sort-interfaces": [ "warn", { type: "natural", order: "asc" } ],
    "perfectionist/sort-named-imports": [ "warn", { type: "natural", order: "asc" } ],
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/no-duplicate-enum-values': 'off',
    '@typescript-eslint/no-unused-vars': [ 'warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
    "@typescript-eslint/member-ordering": [
      "warn",
      {
        "interfaces": { memberTypes: ["field"], order: 'alphabetically' },
      },
    ],
    'vue/no-v-text-v-html-on-component': 'off',
    '@typescript-eslint/no-extra-semi': 'off'
  },
  overrides: [
    {
      files: [
        '**/__tests__/*.{j,t}s?(x)',
        '**/tests/unit/**/*.spec.{j,t}s?(x)',
      ],
      env: {
        jest: true,
      },
    },
    {
      files: [ '**/locales/*.json' ],
      parser: "jsonc-eslint-parser",
      extends: [ 'plugin:jsonc/base' ],
      rules: {
        "jsonc/sort-keys": [
          "error",
          {
            pathPattern: ".*",
            order: { type: "asc", natural: true }
          },
        ],
      },
    },
  ],
}