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

Can't get plugin rules added by cli.addPlugin #12425

Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features

Comments

@fisker
Copy link
Contributor

fisker commented Oct 14, 2019

Tell us about your environment

  • ESLint Version: 6.5.1
  • Node Version: 10.16.3
  • npm Version: 6.9.0

What parser (default, Babel-ESLint, etc.) are you using?

default

const {CLIEngine} = require('eslint')
const unicorn = require('eslint-plugin-unicorn')
const cli = new CLIEngine()
cli.addPlugin('eslint-plugin-unicorn', require('eslint-plugin-unicorn'))
console.log(cli.getRules().has('unicorn/no-process-exit'))

I can't get the plugin rules, Is this by design or a bug?


quick reproduction

md eslint-issues-12425 && cd eslint-issues-12425 && yarn init -y && yarn add eslint eslint-plugin-unicorn

# print true
node -p "new (require('eslint').CLIEngine)({plugins: ['unicorn']}).getRules().has('unicorn/no-process-exit')"

# print false, but should be true
node -p "const cli = new (require('eslint').CLIEngine);cli.addPlugin('eslint-plugin-unicorn', require('eslint-plugin-unicorn'));cli.getRules().has('unicorn/no-process-exit')"
@fisker fisker added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Oct 14, 2019
@fisker
Copy link
Contributor Author

fisker commented Oct 14, 2019

Maybe related to #11871

@mysticatea mysticatea added accepted There is consensus among the team that this change meets the criteria for inclusion core Relates to ESLint's core APIs and features and removed triage An ESLint team member will look at this issue soon labels Oct 15, 2019
@mysticatea
Copy link
Member

Thank you for your report.

That's expected because addPlugin() actually just adds the implementation of a plugin. You still need {plugins: ['unicorn']} if you don't have .eslintrc.* files. We cannot use the rules of the plugins that the configuration doesn't have.

On the other hand, it looks like a bug because

const { CLIEngine } = require('eslint')
const engine = new CLIEngine({ plugins: ["foo"] })

engine.addPlugin('foo', require('eslint-plugin-unicorn'))
console.log(engine.getRules().has('foo/no-process-exit'))

was false. I expected true in this case.

@mysticatea
Copy link
Member

mysticatea commented Oct 15, 2019

As a side note, I'm proposing to remove addPlugin() method in favor of a constructor option in RFC40.

@fisker
Copy link
Contributor Author

fisker commented Oct 15, 2019

The reason I check this, is because I want run plugin against plugin source, so I add cli to do this https://github.com/sindresorhus/eslint-plugin-unicorn/blob/d638e5477d7240ea777dcaf249d0121a5a1910eb/test/lint/lint.js#L11-L25 , I want make sure the plugin is loaded, but get a false result.

According to the comments above, I'm not doing it right.

I should point eslint-plugin-unicorn to local file and use .eslintrc.*, because we can't pass plugin path to engine, right?

@mysticatea
Copy link
Member

In that case, you can use getRules() after the call of executeOnFiles() method for now.

The getRules() method returns the core rules and the rules of the plugins that the last call of executeOnFiles() or executeOnText() used.

Because, internally, the configuration object stores the loaded plugins that the config used. ESLint doesn't load plugins until configurations are determined and configurations are not determined until target files are determined. CLIEngine stores the configuration objects that the last call of executeOnFiles() or executeOnText(), then the getRules() method retrieves plugin rules from the configuration objects.

#11871 has updated CLIEngine to initialize "the configuration objects that the last call of executeOnFiles() or executeOnText() used" with only CLI options.

The addPlugin() looks to not update "the configuration objects that the last call of executeOnFiles() or executeOnText() used." (I think this is a bug because the method changes where ESLint loads plugins from.)
However, an executeOnFiles() call updates the configuration objects.

@fisker
Copy link
Contributor Author

fisker commented Oct 15, 2019

I remember I tried call this after executeOnFiles, because #11871 is reported by me, but also got false. anyway will try later, not on laptop now

@fisker
Copy link
Contributor Author

fisker commented Oct 15, 2019

Tested


$ node -p "const cli = new (require('eslint').CLIEngine);cli.addPlugin('eslint-plugin-unicorn', require('eslint-plugin-unicorn'));cli.getRules().has('unicorn/no-process-exit')"
false

$ node -p "const cli = new (require('eslint').CLIEngine)({useEslintrc: false});cli.addPlugin('eslint-plugin-unicorn', require('eslint-plugin-unicorn'));cli.executeOnText('');cli.getRules().has('unicorn/no-process-exit')"
false

$ node -p "const cli = new (require('eslint').CLIEngine)({useEslintrc: false});cli.addPlugin('eslint-plugin-unicorn', require('eslint-plugin-unicorn'));cli.executeOnFiles(['.']);cli.getRules().has('unicorn/no-process-exit')"
false

@mysticatea
Copy link
Member

That's expected because addPlugin() actually just adds the implementation of a plugin. You still need {plugins: ['unicorn']} if you don't have .eslintrc.* files. We cannot use the rules of the plugins that the configuration doesn't have.
#12425 (comment)

@fisker
Copy link
Contributor Author

fisker commented Oct 16, 2019

Sorry, I miss understood it, my purpose is avoid use {plugins: ['unicorn']}, because this requires a package.json file has eslint-plugin-* as dependency.

Thanks a lot for explaining

@mysticatea mysticatea self-assigned this Oct 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.