Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-arrow-callback fix conflicting with prettier fix #65

Open
ismail-syed opened this issue Oct 16, 2017 · 22 comments
Open

prefer-arrow-callback fix conflicting with prettier fix #65

ismail-syed opened this issue Oct 16, 2017 · 22 comments
Labels

Comments

@ismail-syed
Copy link

ismail-syed commented Oct 16, 2017

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?

@lydell
Copy link
Member

lydell commented Oct 16, 2017

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

It could also be how ESLint applies fixes.

@not-an-aardvark
Copy link
Member

Related: eslint/eslint#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.

@ismail-syed
Copy link
Author

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

@not-an-aardvark
Copy link
Member

Thanks for clarifying. This is actually a bug in eslint-plugin-prettier and is unrelated to the issue that I mentioned in #65 (comment). 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.

@victorporof
Copy link

Are there any plans to fix this?

@BPScott
Copy link
Member

BPScott commented Jul 6, 2019

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.

@ljharb
Copy link

ljharb commented Jul 6, 2019

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

@lydell
Copy link
Member

lydell commented Jul 7, 2019

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.

@victorporof
Copy link

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?

@BPScott
Copy link
Member

BPScott commented Jul 19, 2019

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 :)

@polomoshnov
Copy link

I opened a similar issue in React Boilerplate.

@EvanCahill
Copy link

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.

@fisker
Copy link
Member

fisker commented Jul 14, 2022

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

@JounQin
Copy link
Member

JounQin commented Jul 14, 2022

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?

@fisker
Copy link
Member

fisker commented Jul 14, 2022

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

@devinrhode2
Copy link

devinrhode2 commented Jul 14, 2022

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

@ljharb
Copy link

ljharb commented Jul 14, 2022

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

@devinrhode2
Copy link

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
Copy link

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

@ljharb
Copy link

ljharb commented Jul 14, 2022

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

@devinrhode2
Copy link

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

@kostia7alania
Copy link

any news7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

13 participants