Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[babel 8] Require throw expression to be wrapped in parentheses #16207

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parse-error/standard-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ export default {
SuperNotAllowed:
"`super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?",
SuperPrivateField: "Private fields can't be accessed on super.",
ThrowExpressionRequireParentheses:
"`throw ...` expressions must be wrapped in parentheses.",
TrailingDecorator: "Decorators must be attached to a class element.",
TupleExpressionBarIncorrectEndSyntaxType:
"Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
Expand Down
33 changes: 30 additions & 3 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,17 +613,44 @@ export default abstract class ExpressionParser extends LValParser {
node.operator = this.state.value;
node.prefix = true;

if (this.match(tt._throw)) {
const isThrow = this.match(tt._throw);
let throwMissingParenthesesReported = false;
if (isThrow) {
this.expectPlugin("throwExpressions");
if (
// TODO: Uncomment for the next minor
// this.getPluginOption("throwExpressions", "requireParentheses") ||
process.env.BABEL_8_BREAKING &&
this.input.charCodeAt(this.state.lastTokStart) !==
charCodes.leftParenthesis
) {
this.raise(Errors.ThrowExpressionRequireParentheses, {
at: node,
});
throwMissingParenthesesReported = true;
}
}
const isDelete = this.match(tt._delete);

const isDelete = !isThrow && this.match(tt._delete);
this.next();

node.argument = this.parseMaybeUnary(null, true);

this.checkExpressionErrors(refExpressionErrors, true);

if (this.state.strict && isDelete) {
if (isThrow) {
if (
!throwMissingParenthesesReported &&
// TODO: Uncomment for the next minor
// this.getPluginOption("throwExpressions", "requireParentheses") ||
process.env.BABEL_8_BREAKING &&
!this.match(tt.parenR)
) {
this.raise(Errors.ThrowExpressionRequireParentheses, {
at: node,
});
}
} else if (isDelete && this.state.strict) {
const arg = node.argument;

if (arg.type === "Identifier") {
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-parser/src/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ export function validatePlugins(plugins: PluginList) {
" only supported value is '2023-07'.",
);
}

// TODO: Uncomment in the next minor
// if (hasPlugin(plugins, "throwExpressions")) {
// const requireParentheses = getPluginOption(
// plugins,
// "throwExpressions",
// "requireParentheses",
// );
// if (requireParentheses != null && typeof requireParentheses !== "boolean") {
// throw new Error("'requireParentheses' must be a boolean.");
// }
// }
}

// These plugins are defined using a mixin which extends the parser class.
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-parser/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ export type Plugin =
| "privateIn" // Enabled by default
| "regexpUnicodeSets" // Enabled by default
| "sourcePhaseImports"
| "throwExpressions"
| "topLevelAwait"
// TODO: Remove in the next minor
| "throwExpressions"
| "v8intrinsic"
| ParserPluginWithOptions[0];

Expand All @@ -52,6 +53,8 @@ export type ParserPluginWithOptions =
| ["optionalChainingAssign", { version: "2023-07" }]
| ["pipelineOperator", PipelineOperatorPluginOptions]
| ["recordAndTuple", RecordAndTuplePluginOptions]
// TODO: Uncomment for the next minor
//| ["throwExpressions", { requireParentheses?: boolean }]
| ["flow", FlowPluginOptions]
| ["typescript", TypeScriptPluginOptions];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function test() {
(throw 1, 2);
(3, throw 4);
f(throw 5, 6);
f(7, throw 8);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": ["throwExpressions"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"type": "File",
"start":0,"end":85,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":85}},
"program": {
"type": "Program",
"start":0,"end":85,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":85}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":85,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":85}},
"id": {
"type": "Identifier",
"start":9,"end":13,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":13,"index":13},"identifierName":"test"},
"name": "test"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":16,"end":85,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":6,"column":1,"index":85}},
"body": [
{
"type": "ExpressionStatement",
"start":20,"end":33,"loc":{"start":{"line":2,"column":2,"index":20},"end":{"line":2,"column":15,"index":33}},
"expression": {
"type": "SequenceExpression",
"start":21,"end":31,"loc":{"start":{"line":2,"column":3,"index":21},"end":{"line":2,"column":13,"index":31}},
"expressions": [
{
"type": "UnaryExpression",
"start":21,"end":28,"loc":{"start":{"line":2,"column":3,"index":21},"end":{"line":2,"column":10,"index":28}},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start":27,"end":28,"loc":{"start":{"line":2,"column":9,"index":27},"end":{"line":2,"column":10,"index":28}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
},
{
"type": "NumericLiteral",
"start":30,"end":31,"loc":{"start":{"line":2,"column":12,"index":30},"end":{"line":2,"column":13,"index":31}},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
],
"extra": {
"parenthesized": true,
"parenStart": 20
}
}
},
{
"type": "ExpressionStatement",
"start":36,"end":49,"loc":{"start":{"line":3,"column":2,"index":36},"end":{"line":3,"column":15,"index":49}},
"expression": {
"type": "SequenceExpression",
"start":37,"end":47,"loc":{"start":{"line":3,"column":3,"index":37},"end":{"line":3,"column":13,"index":47}},
"expressions": [
{
"type": "NumericLiteral",
"start":37,"end":38,"loc":{"start":{"line":3,"column":3,"index":37},"end":{"line":3,"column":4,"index":38}},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
},
{
"type": "UnaryExpression",
"start":40,"end":47,"loc":{"start":{"line":3,"column":6,"index":40},"end":{"line":3,"column":13,"index":47}},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start":46,"end":47,"loc":{"start":{"line":3,"column":12,"index":46},"end":{"line":3,"column":13,"index":47}},
"extra": {
"rawValue": 4,
"raw": "4"
},
"value": 4
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 36
}
}
},
{
"type": "ExpressionStatement",
"start":52,"end":66,"loc":{"start":{"line":4,"column":2,"index":52},"end":{"line":4,"column":16,"index":66}},
"expression": {
"type": "CallExpression",
"start":52,"end":65,"loc":{"start":{"line":4,"column":2,"index":52},"end":{"line":4,"column":15,"index":65}},
"callee": {
"type": "Identifier",
"start":52,"end":53,"loc":{"start":{"line":4,"column":2,"index":52},"end":{"line":4,"column":3,"index":53},"identifierName":"f"},
"name": "f"
},
"arguments": [
{
"type": "UnaryExpression",
"start":54,"end":61,"loc":{"start":{"line":4,"column":4,"index":54},"end":{"line":4,"column":11,"index":61}},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start":60,"end":61,"loc":{"start":{"line":4,"column":10,"index":60},"end":{"line":4,"column":11,"index":61}},
"extra": {
"rawValue": 5,
"raw": "5"
},
"value": 5
}
},
{
"type": "NumericLiteral",
"start":63,"end":64,"loc":{"start":{"line":4,"column":13,"index":63},"end":{"line":4,"column":14,"index":64}},
"extra": {
"rawValue": 6,
"raw": "6"
},
"value": 6
}
]
}
},
{
"type": "ExpressionStatement",
"start":69,"end":83,"loc":{"start":{"line":5,"column":2,"index":69},"end":{"line":5,"column":16,"index":83}},
"expression": {
"type": "CallExpression",
"start":69,"end":82,"loc":{"start":{"line":5,"column":2,"index":69},"end":{"line":5,"column":15,"index":82}},
"callee": {
"type": "Identifier",
"start":69,"end":70,"loc":{"start":{"line":5,"column":2,"index":69},"end":{"line":5,"column":3,"index":70},"identifierName":"f"},
"name": "f"
},
"arguments": [
{
"type": "NumericLiteral",
"start":71,"end":72,"loc":{"start":{"line":5,"column":4,"index":71},"end":{"line":5,"column":5,"index":72}},
"extra": {
"rawValue": 7,
"raw": "7"
},
"value": 7
},
{
"type": "UnaryExpression",
"start":74,"end":81,"loc":{"start":{"line":5,"column":7,"index":74},"end":{"line":5,"column":14,"index":81}},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start":80,"end":81,"loc":{"start":{"line":5,"column":13,"index":80},"end":{"line":5,"column":14,"index":81}},
"extra": {
"rawValue": 8,
"raw": "8"
},
"value": 8
}
}
]
}
}
],
"directives": []
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
function test() {
(throw 1, 2);
(3, throw 4);
f(throw 5, 6);
f(7, throw 8);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"BABEL_8_BREAKING": true,
"plugins": ["throwExpressions"]
}