Skip to content

Commit

Permalink
Parsing of catch without parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mck89 committed Dec 21, 2018
1 parent 19feb1a commit 47e9d46
Show file tree
Hide file tree
Showing 8 changed files with 636 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/Peast/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,16 @@ protected function renderNode(Syntax\Node\Node $node)
")";
break;
case "CatchClause":
$code .= "catch" .
$this->renderOpts->sao .
"(" .
$this->renderOpts->sirb .
$this->renderNode($node->getParam()) .
$this->renderOpts->sirb .
")" .
$this->renderStatementBlock($node->getBody(), true);
$code .= "catch";
if ($params = $node->getParam()) {
$code .= $this->renderOpts->sao .
"(" .
$this->renderOpts->sirb .
$this->renderNode($params) .
$this->renderOpts->sirb .
")";
}
$code .= $this->renderStatementBlock($node->getBody(), true);
break;
case "ClassExpression":
case "ClassDeclaration":
Expand Down
29 changes: 29 additions & 0 deletions lib/Peast/Syntax/ES2019/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,33 @@
*/
class Parser extends \Peast\Syntax\ES2018\Parser
{
/**
* Parses the catch block of a try-catch statement
*
* @return Node\CatchClause|null
*/
protected function parseCatch()
{
if ($token = $this->scanner->consume("catch")) {

$node = $this->createNode("CatchClause", $token);

if ($this->scanner->consume("(")) {
if (!($param = $this->parseCatchParameter()) ||
!$this->scanner->consume(")")) {
return $this->error();
}
$node->setParam($param);
}

if (!($body = $this->parseBlock())) {
return $this->error();
}

$node->setBody($body);

return $this->completeNode($node);
}
return null;
}
}
3 changes: 2 additions & 1 deletion lib/Peast/Syntax/Node/CatchClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public function getParam()
*
* @return $this
*/
public function setParam(Pattern $param)
public function setParam($param)
{
$this->assertType($param, "Pattern", true);
$this->param = $param;
return $this;
}
Expand Down
11 changes: 11 additions & 0 deletions test/Peast/Syntax/ES2019/ES2019Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ protected function getTestVersions()
return array("ES2015", "ES2016", "ES2017", "ES2018", "ES2019");
}

protected function getExcludedTests()
{
$excluded = parent::getExcludedTests();
return array_merge(
$excluded,
array(
"TryStatement/InvalidCatch2.js"
)
);
}

public function stringCharsProvider()
{
$chars = parent::stringCharsProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try {
fn();
} catch {
console.log("error");
}
/**************************************************/
try{fn();}catch{console.log("error");}
/**************************************************/
try
{
fn( );
} catch
{
console.log( "error" );
}
Loading

0 comments on commit 47e9d46

Please sign in to comment.