white lists getter and setter acceseors for having stage-2 decorators (#766)

- adds `get` and `set` kind in addition to `method` to the list of allowed class members for having a decorator,
- adds tests for this two cases (decorator + set and decorator + get)
This commit is contained in:
M Behzad 2017-10-31 16:13:04 +01:00 committed by Daniel Tschinder
parent 86abc16b37
commit fee7de2c1d
5 changed files with 375 additions and 1 deletions

View File

@ -899,7 +899,7 @@ export default class StatementParser extends ExpressionParser {
if (
this.hasPlugin("decorators2") &&
member.kind != "method" &&
["method", "get", "set"].indexOf(member.kind) === -1 &&
member.decorators &&
member.decorators.length > 0
) {

View File

@ -0,0 +1,3 @@
class A {
@foo get getter(){}
}

View File

@ -0,0 +1,175 @@
{
"type": "File",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 21
}
},
"decorators": [
{
"type": "Decorator",
"start": 12,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 6
}
},
"expression": {
"type": "Identifier",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "foo"
},
"name": "foo"
}
}
],
"static": false,
"key": {
"type": "Identifier",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 17
},
"identifierName": "getter"
},
"name": "getter"
},
"computed": false,
"kind": "get",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 29,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 19
},
"end": {
"line": 2,
"column": 21
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
class A {
@foo set setter(_val){}
}

View File

@ -0,0 +1,193 @@
{
"type": "File",
"start": 0,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 37,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 25
}
},
"decorators": [
{
"type": "Decorator",
"start": 12,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 6
}
},
"expression": {
"type": "Identifier",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "foo"
},
"name": "foo"
}
}
],
"static": false,
"key": {
"type": "Identifier",
"start": 21,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 17
},
"identifierName": "setter"
},
"name": "setter"
},
"computed": false,
"kind": "set",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 28,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 22
},
"identifierName": "_val"
},
"name": "_val"
}
],
"body": {
"type": "BlockStatement",
"start": 33,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 23
},
"end": {
"line": 2,
"column": 25
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}