eslint / json

JSON language plugin for ESLint
Apache License 2.0
46 stars 4 forks source link

Bug: (fill in) #46

Closed ajslater closed 3 weeks ago

ajslater commented 3 weeks ago

Environment

ESLint version: 9.14.0 @eslint/json version: 0.6.0 Node version: 23.1.0 npm version: 10.9.0 Operating System: macOS

Which language are you using?

json

What did you do?

Javascript rules are run on json and eslint crashes. package.json ```json { "devDependencies": { "@eslint/json": "^0.6.0", "eslint": "^9.14.0" } } ``` eslint.config.js: ```js import js from "@eslint/js"; import json from "@eslint/json"; export default [ js.configs.recommended, { files: ["**/*.json"], language: "json/json", ...json.configs.recommended, }, ]; ``` Test case output: ```sh $ npx eslint package.json (node:43669) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require(). Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) (node:43698) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///Users/aj/Code/testjson/eslint.config.js?mtime=1730854966249 is not specified and it doesn't parse as CommonJS. Reparsing as ES module because module syntax was detected. This incurs a performance overhead. To eliminate this warning, add "type": "module" to /Users/aj/Code/testjson/package.json. (Use `node --trace-warnings ...` to show where the warning was created) Oops! Something went wrong! :( ESLint: 9.14.0 TypeError: Error while loading rule 'no-irregular-whitespace': sourceCode.getAllComments is not a function Occurred while linting /Users/aj/Code/testjson/package.json at Object.create (/Users/aj/Code/testjson/node_modules/eslint/lib/rules/no-irregular-whitespace.js:87:41) at createRuleListeners (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:943:21) at /Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:1068:84 at Array.forEach () at runRules (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:999:34) at #flatVerifyWithoutProcessors (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:1911:31) at Linter._verifyWithFlatConfigArrayAndWithoutProcessors (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:1992:49) at Linter._verifyWithFlatConfigArray (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:2081:21) at Linter.verify (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:1528:61) at Linter.verifyAndFix (/Users/aj/Code/testjson/node_modules/eslint/lib/linter/linter.js:2319:29) ```

What did you expect to happen?

using @eslint/json should be possible an eslint.config.js along side other rules for javascript. This is possible with @eslint/markdown if you specify the markdown processor, but @eslint/json has no included separate processor.

eslint-plugin-json and the more capable eslint-plugin-jsonc do not fail in this manner, I think because they, by default, use their own processors.

I was expecting this plugin to be able to be used to lint json included in a largely javascript project, and not only for an exclusively json project.

What actually happened?

eslint crashes trying to use javascript rules on a JSONSourceCode object. If these rules are disabled. rules from every plugin in the eslint.config.js must be disabled in the *.json section.

Link to Minimal Reproducible Example

https://github.com/ajslater/eslint-json-example

Participation

Additional comments

This plugin seemed to be advertised to be a replacement fro eslint-plugin-json and eslint-plugin-jsonc, but isn't if it can't exist alongside other plugins. Perhaps I'm misunderstanding it's utility?

geiregjo commented 3 weeks ago

Experiencing the same issue. I've been looking at it for a long time assuming I did something wrong.

fasttime commented 3 weeks ago

Thanks for the issue @ajslater. The recommended usage for @eslint/js (and also for @eslint/json) is to include a files specifier along with the other config properties. I.e. in the case of your config:

import js from "@eslint/js";
import json from "@eslint/json";

export default [
  {
    files: ["**/*.js"],
    rules: js.configs.recommended.rules,
  },
  {
    files: ["**/*.json"],
    language: "json/json",
    ...json.configs.recommended,
  },
];

The files list in the first config object causes the rules in js.configs.recommended to apply only to files with the .js extension, if that's what you are using for your JavaScript files, and .json files won't be linted by those rules any more. Does this solve your problem?

Note that unlike eslint-plugin-json and eslint-plugin-jsonc this is a language plugin. There is no processor that converts the JSON text into parsable JavaScript code. Instead, the text is parsed directly by a JSON parser and the output of the parser is fed into the rules, which recognize that particular format.