ota-meshi / eslint-plugin-jsonc

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

how to use eslint-disable in json files #336

Closed manojkiraneda closed 2 months ago

manojkiraneda commented 2 months ago

hi there, I am new to javascript language & eslint as well, just trying to explore how i could leverage eslint along with eslint-plugin-jsonc in my project(c++ project with lot of JSON files with comments) CI infrastructure to lint and catch JSON errors.

here is my config file in CI:

import eslintPluginJsonc from 'eslint-plugin-jsonc';
export default [
  ...eslintPluginJsonc.configs['flat/recommended-with-jsonc'],
];

when i try to test my repository:

manojeda@manojeda-vm:$ eslint -c eslint.config.mjs  .

/home/manojeda/Documents/Code/pldm-workspace/test/pldm/host-bmc/test/host_effecter_jsons/malformed/dbus_to_host_effecter.json
  2:12  error  Parsing error: Unexpected token ':'

✖ 1 problems (1 errors, 0 warnings)

The json file which was marked as error - was intentionally written as a malformed JSON for unit testing purposes, so i figured that we have two ways to ignore the file.

  1. using eslint-disable constructs.
  2. using ignore in the flat configs.

I don't prefer option 2 , since my CI runs on multiple repositories and each of them can have malformed JSON's checked in at various places & i don't like developers of the repositories to add their ignore patterns in the config file(global one for all repositories).

So, i am left with option 1 , but when i try using the eslint-diable construct in the JSON file as mentioned in the readme , it does not seem to have any effect on the result, what am i missing here ?

manojeda@manojeda-vm:pldm$ cat /home/manojeda/Documents/Code/pldm-workspace/test/pldm/host-bmc/test/host_effecter_jsons/malformed/dbus_to_host_effecter.json
/* eslint-disable */
"effecters": [
    {
        "dbus_info": {
            "object_path": "/xyz/openbmc_project/control/host0/boot",
            "interface": "xyz.openbmc_project.Control.Boot.Mode",
            "property_values": [
                "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"
                ]
        }
        ]
manojeda@manojeda-vm:$ cat eslint.config.mjs
import eslintPluginJsonc from 'eslint-plugin-jsonc';
export default [
  ...eslintPluginJsonc.configs['flat/recommended-with-jsonc'],
];

manojeda@manojeda-vm:$ eslint -c eslint.config.mjs  .

/home/manojeda/Documents/Code/pldm-workspace/test/pldm/host-bmc/test/host_effecter_jsons/malformed/dbus_to_host_effecter.json
  2:12  error  Parsing error: Unexpected token ':'

✖ 1 problems (1 errors, 0 warnings)
ota-meshi commented 2 months ago

Hi @manojkiraneda

  1. using eslint-disable constructs.
  2. using ignore in the flat configs.

eslint-disable is not available because it only works with JSON files that can be parsed. This is a limitation of ESLint. Please select option 2.

manojkiraneda commented 2 months ago

sure, thanks for your suggestion @ota-meshi.