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.

Implement autofixing

See original GitHub issue

I’ve been doing some thinking about how this would work. I know that JSCS is pursuing using a concrete syntax tree (CST) for this purpose, and that might be useful for some fixes, but waiting for that effort to complete and then getting a working implementation for us to use would likely take a whole lot of time. So, I’ve been trying to figure out how to get autofixing in the short-term and came up with a plan. The basic plan is:

  1. Have autofixing run in a separate mode. You’d need to pass --fix on the command line and in that mode, you will not receive linting errors output (but you will receive parsing error output). This mode will only apply fixes and not lint the code otherwise because a change from one fix might alter the results of the linting.
  2. Have autofixing not available to plugins, at least initially. I want to make sure that if we expose something to plugins that it’s our long-term approach and I’m just not sure enough right now.
  3. Allow autofixing to happen inside of rules in order to avoid duplicate logic.

So how would I achieve this?

First, I’d create a new object called Fixer that would be passed in as the second argument to a rule function when ESLint is running in autofix mode, such as:

module.exports = function(context, fixer) {

};

The fixer object will have these methods:

  • insert(index, text) - inserts the given text at the given range index in the source
  • remove([start, end]) - removes the text in the given range (an array, so you can pass in node.range)
  • replace([start, end], text) - removes the text in the given range and replaces it with the given text

So the fixer object will collect all of the requested changes into an array and then do a reverse sort by range index. It can then apply the changes from the end of the string towards the beginning so that it will be accurate.

This approach should work for:

  • semi
  • indent
  • *-spacing
  • quotes

I’m not sure, maybe there are others. The functionality is obviously limited to non-intersecting ranges of changes, so we’d be unlikely to do more complex fixes like changing a function expression to an arrow function, but we could at least get the stylistic stuff fixed fairly easily.

I think this will end up being a small amount of work to get a really useful feature, so at least we’ll be able to autofix some of the more nitpicky things and maybe consider a CST once it has developed more fully.

Thoughts?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:26 (24 by maintainers)

github_iconTop GitHub Comments

1reaction
ExplodingCabbagecommented, Dec 8, 2015

Quick suggestion - @nzakas, could you add a note to the bottom of https://github.com/eslint/eslint/issues/1332 that this feature now exists? https://github.com/eslint/eslint/issues/1332 is the top Google result for “eslint fix issues” and claims that the feature will never be implemented; it’d help the discoverability of this feature if there were a note there about the current situation.

0reactions
nzakascommented, Sep 9, 2015

Another case that’s really hard: processed text. See my comment: https://github.com/eslint/eslint/pull/3635#discussion_r39084750

I’m thinking it might be best to just not do fixing for processed text in this first go-around as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implement autofix · Issue #25 · charliermarsh/ruff - GitHub
Here is a bunch of tools that I use for autofixing the code. Might be helpfull for both reference and ideas to carry...
Read more >
Powerfully autofixing code with Semgrep's new AST-based ...
tl;dr: In this blog post, I explain why AST-based autofix is better than text-based autofix and how Semgrep implements AST-based autofix to ...
Read more >
Auto-fix and format your JavaScript with ESLint - IBM Developer
Get the quick tips and tricks you need to use ESLint to autofix and format your JavaScript.
Read more >
How To Enable Linting on Save with Visual Studio Code and ...
Learn how to lint your code on every file save using ESLint rules and VS Code settings.
Read more >
AutoFix Example: Fixes to the Implementation
AutoFix : Fixing the Implementation. Figure 1.a shows a faulty routine in class TWO_WAY_SORTED_SET . The routine move_item takes an argument v ,...
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