question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow more control over fixes in CLIEngine

See original GitHub issue

I’ll talk a little about the use case I’m attempting to solve, and then my proposed solution:

Problem

Currently, it is not a simple matter to tell ESLint to apply fixes from only one rule, or to disable particular rules from fixes. Per-rule autofixing is being discussed in https://github.com/eslint/eslint/issues/8018, and @ljharb mentioned his use case of fixing one single rule at a time. In a subsequent chat discussion, I learned that Airbnb also uses inline config to change some rules to warn severity, and he would like to avoid applying fixes to those lines. Currently, there is no built-in way that I know of for ESLint to support this use case.

Potential Solution

I would like to write a CLI tool (possibly an enhancement to eslint-nibble which would allow users to autofix one single rule at a time, while still respecting the rest of the eslint configuration in their .eslintrc file. Furthermore, I would like to allow an option to not fix messages with a severity of 1.

Unfortunately, this is not currently possible. To apply fixes with CLIEngine, it’s necessary to configure it with fix: true. This adds an output field to the results, which CLIEngine.outputFixes() applies to the filesystem. However, the creation of the error messages and output occur together, when calling CLIEngine.executeOnFiles or CLIEngine.executeOnText.

Proposal

My proposal is to allow users to modify the messages within report.results (for example, to strip out any messages with severity: 1 or all except a particular rule), before fixes are applied.

I have experimented with adding an applyFixesToReport method to CLIEngine, which would accept a report object, do a little bit of work to get sourceFile objects using our existing utilities, and then call SourceCodeFixer.applyFixes(), returning a new report including an output string if fixes were applied from the messages in the originally supplied report.

The changes to ESLint are pretty minimal using this approach, and would allow much more flexibility in the fixes that ESLint performs. I’d be happy to submit a PR if this suggestion is approved.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
nzakascommented, Feb 9, 2017

What if fix could be a function that returns true if a passed-in message should be fixed?

2reactions
IanVScommented, Feb 8, 2017

@ilyavolodin unfortunately it’s not quite that easy. .eslintrc contains more than just rules, including parser configurations, and --no-eslintrc will wipe all of that out. Also, you wouldn’t be able to avoid fixing warnings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Command Line Interface - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
fluid-eslint | Yarn - Package Manager
The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and details, see the configuration docs)....
Read more >
Changelog — Dragonfly 0.35.0 documentation - Read the Docs
Change command-line interface (CLI) to allow using multiple engine options arguments. Change load-directory CLI command to accept more than one directory.
Read more >
ESLint (with `autoFixOnSave: boolean | string[]`)
Integrates ESLint JavaScript into VS Code. The plugin is a fork of "vscode-eslint", with added ability to specify a set of rules to...
Read more >
10 ESLint Best Practices - CLIMB
The API provides access to all of the same features as the CLI engine, but it's much faster and more efficient. Plus, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found