Is it safe to apply autofix from ESLint?

10,606

Mostly yes, but I recently came across an issue when using the autofix option. The codebase I work with uses Mithril v.0.2, which initializes the component's controller using the new keyword. The new keyword only works on constructors, but the eslint --fix command (specifically the prefer-arrow-callback replaced some of my anonymous functions with arrow functions. Which caused some errors, since arrow functions are not constructors.

So my case was like this:

const component = {};
// My method in this object used to be a regular function like the one below
component.normalFunction = function () {
    return {
        test: true
    };
}

// Eslint autofix changed it to be an arrow function
component.arrowFunction = () => {
    return {
        test: true
    };
}

try {
  new component.normalFunction();
  new component.arrowFunction();
} catch (err) {
  document.body.prepend(err);
}

So far that was the only issue I have found with the autofix rule. In my case I ran eslint autofix excluding that rule.

Share:
10,606
lilezek
Author by

lilezek

Updated on December 05, 2022

Comments

  • lilezek
    lilezek over 1 year

    I'd like to format several files but I'm worried about breaking code.

    When I'm saying if it is safe for not, I'm saying that the execution of the code before and after is exactly the same.

    For instance, there are rules that look pretty safe to be applied (like indenting). I suppose that changing the number of spaces won't affect the execution of the code.

    Then, there are rules that don't look so safe. For instance, changing var to let or const could cause a different execution as var is not exactly the same as let.

    So, I'm wondering if there are any auto-fix rule from ESLint that can change the code so the execution is different when applied the --fix thing.