Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joeprivettsmart opened this issue Dec 21, 2023 · 2 comments
Open

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

joeprivettsmart opened this issue Dec 21, 2023 · 2 comments

Comments

@joeprivettsmart
Copy link

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

@scagood
Copy link

scagood commented Jan 16, 2024

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
Copy link

reddist commented Apr 3, 2024

@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 }
          },
        ],
      },
    },
  ],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants