Skip to content

Commit 391fcc4

Browse files
helixbassGeoffreyBooth
authored andcommitted
Class AST: bound/computed properties, executable body (#5208)
* executable class body aST * tests for bound/computed * remove * computed
1 parent 7b2fb18 commit 391fcc4

File tree

4 files changed

+265
-39
lines changed

4 files changed

+265
-39
lines changed

lib/coffeescript/nodes.js

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,7 @@ exports.Class = class Class extends Base
26682668
else if method.bound
26692669
@boundMethods.push method
26702670

2671+
return unless o.compiling
26712672
if initializer.length isnt expressions.length
26722673
@body.expressions = (expression.hoist() for expression in initializer)
26732674
new Block expressions
@@ -2924,7 +2925,7 @@ exports.ClassProperty = class ClassProperty extends Base
29242925
key: @name.ast o, LEVEL_LIST
29252926
value: @value.ast o, LEVEL_LIST
29262927
static: !!@isStatic
2927-
computed: no
2928+
computed: @name instanceof ComputedPropertyName
29282929
operator: @operatorToken?.value ? '='
29292930
staticClassName: @staticClassName?.ast(o) ? null
29302931

@@ -3881,14 +3882,15 @@ exports.Code = class Code extends Base
38813882
return
38823883
static: !!@isStatic
38833884
key: @name.ast o
3884-
computed: no
3885+
computed: @name instanceof ComputedPropertyName or @name.name instanceof ComputedPropertyName
38853886
kind:
38863887
if @ctor
38873888
'constructor'
38883889
else
38893890
'method'
38903891
operator: @operatorToken?.value ? '='
38913892
staticClassName: @isStatic.staticClassName?.ast(o) ? null
3893+
bound: !!@bound
38923894

38933895
astProperties: (o) ->
38943896
return Object.assign

test/abstract_syntax_tree.coffee

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,12 +1328,15 @@ test "AST as expected for Class node", ->
13281328
async: no
13291329
params: []
13301330
body: EMPTY_BLOCK
1331+
bound: no
13311332
]
13321333

13331334
testExpression '''
13341335
a = class A
13351336
b: ->
13361337
c
1338+
d: =>
1339+
e
13371340
''',
13381341
type: 'AssignmentExpression'
13391342
right:
@@ -1359,14 +1362,34 @@ test "AST as expected for Class node", ->
13591362
expression: ID 'c'
13601363
]
13611364
operator: ':'
1365+
bound: no
1366+
,
1367+
type: 'ClassMethod'
1368+
static: no
1369+
key: ID 'd'
1370+
computed: no
1371+
kind: 'method'
1372+
id: null
1373+
generator: no
1374+
async: no
1375+
params: []
1376+
body:
1377+
type: 'BlockStatement'
1378+
body: [
1379+
type: 'ExpressionStatement'
1380+
expression: ID 'e'
1381+
]
1382+
operator: ':'
1383+
bound: yes
13621384
]
13631385

13641386
testStatement '''
13651387
class A
13661388
@b: ->
1367-
@c = ->
1389+
@c = =>
13681390
@d: 1
13691391
@e = 2
1392+
j = 5
13701393
A.f = 3
13711394
A.g = ->
13721395
this.h = ->
@@ -1392,6 +1415,7 @@ test "AST as expected for Class node", ->
13921415
staticClassName:
13931416
type: 'ThisExpression'
13941417
shorthand: yes
1418+
bound: no
13951419
,
13961420
type: 'ClassMethod'
13971421
static: yes
@@ -1407,6 +1431,7 @@ test "AST as expected for Class node", ->
14071431
staticClassName:
14081432
type: 'ThisExpression'
14091433
shorthand: yes
1434+
bound: yes
14101435
,
14111436
type: 'ClassProperty'
14121437
static: yes
@@ -1427,6 +1452,12 @@ test "AST as expected for Class node", ->
14271452
staticClassName:
14281453
type: 'ThisExpression'
14291454
shorthand: yes
1455+
,
1456+
type: 'ExpressionStatement'
1457+
expression:
1458+
type: 'AssignmentExpression'
1459+
left: ID 'j'
1460+
right: NUMBER 5
14301461
,
14311462
type: 'ClassProperty'
14321463
static: yes
@@ -1448,6 +1479,7 @@ test "AST as expected for Class node", ->
14481479
body: EMPTY_BLOCK
14491480
operator: '='
14501481
staticClassName: ID 'A'
1482+
bound: no
14511483
,
14521484
type: 'ClassMethod'
14531485
static: yes
@@ -1463,6 +1495,7 @@ test "AST as expected for Class node", ->
14631495
staticClassName:
14641496
type: 'ThisExpression'
14651497
shorthand: no
1498+
bound: no
14661499
,
14671500
type: 'ClassProperty'
14681501
static: yes
@@ -1479,6 +1512,9 @@ test "AST as expected for Class node", ->
14791512
class A
14801513
b: 1
14811514
[c]: 2
1515+
[d]: ->
1516+
@[e]: ->
1517+
@[f]: 3
14821518
''',
14831519
type: 'ClassDeclaration'
14841520
id: ID 'A'
@@ -1495,41 +1531,47 @@ test "AST as expected for Class node", ->
14951531
key: ID 'c'
14961532
value: NUMBER 2
14971533
computed: yes
1534+
,
1535+
type: 'ClassMethod'
1536+
static: no
1537+
key: ID 'd'
1538+
computed: yes
1539+
kind: 'method'
1540+
id: null
1541+
generator: no
1542+
async: no
1543+
params: []
1544+
body: EMPTY_BLOCK
1545+
operator: ':'
1546+
bound: no
1547+
,
1548+
type: 'ClassMethod'
1549+
static: yes
1550+
key: ID 'e'
1551+
computed: yes
1552+
kind: 'method'
1553+
id: null
1554+
generator: no
1555+
async: no
1556+
params: []
1557+
body: EMPTY_BLOCK
1558+
operator: ':'
1559+
bound: no
1560+
staticClassName:
1561+
type: 'ThisExpression'
1562+
shorthand: yes
1563+
,
1564+
type: 'ClassProperty'
1565+
static: yes
1566+
key: ID 'f'
1567+
computed: yes
1568+
value: NUMBER 3
1569+
operator: ':'
1570+
staticClassName:
1571+
type: 'ThisExpression'
1572+
shorthand: yes
14981573
]
14991574

1500-
# test "AST as expected for ExecutableClassBody node", ->
1501-
# code = """
1502-
# class Klass
1503-
# privateStatic = if 42 then yes else no
1504-
# getPrivateStatic: -> privateStatic
1505-
# """
1506-
# testExpression code,
1507-
# type: 'Class'
1508-
# variable:
1509-
# value: 'Klass'
1510-
# body:
1511-
# type: 'Block'
1512-
# expressions: [
1513-
# type: 'Assign'
1514-
# variable:
1515-
# value: 'privateStatic'
1516-
# value:
1517-
# type: 'If'
1518-
# ,
1519-
# type: 'Obj'
1520-
# generated: yes
1521-
# properties: [
1522-
# type: 'Assign'
1523-
# variable:
1524-
# value: 'getPrivateStatic'
1525-
# value:
1526-
# type: 'Code'
1527-
# body:
1528-
# type: 'Value'
1529-
# properties: []
1530-
# ]
1531-
# ]
1532-
15331575
test "AST as expected for ModuleDeclaration node", ->
15341576
testStatement 'export {X}',
15351577
type: 'ExportNamedDeclaration'

0 commit comments

Comments
 (0)