-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[CS2] Compile computed properties to ES2015 equivalent #4338
Conversation
This is a fairly small change that simplifies the code generation for computed properties as they're now generated in the object initializer like regular properties.
Also, the first commit is actually just compiling some files in |
if prop not instanceof Assign | ||
prop = new Assign prop, prop, 'object' | ||
if prop not instanceof Comment and prop not instanceof Assign | ||
if prop.isComplex() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, dynamic keys were being detected with...
(prop.variable or prop).base instanceof Parens
... but this seems simpler. I don't know all the ins and outs though, so I might be missing something.
Nice stuff! |
@connec this looks good, do you mind just giving an example of some CoffeeScript with computed properties, and what it was compiling to before and after this PR? |
@GeoffreyBooth Here's an example: 76c076d |
Sure: Current (1.11.1):
In this PR:
|
This looks good to me. I updated your branch with the latest |
Go for it! |
Following on from #3597, wouldn't it now be preferable to simply allow the existing ES6 |
@danielbayley the purpose wasn't to add or change any syntax, interpolations were already allowed in object keys, this just simplifies the output by compiling to ES6. Might be worth having a separate discussion about Personally I prefer CS' approach to this. If "hello" is a valid key, then so too should be "#{hello}". ES' handling of template strings is needlessly restrictive imo. |
@connec Is this what you were referring to? key = 'key'
obj = {}
obj[key] = 'value' Obviously this is nothing new, but just to clarify… "#{key}": 'value' # works… cool! Leave it be.
[key]: 'value' # is valid ES6 but I think should also work in CS. Maybe you're right about it being a separate discussion; is it worth opening another issue? |
Sorry, I just meant that this syntax was valid before the PR: key = 'key'
obj =
"#{key}": 'value' It used to compile to (roughly): var key, obj
key = 'key';
obj = (
ref = {},
ref['' + key] = 'value',
ref
); And now compiles to: var key, obj
key = 'key';
obj = {
['' + key]: 'value'
};
I'm not sure, @GeoffreyBooth may have an opinion on this. |
Personally I don’t see why Perhaps open a new issue requesting the new syntax. Please prefix it with |
I guess we more or less have to support |
Good point 😢 I still think it's consistent to allow interpolations in keys though. |
Me too. |
This is a fairly small change that simplifies the code generation for computed properties as they're now generated in the object initializer like regular properties.
I ended up doing this whilst working on fully switching to ES2015 splats. Targets the
2
branch since the output won't compile without ES2015 support.