Allow super in class properties (#499)
This commit is contained in:
@@ -381,7 +381,11 @@ export default class ExpressionParser extends LValParser {
|
|||||||
|
|
||||||
switch (this.state.type) {
|
switch (this.state.type) {
|
||||||
case tt._super:
|
case tt._super:
|
||||||
if (!this.state.inMethod && !this.options.allowSuperOutsideMethod) {
|
if (
|
||||||
|
!this.state.inMethod &&
|
||||||
|
!this.state.inClassProperty &&
|
||||||
|
!this.options.allowSuperOutsideMethod
|
||||||
|
) {
|
||||||
this.raise(this.state.start, "'super' outside of function or class");
|
this.raise(this.state.start, "'super' outside of function or class");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -772,19 +772,23 @@ export default class StatementParser extends ExpressionParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parseClassProperty(node) {
|
parseClassProperty(node) {
|
||||||
|
const hasPlugin = this.hasPlugin("classProperties");
|
||||||
const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled.";
|
const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled.";
|
||||||
if (!node.typeAnnotation && !this.hasPlugin("classProperties")) {
|
if (!node.typeAnnotation && !hasPlugin) {
|
||||||
this.raise(node.start, noPluginMsg);
|
this.raise(node.start, noPluginMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.state.inClassProperty = true;
|
||||||
|
|
||||||
if (this.match(tt.eq)) {
|
if (this.match(tt.eq)) {
|
||||||
if (!this.hasPlugin("classProperties")) this.raise(this.state.start, noPluginMsg);
|
if (!hasPlugin) this.raise(this.state.start, noPluginMsg);
|
||||||
this.next();
|
this.next();
|
||||||
node.value = this.parseMaybeAssign();
|
node.value = this.parseMaybeAssign();
|
||||||
} else {
|
} else {
|
||||||
node.value = null;
|
node.value = null;
|
||||||
}
|
}
|
||||||
this.semicolon();
|
this.semicolon();
|
||||||
|
this.state.inClassProperty = false;
|
||||||
return this.finishNode(node, "ClassProperty");
|
return this.finishNode(node, "ClassProperty");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export default class State {
|
|||||||
this.inAsync =
|
this.inAsync =
|
||||||
this.inPropertyName =
|
this.inPropertyName =
|
||||||
this.inType =
|
this.inType =
|
||||||
|
this.inClassProperty =
|
||||||
this.noAnonFunctionType =
|
this.noAnonFunctionType =
|
||||||
false;
|
false;
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ export default class State {
|
|||||||
inAsync: boolean;
|
inAsync: boolean;
|
||||||
inType: boolean;
|
inType: boolean;
|
||||||
inPropertyName: boolean;
|
inPropertyName: boolean;
|
||||||
|
inClassProperty: boolean;
|
||||||
|
|
||||||
// Labels in scope.
|
// Labels in scope.
|
||||||
labels: Array<Object>;
|
labels: Array<Object>;
|
||||||
|
|||||||
3
test/fixtures/experimental/class-properties/super/actual.js
vendored
Normal file
3
test/fixtures/experimental/class-properties/super/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
class Fails extends class { c(){} } {
|
||||||
|
c = super.c();
|
||||||
|
}
|
||||||
272
test/fixtures/experimental/class-properties/super/expected.json
vendored
Normal file
272
test/fixtures/experimental/class-properties/super/expected.json
vendored
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 56,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 56,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "script",
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 56,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 6,
|
||||||
|
"end": 11,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 11
|
||||||
|
},
|
||||||
|
"identifierName": "Fails"
|
||||||
|
},
|
||||||
|
"name": "Fails"
|
||||||
|
},
|
||||||
|
"superClass": {
|
||||||
|
"type": "ClassExpression",
|
||||||
|
"start": 20,
|
||||||
|
"end": 35,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 20
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 35
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": null,
|
||||||
|
"superClass": null,
|
||||||
|
"body": {
|
||||||
|
"type": "ClassBody",
|
||||||
|
"start": 26,
|
||||||
|
"end": 35,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 26
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 35
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassMethod",
|
||||||
|
"start": 28,
|
||||||
|
"end": 33,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 28
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 33
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"static": false,
|
||||||
|
"computed": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 28,
|
||||||
|
"end": 29,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 28
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 29
|
||||||
|
},
|
||||||
|
"identifierName": "c"
|
||||||
|
},
|
||||||
|
"name": "c"
|
||||||
|
},
|
||||||
|
"kind": "method",
|
||||||
|
"id": null,
|
||||||
|
"generator": false,
|
||||||
|
"expression": false,
|
||||||
|
"async": false,
|
||||||
|
"params": [],
|
||||||
|
"body": {
|
||||||
|
"type": "BlockStatement",
|
||||||
|
"start": 31,
|
||||||
|
"end": 33,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 31
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 33
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"body": [],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
"type": "ClassBody",
|
||||||
|
"start": 36,
|
||||||
|
"end": 56,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 36
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ClassProperty",
|
||||||
|
"start": 40,
|
||||||
|
"end": 54,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 2
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 16
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"static": false,
|
||||||
|
"computed": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 40,
|
||||||
|
"end": 41,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 2
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 3
|
||||||
|
},
|
||||||
|
"identifierName": "c"
|
||||||
|
},
|
||||||
|
"name": "c"
|
||||||
|
},
|
||||||
|
"value": {
|
||||||
|
"type": "CallExpression",
|
||||||
|
"start": 44,
|
||||||
|
"end": 53,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 15
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"callee": {
|
||||||
|
"type": "MemberExpression",
|
||||||
|
"start": 44,
|
||||||
|
"end": 51,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 13
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"object": {
|
||||||
|
"type": "Super",
|
||||||
|
"start": 44,
|
||||||
|
"end": 49,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 11
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"property": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 50,
|
||||||
|
"end": 51,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 12
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 13
|
||||||
|
},
|
||||||
|
"identifierName": "c"
|
||||||
|
},
|
||||||
|
"name": "c"
|
||||||
|
},
|
||||||
|
"computed": false
|
||||||
|
},
|
||||||
|
"arguments": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
3
test/fixtures/experimental/class-properties/super/options.json
vendored
Normal file
3
test/fixtures/experimental/class-properties/super/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["classProperties"]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user