From f9a6cfb04de5ff80ea18ddd942e0168ab6f6b633 Mon Sep 17 00:00:00 2001 From: Shuhei Kagawa Date: Fri, 11 Mar 2016 00:09:18 +0900 Subject: [PATCH] parse parameter decorators --- src/parser/lval.js | 7 + .../experimental/decorators/options.json | 3 + .../decorators/parameter-decorator/actual.js | 3 + .../parameter-decorator/expected.json | 1160 +++++++++++++++++ 4 files changed, 1173 insertions(+) create mode 100644 test/fixtures/experimental/decorators/options.json create mode 100644 test/fixtures/experimental/decorators/parameter-decorator/actual.js create mode 100644 test/fixtures/experimental/decorators/parameter-decorator/expected.json diff --git a/src/parser/lval.js b/src/parser/lval.js index fe3e6bb34e..e3b485eb71 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -162,7 +162,14 @@ pp.parseBindingList = function (close, allowEmpty, allowTrailingComma) { this.expect(close); break; } else { + let decorators = []; + while (this.match(tt.at)) { + decorators.push(this.parseDecorator()); + } let left = this.parseMaybeDefault(); + if (decorators.length) { + left.decorators = decorators; + } this.parseAssignableListItemTypes(left); elts.push(this.parseMaybeDefault(null, null, left)); } diff --git a/test/fixtures/experimental/decorators/options.json b/test/fixtures/experimental/decorators/options.json new file mode 100644 index 0000000000..d760b0afda --- /dev/null +++ b/test/fixtures/experimental/decorators/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["decorators"] +} diff --git a/test/fixtures/experimental/decorators/parameter-decorator/actual.js b/test/fixtures/experimental/decorators/parameter-decorator/actual.js new file mode 100644 index 0000000000..c2d7fd2405 --- /dev/null +++ b/test/fixtures/experimental/decorators/parameter-decorator/actual.js @@ -0,0 +1,3 @@ +class Foo { + constructor(@foo() x, @bar({ a: 123 }) @baz() y) {} +} diff --git a/test/fixtures/experimental/decorators/parameter-decorator/expected.json b/test/fixtures/experimental/decorators/parameter-decorator/expected.json new file mode 100644 index 0000000000..139cd1c1a9 --- /dev/null +++ b/test/fixtures/experimental/decorators/parameter-decorator/expected.json @@ -0,0 +1,1160 @@ +{ + "type": "File", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 10, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "name": "constructor" + }, + "static": false, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "name": "x", + "decorators": [ + { + "type": "Decorator", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "expression": { + "type": "CallExpression", + "start": 27, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "callee": { + "type": "Identifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "name": "foo" + }, + "arguments": [] + } + } + ] + }, + { + "type": "Identifier", + "start": 60, + "end": 61, + "loc": { + "start": { + "line": 2, + "column": 48 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "name": "y", + "decorators": [ + { + "type": "Decorator", + "start": 36, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 40 + } + }, + "expression": { + "type": "CallExpression", + "start": 37, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 40 + } + }, + "callee": { + "type": "Identifier", + "start": 37, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "name": "bar" + }, + "arguments": [ + { + "type": "ObjectExpression", + "start": 41, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 43, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 43, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 32 + } + }, + "name": "a" + }, + "value": { + "type": "NumericLiteral", + "start": 46, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "extra": { + "rawValue": 123, + "raw": "123" + }, + "value": 123 + } + } + ] + } + ] + } + }, + { + "type": "Decorator", + "start": 53, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 47 + } + }, + "expression": { + "type": "CallExpression", + "start": 54, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 47 + } + }, + "callee": { + "type": "Identifier", + "start": 54, + "end": 57, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "name": "baz" + }, + "arguments": [] + } + } + ] + } + ], + "body": { + "type": "BlockStatement", + "start": 63, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 51 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [], + "tokens": [ + { + "type": { + "label": "class", + "keyword": "class", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": "class", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "Foo", + "start": 6, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": { + "label": "{", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "constructor", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 13 + } + } + }, + { + "type": { + "label": "(", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + { + "type": { + "label": "@", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "foo", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + } + }, + { + "type": { + "label": "(", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + { + "type": { + "label": ")", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 31, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "x", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + } + }, + { + "type": { + "label": ",", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 23 + } + } + }, + { + "type": { + "label": "@", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "bar", + "start": 37, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 28 + } + } + }, + { + "type": { + "label": "(", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + } + } + }, + { + "type": { + "label": "{", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 30 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "a", + "start": 43, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 32 + } + } + }, + { + "type": { + "label": ":", + "beforeExpr": true, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 33 + } + } + }, + { + "type": { + "label": "num", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "value": 123, + "start": 46, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 37 + } + } + }, + { + "type": { + "label": "}", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 50, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 38 + }, + "end": { + "line": 2, + "column": 39 + } + } + }, + { + "type": { + "label": ")", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + { + "type": { + "label": "@", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start": 53, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 42 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "baz", + "start": 54, + "end": 57, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 45 + } + } + }, + { + "type": { + "label": "(", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 45 + }, + "end": { + "line": 2, + "column": 46 + } + } + }, + { + "type": { + "label": ")", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 46 + }, + "end": { + "line": 2, + "column": 47 + } + } + }, + { + "type": { + "label": "name", + "beforeExpr": false, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "value": "y", + "start": 60, + "end": 61, + "loc": { + "start": { + "line": 2, + "column": 48 + }, + "end": { + "line": 2, + "column": 49 + } + } + }, + { + "type": { + "label": ")", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 61, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 49 + }, + "end": { + "line": 2, + "column": 50 + } + } + }, + { + "type": { + "label": "{", + "beforeExpr": true, + "startsExpr": true, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 63, + "end": 64, + "loc": { + "start": { + "line": 2, + "column": 51 + }, + "end": { + "line": 2, + "column": 52 + } + } + }, + { + "type": { + "label": "}", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 64, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 52 + }, + "end": { + "line": 2, + "column": 53 + } + } + }, + { + "type": { + "label": "}", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null + }, + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + { + "type": { + "label": "eof", + "beforeExpr": false, + "startsExpr": false, + "rightAssociative": false, + "isLoop": false, + "isAssign": false, + "prefix": false, + "postfix": false, + "binop": null, + "updateContext": null + }, + "start": 68, + "end": 68, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + } + } + ] +}