Typescript: CodeAction: convert lambda expression declaration to function

Created on 10 Apr 2018  Β·  15Comments  Β·  Source: microsoft/TypeScript



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

Awaiting More Feedback Refactorings Suggestion

Most helpful comment

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

All 15 comments

image

from IDEA

+1. Add braces is very useful, too.

+1. Add braces is very useful, too.

Kingwl's proposals always help me a lot.

Kingwl's proposals always help AngryPowman a lot.

Kingwl's proposals always help AngryPowman a lottery.

@ly0's comments always help reiterate @vizee's points.

I can't live without Kingwl's proposals.

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.

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

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

I've got a question about reversing your example, @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>;
  }
}

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'

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.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CyrusNajmabadi picture CyrusNajmabadi  Β·  3Comments

seanzer picture seanzer  Β·  3Comments

bgrieder picture bgrieder  Β·  3Comments

blendsdk picture blendsdk  Β·  3Comments

uber5001 picture uber5001  Β·  3Comments