eslint-plugin-json icon indicating copy to clipboard operation
eslint-plugin-json copied to clipboard

How do you get this to work?

Open trusktr opened this issue 4 years ago • 6 comments

I have a config file like this:

.eslintrc.js
module.exports = {
  root: true,

  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
  },

  env: {
    browser: true,
    node: true,
    es2017: true,
  },

  plugins: [
    'json', // doesn't seem to do anything with JSON files
    'promise', // https://github.com/xjamundx/eslint-plugin-promise
    'html',
  ],

  extends: [
    // Uses the recommended rules from the @typescript-eslint/eslint-plugin
    'plugin:@typescript-eslint/recommended',
    // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    'prettier/@typescript-eslint',
    'plugin:json/recommended',
    // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
    'plugin:prettier/recommended',
  ],

  // add your custom rules here
  rules: {
    'no-debugger': 'error',
    'prefer-const': 'error',
    'one-var': ['error', 'never'],
    'no-var': 'error',
    'no-return-assign': ['error', 'except-parens'],
    'brace-style': ['error', '1tbs', {allowSingleLine: false}],
    'quote-props': ['error', 'as-needed'],
    curly: ['error', 'multi-or-nest', 'consistent'],

    // --- PROMISE ---------------------------------
    // from https://github.com/xjamundx/eslint-plugin-promise

    'promise/always-return': 'off',
    'promise/no-return-wrap': 'error',
    'promise/param-names': 'error',
    'promise/catch-or-return': 'error',
    'promise/no-new-statics': 'error',
    'promise/no-return-in-finally': 'error',

    // --- TYPESCRIPT ------------------------------

    'no-undef': 0,
    'no-unused-vars': 0,

    '@typescript-eslint/naming-convention': 'off', // — Require community JS/TS variable naming conventions. TODO, this one has lots of options.
    '@typescript-eslint/adjacent-overload-signatures': 'error', // — Require that member overloads be consecutive
    '@typescript-eslint/explicit-function-return-type': 'error', // — Require explicit return types on functions and class methods
    '@typescript-eslint/explicit-module-boundary-types': 'off', // — Require explicit types on all things that are exported from modules. Off because we handle this with the explicit-function-return-type rule paired with (soon) the noImplicitAny option in tsconfig.
    '@typescript-eslint/consistent-type-assertions': 'error', // — Enforces the use of `as` Type assertions instead of <Type> assertions.
    '@typescript-eslint/no-array-constructor': 'error', // — Disallow generic Array constructors
    '@typescript-eslint/no-explicit-any': 'off', // — Disallow usage of the any type
    '@typescript-eslint/no-inferrable-types': 'off', // — Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean.
    '@typescript-eslint/no-namespace': 'off', // — Disallow the use of custom TypeScript modules and namespaces
    '@typescript-eslint/triple-slash-reference': 'error', // — Disallow /// <reference path="" /> comments
    '@typescript-eslint/no-unused-vars': 'off', // — Off beacuse TypeScript gives us these errors already.
    '@typescript-eslint/no-var-requires': 'off', // — Disallows the use of require statements except in import statements
    '@typescript-eslint/type-annotation-spacing': ['error', {}], // — Require consistent spacing around type annotations
    '@typescript-eslint/no-non-null-assertion': 'off', // — Prevents non-null assertiong with `!`
  },

  settings: {},
}

And then I try to run

eslint package.json

and it finishes without outputting anything to console, regardless of what format I have in my package.json.


I've tried getting eslint-plugin-json to work a few times over the past couple years, over several versions of eslint, but it always seems not to do anything.

trusktr avatar Oct 28 '20 19:10 trusktr

I previously had babel-eslint parser (and an override only for .ts files) but that also didn't seem to do anything with respect to JSON files (it looked the same as above except parser: 'babel-eslint' and no typescript stuff).

trusktr avatar Oct 28 '20 20:10 trusktr

Random thought: if I have a custom eslintrc, do I perhaps need to make an item for JSON files to overrides to return some settings back to normal?

trusktr avatar Oct 29 '20 01:10 trusktr

Hmm, I don't have json in my plugins now, but I do have 'plugin:json/recommended' listed in extends, and I do see an error like

/home/trusktr/src/velodyne_sw+vella-frontend-projects/pkgs/mapper-annotated-scene/tsconfig.json
  2:17  error  Comment not allowed  json/*

Getting somewhere. I don't see red squigglies in the code while looking at JSON files (only other types of files) in VS Code.

EDIT: Oh, interesting, now that I removed json I see a bunch of errors as if it is checking it as a .js file. So it's working to some degree.

My main motivation is to get red squigglies in VS Code, but if I can't, then I think prettier is enough and eslint-plugin-json is probably not needed.

trusktr avatar Oct 29 '20 02:10 trusktr

faced with the same issue

Maks-Yaremenko avatar Apr 11 '21 09:04 Maks-Yaremenko

Same issue here, this plugin isn't running at all

BoisAuLit avatar Oct 03 '22 14:10 BoisAuLit

After some attempts I got this to work today. Here are some things you might be missing:

  • in your tsconfig.json, make sure you have the "include" entry, which should contain among its values something like **/*.json (e.g. "include": ["src", "**/*.json"],). See the 'include' section the TS docs:

If a glob pattern doesn’t include a file extension, then only files with supported extensions are included (e.g. .ts, .tsx, and .d.ts by default, with .js and .jsx if allowJs is set to true).

I suspect that if we didn't have a tsconfig file (e.g. if it was a JS project), then adding something like extraFileExtensions: [".json"] to the parserOptions: in the eslint config file might work. But when there is a tsconfig file, it seems to take precedence.

  • make sure to add the .json files explicitly when running the eslint command, by adding json as an extension, i.e. something like eslint ./src ./test --ext .ts --ext .json

Other notes:

  • The comment about apparently not needing to add json as an entry in the plugins section of the eslint config file seems to be true, but I'm keeping it there anyway, for consistency / convention.

  • Apart from that, in my eslint config file I kept the value "plugin:json/recommended" in the extends section (seems to be the important one); and I also added explicitly the rule "json/*": "error" (even though this is not necessary, but wanted to make it explicit).

romenrg avatar Dec 22 '22 00:12 romenrg