From b5e23835ce6875605078adc4c8142a1214fc3007 Mon Sep 17 00:00:00 2001 From: Peeyush Kushwaha Date: Sat, 17 Jun 2017 18:12:14 +0530 Subject: [PATCH] Disallow exports between decorator & class body For stage 2 decorators --- src/parser/statement.js | 4 + .../export-decorators-on-class/expected.json | 116 --------- .../export-decorators-on-class/options.json | 3 +- .../actual.js | 7 - .../expected.json | 240 ------------------ .../options.json | 3 - .../actual.js | 2 - .../options.json | 4 - .../no-export-decorators/actual.js | 2 - .../no-export-decorators/options.json | 4 - 10 files changed, 6 insertions(+), 379 deletions(-) delete mode 100644 test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/expected.json delete mode 100644 test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/actual.js delete mode 100644 test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/expected.json delete mode 100644 test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/options.json delete mode 100644 test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/actual.js delete mode 100644 test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/options.json delete mode 100644 test/fixtures/experimental/decorators-stage-2/no-export-decorators/actual.js delete mode 100644 test/fixtures/experimental/decorators-stage-2/no-export-decorators/options.json diff --git a/src/parser/statement.js b/src/parser/statement.js index 361b251a98..726ed65922 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -157,6 +157,10 @@ export default class StatementParser extends ExpressionParser { } parseDecorators(allowExport?: boolean): void { + if (this.hasPlugin("decorators-stage-2")) { + allowExport = false; + } + while (this.match(tt.at)) { const decorator = this.parseDecorator(); this.state.decorators.push(decorator); diff --git a/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/expected.json b/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/expected.json deleted file mode 100644 index 6933de60e7..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/expected.json +++ /dev/null @@ -1,116 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 23 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 23 - } - }, - "sourceType": "module", - "body": [ - { - "type": "ExportDefaultDeclaration", - "start": 5, - "end": 28, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 23 - } - }, - "declaration": { - "type": "ClassDeclaration", - "start": 20, - "end": 28, - "loc": { - "start": { - "line": 2, - "column": 15 - }, - "end": { - "line": 2, - "column": 23 - } - }, - "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": "foo" - }, - "name": "foo" - } - } - ], - "id": null, - "superClass": null, - "body": { - "type": "ClassBody", - "start": 26, - "end": 28, - "loc": { - "start": { - "line": 2, - "column": 21 - }, - "end": { - "line": 2, - "column": 23 - } - }, - "body": [] - } - } - } - ], - "directives": [] - } -} \ No newline at end of file diff --git a/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/options.json b/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/options.json index 2104ca4328..43922816e1 100644 --- a/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/options.json +++ b/test/fixtures/experimental/decorators-stage-2/export-decorators-on-class/options.json @@ -1,3 +1,4 @@ { - "sourceType": "module" + "sourceType": "module", + "throws": "Leading decorators must be attached to a class declaration (2:0)" } diff --git a/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/actual.js b/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/actual.js deleted file mode 100644 index d0d8d6c4ec..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/actual.js +++ /dev/null @@ -1,7 +0,0 @@ -@ParentDecorator -export default class ParentClass { - makeNestedClass() { - class NestedClass { - } - } -} diff --git a/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/expected.json b/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/expected.json deleted file mode 100644 index 2c797decfc..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/expected.json +++ /dev/null @@ -1,240 +0,0 @@ -{ - "type": "File", - "start": 0, - "end": 109, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 7, - "column": 1 - } - }, - "program": { - "type": "Program", - "start": 0, - "end": 109, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 7, - "column": 1 - } - }, - "sourceType": "module", - "body": [ - { - "type": "ExportDefaultDeclaration", - "start": 17, - "end": 109, - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 7, - "column": 1 - } - }, - "declaration": { - "type": "ClassDeclaration", - "start": 32, - "end": 109, - "loc": { - "start": { - "line": 2, - "column": 15 - }, - "end": { - "line": 7, - "column": 1 - } - }, - "decorators": [ - { - "type": "Decorator", - "start": 0, - "end": 16, - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 16 - } - }, - "expression": { - "type": "Identifier", - "start": 1, - "end": 16, - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 16 - }, - "identifierName": "ParentDecorator" - }, - "name": "ParentDecorator" - } - } - ], - "id": { - "type": "Identifier", - "start": 38, - "end": 49, - "loc": { - "start": { - "line": 2, - "column": 21 - }, - "end": { - "line": 2, - "column": 32 - }, - "identifierName": "ParentClass" - }, - "name": "ParentClass" - }, - "superClass": null, - "body": { - "type": "ClassBody", - "start": 50, - "end": 109, - "loc": { - "start": { - "line": 2, - "column": 33 - }, - "end": { - "line": 7, - "column": 1 - } - }, - "body": [ - { - "type": "ClassMethod", - "start": 54, - "end": 107, - "loc": { - "start": { - "line": 3, - "column": 2 - }, - "end": { - "line": 6, - "column": 3 - } - }, - "static": false, - "computed": false, - "key": { - "type": "Identifier", - "start": 54, - "end": 69, - "loc": { - "start": { - "line": 3, - "column": 2 - }, - "end": { - "line": 3, - "column": 17 - }, - "identifierName": "makeNestedClass" - }, - "name": "makeNestedClass" - }, - "kind": "method", - "id": null, - "generator": false, - "expression": false, - "async": false, - "params": [], - "body": { - "type": "BlockStatement", - "start": 72, - "end": 107, - "loc": { - "start": { - "line": 3, - "column": 20 - }, - "end": { - "line": 6, - "column": 3 - } - }, - "body": [ - { - "type": "ClassDeclaration", - "start": 78, - "end": 103, - "loc": { - "start": { - "line": 4, - "column": 4 - }, - "end": { - "line": 5, - "column": 5 - } - }, - "id": { - "type": "Identifier", - "start": 84, - "end": 95, - "loc": { - "start": { - "line": 4, - "column": 10 - }, - "end": { - "line": 4, - "column": 21 - }, - "identifierName": "NestedClass" - }, - "name": "NestedClass" - }, - "superClass": null, - "body": { - "type": "ClassBody", - "start": 96, - "end": 103, - "loc": { - "start": { - "line": 4, - "column": 22 - }, - "end": { - "line": 5, - "column": 5 - } - }, - "body": [] - } - } - ], - "directives": [] - } - } - ] - } - } - } - ], - "directives": [] - } -} \ No newline at end of file diff --git a/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/options.json b/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/options.json deleted file mode 100644 index 2104ca4328..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/export-default-with-nested-class/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "sourceType": "module" -} diff --git a/test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/actual.js b/test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/actual.js deleted file mode 100644 index c5a55d643f..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -@foo -export default function f(){}; \ No newline at end of file diff --git a/test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/options.json b/test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/options.json deleted file mode 100644 index 61c2c23daa..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/no-export-decorators-without-class/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "throws": "You can only use decorators on an export when exporting a class (2:0)", - "sourceType": "module" -} diff --git a/test/fixtures/experimental/decorators-stage-2/no-export-decorators/actual.js b/test/fixtures/experimental/decorators-stage-2/no-export-decorators/actual.js deleted file mode 100644 index 4630d555a4..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/no-export-decorators/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -@foo -export default 0; \ No newline at end of file diff --git a/test/fixtures/experimental/decorators-stage-2/no-export-decorators/options.json b/test/fixtures/experimental/decorators-stage-2/no-export-decorators/options.json deleted file mode 100644 index 61c2c23daa..0000000000 --- a/test/fixtures/experimental/decorators-stage-2/no-export-decorators/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "throws": "You can only use decorators on an export when exporting a class (2:0)", - "sourceType": "module" -}