JamieMason / eslint-plugin-prefer-arrow-functions

Auto-fix plain Functions into Arrow Functions, in all cases where conversion would result in the same behaviour
https://www.npmjs.com/package/eslint-plugin-prefer-arrow-functions
MIT License
43 stars 11 forks source link

Functions containing dollar signs (`$`) are incorrectly fixed #28

Closed renato-bohler closed 4 months ago

renato-bohler commented 5 months ago

Description

While validating this plugin against a big codebase, I found an edge case where it auto-fixes incorrectly.

If the function has a dollar sign ($) on its body, it gets incorrectly transformed somehow.

Example 1

export function foo() {
  const bar = '$';
  return bar
};

Gets transformed into an invalid syntax. Notice the lack of the dollar sign, the missing single quote, and the extra semicolon at the end:

export const foo = () => {
  const bar = ';
  return bar
};;

Example 2

If the dollar sign is in backticks:

export function foo() {
  const bar = `$`;
  return bar
};

Then this happens:

export const foo = () => {
  const bar = `(PARAMS)RETURN_TYPE => ;
  return bar
};;

(PARAMS)RETURN_TYPE => is leaking from the writeArrowFunction function somehow

https://github.com/JamieMason/eslint-plugin-prefer-arrow-functions/blob/9b76409f17deaa5d820c92adbb32cdc462fb3ec4/src/prefer-arrow-functions.ts#L137-L146

Example 3

If there are two dollar signs, then the transformation is different, but still incorrect:

export function foo() {
  const bar = `$$`;
  return bar
};

And this happens:

export const foo = () => {
  const bar = `const foo = ;
  return bar
};;

Suggested Solution

No idea why that happens, so I'd have to investigate. Probably need to escape $ when replacing or something?

Help Needed

I just need time to look into that.

JamieMason commented 5 months ago

haha wow, this is an interesting one.