Merge pull request #587 from peey/decorators-stage-2

Decorators Stage 2 Parsing
This commit is contained in:
Henry Zhu
2017-06-22 10:20:08 -04:00
committed by GitHub
48 changed files with 1572 additions and 4 deletions

View File

@@ -0,0 +1,2 @@
@foo('bar')
class Foo {}

View File

@@ -0,0 +1,154 @@
{
"type": "File",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"expression": {
"type": "CallExpression",
"start": 1,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 11
}
},
"callee": {
"type": "Identifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [
{
"type": "StringLiteral",
"start": 5,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 10
}
},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
]
}
}
],
"id": {
"type": "Identifier",
"start": 18,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 22,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 12
}
},
"body": []
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,4 @@
@abc
class Foo {
}

View File

@@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"expression": {
"type": "Identifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "abc"
},
"name": "abc"
}
}
],
"id": {
"type": "Identifier",
"start": 11,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 15,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 4,
"column": 1
}
},
"body": []
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,6 @@
var foo = @dec class Bar {
@baz
bam() {
f();
}
}

View File

@@ -0,0 +1,308 @@
{
"type": "File",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 6,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "foo"
},
"name": "foo"
},
"init": {
"type": "ClassExpression",
"start": 10,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 6,
"column": 1
}
},
"decorators": [
{
"type": "Decorator",
"start": 10,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 14
}
},
"expression": {
"type": "Identifier",
"start": 11,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 14
},
"identifierName": "dec"
},
"name": "dec"
}
}
],
"id": {
"type": "Identifier",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 24
},
"identifierName": "Bar"
},
"name": "Bar"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 25,
"end": 58,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 6,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 29,
"end": 56,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 5,
"column": 3
}
},
"decorators": [
{
"type": "Decorator",
"start": 29,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 6
}
},
"expression": {
"type": "Identifier",
"start": 30,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "baz"
},
"name": "baz"
}
}
],
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 36,
"end": 39,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 5
},
"identifierName": "bam"
},
"name": "bam"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 42,
"end": 56,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 5,
"column": 3
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 48,
"end": 52,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 8
}
},
"expression": {
"type": "CallExpression",
"start": 48,
"end": 51,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 7
}
},
"callee": {
"type": "Identifier",
"start": 48,
"end": 49,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 5
},
"identifierName": "f"
},
"name": "f"
},
"arguments": []
}
}
],
"directives": []
}
}
]
}
}
}
],
"kind": "var"
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
class A {
@dec *m(){}
}

View File

@@ -0,0 +1,175 @@
{
"type": "File",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 25,
"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": 25,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 13
}
},
"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": "dec"
},
"name": "dec"
}
}
],
"static": false,
"kind": "method",
"computed": false,
"key": {
"type": "Identifier",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "m"
},
"name": "m"
},
"id": null,
"generator": true,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 21,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 13
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,4 @@
class A {
@a.b.c.d(e, f)
m(){}
}

View File

@@ -0,0 +1,325 @@
{
"type": "File",
"start": 0,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"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": 36,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 7
}
},
"decorators": [
{
"type": "Decorator",
"start": 12,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"expression": {
"type": "CallExpression",
"start": 13,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 16
}
},
"callee": {
"type": "MemberExpression",
"start": 13,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 10
}
},
"object": {
"type": "MemberExpression",
"start": 13,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 8
}
},
"object": {
"type": "MemberExpression",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 6
}
},
"object": {
"type": "Identifier",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 4
},
"identifierName": "a"
},
"name": "a"
},
"property": {
"type": "Identifier",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "b"
},
"name": "b"
},
"computed": false
},
"property": {
"type": "Identifier",
"start": 17,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 8
},
"identifierName": "c"
},
"name": "c"
},
"computed": false
},
"property": {
"type": "Identifier",
"start": 19,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
},
"identifierName": "d"
},
"name": "d"
},
"computed": false
},
"arguments": [
{
"type": "Identifier",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "e"
},
"name": "e"
},
{
"type": "Identifier",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 15
},
"identifierName": "f"
},
"name": "f"
}
]
}
}
],
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 29,
"end": 30,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 3
},
"identifierName": "m"
},
"name": "m"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 32,
"end": 34,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 7
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,2 @@
export default
@bar class Foo { }

