Skip to content

Commit

Permalink
fix: Throws on new foo?.bar!() (#15439)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Feb 21, 2023
1 parent 61be17e commit a2fdc20
Show file tree
Hide file tree
Showing 17 changed files with 628 additions and 75 deletions.
24 changes: 9 additions & 15 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -765,10 +765,15 @@ export default abstract class ExpressionParser extends LValParser {
let optional = false;

if (type === tt.questionDot) {
if (noCalls && this.lookaheadCharCode() === charCodes.leftParenthesis) {
// stop at `?.` when parsing `new a?.()`
state.stop = true;
return base;
if (noCalls) {
this.raise(Errors.OptionalChainingNoNew, {
at: this.state.startLoc,
});
if (this.lookaheadCharCode() === charCodes.leftParenthesis) {
// stop at `?.` when parsing `new a?.()`
state.stop = true;
return base;
}
}
state.optionalChainMember = optional = true;
this.next();
Expand Down Expand Up @@ -1919,17 +1924,6 @@ export default abstract class ExpressionParser extends LValParser {
node.callee = this.parseNoCallExpr();
if (node.callee.type === "Import") {
this.raise(Errors.ImportCallNotNewExpression, { at: node.callee });
} else if (
this.isOptionalChain(node.callee) &&
!node.callee.extra?.parenthesized
) {
this.raise(Errors.OptionalChainingNoNew, {
at: this.state.lastTokEndLoc,
});
} else if (this.eat(tt.questionDot)) {
this.raise(Errors.OptionalChainingNoNew, {
at: this.state.startLoc,
});
}
}

Expand Down
7 changes: 0 additions & 7 deletions packages/babel-parser/src/parser/util.ts
Expand Up @@ -290,13 +290,6 @@ export default abstract class UtilParser extends Tokenizer {
);
}

isOptionalChain(node: Node): boolean {
return (
node.type === "OptionalMemberExpression" ||
node.type === "OptionalCallExpression"
);
}

isObjectProperty(
node: Node,
): node is ObjectProperty | EstreePropertyDefinition {
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -539,10 +539,6 @@ export default (superClass: typeof Parser) =>
return super.hasPropertyAsPrivateName(node);
}

isOptionalChain(node: N.Node): boolean {
return node.type === "ChainExpression";
}

// @ts-expect-error override interfaces
isObjectProperty(node: N.Node): boolean {
return node.type === "Property" && node.kind === "init" && !node.method;
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":12,"index":12}},
"errors": [
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (1:10)"
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (1:5)"
],
"program": {
"type": "Program",
Expand Down
@@ -1 +1,5 @@
new foo?.bar();
new foo.bar?.();
new foo?.[0];

new (foo?.bar)();
@@ -1,3 +1,3 @@
{
"createParenthesizedExpressions": true
"createParenthesizedExpressions": false
}
@@ -1,36 +1,125 @@
{
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}},
"start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":17,"index":65}},
"errors": [
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (1:7)",
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (2:11)",
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (3:7)"
],
"program": {
"type": "Program",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}},
"start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":17,"index":65}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}},
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"expression": {
"type": "NewExpression",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":16,"index":16}},
"start":0,"end":14,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":14,"index":14}},
"callee": {
"type": "ParenthesizedExpression",
"start":4,"end":14,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":14,"index":14}},
"expression": {
"type": "OptionalMemberExpression",
"start":5,"end":13,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":13,"index":13}},
"type": "OptionalMemberExpression",
"start":4,"end":12,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":12,"index":12}},
"object": {
"type": "Identifier",
"start":4,"end":7,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":7,"index":7},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12},"identifierName":"bar"},
"name": "bar"
},
"optional": true
},
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":16,"end":32,"loc":{"start":{"line":2,"column":0,"index":16},"end":{"line":2,"column":16,"index":32}},
"expression": {
"type": "OptionalCallExpression",
"start":16,"end":31,"loc":{"start":{"line":2,"column":0,"index":16},"end":{"line":2,"column":15,"index":31}},
"callee": {
"type": "NewExpression",
"start":16,"end":27,"loc":{"start":{"line":2,"column":0,"index":16},"end":{"line":2,"column":11,"index":27}},
"callee": {
"type": "MemberExpression",
"start":20,"end":27,"loc":{"start":{"line":2,"column":4,"index":20},"end":{"line":2,"column":11,"index":27}},
"object": {
"type": "Identifier",
"start":5,"end":8,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":8,"index":8},"identifierName":"foo"},
"start":20,"end":23,"loc":{"start":{"line":2,"column":4,"index":20},"end":{"line":2,"column":7,"index":23},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":13,"index":13},"identifierName":"bar"},
"start":24,"end":27,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":11,"index":27},"identifierName":"bar"},
"name": "bar"
}
},
"arguments": []
},
"optional": true,
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":33,"end":46,"loc":{"start":{"line":3,"column":0,"index":33},"end":{"line":3,"column":13,"index":46}},
"expression": {
"type": "NewExpression",
"start":33,"end":45,"loc":{"start":{"line":3,"column":0,"index":33},"end":{"line":3,"column":12,"index":45}},
"callee": {
"type": "OptionalMemberExpression",
"start":37,"end":45,"loc":{"start":{"line":3,"column":4,"index":37},"end":{"line":3,"column":12,"index":45}},
"object": {
"type": "Identifier",
"start":37,"end":40,"loc":{"start":{"line":3,"column":4,"index":37},"end":{"line":3,"column":7,"index":40},"identifierName":"foo"},
"name": "foo"
},
"computed": true,
"property": {
"type": "NumericLiteral",
"start":43,"end":44,"loc":{"start":{"line":3,"column":10,"index":43},"end":{"line":3,"column":11,"index":44}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"optional": true
"value": 0
},
"optional": true
},
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":48,"end":65,"loc":{"start":{"line":5,"column":0,"index":48},"end":{"line":5,"column":17,"index":65}},
"expression": {
"type": "NewExpression",
"start":48,"end":64,"loc":{"start":{"line":5,"column":0,"index":48},"end":{"line":5,"column":16,"index":64}},
"callee": {
"type": "OptionalMemberExpression",
"start":53,"end":61,"loc":{"start":{"line":5,"column":5,"index":53},"end":{"line":5,"column":13,"index":61}},
"object": {
"type": "Identifier",
"start":53,"end":56,"loc":{"start":{"line":5,"column":5,"index":53},"end":{"line":5,"column":8,"index":56},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":58,"end":61,"loc":{"start":{"line":5,"column":10,"index":58},"end":{"line":5,"column":13,"index":61},"identifierName":"bar"},
"name": "bar"
},
"optional": true,
"extra": {
"parenthesized": true,
"parenStart": 52
}
},
"arguments": []
Expand Down
@@ -1,4 +1,5 @@
new foo?.bar();
new foo.bar?.();
new foo?.[0];

new (foo?.bar)();
@@ -1,3 +1,3 @@
{
"createParenthesizedExpressions": false
"createParenthesizedExpressions": true
}
@@ -1,13 +1,14 @@
{
"type": "File",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":17,"index":51}},
"start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":17,"index":65}},
"errors": [
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (1:12)",
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (2:13)"
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (1:7)",
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (2:11)",
"SyntaxError: Constructors in/after an Optional Chain are not allowed. (3:7)"
],
"program": {
"type": "Program",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":17,"index":51}},
"start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":17,"index":65}},
"sourceType": "script",
"interpreter": null,
"body": [
Expand Down Expand Up @@ -40,50 +41,85 @@
"type": "ExpressionStatement",
"start":16,"end":32,"loc":{"start":{"line":2,"column":0,"index":16},"end":{"line":2,"column":16,"index":32}},
"expression": {
"type": "NewExpression",
"type": "OptionalCallExpression",
"start":16,"end":31,"loc":{"start":{"line":2,"column":0,"index":16},"end":{"line":2,"column":15,"index":31}},
"callee": {
"type": "MemberExpression",
"start":20,"end":27,"loc":{"start":{"line":2,"column":4,"index":20},"end":{"line":2,"column":11,"index":27}},
"object": {
"type": "Identifier",
"start":20,"end":23,"loc":{"start":{"line":2,"column":4,"index":20},"end":{"line":2,"column":7,"index":23},"identifierName":"foo"},
"name": "foo"
"type": "NewExpression",
"start":16,"end":27,"loc":{"start":{"line":2,"column":0,"index":16},"end":{"line":2,"column":11,"index":27}},
"callee": {
"type": "MemberExpression",
"start":20,"end":27,"loc":{"start":{"line":2,"column":4,"index":20},"end":{"line":2,"column":11,"index":27}},
"object": {
"type": "Identifier",
"start":20,"end":23,"loc":{"start":{"line":2,"column":4,"index":20},"end":{"line":2,"column":7,"index":23},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":24,"end":27,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":11,"index":27},"identifierName":"bar"},
"name": "bar"
}
},
"computed": false,
"property": {
"type": "Identifier",
"start":24,"end":27,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":11,"index":27},"identifierName":"bar"},
"name": "bar"
}
"arguments": []
},
"optional": true,
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":34,"end":51,"loc":{"start":{"line":4,"column":0,"index":34},"end":{"line":4,"column":17,"index":51}},
"start":33,"end":46,"loc":{"start":{"line":3,"column":0,"index":33},"end":{"line":3,"column":13,"index":46}},
"expression": {
"type": "NewExpression",
"start":34,"end":50,"loc":{"start":{"line":4,"column":0,"index":34},"end":{"line":4,"column":16,"index":50}},
"start":33,"end":45,"loc":{"start":{"line":3,"column":0,"index":33},"end":{"line":3,"column":12,"index":45}},
"callee": {
"type": "OptionalMemberExpression",
"start":39,"end":47,"loc":{"start":{"line":4,"column":5,"index":39},"end":{"line":4,"column":13,"index":47}},
"start":37,"end":45,"loc":{"start":{"line":3,"column":4,"index":37},"end":{"line":3,"column":12,"index":45}},
"object": {
"type": "Identifier",
"start":39,"end":42,"loc":{"start":{"line":4,"column":5,"index":39},"end":{"line":4,"column":8,"index":42},"identifierName":"foo"},
"start":37,"end":40,"loc":{"start":{"line":3,"column":4,"index":37},"end":{"line":3,"column":7,"index":40},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"computed": true,
"property": {
"type": "Identifier",
"start":44,"end":47,"loc":{"start":{"line":4,"column":10,"index":44},"end":{"line":4,"column":13,"index":47},"identifierName":"bar"},
"name": "bar"
"type": "NumericLiteral",
"start":43,"end":44,"loc":{"start":{"line":3,"column":10,"index":43},"end":{"line":3,"column":11,"index":44}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
},
"optional": true,
"extra": {
"parenthesized": true,
"parenStart": 38
"optional": true
},
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":48,"end":65,"loc":{"start":{"line":5,"column":0,"index":48},"end":{"line":5,"column":17,"index":65}},
"expression": {
"type": "NewExpression",
"start":48,"end":64,"loc":{"start":{"line":5,"column":0,"index":48},"end":{"line":5,"column":16,"index":64}},
"callee": {
"type": "ParenthesizedExpression",
"start":52,"end":62,"loc":{"start":{"line":5,"column":4,"index":52},"end":{"line":5,"column":14,"index":62}},
"expression": {
"type": "OptionalMemberExpression",
"start":53,"end":61,"loc":{"start":{"line":5,"column":5,"index":53},"end":{"line":5,"column":13,"index":61}},
"object": {
"type": "Identifier",
"start":53,"end":56,"loc":{"start":{"line":5,"column":5,"index":53},"end":{"line":5,"column":8,"index":56},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":58,"end":61,"loc":{"start":{"line":5,"column":10,"index":58},"end":{"line":5,"column":13,"index":61},"identifierName":"bar"},
"name": "bar"
},
"optional": true
}
},
"arguments": []
Expand Down
@@ -0,0 +1,7 @@
new foo?.bar();
new foo.bar?.();
new foo?.bar!()
new foo?.[0];
new foo?.bar![0];

new (foo?.bar)();
@@ -0,0 +1,4 @@
{
"createParenthesizedExpressions": false,
"plugins": ["typescript"]
}

0 comments on commit a2fdc20

Please sign in to comment.