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.

Providing a mechanism to perform unsafe fixes

See original GitHub issue

At the moment, core rules only autofix problems if they can guarantee that the fixes will never break the code. This is a useful guarantee, because people can run --fix without worrying about whether their code will break. However, it’s also an unfortunate roadblock in some situations. Rules like sort-keys and no-return-await will never be fixable under the current system, because autofixing these rules could break the code in exceedingly unusual cases (even though the autofix would be convenient for the user in the vast majority of cases).

To make autofix more useful for these cases, I think we should consider adding a way for a rule to suggest an “unsafe” fix. This has been brought up several times in previous issues (e.g. https://github.com/eslint/eslint/issues/7714). While I’m not sure about what the behavior should be here (or if it’s worth doing at all), I think it’s at least worth discussing the idea.

First, here are some general design constraints for unsafe fixes:

  • A rule should be able to specify that its fixes are unsafe, preferably on the level of an individual fix (rather than applying the label to every fix produced by the rule).
  • It should still be possible to quickly and easily run “safe” fixes with no additional interaction.

Given those constraints, I see two possible approaches. The first approach is to have a CLI flag like --unsafe-fix that allows the user to opt-in to unsafe fixes. The second approach is to interactively prompt the user about each unsafe fix.

--unsafe-fix CLI flag

Pros:

  • There is precedent for this; for example, Uglify has an opt-in flag for unsafe minification.
  • This is the easiest option, assuming everything goes well and it doesn’t break your code.

Cons:

  • This might cause subtle bugs in the user’s code. Even if the user is kind of opting into it and it happens rarely, I’m still not entirely comfortable with that.
  • Some fixes might be more “unsafe” than others. If a user has lots of rules enabled and they’re worried about unsafe fixes, they might only want to apply some unsafe fixes, e.g. if they read the docs for a few rules and confirm that those particular fixes won’t break their code. (Possible solution: we could allow the user to opt-in to unsafe fixes for particular rules with the flag.)

Interactive prompts for unsafe fixes

Pros:

  • This is much less likely to break the user’s code.
  • The user can easily choose to apply only specific fixes.

Cons:

  • This is much less convenient for the user.
  • In order to be worthwhile at all, it would have to be significantly faster than fixing the code manually.
  • The user might be confused about why a particular fix is unsafe – some fixes are unsafe for very subtle reasons. In order to clarify this, a rule would probably have be able to provide an explanation somehow.

No changes (i.e. keep the current behavior)

Pros:

  • The UI remains simple.
  • ESLint never breaks anyone’s code.

Cons:

  • There is a lot of missed potential for autofixes that could save the user’s time.

I’m interested in hearing feedback (from both the team and the community) on these options. Do you agree with my analysis of the available options? Is there a solution I’ve missed? Is it worth adding a feature like this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tsheaffcommented, May 1, 2017

I see that this was closed last month. For what it’s worth, I’m a big proponent of the Interactive prompts proposal.

In my case, our large codebase has highly inconsistent usage of camelCase and snake_case, and even though a --fix option for camelcase rule has some subtle edge-cases that may cause bugs, the vast majority of these would actually be much safer to do with an automatic tool.

@nzakas you mention other tools that automatically make complex changes to a js codebase, but I’m not familiar with any that are nearly as reliable as eslint. Any specific packages you had in mind?

My two cents strongly in favor of the Interactive prompts option — would save me and my team a lot of time and almost certainly reduce the number of bugs in our migration, not increase it.

1reaction
albertocommented, Mar 11, 2017

Thanks for your interest in improving eslint. Unfortunately, it looks like this issue didn’t get enough support from the team and so I’m closing it. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach consensus after 21 days tend to never do it, and as such, we close those issues. This doesn’t mean the idea isn’t interesting, just that it’s not something the team can commit to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Machine Guarding - Introduction - Hazardous Motions/Actions
A wide variety of mechanical motions and actions may present hazards to the worker. These can include the movement of rotating members, reciprocating...
Read more >
Energy Isolation/Lock-Out/Tag-Out Program - UCSB EHS
Lockout-Tagout refers to the safety procedure used in industry and research settings to insure that dangerous machines have been properly shut-down and are ......
Read more >
DNA proofreading and repair (article) | Khan Academy
If DNA gets damaged, it can be repaired by various mechanisms, including chemical reversal, excision repair, and double-stranded break repair. Introduction.
Read more >
Hazard identification, risk assessment and risk control
Using the ThinkSafe steps · 1. Spot the hazard · 2. Assess the risk · 3. Make the changes.
Read more >
Farm safety: Unsafe machinery repairs can end tragically
Incident Prevention Strategies: · Do not continue to use machinery that is in need of repair. · Establish policies and practices that ensure ......
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