Skip to content

Commit

Permalink
Fix #4464: backticked expressions in class body (#4712)
Browse files Browse the repository at this point in the history
* Fix #4464: backticked expressions in class body should be left in the body, not hoisted

* Fix #4464: backticked expressions in class body should be left in the body, not hoisted

* Simplify fix for #4464

This uses more of the existing machinery for moving class body
expressions into the initializer.

* Clarify the purpose of Class::addInitializerExpression

* Further clarify the purpose of Class::addInitializerExpression

* Add reference to class fields; format
 comment wrapping

* Reapply 1d3af8c, that got lost because of rebase/force-push shenanigans

* Updated output
  • Loading branch information
connec authored and GeoffreyBooth committed Sep 21, 2017
1 parent 5cbd25f commit 27eff5c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
19 changes: 16 additions & 3 deletions lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1752,10 +1752,23 @@ exports.Class = class Class extends Base

# Add an expression to the class initializer
#
# NOTE Currently, only methods and static methods are valid in ES class initializers.
# When additional expressions become valid, this method should be updated to handle them.
# This is the key method for determining whether an expression in a class
# body should appear in the initializer or the executable body. If the given
# `node` is valid in a class body the method will return a (new, modified,
# or identical) node for inclusion in the class initializer, otherwise
# nothing will be returned and the node will appear in the executable body.
#
# At time of writing, only methods (instance and static) are valid in ES
# class initializers. As new ES class features (such as class fields) reach
# Stage 4, this method will need to be updated to support them. We
# additionally allow `PassthroughLiteral`s (backticked expressions) in the
# initializer as an escape hatch for ES features that are not implemented
# (e.g. getters and setters defined via the `get` and `set` keywords as
# opposed to the `Object.defineProperty` method).
addInitializerExpression: (node) ->
if @validInitializerMethod node
if node.unwrapAll() instanceof PassthroughLiteral
node
else if @validInitializerMethod node
@addInitializerMethod node
else
null
Expand Down
15 changes: 15 additions & 0 deletions test/classes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1833,3 +1833,18 @@ test "#4591: super.x.y, super['x'].y", ->
eq 2, b.t
eq 2, b.s
eq 2, b.r

test "#4464: backticked expressions in class body", ->
class A
`get x() { return 42; }`

class B
`get x() { return 42; }`
constructor: ->
@y = 84

a = new A
eq 42, a.x
b = new B
eq 42, b.x
eq 84, b.y

0 comments on commit 27eff5c

Please sign in to comment.