Class AST: bound/computed properties, executable body (#5208)

* executable class body aST

* tests for bound/computed

* remove

* computed
This commit is contained in:
Julian Rosse
2019-04-29 16:42:36 -04:00
committed by Geoffrey Booth
parent 7b2fb18a0b
commit 391fcc4afa
4 changed files with 265 additions and 39 deletions

View File

@@ -3981,6 +3981,9 @@
}
}
}
if (!o.compiling) {
return;
}
if (initializer.length !== expressions.length) {
this.body.expressions = (function() {
var l, len3, results;
@@ -4339,7 +4342,7 @@
key: this.name.ast(o, LEVEL_LIST),
value: this.value.ast(o, LEVEL_LIST),
static: !!this.isStatic,
computed: false,
computed: this.name instanceof ComputedPropertyName,
operator: (ref1 = (ref2 = this.operatorToken) != null ? ref2.value : void 0) != null ? ref1 : '=',
staticClassName: (ref3 = (ref4 = this.staticClassName) != null ? ref4.ast(o) : void 0) != null ? ref3 : null
};
@@ -5775,10 +5778,11 @@
return {
static: !!this.isStatic,
key: this.name.ast(o),
computed: false,
computed: this.name instanceof ComputedPropertyName || this.name.name instanceof ComputedPropertyName,
kind: this.ctor ? 'constructor' : 'method',
operator: (ref1 = (ref2 = this.operatorToken) != null ? ref2.value : void 0) != null ? ref1 : '=',
staticClassName: (ref3 = (ref4 = this.isStatic.staticClassName) != null ? ref4.ast(o) : void 0) != null ? ref3 : null
staticClassName: (ref3 = (ref4 = this.isStatic.staticClassName) != null ? ref4.ast(o) : void 0) != null ? ref3 : null,
bound: !!this.bound
};
}

View File

@@ -2668,6 +2668,7 @@ exports.Class = class Class extends Base
else if method.bound
@boundMethods.push method
return unless o.compiling
if initializer.length isnt expressions.length
@body.expressions = (expression.hoist() for expression in initializer)
new Block expressions
@@ -2924,7 +2925,7 @@ exports.ClassProperty = class ClassProperty extends Base
key: @name.ast o, LEVEL_LIST
value: @value.ast o, LEVEL_LIST
static: !!@isStatic
computed: no
computed: @name instanceof ComputedPropertyName
operator: @operatorToken?.value ? '='
staticClassName: @staticClassName?.ast(o) ? null
@@ -3881,7 +3882,7 @@ exports.Code = class Code extends Base
return
static: !!@isStatic
key: @name.ast o
computed: no
computed: @name instanceof ComputedPropertyName or @name.name instanceof ComputedPropertyName
kind:
if @ctor
'constructor'
@@ -3889,6 +3890,7 @@ exports.Code = class Code extends Base
'method'
operator: @operatorToken?.value ? '='
staticClassName: @isStatic.staticClassName?.ast(o) ? null
bound: !!@bound
astProperties: (o) ->
return Object.assign

View File

@@ -1328,12 +1328,15 @@ test "AST as expected for Class node", ->
async: no
params: []
body: EMPTY_BLOCK
bound: no
]
testExpression '''
a = class A
b: ->
c
d: =>
e
''',
type: 'AssignmentExpression'
right:
@@ -1359,14 +1362,34 @@ test "AST as expected for Class node", ->
expression: ID 'c'
]
operator: ':'
bound: no
,
type: 'ClassMethod'
static: no
key: ID 'd'
computed: no
kind: 'method'
id: null
generator: no
async: no
params: []
body:
type: 'BlockStatement'
body: [
type: 'ExpressionStatement'
expression: ID 'e'
]
operator: ':'
bound: yes
]
testStatement '''
class A
@b: ->
@c = ->
@c = =>
@d: 1
@e = 2
j = 5
A.f = 3
A.g = ->
this.h = ->
@@ -1392,6 +1415,7 @@ test "AST as expected for Class node", ->
staticClassName:
type: 'ThisExpression'
shorthand: yes
bound: no
,
type: 'ClassMethod'
static: yes
@@ -1407,6 +1431,7 @@ test "AST as expected for Class node", ->
staticClassName:
type: 'ThisExpression'
shorthand: yes
bound: yes
,
type: 'ClassProperty'
static: yes
@@ -1427,6 +1452,12 @@ test "AST as expected for Class node", ->
staticClassName:
type: 'ThisExpression'
shorthand: yes
,
type: 'ExpressionStatement'
expression:
type: 'AssignmentExpression'
left: ID 'j'
right: NUMBER 5
,
type: 'ClassProperty'
static: yes
@@ -1448,6 +1479,7 @@ test "AST as expected for Class node", ->
body: EMPTY_BLOCK
operator: '='
staticClassName: ID 'A'
bound: no
,
type: 'ClassMethod'
static: yes
@@ -1463,6 +1495,7 @@ test "AST as expected for Class node", ->
staticClassName:
type: 'ThisExpression'
shorthand: no
bound: no
,
type: 'ClassProperty'
static: yes
@@ -1479,6 +1512,9 @@ test "AST as expected for Class node", ->
class A
b: 1
[c]: 2
[d]: ->
@[e]: ->
@[f]: 3
''',
type: 'ClassDeclaration'
id: ID 'A'
@@ -1495,41 +1531,47 @@ test "AST as expected for Class node", ->
key: ID 'c'
value: NUMBER 2
computed: yes
,
type: 'ClassMethod'
static: no
key: ID 'd'
computed: yes
kind: 'method'
id: null
generator: no
async: no
params: []
body: EMPTY_BLOCK
operator: ':'
bound: no
,
type: 'ClassMethod'
static: yes
key: ID 'e'
computed: yes
kind: 'method'
id: null
generator: no
async: no
params: []
body: EMPTY_BLOCK
operator: ':'
bound: no
staticClassName:
type: 'ThisExpression'
shorthand: yes
,
type: 'ClassProperty'
static: yes
key: ID 'f'
computed: yes
value: NUMBER 3
operator: ':'
staticClassName:
type: 'ThisExpression'
shorthand: yes
]
# test "AST as expected for ExecutableClassBody node", ->
# code = """
# class Klass
# privateStatic = if 42 then yes else no
# getPrivateStatic: -> privateStatic
# """
# testExpression code,
# type: 'Class'
# variable:
# value: 'Klass'
# body:
# type: 'Block'
# expressions: [
# type: 'Assign'
# variable:
# value: 'privateStatic'
# value:
# type: 'If'
# ,
# type: 'Obj'
# generated: yes
# properties: [
# type: 'Assign'
# variable:
# value: 'getPrivateStatic'
# value:
# type: 'Code'
# body:
# type: 'Value'
# properties: []
# ]
# ]
test "AST as expected for ModuleDeclaration node", ->
testStatement 'export {X}',
type: 'ExportNamedDeclaration'

