xsburg / vscode-javascript-booster

Sprinkle extra refactorings, code actions and commands over your JavaScript! 🍩 TypeScript and Flow are first class citizens as well!
https://marketplace.visualstudio.com/items?itemName=sburg.vscode-javascript-booster
159 stars 13 forks source link

Convert to arrow function loses generic type #43

Open chrisj-back2work opened 2 years ago

chrisj-back2work commented 2 years ago

Thank you for a very helpful tool.

Today I found a problem when the refactor target uses generics.

Original code --

function isEnumKey<T extends Enum>(enumSrc: T, key: unknown): key is keyof T {
  return Number.isInteger(enumSrc[key as keyof T])
}

Refactored using "Convert to arrow function" --

const isEnumKey = (enumSrc: T, key: unknown): key is keyof T => {
  return Number.isInteger(enumSrc[key as keyof T])
}

... which results in Cannot find name 'T'. ts(2304)

What the refactor should result in --

const isEnumKey = <T extends Enum>(enumSrc: T, key: unknown): key is keyof T => {
  return Number.isInteger(enumSrc[key as keyof T])
}

Does JavaScript Booster have a way to verify if a refactor introduces a compile error?

xsburg commented 2 years ago

Thank you for reporting the issue. It's clearly an uncovered scenario that should be fixed.

In general, JavaScript Booster uses Babel to manipulate code, which prevents syntax errors but cannot prevent semantic errors as above. There are normally two sorts of bugs that might arise in such an approach: the code action shouldn't be available in a certain scenario (therefore the trigger code should be fixed); the code action doesn't work as expected (we need to fix the action logic and support the scenario).