Skip to content

CodeAction: convert lambda expression declaration to function  #23299

Closed

Description

@Kingwl

TypeScript Version: 2.7.0-dev.201xxxxx

Code

const a = (arg: number) => (
    <div>{arg}foo</div>
)

Expected behavior:

function a (arg: number) {
  return (
    <div>{arg}foo</div>
  )
}

Actual behavior:
none

btw: also convert function to lambda expression

Activity

  1. changed the title [-]codeAction: convert lambda expression declaration to function [/-] [+]CodeAction: convert lambda expression declaration to function [/+] on Apr 10, 2018
  2. Kingwl commented on Apr 12, 2018

    @Kingwl
    ContributorAuthor

    image

    from IDEA

  3. zaaack commented on Apr 12, 2018

    @zaaack

    +1. Add braces is very useful, too.

  4. zzj3720 commented on Apr 12, 2018

    @zzj3720

    +1. Add braces is very useful, too.

  5. AngryPowman commented on Apr 12, 2018

    @AngryPowman

    Kingwl's proposals always help me a lot.

  6. vizee commented on Apr 12, 2018

    @vizee

    Kingwl's proposals always help AngryPowman a lot.

  7. ly0 commented on Apr 12, 2018

    @ly0

    Kingwl's proposals always help AngryPowman a lottery.

  8. DanielRosenwasser commented on Apr 12, 2018

    @DanielRosenwasser
    Member

    latyas (@ly0)'s comments always help reiterate vizee (@vizee)'s points.

  9. whtsky commented on Apr 18, 2018

    @whtsky

    I can't live without Kingwl's proposals.

  10. D0nGiovanni commented on Oct 3, 2018

    @D0nGiovanni
    Contributor

    Hi

    We're a team of two, currently on our bachelor thesis. We'd like to implement this issue. Estimated timeframe is about 2 weeks, at most 4 weeks. We're looking to do other refactorings and code fixes as well, but this would come first.

  11. Kingwl commented on Oct 5, 2018

    @Kingwl
    ContributorAuthor

    could bachelor thesis based on an open source project with Apache License 2.0?😂
    I suggest you confirm it in advance.

  12. D0nGiovanni commented on Oct 5, 2018

    @D0nGiovanni
    Contributor

    I don't know about other countries, but I don't see why it couldn't. It's approved anyway. We're already 3 weeks in. It's more of an engineering project than anything else.
    I looked it up anyway, because I was curious. I didn't find any restrictions regarding license.

    But we're getting slightly off-topic here... :P

  13. D0nGiovanni commented on Oct 12, 2018

    @D0nGiovanni
    Contributor

    I've got a question about reversing your example, Wenlu Wang (@Kingwl). Of course, reversing an anonymous function makes sense. (More likely to go from right to left here)

    foo(function (arg) { return <div>{arg}foo</div>; });
    

    to

    foo(arg => <div>{arg}foo</div>);
    

    or even

    foo(arg => <div>{arg}foo</div>);
    

    to

    foo(bar);
    function bar (arg) {
      return <div>{arg}foo</div>; 
    }
    

    But what about named function to lambda? Is that actually a use-case? Afaik, Webstorm does not support this. Maybe for a good reason?

    function a (arg: number) {
      return (
        <div>{arg}foo</div>
      )
    }
    

    to

    const a = (arg: number) => (
        <div>{arg}foo</div>
    )
    

    Last question (for now): Do we also support lambda to method? Of course, we would have to check for class scope.

    class kitty{
      someMethod(){
        foo(arg => <div>{arg}foo</div>);
      }
    }
    

    to

    class kitty{
      someMethod(){
        foo(bar);
      }
      bar (arg) {
        return <div>{arg}foo</div>;
      }
    }
    
  14. Kingwl commented on Oct 12, 2018

    @Kingwl
    ContributorAuthor

    lambda to method or the first case seems another refactor extract to(or combined more transform)

    But what about the named function to lambda? Is that actually a use-case? Afaik,

    if you want to edit, convert, or transform the parameter, but actually, add braces could cover the case

    Webstorm does not support this. Maybe for a good reason?
    yes, such as 'Hoisting'

  15. D0nGiovanni commented on Oct 12, 2018

    @D0nGiovanni
    Contributor

    You are right. They are different - and also already implemented.

    So to clarify, there are only 2 cases that are in the scope of this refactor. Your example and this:

    someFunction(function (arg) { return <div>{arg}foo</div>; }); <=> someFunction(arg => <div>{arg}foo</div>);

    Edit: But lambda to named function should not be supported because of hoisting - or loss thereof.

  16. added a commit that references this issue on Oct 31, 2018
  17. Kingwl commented on Jun 2, 2020

    @Kingwl
    ContributorAuthor

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: LS: Refactoringse.g. extract to constant or function, rename symbolSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions