Skip to content

Commit 27eff5c

Browse files
connecGeoffreyBooth
authored andcommitted
Fix #4464: backticked expressions in class body (#4712)
* 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
1 parent 5cbd25f commit 27eff5c

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

lib/coffeescript/nodes.js

+16-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

+16-3
Original file line numberDiff line numberDiff line change
@@ -1752,10 +1752,23 @@ exports.Class = class Class extends Base
17521752

17531753
# Add an expression to the class initializer
17541754
#
1755-
# NOTE Currently, only methods and static methods are valid in ES class initializers.
1756-
# When additional expressions become valid, this method should be updated to handle them.
1755+
# This is the key method for determining whether an expression in a class
1756+
# body should appear in the initializer or the executable body. If the given
1757+
# `node` is valid in a class body the method will return a (new, modified,
1758+
# or identical) node for inclusion in the class initializer, otherwise
1759+
# nothing will be returned and the node will appear in the executable body.
1760+
#
1761+
# At time of writing, only methods (instance and static) are valid in ES
1762+
# class initializers. As new ES class features (such as class fields) reach
1763+
# Stage 4, this method will need to be updated to support them. We
1764+
# additionally allow `PassthroughLiteral`s (backticked expressions) in the
1765+
# initializer as an escape hatch for ES features that are not implemented
1766+
# (e.g. getters and setters defined via the `get` and `set` keywords as
1767+
# opposed to the `Object.defineProperty` method).
17571768
addInitializerExpression: (node) ->
1758-
if @validInitializerMethod node
1769+
if node.unwrapAll() instanceof PassthroughLiteral
1770+
node
1771+
else if @validInitializerMethod node
17591772
@addInitializerMethod node
17601773
else
17611774
null

test/classes.coffee

+15
Original file line numberDiff line numberDiff line change
@@ -1833,3 +1833,18 @@ test "#4591: super.x.y, super['x'].y", ->
18331833
eq 2, b.t
18341834
eq 2, b.s
18351835
eq 2, b.r
1836+
1837+
test "#4464: backticked expressions in class body", ->
1838+
class A
1839+
`get x() { return 42; }`
1840+
1841+
class B
1842+
`get x() { return 42; }`
1843+
constructor: ->
1844+
@y = 84
1845+
1846+
a = new A
1847+
eq 42, a.x
1848+
b = new B
1849+
eq 42, b.x
1850+
eq 84, b.y

0 commit comments

Comments
 (0)