Skip to content

Commit

Permalink
feat: support try catch #24
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jul 10, 2019
1 parent 176f862 commit 533d704
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
61 changes: 31 additions & 30 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ export function emitFile(
// return emitLabeledStatement(<LabeledStatement>node);
case SyntaxKind.ThrowStatement:
return emitThrowStatement(<ts.ThrowStatement>node);
// case SyntaxKind.TryStatement:
// return emitTryStatement(<TryStatement>node);
case SyntaxKind.TryStatement:
return emitTryStatement(<ts.TryStatement>node);
// case SyntaxKind.DebuggerStatement:
// return emitDebuggerStatement(<DebuggerStatement>node);

Expand Down Expand Up @@ -428,8 +428,8 @@ export function emitFile(
return emitDefaultClause(<ts.DefaultClause>node);
case SyntaxKind.HeritageClause:
return emitHeritageClause(<ts.HeritageClause>node);
// case SyntaxKind.CatchClause:
// return emitCatchClause(<CatchClause>node);
case SyntaxKind.CatchClause:
return emitCatchClause(<ts.CatchClause>node);

// // Property assignments
case SyntaxKind.PropertyAssignment:
Expand Down Expand Up @@ -1655,21 +1655,21 @@ export function emitFile(
writeSemicolon();
}

// function emitTryStatement(node: TryStatement) {
// emitTokenWithComment(SyntaxKind.TryKeyword, node.pos, writeKeyword, node);
// writeSpace();
// emit(node.tryBlock);
// if (node.catchClause) {
// writeLineOrSpace(node);
// emit(node.catchClause);
// }
// if (node.finallyBlock) {
// writeLineOrSpace(node);
// emitTokenWithComment(SyntaxKind.FinallyKeyword, (node.catchClause || node.tryBlock).end, writeKeyword, node);
// writeSpace();
// emit(node.finallyBlock);
// }
// }
function emitTryStatement(node: ts.TryStatement) {
emitTokenWithComment(SyntaxKind.TryKeyword, node.pos, writeKeyword, node);
writeSpace();
emit(node.tryBlock);
if (node.catchClause) {
writeLineOrSpace(node);
emit(node.catchClause);
}
if (node.finallyBlock) {
writeLineOrSpace(node);
emitTokenWithComment(SyntaxKind.FinallyKeyword, (node.catchClause || node.tryBlock).end, writeKeyword, node);
writeSpace();
emit(node.finallyBlock);
}
}

// function emitDebuggerStatement(node: DebuggerStatement) {
// writeToken(SyntaxKind.DebuggerKeyword, node.pos, writeKeyword);
Expand Down Expand Up @@ -2328,17 +2328,18 @@ export function emitFile(
emitList(node, node.types, ListFormat.HeritageClauseTypes);
}

// function emitCatchClause(node: CatchClause) {
// const openParenPos = emitTokenWithComment(SyntaxKind.CatchKeyword, node.pos, writeKeyword, node);
// writeSpace();
// if (node.variableDeclaration) {
// emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
// emit(node.variableDeclaration);
// emitTokenWithComment(SyntaxKind.CloseParenToken, node.variableDeclaration.end, writePunctuation, node);
// writeSpace();
// }
// emit(node.block);
// }
function emitCatchClause(node: ts.CatchClause) {
const openParenPos = emitTokenWithComment(SyntaxKind.CatchKeyword, node.pos, writeKeyword, node);
writeSpace();
if (node.variableDeclaration) {
emitTokenWithComment(SyntaxKind.OpenParenToken, openParenPos, writePunctuation, node);
emitIdentifier(ts.createIdentifier('\\Exception'));
emitWithLeadingSpace(node.variableDeclaration);
emitTokenWithComment(SyntaxKind.CloseParenToken, node.variableDeclaration.end, writePunctuation, node);
writeSpace();
}
emit(node.block);
}

// //
// // Property assignments
Expand Down
7 changes: 6 additions & 1 deletion test/features/exception.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
namespace test\case_exception;
throw new \Exception("error!");
try {
throw new \Exception("error!");
}
catch (\Exception $e) {
echo "error";
}
$a = "hard";
throw new \Exception("a " . $a . " error!");
throw new \Exception($a . "!");
7 changes: 6 additions & 1 deletion test/features/exception.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

throw 'error!';
try {
throw 'error!';
}
catch (e) {
console.log('error');
}

const a = 'hard';

Expand Down

0 comments on commit 533d704

Please sign in to comment.