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

[eslint-plugin-no-autofix] Same exception as in closed issue #72, after the no-autofix vers 1.1.1 was released #73

Closed
ababalau opened this issue Apr 22, 2021 · 3 comments · Fixed by #89
Assignees
Labels
bug Something isn't working

Comments

@ababalau
Copy link

Tell us about your environment

eslint: 7.24.0 (I updated it compared with #72)
eslint-plugin-json: 2.1.2
eslint-plugin-json-format: 2.0.1
eslint-plugin-no-autofix: 1.1.1
.eslintrc:
............
"plugins": [
"json",
"no-autofix"
],
"extends": "eslint:recommended",
"rules": {
"accessor-pairs": 0,
"json/*": [
"error",
"allowComments"
],
"array-bracket-spacing": [
0,
"never"
],
"no-useless-catch": 1,
"no-unused-labels": "off",
"no-autofix/no-unused-labels": "warn"
},
............

Console output:

Expected behavior
Running
eslint . --fix
should return no issue
Actual behavior
Console output (after introducing some console.logs to understand what happens. See the in bold messages):

eslint . --fix

pkg= eslint-plugin-no-autofix
plugins= [
'eslint-plugin-json',
'eslint-plugin-json-format',
'eslint-plugin-prettier'
]
pluginName= json
pluginName= json-format

Oops! Something went wrong! :(

ESLint: 7.24.0

TypeError: Failed to load plugin 'no-autofix' declared in '.eslintrc': Cannot convert undefined or null to object
at Function.keys ()
at /Users/i336162/Projects/etd/etd-cloud-backend-services/node_modules/eslint-plugin-no-autofix/lib/rules.js:62:12
at Array.forEach ()
at Object. (/Users/i336162/Projects/etd/etd-cloud-backend-services/node_modules/eslint-plugin-no-autofix/lib/rules.js:58:9)
at Module._compile (/Users/i336162/Projects/etd/etd-cloud-backend-services/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (/Users/i336162/Projects/etd/etd-cloud-backend-services/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! etd-cloud-backend-services@1.25.3 lint:dev: eslint . --fix
npm ERR! Exit status 2

The code in question is the same one as mentioned in #72.
With console.logs, the code looks like this

"...........
// support 3rd-party plugins
// TODO: find a safer way, as this is no reliable(depends on the package manager)
// TODO: support scoped package. e.g. @typescript-eslint/eslint-plugin
const root = findUp.sync("package.json", { cwd: path.join(__dirname, "../../") });
const mdir = path.join(root, "../node_modules/");
console.log("pkg=", pkg.name);
const plugins = fs.readdirSync(mdir).filter(it => /^eslint-plugin/u.test(it) && it !== pkg.name);
console.log("plugins=", plugins);

plugins.forEach(it => {
const plugin = require(it);
const pluginName = it.replace(/^eslint-plugin-/u, "");
console.log("pluginName=", pluginName);
Object.keys(plugin.rules).forEach(rule => {
allRules[${pluginName}/${rule}] = getNonFixableRule(plugin.rules[rule]);
});
});

module.exports = allRules;"

  • This code assumes that all eslint-plugin modules have an exported 'rules' field, which is probably created from .eslintrc if the module is declared in the plugins array but only if there is a rule for that module in the rules array.
  • As the console.logs shows, this is the case for the eslint-plugin-json, but not for the eslint-plugin-json-format, which is not declared in eslintrc, nor it has a rule defined for it even if I added in the plugins array, but is present in my node_modules directory (probably we had some rules in the past that we do not use anymore, and we removed it from eslintrc)
  • So I think the fix needs to get plugins not only from what is in the node_modules but it has to intersect with the list of plugins from .eslintrc that have rules declared in the rules.

Best regards,
Adrian

@ababalau ababalau added the bug Something isn't working label Apr 22, 2021
@ababalau
Copy link
Author

For now I will remove the 'eslint-plugin-json-format','eslint-plugin-prettier' plugins from my dependencies, as I don't have rules for them, and I checked the excepion is no longer issued if I do that, but I still think that the eslint check should not depend on what we have in exces in node_modules, but mostly on what we declared in .eslinrc as plugins and rules.

@aladdin-add
Copy link
Owner

aladdin-add commented Apr 22, 2021

there was some hacky things in the plugin: eslint plugins were designed to be not dependent on others, but in the plugin, it has to load rules in them.

haven't thought a plugin can contain no rules>︿<. a quick fix would be just skip these plugins.

@aladdin-add
Copy link
Owner

For now I will remove the 'eslint-plugin-json-format','eslint-plugin-prettier' plugins from my dependencies, as I don't have rules for them,

yes, but you have to list these plugins in (dev)Dependencies, to make sure them installed in 'node_modules/'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants