Skip to content

Commit 7b2fb18

Browse files
helixbassGeoffreyBooth
authored andcommitted
Class prototype property AST (#5205)
* class prototype property AST * consistent naming * extract fix for #5204
1 parent 5596a5c commit 7b2fb18

File tree

4 files changed

+192
-1
lines changed

4 files changed

+192
-1
lines changed

lib/coffeescript/nodes.js

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,8 @@ exports.Class = class Class extends Base
26942694
@addInitializerMethod node
26952695
else if not o.compiling and @validClassProperty node
26962696
@addClassProperty node
2697+
else if not o.compiling and @validClassPrototypeProperty node
2698+
@addClassPrototypeProperty node
26972699
else
26982700
null
26992701

@@ -2736,6 +2738,17 @@ exports.Class = class Class extends Base
27362738
operatorToken
27372739
}).withLocationDataFrom assign
27382740

2741+
validClassPrototypeProperty: (node) ->
2742+
return no unless node instanceof Assign
2743+
node.context is 'object' and not node.variable.hasProperties()
2744+
2745+
addClassPrototypeProperty: (assign) ->
2746+
{variable, value} = assign
2747+
new ClassPrototypeProperty({
2748+
name: variable.base
2749+
value
2750+
}).withLocationDataFrom assign
2751+
27392752
makeDefaultConstructor: ->
27402753
ctor = @addInitializerMethod new Assign (new Value new PropertyName 'constructor'), new Code
27412754
@body.unshift ctor
@@ -2915,6 +2928,20 @@ exports.ClassProperty = class ClassProperty extends Base
29152928
operator: @operatorToken?.value ? '='
29162929
staticClassName: @staticClassName?.ast(o) ? null
29172930

2931+
exports.ClassPrototypeProperty = class ClassPrototypeProperty extends Base
2932+
constructor: ({@name, @value}) ->
2933+
super()
2934+
2935+
children: ['name', 'value']
2936+
2937+
isStatement: YES
2938+
2939+
astProperties: (o) ->
2940+
return
2941+
key: @name.ast o, LEVEL_LIST
2942+
value: @value.ast o, LEVEL_LIST
2943+
computed: @name instanceof ComputedPropertyName
2944+
29182945
#### Import and Export
29192946

29202947
exports.ModuleDeclaration = class ModuleDeclaration extends Base

test/abstract_syntax_tree.coffee

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,28 @@ test "AST as expected for Class node", ->
14751475
shorthand: no
14761476
]
14771477

1478+
testStatement '''
1479+
class A
1480+
b: 1
1481+
[c]: 2
1482+
''',
1483+
type: 'ClassDeclaration'
1484+
id: ID 'A'
1485+
superClass: null
1486+
body:
1487+
type: 'ClassBody'
1488+
body: [
1489+
type: 'ClassPrototypeProperty'
1490+
key: ID 'b'
1491+
value: NUMBER 1
1492+
computed: no
1493+
,
1494+
type: 'ClassPrototypeProperty'
1495+
key: ID 'c'
1496+
value: NUMBER 2
1497+
computed: yes
1498+
]
1499+
14781500
# test "AST as expected for ExecutableClassBody node", ->
14791501
# code = """
14801502
# class Klass

test/abstract_syntax_tree_location_data.coffee

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7010,3 +7010,98 @@ test "AST as expected for Class node", ->
70107010
end:
70117011
line: 9
70127012
column: 12
7013+
7014+
testAstLocationData '''
7015+
class A
7016+
b: 1
7017+
[c]: 2
7018+
''',
7019+
type: 'ClassDeclaration'
7020+
body:
7021+
body: [
7022+
key:
7023+
start: 10
7024+
end: 11
7025+
range: [10, 11]
7026+
loc:
7027+
start:
7028+
line: 2
7029+
column: 2
7030+
end:
7031+
line: 2
7032+
column: 3
7033+
value:
7034+
start: 13
7035+
end: 14
7036+
range: [13, 14]
7037+
loc:
7038+
start:
7039+
line: 2
7040+
column: 5
7041+
end:
7042+
line: 2
7043+
column: 6
7044+
start: 10
7045+
end: 14
7046+
range: [10, 14]
7047+
loc:
7048+
start:
7049+
line: 2
7050+
column: 2
7051+
end:
7052+
line: 2
7053+
column: 6
7054+
,
7055+
key:
7056+
start: 18
7057+
end: 19
7058+
range: [18, 19]
7059+
loc:
7060+
start:
7061+
line: 3
7062+
column: 3
7063+
end:
7064+
line: 3
7065+
column: 4
7066+
value:
7067+
start: 22
7068+
end: 23
7069+
range: [22, 23]
7070+
loc:
7071+
start:
7072+
line: 3
7073+
column: 7
7074+
end:
7075+
line: 3
7076+
column: 8
7077+
start: 17
7078+
end: 23
7079+
range: [17, 23]
7080+
loc:
7081+
start:
7082+
line: 3
7083+
column: 2
7084+
end:
7085+
line: 3
7086+
column: 8
7087+
]
7088+
start: 8
7089+
end: 23
7090+
range: [8, 23]
7091+
loc:
7092+
start:
7093+
line: 2
7094+
column: 0
7095+
end:
7096+
line: 3
7097+
column: 8
7098+
start: 0
7099+
end: 23
7100+
range: [0, 23]
7101+
loc:
7102+
start:
7103+
line: 1
7104+
column: 0
7105+
end:
7106+
line: 3
7107+
column: 8

0 commit comments

Comments
 (0)