From f797454a186e5c1d01574bf26a037d60532ca597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 12 Apr 2018 14:58:25 +0200 Subject: [PATCH] Disallow super in functions in class properties --- packages/babylon/src/parser/statement.js | 3 + .../super-inside-arrow-function/input.js | 3 + .../super-inside-arrow-function/options.json | 3 + .../super-inside-arrow-function/output.json | 201 ++++++++++++++++++ .../super-inside-function/input.js | 5 + .../super-inside-function/options.json | 4 + 6 files changed, 219 insertions(+) create mode 100644 packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/input.js create mode 100644 packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/options.json create mode 100644 packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/output.json create mode 100644 packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/input.js create mode 100644 packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/options.json diff --git a/packages/babylon/src/parser/statement.js b/packages/babylon/src/parser/statement.js index 06de629e04..ce4722a437 100644 --- a/packages/babylon/src/parser/statement.js +++ b/packages/babylon/src/parser/statement.js @@ -814,8 +814,10 @@ export default class StatementParser extends ExpressionParser { const oldInFunc = this.state.inFunction; const oldInMethod = this.state.inMethod; const oldInGenerator = this.state.inGenerator; + const oldInClassProperty = this.state.inClassProperty; this.state.inFunction = true; this.state.inMethod = false; + this.state.inClassProperty = false; this.initFunction(node, isAsync); @@ -860,6 +862,7 @@ export default class StatementParser extends ExpressionParser { this.state.inFunction = oldInFunc; this.state.inMethod = oldInMethod; this.state.inGenerator = oldInGenerator; + this.state.inClassProperty = oldInClassProperty; return node; } diff --git a/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/input.js b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/input.js new file mode 100644 index 0000000000..6cc3a4087c --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/input.js @@ -0,0 +1,3 @@ +class A { + foo = () => super.x(); +} diff --git a/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/options.json b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/options.json new file mode 100644 index 0000000000..9c27576d4a --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties"] +} diff --git a/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/output.json b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/output.json new file mode 100644 index 0000000000..decf9e8a8b --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-arrow-function/output.json @@ -0,0 +1,201 @@ +{ + "type": "File", + "start": 0, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 36, + "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": 36, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 12, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 12, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "computed": false, + "value": { + "type": "ArrowFunctionExpression", + "start": 18, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "CallExpression", + "start": 24, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "callee": { + "type": "MemberExpression", + "start": 24, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "object": { + "type": "Super", + "start": 24, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + "property": { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 21 + }, + "identifierName": "x" + }, + "name": "x" + }, + "computed": false + }, + "arguments": [] + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/input.js b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/input.js new file mode 100644 index 0000000000..fd5e5b8c97 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/input.js @@ -0,0 +1,5 @@ +class A { + foo = function fn() { + super.x(); + } +} diff --git a/packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/options.json b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/options.json new file mode 100644 index 0000000000..386c2968e7 --- /dev/null +++ b/packages/babylon/test/fixtures/experimental/class-properties/super-inside-function/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["classProperties"], + "throws": "super is only allowed in object methods and classes (3:4)" +}