eslint-plugin-prettier icon indicating copy to clipboard operation
eslint-plugin-prettier copied to clipboard

prefer-arrow-callback fix conflicting with prettier fix

Open ismail-syed opened this issue 6 years ago • 22 comments

Edit by @lydell: TL;DR We recommend turning off these rules for the time being:

{
  "rules": {
    "arrow-body-style": "off",
    "prefer-arrow-callback": "off"
  }
}

What version of eslint are you using? v4.9.0

What version of prettier are you using? v1.7.4

What version of eslint-plugin-prettier are you using? v2.3.1

Please paste any applicable config files that you're using (e.g. .prettierrc or .eslintrc files) https://github.com/ismail-syed/prettier-eslint-config-invalid-code

What source code are you linting?

function foo() {
  return isTrue && [0,1,2].map(function(num) {
    return num * 2;
  });
}

What did you expect to happen? The code above should be formatted as per prettiers config and also should adhere to that prefer-arrow-callback fix

What actually happened? Invalid code was generated, closing parenthesis is missing on the return statement.

function foo() {
  return (
    isTrue &&
    [0, 1, 2].map((num) => {
    return num * 2;
  });
}

Is the underlying issue from the prefer-arrow-callback fixer or the prettier plugin fixer?

ismail-syed avatar Oct 16 '17 19:10 ismail-syed

Is the underlying issue from the prefer-arrow-callback fixer or the prettier plugin fixer?

It could also be how ESLint applies fixes.

lydell avatar Oct 16 '17 19:10 lydell

Related: https://github.com/eslint/eslint/issues/7928

tl;dr: Autofixes from rules can, on rare occasions, conflict with each other. Rules can work around the issue by coarsening their fixes to ensure that the "fix range" contains the entire range of text that caused the particular problem to be reported, rather than just the actual text replacement.

For convenience, could you paste the output of running ESLint without --fix when those two rules are enabled? I think this might be a problem with the core prefer-arrow-callback rule, but I'm not sure.

not-an-aardvark avatar Oct 16 '17 19:10 not-an-aardvark

Here is the output of running ESLint without --fix on issue2.js.

╰─$ yarn run lint
yarn run v1.1.0
$ eslint issue2.js --max-warnings 0

/Users/ismailsyed/test/prettier-eslint-config-invalid-code/issue2.js
  2:10  error  Replace `isTrue·&&·[0,1,` with `(⏎····isTrue·&&⏎····[0,·1,·`  prettier/prettier
  2:32  error  Unexpected function expression                                prefer-arrow-callback
  3:5   error  Insert `··`                                                   prettier/prettier  4:1   error  Replace `··}` with `····})⏎··`                                prettier/prettier

✖ 4 problems (4 errors, 0 warnings)
  4 errors, 0 warnings potentially fixable with the `--fix` option.

With --fix, I see parsing errors:

screen shot 2017-10-16 at 4 50 27 pm

ismail-syed avatar Oct 16 '17 20:10 ismail-syed

Thanks for clarifying. This is actually a bug in eslint-plugin-prettier and is unrelated to the issue that I mentioned in https://github.com/prettier/eslint-plugin-prettier/issues/65#issuecomment-337017965. The problem is that eslint-plugin-prettier is providing separate "fixes" for each problem it reports, and the fixes are dependent on each other (in this case, one fix adds an opening paren, and another fix adds a closing paren). However, ESLint doesn't guarantee that it will be able to apply any given fix (e.g. if a fix overlaps with a fix from another rule).

It would probably be pretty difficult to determine which fixes need to be applied together as part of eslint-plugin-prettier. A simpler solution might be to just provide one "fix" which replaces the entire source text.

not-an-aardvark avatar Oct 25 '17 06:10 not-an-aardvark

Are there any plans to fix this?

victorporof avatar Jul 06 '19 19:07 victorporof

I believe the only way to fix this - changing the plugin so every change is batched into a single "fix" - results in a much worse user experience than the current behaviour of multiple smaller fixes in the common case of where you have multiple small and independent fixes. I don't think the tradeoff is worth it so I think this will end up remaining unfixable.

BPScott avatar Jul 06 '19 20:07 BPScott

Not necessarily - it could just be sure to specifically replace entire arrow functions at once, no?

ljharb avatar Jul 06 '19 21:07 ljharb

I think there’s one more way to fix this – taking a really deep dive into the ESLint autofix code and trying to fix the problem at its core. Sounds harder though.

lydell avatar Jul 07 '19 06:07 lydell

I believe the only way to fix this - changing the plugin so every change is batched into a single "fix" - results in a much worse user experience than the current behaviour of multiple smaller fixes in the common case of where you have multiple small and independent fixes. I don't think the tradeoff is worth it so I think this will end up remaining unfixable.

What's the differences in user experience and why do you consider them to be worse?

victorporof avatar Jul 07 '19 07:07 victorporof

What's the differences in user experience and why do you consider them to be worse?

Thanks for calling me out on that, I knew I was being lazy with that handwaving :)

Currently this plugin does the following:

  • Grab the entire contents of a JS file and push it into prettier
  • Compare the original input with prettier's output. For each block of differences then mark them as changes in eslint.

This means that if you have a file like:

