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

PHP8 non-capturing exception #755

Merged
merged 2 commits into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ast/catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const KIND = "catch";
* @constructor Catch
* @extends {Statement}
* @property {Identifier[]} what
* @property {Variable} variable
* @property {Variable|null} variable
* @property {Statement} body
* @see http://php.net/manual/en/language.exceptions.php
*/
Expand Down
7 changes: 5 additions & 2 deletions src/parser/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
* ```ebnf
* try ::= T_TRY '{' inner_statement* '}'
* (
* T_CATCH '(' namespace_name variable ')' '{' inner_statement* '}'
* T_CATCH '(' namespace_name (variable)? ')' '{' inner_statement* '}'
* )*
* (T_FINALLY '{' inner_statement* '}')?
* ```
Expand All @@ -28,7 +28,10 @@ module.exports = {
const item = this.node("catch");
this.next().expect("(") && this.next();
const what = this.read_list(this.read_namespace_name, "|", false);
const variable = this.read_variable(true, false);
let variable = null;
if (this.token === this.tok.T_VARIABLE) {
variable = this.read_variable(true, false);
}
this.expect(")");
catches.push(item(this.next().read_statement(), what, variable));
}
Expand Down
125 changes: 125 additions & 0 deletions test/snapshot/__snapshots__/try.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,71 @@ Program {
}
`;

exports[`boolean multiple catch without variable 1`] = `
Program {
"children": Array [
Try {
"always": null,
"body": Block {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": Name {
"kind": "name",
"name": "call",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
],
"kind": "block",
},
"catches": Array [
Catch {
"body": Block {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": Name {
"kind": "name",
"name": "do_something",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
],
"kind": "block",
},
"kind": "catch",
"variable": null,
"what": Array [
Name {
"kind": "name",
"name": "MyException",
"resolution": "uqn",
},
Name {
"kind": "name",
"name": "MyOtherException",
"resolution": "uqn",
},
],
},
],
"kind": "try",
},
],
"errors": Array [],
"kind": "program",
}
`;

exports[`boolean qualified name 1`] = `
Program {
"children": Array [
Expand Down Expand Up @@ -552,3 +617,63 @@ Program {
"kind": "program",
}
`;

exports[`boolean without variable 1`] = `
Program {
"children": Array [
Try {
"always": null,
"body": Block {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": Name {
"kind": "name",
"name": "call",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
],
"kind": "block",
},
"catches": Array [
Catch {
"body": Block {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": Name {
"kind": "name",
"name": "do_something",
"resolution": "uqn",
},
},
"kind": "expressionstatement",
},
],
"kind": "block",
},
"kind": "catch",
"variable": null,
"what": Array [
Name {
"kind": "name",
"name": "Exception",
"resolution": "uqn",
},
],
},
],
"kind": "try",
},
],
"errors": Array [],
"kind": "program",
}
`;
12 changes: 12 additions & 0 deletions test/snapshot/try.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe("boolean", () => {
)
).toMatchSnapshot();
});
it("without variable", () => {
expect(
parser.parseEval("try { call(); } catch (Exception) { do_something(); }")
).toMatchSnapshot();
});
it("qualified name", () => {
expect(
parser.parseEval(
Expand Down Expand Up @@ -51,6 +56,13 @@ describe("boolean", () => {
)
).toMatchSnapshot();
});
it("multiple catch without variable", () => {
expect(
parser.parseEval(
"try { call(); } catch (MyException | MyOtherException) { do_something(); }"
)
).toMatchSnapshot();
});
it("multiple catch #2", () => {
expect(
parser.parseEval(
Expand Down