Skip to content

Class prototype property AST #5205

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

Merged
merged 3 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 48 additions & 1 deletion lib/coffeescript/nodes.js

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

27 changes: 27 additions & 0 deletions 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 @@ -2911,6 +2924,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