View File

@@ -7105,3 +7105,181 @@ test "AST as expected for Class node", ->
end:
line: 3
column: 8
testAstLocationData '''
class A
@[b]: 1
@[c]: ->
''',
type: 'ClassDeclaration'
body:
body: [
key:
start: 12
end: 13
range: [12, 13]
loc:
start:
line: 2
column: 4
end:
line: 2
column: 5
staticClassName:
start: 10
end: 11
range: [10, 11]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 3
value:
start: 16
end: 17
range: [16, 17]
loc:
start:
line: 2
column: 8
end:
line: 2
column: 9
start: 10
end: 17
range: [10, 17]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 9
,
key:
start: 22
end: 23
range: [22, 23]
loc:
start:
line: 3
column: 4
end:
line: 3
column: 5
staticClassName:
start: 20
end: 21
range: [20, 21]
loc:
start:
line: 3
column: 2
end:
line: 3
column: 3
start: 20
end: 28
range: [20, 28]
loc:
start:
line: 3
column: 2
end:
line: 3
column: 10
]
start: 8
end: 28
range: [8, 28]
loc:
start:
line: 2
column: 0
end:
line: 3
column: 10
start: 0
end: 28
range: [0, 28]
loc:
start:
line: 1
column: 0
end:
line: 3
column: 10
testAstLocationData '''
class A
b = 1
''',
type: 'ClassDeclaration'
body:
body: [
expression:
left:
start: 10
end: 11
range: [10, 11]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 3
right:
start: 14
end: 15
range: [14, 15]
loc:
start:
line: 2
column: 6
end:
line: 2
column: 7
start: 10
end: 15
range: [10, 15]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 7
start: 10
end: 15
range: [10, 15]
loc:
start:
line: 2
column: 2
end:
line: 2
column: 7
]
start: 8
end: 15
range: [8, 15]
loc:
start:
line: 2
column: 0
end:
line: 2
column: 7
start: 0
end: 15
range: [0, 15]
loc:
start:
line: 1
column: 0
end:
line: 2
column: 7