Skip to content
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

Method shorthands should allow named function expressions #127

Open
logarytm opened this issue May 20, 2016 · 1 comment
Open

Method shorthands should allow named function expressions #127

logarytm opened this issue May 20, 2016 · 1 comment

Comments

@logarytm
Copy link

logarytm commented May 20, 2016

Lebab will transform code like the following:

var object = {
  foo: function () {
    bar();
  }
};

into the short method, but will leave

var object = {
  foo: function foo() {
    bar();
  }
};

intact.

Though most of the world is probably using the first convention, this is quite inconsistent as method shorthands (well, at least in V8) set Function#name:

sh-4.3$ node -v
v6.1.0
sh-4.3$ node
> let object = { foo() { console.log('hello'); } }
undefined
> object.foo.name
'foo'

There are also cases where the object property is different than the function name:

{
  hello: function foo() {
   // ...
  }
}

that could be either ignored (ie. left intact) or use the property name to maintain compatibility (which would through break if the code relies on Function#name.

@nene
Copy link
Collaborator

nene commented May 20, 2016

Named function expressions are currently ignored to keep the transformation safe in case of recursive functions like:

obj = {
    foo: function foo(n) {
        return n === 0 || foo(n-1);
    }
};
obj.foo(2); // returns true

The above function will work, but if we try to write it using method syntax, the code won't work:

obj = {
    foo(n) {
        return n === 0 || foo(n-1);
    }
};
obj.foo(2); // ReferenceError: foo is not defined

Of course Lebab could be smarter and detect whether the function name is actually referenced inside it. But the current implementation doesn't bother with that complexity and just skips these to be on the safe side.

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

No branches or pull requests

2 participants