const a = 0;
const b=0;
const c = 0;
const d=0;
const e = 0;

it will report changes (adding the spacing around the equals) on lines 2 and 4.

const a = 0;
-const b=0;
+const b = 0;
const c = 0;
-const d=0;
+const d = 0;
const e = 0;

This "find the smallest block of changes" logic gives a nice DX experience as it means the smallest change a user needs to make is shown.


The naive shotgun approach that would solve the problem, but give a crappy DX is to not try and split out changes into atomic parts, and instead batch all the changes into one big one. In this case the suggested fix for the above code would be:

const a = 0;
-const b=0;
-const c = 0;
-const d=0;
+const b = 0;
+const c = 0;
+const d = 0;
const e = 0;

Note that the c line is caught up in the change too despite it not requiring any modifications. This seems kinda reasonable in a small example but what if you had a 100 line file and the first change requested by prettier was on line 2 and the last change requested was on like 70. This algorithm would say that lines 2-70 would all need to change, despite most of that content being identical. Making two mistakes and large swaths of your file getting a red wavy line of whoops underneath it is a bad experience as it makes it very hard to tell what actually got changed.

While this approach is simple to implement the potential for making hard to understand suggestions that make it untenable IMO.


The 3rd approach is @ljharb's - a diff mechanism that understands JS and can block changes within a function into a single change. This would solve the problem but a very quick search came up with no drop-in solution available at the moment, and my gut thinks implementing a diffing algorithm that understands JS logic would be complex and much slower than the current algorithm.

If there's a way that this can happen without adding too much complexity to our codebase and doesn't have a major performance impact then I'd be interested in reviewing a PR but I don't have much interest in attempting to solve this personally as my gut thinks it's going to be a lot of effort and a lot of complexity for worse overall performance for a problem that only crops up occasionally. Happy to be proved wrong though :)

BPScott avatar Jul 19 '19 05:07 BPScott

I opened a similar issue in React Boilerplate.

polomoshnov avatar Feb 06 '20 01:02 polomoshnov

Is it possible to know when we are running in auto fix mode and decide to create a single fix instead of individual fixes? This way the DX of showing the smallest diffs will still exist when running without --fix for example with the eslint vscode plugin, but when it comes to auto fixing all rules there will only be one fix to apply so it can't conflict with other fixes.

EvanCahill avatar May 29 '20 22:05 EvanCahill

I wonder if arrow-body-style and prefer-arrow-callback extend the fix range will fix the issue?

fisker avatar Jul 14 '22 11:07 fisker

I wonder if arrow-body-style and prefer-arrow-callback extend the fix range will fix the issue?

Or maybe we can introduce a prettier/arrow-body-style and prettier/prefer-arrow-callback rule?

JounQin avatar Jul 14 '22 11:07 JounQin

That's what I was thinking, but I don't know if it works.

fisker avatar Jul 14 '22 16:07 fisker

I think I found another rule that can cause problems:

getInitialState is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.eslintreact/no-arrow-function-lifecycle

export default class Root extends Component {
  getInitialState = () => ({
    errorImporting: null,
    errorParsing: null,
    errorUploading: null,
    file: null,
    fromExtension: false,
    importSuccess: false,
    isImporting: false,
    isParsing: false,
    isUploading: false,
    parsedResults: null,
    showLongRunningMessage: false,
  });
}

CleanShot 2022-07-14 at 13 16 56

Here's eslintrc and package.json: https://gist.github.com/devinrhode2/9dc0e0debee7d2dcdf1afbb4af62fee3

devinrhode2 avatar Jul 14 '22 18:07 devinrhode2

Why would that rule cause problems? Prettier is about formatting; that rule is about actual runtime semantics.

ljharb avatar Jul 14 '22 18:07 ljharb

Produces this code:

export default class Root extends Component {
  getInitialState() { return {
    errorImporting: null,
    errorParsing: null,
    errorUploading: null,
    file: null,
    fromExtension: false,
    importSuccess: false,
    isImporting: false,
    isParsing: false,
    isUploading: false,
    parsedResults: null,
    showLongRunningMessage: false,
  }; }); // Extra trailing paren
}

devinrhode2 avatar Jul 14 '22 18:07 devinrhode2

Probably this is what it should produce:

export default class Root extends Component {
  getInitialState() {
    return {
      errorImporting: null,
      errorParsing: null,
      errorUploading: null,
      file: null,
      fromExtension: false,
      importSuccess: false,
      isImporting: false,
      isParsing: false,
      isUploading: false,
      parsedResults: null,
      showLongRunningMessage: false,
    };
  }
}

I had to manually delete trailing paren to get it to format correctly

devinrhode2 avatar Jul 14 '22 18:07 devinrhode2

@devinrhode2 that sounds like a bug in the rule's autofix; i'd appreciate you filing an issue on eslint-plugin-react :-)

ljharb avatar Jul 14 '22 18:07 ljharb

yeah it actually seems totally unrelated to prettier. Tried disabling all the prettier stuff, run just eslint autofix, and still happening.

devinrhode2 avatar Jul 14 '22 19:07 devinrhode2

any news7

kostia7alania avatar Apr 03 '23 12:04 kostia7alania