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

Class prototype property AST #5205

Merged
merged 3 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions lib/coffeescript/nodes.js

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

33 changes: 32 additions & 1 deletion src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,8 @@ exports.Class = class Class extends Base
@addInitializerMethod node
else if not o.compiling and @validClassProperty node
@addClassProperty node
else if not o.compiling and @validClassPrototypeProperty node
@addClassPrototypeProperty node
else
null

Expand Down Expand Up @@ -2736,6 +2738,17 @@ exports.Class = class Class extends Base
operatorToken
}).withLocationDataFrom assign

validClassPrototypeProperty: (node) ->
return no unless node instanceof Assign
node.context is 'object' and not node.variable.hasProperties()

addClassPrototypeProperty: (assign) ->
{variable, value} = assign
new ClassPrototypeProperty({
name: variable.base
value
}).withLocationDataFrom assign

makeDefaultConstructor: ->
ctor = @addInitializerMethod new Assign (new Value new PropertyName 'constructor'), new Code
@body.unshift ctor
Expand Down Expand Up @@ -2883,7 +2896,11 @@ exports.ExecutableClassBody = class ExecutableClassBody extends Base
# The class scope is not available yet, so return the assignment to update later
assign = @externalCtor = new Assign new Value, value
else if not assign.variable.this
name = new (if base.shouldCache() then Index else Access) base
name =
if base instanceof ComputedPropertyName
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for #5204

new Index base.value
else
new (if base.shouldCache() then Index else Access) base
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the conditional logic here (so just new Access base) no tests break. But I don't feel completely confident removing it. I traced the code's origin back to #4354

prototype = new Access new PropertyName 'prototype'
variable = new Value new ThisLiteral(), [ prototype, name ]

Expand Down Expand Up @@ -2911,6 +2928,20 @@ exports.ClassProperty = class ClassProperty extends Base
operator: @operatorToken?.value ? '='
staticClassName: @staticClassName?.ast(o) ? null

exports.ClassPrototypeProperty = class ClassPrototypeProperty extends Base
constructor: ({@name, @value}) ->
super()

children: ['name', 'value']

isStatement: YES

astProperties: (o) ->
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mimics the properties of Babel AST types like ClassProperty/ObjectProperty

return
key: @name.ast o, LEVEL_LIST
value: @value.ast o, LEVEL_LIST
computed: @name instanceof ComputedPropertyName

#### Import and Export

exports.ModuleDeclaration = class ModuleDeclaration extends Base
Expand Down
22 changes: 22 additions & 0 deletions test/abstract_syntax_tree.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,28 @@ test "AST as expected for Class node", ->
shorthand: no
]

testStatement '''
class A
b: 1
[c]: 2
''',
type: 'ClassDeclaration'
id: ID 'A'
superClass: null
body:
type: 'ClassBody'
body: [
type: 'ClassPrototypeProperty'
key: ID 'b'
value: NUMBER 1
computed: no
,
type: 'ClassPrototypeProperty'
key: ID 'c'
value: NUMBER 2
computed: yes
]

# test "AST as expected for ExecutableClassBody node", ->
# code = """
# class Klass
Expand Down
95 changes: 95 additions & 0 deletions test/abstract_syntax_tree_location_data.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7010,3 +7010,98 @@ test "AST as expected for Class node", ->
end:
line: 9
column: 12

testAstLocationData '''
class A
b: 1
[c]: 2
''',
type: 'ClassDeclaration'
body:
body: [
key:
start: 10
end: 11
range: [10, 11]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 3
value:
start: 13
end: 14
range: [13, 14]
loc:
start:
line: 2
column: 5
end:
line: 2
column: 6
start: 10
end: 14
range: [10, 14]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 6
,
key:
start: 18
end: 19
range: [18, 19]
loc:
start:
line: 3
column: 3
end:
line: 3
column: 4
value:
start: 22
end: 23
range: [22, 23]
loc:
start:
line: 3
column: 7
end:
line: 3
column: 8
start: 17
end: 23
range: [17, 23]
loc:
start:
line: 3
column: 2
end:
line: 3
column: 8
]
start: 8
end: 23
range: [8, 23]
loc:
start:
line: 2
column: 0
end:
line: 3
column: 8
start: 0
end: 23
range: [0, 23]
loc:
start:
line: 1
column: 0
end:
line: 3
column: 8
8 changes: 8 additions & 0 deletions test/classes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1930,3 +1930,11 @@ test "#5085: Bug: @ reference to class not maintained in do block", ->

eq thisFoo, 'foo assigned in class'
eq thisBar, 'foo assigned in class'

test "#5204: Computed class property", ->
foo = 'bar'
class A
[foo]: 'baz'
a = new A()
eq a.bar, 'baz'
eq A::bar, 'baz'