View File

@@ -0,0 +1,132 @@
{
"type": "File",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"program": {
"type": "Program",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"declaration": {
"type": "ClassExpression",
"start": 16,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"decorators": [
{
"type": "Decorator",
"start": 16,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 4
}
},
"expression": {
"type": "Identifier",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 4
},
"identifierName": "bar"
},
"name": "bar"
}
}
],
"id": {
"type": "Identifier",
"start": 27,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 14
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 31,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 18
}
},
"body": []
}
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@@ -0,0 +1,3 @@
class Foo {
constructor(@foo x) {}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators cannot be used to decorate parameters (2:14)"
}

View File

@@ -0,0 +1,3 @@
class A {
@dec name = 0
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["classProperties", "decorators2"],
"throws": "Stage 2 decorators may only be used with a class or a class method (2:2)"
}

View File

@@ -0,0 +1,6 @@
class Foo {
@bar[bizz]
abc() {
}
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["decorators2", "classProperties"],
"throws": "Stage 2 decorators may only be used with a class or a class method (2:2)"
}

View File

@@ -0,0 +1,4 @@
class Foo {
@abc
constructor(){}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "You can't attach decorators to a class constructor (2:2)"
}

View File

@@ -0,0 +1,2 @@
@foo
export default class {}

View File

@@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Leading decorators must be attached to a class declaration (2:0)"
}

View File

@@ -0,0 +1 @@
function func(@foo x) {}

View File

@@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators cannot be used to decorate parameters (1:14)"
}

View File

@@ -0,0 +1,3 @@
var obj = {
method(@foo x) {}
};

View File

@@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators cannot be used to decorate parameters (2:9)"
}

View File

@@ -0,0 +1,5 @@
var o = {
@baz
foo() {
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators disallow object literal property decorators (2:2)"
}

View File

@@ -0,0 +1,3 @@
class A {
@dec #name = 0
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["classProperties", "classPrivateProperties", "decorators2"],
"throws": "Stage 2 decorators may only be used with a class or a class method (2:2)"
}

View File

@@ -0,0 +1,4 @@
class A {
@a;
m(){}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Decorators must not be followed by a semicolon (2:5)"
}

View File

@@ -0,0 +1,3 @@
class A {
@dec static name = 0
}

View File

@@ -0,0 +1,4 @@
{
"plugins": ["classProperties", "decorators2"],
"throws": "Stage 2 decorators may only be used with a class or a class method (2:2)"
}

View File

@@ -0,0 +1,4 @@
class A {
@dec
['name'](){}
}

View File

@@ -0,0 +1,178 @@
{
"type": "File",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"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": 4,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 12,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 14
}
},
"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": "dec"
},
"name": "dec"
}
}
],
"static": false,
"computed": true,
"key": {
"type": "StringLiteral",
"start": 20,
"end": 26,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 9
}
},
"extra": {
"rawValue": "name",
"raw": "'name'"
},
"value": "name"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 29,
"end": 31,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 14
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,3 @@
{
"plugins": ["decorators2"]
}

View File

@@ -0,0 +1,2 @@
// we don't need any source code for this error to trigger,
// it should trigger just because both old and new decorator plugins exist together

View File

@@ -0,0 +1,4 @@
{
"plugins": ["decorators", "decorators2"],
"throws": "Cannot use decorators and decorators2 plugin together"
}

View File

@@ -0,0 +1,2 @@
@(bar.baz)
class Foo {}

View File

@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:1)"
}

View File

@@ -0,0 +1,2 @@
@foo().bar
class Baz {}

View File

@@ -0,0 +1,3 @@
{
"throws": "Leading decorators must be attached to a class declaration (1:6)"
}