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

Lexer: Backticks should allow adding code to class body #4464

Closed
wcjohnson opened this issue Mar 13, 2017 · 11 comments
Closed

Lexer: Backticks should allow adding code to class body #4464

wcjohnson opened this issue Mar 13, 2017 · 11 comments

Comments

@wcjohnson
Copy link

ES2015 spc allows method definitions to be prefixed by get or set in which case they are transformed to property getters and setters.

Of the following, only A generates the corresponding ES2015 output:

class A
	x: -> 42

class B
	get x: -> 42

class C
	`get` x: -> 42

class D
	`get x`: -> 42

class E
	`get x() { return 42; }`

B makes a hoisted class body (as expected), C and D are syntax errors, and E, which would seem like it should be the ultimate escape valve, generates a hoisted class body as well.

It would be nice to have some kind of escape valve for modifying the definitions within the non-hoisted portion of ES2015 class body. It would be particularly nice if backticks could be made to serve this function, as they can help support other possible future syntax (e.g. decorators) as well.

@GeoffreyBooth
Copy link
Collaborator

@connec

See also coffeescript6/discuss#17

@GeoffreyBooth GeoffreyBooth added this to the 2.0.0 milestone Mar 13, 2017
@connec
Copy link
Collaborator

connec commented Mar 13, 2017

Recommended escape valve atm would be to use Object.defineProperty.

class A
  Object.defineProperty @prototype, 'x', get: -> 42

@wcjohnson
Copy link
Author

After reading that other thread I understand all the objections to mucking around with syntax, and yeah, just going back to ES5 works (though now I'm doing the job my transpiler is supposed to do for me!)

However, I would think that on general principle, there should be some backtick-esque escape valve for getting stuff inside of the inner class body.

@GeoffreyBooth
Copy link
Collaborator

Perhaps this issue should be more about “backticks should allow inserting code into class body” or whatever you call the area that’s not the executable class body? And we can keep the other issue for discussing adding support for getters/setters.

@wcjohnson
Copy link
Author

If done right, you could probably kill both birds with one stone. If my example C wasn't a syntax error, but instead caused the backticked code to be directly inserted into the class body, you would have solved both getters and decorators.

I think it might also bypass the grammar ambiguities referenced int he other thread too, but I am not familiar with the CS lexer so I could be wrong about that.

@GeoffreyBooth
Copy link
Collaborator

Well we never want to require people to use backticks. We went through the trouble of adding support for import/export primarily to avoid that.

Decorators is an unusual case as the spec isn’t finalized yet, and it’s our policy not to support unfinalized specs. So backtick support for what you’re talking about would be a good thing to have, so that people can use decorators until the spec is finalized and then we add native support for them.

@GeoffreyBooth GeoffreyBooth changed the title [CS2] ES2015 "get" and "set" in method definitions? [CS2] Backticks should allow adding code to class body Mar 13, 2017
@wcjohnson
Copy link
Author

I think if it could be made to work, it would be a winning move on the whole, even if you went on to add a full syntax for getters and setters later.

  • It obeys the semantic intent of backticks, which is a "just insert this raw code here" escape valve.
  • It gives you getters and setters with a syntax that looks pretty much like you'd expect, maybe something like what the integrated syntax looks like later. (Although the grammar ambiguities mentioned in the other thread look bad for the integrated syntax.)
  • It lets you escape out to other future JS features that involve different syntax in the class body -- decorators, etc.

@GeoffreyBooth GeoffreyBooth changed the title [CS2] Backticks should allow adding code to class body [CS2] Lexer: Backticks should allow adding code to class body May 6, 2017
@GeoffreyBooth GeoffreyBooth removed this from the 2.0.0 milestone Aug 14, 2017
GeoffreyBooth added a commit to GeoffreyBooth/coffeescript that referenced this issue Aug 30, 2017
@GeoffreyBooth
Copy link
Collaborator

See #4668. That PR would allow your example E.

@GeoffreyBooth GeoffreyBooth changed the title [CS2] Lexer: Backticks should allow adding code to class body Lexer: Backticks should allow adding code to class body Sep 8, 2017
connec pushed a commit to GeoffreyBooth/coffeescript that referenced this issue Sep 20, 2017
connec pushed a commit to GeoffreyBooth/coffeescript that referenced this issue Sep 20, 2017
connec added a commit to GeoffreyBooth/coffeescript that referenced this issue Sep 20, 2017
This uses more of the existing machinery for moving class body
expressions into the initializer.
connec added a commit to GeoffreyBooth/coffeescript that referenced this issue Sep 20, 2017
This uses more of the existing machinery for moving class body
expressions into the initializer.
@connec
Copy link
Collaborator

connec commented Sep 20, 2017

Closed prematurely.

@connec connec reopened this Sep 20, 2017
@GeoffreyBooth
Copy link
Collaborator

No worries. I think the comment should also say something along the lines of “class properties and class fields go here when they reach Stage 4.” I found such comments very helpful when updating things for CS2.

@GeoffreyBooth
Copy link
Collaborator

Fixed via #4668 and #4712.

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

3 participants