Disallow super in functions in class properties

This commit is contained in:
Nicolò Ribaudo 2018-04-12 14:58:25 +02:00
parent ec882be44d
commit f797454a18
6 changed files with 219 additions and 0 deletions

View File

@ -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;
}

View File

@ -0,0 +1,3 @@
class A {
foo = () => super.x();
}

View File

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

View File

@ -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": []
}
}

View File

@ -0,0 +1,5 @@
class A {
foo = function fn() {
super.x();
}
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["classProperties"],
"throws": "super is only allowed in object methods and classes (3:4)"
}