Skip to content

Commit 9a256c7

Browse files
committed
fix: set top level super when AllowSuperOutsideMethod is true
1 parent 077bd3e commit 9a256c7

File tree

8 files changed

+130
-10
lines changed

8 files changed

+130
-10
lines changed

packages/babel-parser/src/parser/expression.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,16 +1525,9 @@ export default abstract class ExpressionParser extends LValParser {
15251525
parseSuper(): N.Super {
15261526
const node = this.startNode<N.Super>();
15271527
this.next(); // eat `super`
1528-
if (
1529-
this.match(tt.parenL) &&
1530-
!this.scope.allowDirectSuper &&
1531-
!(this.optionFlags & OptionFlags.AllowSuperOutsideMethod)
1532-
) {
1528+
if (this.match(tt.parenL) && !this.scope.allowDirectSuper) {
15331529
this.raise(Errors.SuperNotAllowed, node);
1534-
} else if (
1535-
!this.scope.allowSuper &&
1536-
!(this.optionFlags & OptionFlags.AllowSuperOutsideMethod)
1537-
) {
1530+
} else if (!this.scope.allowSuper) {
15381531
this.raise(Errors.UnexpectedSuper, node);
15391532
}
15401533

packages/babel-parser/src/parser/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ export default abstract class UtilParser extends Tokenizer {
369369
if (this.optionFlags & OptionFlags.AllowReturnOutsideFunction) {
370370
paramFlags |= ParamKind.PARAM_RETURN;
371371
}
372-
this.scope.enter(ScopeFlag.PROGRAM);
372+
let scopeFlags = ScopeFlag.PROGRAM;
373+
if (this.optionFlags & OptionFlags.AllowSuperOutsideMethod) {
374+
scopeFlags |= ScopeFlag.SUPER | ScopeFlag.DIRECT_SUPER;
375+
}
376+
this.scope.enter(scopeFlags);
373377
this.prodParam.enter(paramFlags);
374378
}
375379

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function f() {
2+
super();
3+
super.property;
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"allowSuperOutsideMethod": true
3+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":45}},
4+
"errors": [
5+
"SyntaxError: `super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class? (2:2)",
6+
"SyntaxError: 'super' is only allowed in object methods and classes. (3:2)"
7+
],
8+
"program": {
9+
"type": "Program",
10+
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":45}},
11+
"sourceType": "script",
12+
"interpreter": null,
13+
"body": [
14+
{
15+
"type": "FunctionDeclaration",
16+
"start":0,"end":45,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":1,"index":45}},
17+
"id": {
18+
"type": "Identifier",
19+
"start":9,"end":10,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":10,"index":10},"identifierName":"f"},
20+
"name": "f"
21+
},
22+
"generator": false,
23+
"async": false,
24+
"params": [],
25+
"body": {
26+
"type": "BlockStatement",
27+
"start":13,"end":45,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":4,"column":1,"index":45}},
28+
"body": [
29+
{
30+
"type": "ExpressionStatement",
31+
"start":17,"end":25,"loc":{"start":{"line":2,"column":2,"index":17},"end":{"line":2,"column":10,"index":25}},
32+
"expression": {
33+
"type": "CallExpression",
34+
"start":17,"end":24,"loc":{"start":{"line":2,"column":2,"index":17},"end":{"line":2,"column":9,"index":24}},
35+
"callee": {
36+
"type": "Super",
37+
"start":17,"end":22,"loc":{"start":{"line":2,"column":2,"index":17},"end":{"line":2,"column":7,"index":22}}
38+
},
39+
"arguments": []
40+
}
41+
},
42+
{
43+
"type": "ExpressionStatement",
44+
"start":28,"end":43,"loc":{"start":{"line":3,"column":2,"index":28},"end":{"line":3,"column":17,"index":43}},
45+
"expression": {
46+
"type": "MemberExpression",
47+
"start":28,"end":42,"loc":{"start":{"line":3,"column":2,"index":28},"end":{"line":3,"column":16,"index":42}},
48+
"object": {
49+
"type": "Super",
50+
"start":28,"end":33,"loc":{"start":{"line":3,"column":2,"index":28},"end":{"line":3,"column":7,"index":33}}
51+
},
52+
"computed": false,
53+
"property": {
54+
"type": "Identifier",
55+
"start":34,"end":42,"loc":{"start":{"line":3,"column":8,"index":34},"end":{"line":3,"column":16,"index":42},"identifierName":"property"},
56+
"name": "property"
57+
}
58+
}
59+
}
60+
],
61+
"directives": []
62+
}
63+
}
64+
],
65+
"directives": []
66+
}
67+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
super();
2+
super.property;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"allowSuperOutsideMethod": true
3+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":15,"index":24}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":15,"index":24}},
7+
"sourceType": "script",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ExpressionStatement",
12+
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
13+
"expression": {
14+
"type": "CallExpression",
15+
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}},
16+
"callee": {
17+
"type": "Super",
18+
"start":0,"end":5,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":5,"index":5}}
19+
},
20+
"arguments": []
21+
}
22+
},
23+
{
24+
"type": "ExpressionStatement",
25+
"start":9,"end":24,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":15,"index":24}},
26+
"expression": {
27+
"type": "MemberExpression",
28+
"start":9,"end":23,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":14,"index":23}},
29+
"object": {
30+
"type": "Super",
31+
"start":9,"end":14,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":5,"index":14}}
32+
},
33+
"computed": false,
34+
"property": {
35+
"type": "Identifier",
36+
"start":15,"end":23,"loc":{"start":{"line":2,"column":6,"index":15},"end":{"line":2,"column":14,"index":23},"identifierName":"property"},
37+
"name": "property"
38+
}
39+
}
40+
}
41+
],
42+
"directives": []
43+
}
44+
}

0 commit comments

Comments
 (0)