Disallow super() in class properties

This commit is contained in:
Nicolò Ribaudo 2018-04-12 21:58:34 +02:00
parent a62cfe9045
commit a86d14de61
5 changed files with 28 additions and 0 deletions

View File

@ -1294,10 +1294,13 @@ export default class StatementParser extends ExpressionParser {
parseClassPrivateProperty(
node: N.ClassPrivateProperty,
): N.ClassPrivateProperty {
const oldInMethod = this.state.inMethod;
this.state.inMethod = false;
this.state.inClassProperty = true;
node.value = this.eat(tt.eq) ? this.parseMaybeAssign() : null;
this.semicolon();
this.state.inClassProperty = false;
this.state.inMethod = oldInMethod;
return this.finishNode(node, "ClassPrivateProperty");
}
@ -1306,6 +1309,8 @@ export default class StatementParser extends ExpressionParser {
this.expectPlugin("classProperties");
}
const oldInMethod = this.state.inMethod;
this.state.inMethod = false;
this.state.inClassProperty = true;
if (this.match(tt.eq)) {
@ -1317,6 +1322,7 @@ export default class StatementParser extends ExpressionParser {
}
this.semicolon();
this.state.inClassProperty = false;
this.state.inMethod = oldInMethod;
return this.finishNode(node, "ClassProperty");
}

View File

@ -0,0 +1,7 @@
class A extends B {
constructor() {
class C extends D {
#foo = super();
}
}
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["classPrivateProperties"],
"throws": "super() is only valid inside a class constructor. Make sure the method name is spelled exactly as 'constructor'. (4:13)"
}

View File

@ -0,0 +1,7 @@
class A extends B {
constructor() {
class C extends D {
foo = super();
}
}
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["classProperties"],
"throws": "super() is only valid inside a class constructor. Make sure the method name is spelled exactly as 'constructor'. (4:12)"
}