Skip to content

Refactorings to convert from function declarations to function expressions #44080

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

Open
5 tasks done
OliverJAsh opened this issue May 13, 2021 · 2 comments
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@OliverJAsh
Copy link
Contributor

Suggestion

🔍 Search Terms

refactoring quick fix vs code action

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

We already have refactorings to convert function expressions (arrow or non-arrow) to a function declaration, e.g. we can use "convert to named function" on both of these:

const fn1 = function () {
    return 1;
};

const fn2 = () => {
    return 1;
};

However, there is no refactoring to convert in the opposite direction—from a function declaration to a function expression.

I would like to suggest adding these refactorings so we cover all possibilities.

Note we do already have these refactorings (#28250):

  • "Convert to arrow function"
  • "Convert to anonymous function"

However these only work on function expressions—they don't work on function declarations. Perhaps we could fix this by extending the scope of those refactorings so they do work on function declarations.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 13, 2021
@RyanCavanaugh
Copy link
Member

Curious, why would you want to?

@OliverJAsh
Copy link
Contributor Author

OliverJAsh commented May 13, 2021

We use function expressions instead of declarations for numerous reasons: https://gist.github.com/OliverJAsh/71cfca924b4cd144b57a764d7fb1cdff

Extract to function creates a declaration so I have to convert it (related: #31860).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants