Skip to content

Commit

Permalink
feat: support new Error
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Mar 26, 2020
1 parent 9f54c7b commit 6c9de43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,15 +1637,24 @@ export function emitFile(
// }

function emitThrowStatement(node: ts.ThrowStatement) {
// 只支持字符串
if (!isStringLike(node.expression, typeChecker)) {
return;
if (isStringLike(node.expression, typeChecker)) {
emitTokenWithComment(SyntaxKind.ThrowKeyword, node.pos, writeKeyword, node);
emitExpressionWithLeadingSpace(
ts.createNew(ts.createIdentifier('\\Exception'), null, [node.expression])
);
writeSemicolon();
}
if (
ts.isNewExpression(node.expression)
&& ts.isIdentifier(node.expression.expression)
&& node.expression.expression.getText() === 'Error'
) {
emitTokenWithComment(SyntaxKind.ThrowKeyword, node.pos, writeKeyword, node);
emitExpressionWithLeadingSpace(
ts.createNew(ts.createIdentifier('\\Exception'), null, [node.expression.arguments[0]])
);
writeSemicolon();
}
emitTokenWithComment(SyntaxKind.ThrowKeyword, node.pos, writeKeyword, node);
emitExpressionWithLeadingSpace(
ts.createNew(ts.createIdentifier('\\Exception'), null, [node.expression])
);
writeSemicolon();
}

function emitTryStatement(node: ts.TryStatement) {
Expand Down
2 changes: 2 additions & 0 deletions test/features/exception.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exception
```ts
try {
throw 'error!';
throw new Error('error!');
}
catch (e) {
console.log('error' + e.message);
Expand All @@ -21,6 +22,7 @@ throw a + '!';
```php
try {
throw new \Exception("error!");
throw new \Exception("error!");
}
catch (\Exception $e) {
echo "error" . $e->getMessage();
Expand Down

0 comments on commit 6c9de43

Please sign in to comment.