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

Support static fields in class transform #291

Open
seanrowe opened this issue Apr 27, 2019 · 3 comments
Open

Support static fields in class transform #291

seanrowe opened this issue Apr 27, 2019 · 3 comments

Comments

@seanrowe
Copy link

seanrowe commented Apr 27, 2019

When you have code like

ClassName.someFunction = function() {}

It does not become a static method in the class.

Also, if you have code like this

Object.defineProperty(ClassName, "variableName", { value: true })

it does not become a static variable of the generated class.

@nene
Copy link
Collaborator

nene commented May 1, 2019

Yeah, static methods and fields are currently not supported.

@nene nene changed the title Static Support static fields in class transform May 1, 2019
@seanrowe
Copy link
Author

seanrowe commented May 3, 2019

Yeah, static methods and fields are currently not supported.

I really appreciate you guys making this. You have saved me so much time.

@nene
Copy link
Collaborator

nene commented May 3, 2019

Adding support for static fields is currently problematic as this is not part of official ECMAScript syntax and Espree parser (which Lebab uses) doesn't support it.

Supporting static methods should be simpler. At least as long as they are attached to functions that also have methods in their prototype:

MyFunction() {}
MyFunction.prototype.doX = function(){}
MyFunction.staticX = function(){}

When a function has no methods defined in its prototype we currently don't detect it as a class. I think that won't change with static methods - without the prototype it's a bit too vague of a guess whether it should be a class or not. So the following would not be transformed:

MyFunction() {}
MyFunction.staticX = function(){}

Similarly a common pattern in e.g. Java is to have a class with only static methods, while in JavaScript one would just write an object:

const MyStaticObj = {
    staticFoo() {},
    staticBar() {}
};

This too would be left as-is.

@seanrowe if you have some example real-world code with ES5 static methods that you could share, that would be helpful (just to gather some additional real-world examples).

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