Skip to content

Commit

Permalink
feat: support error property
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jul 10, 2019
1 parent 533d704 commit e5fd213
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/features/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file Exception
* @author cxtom(cxtom2008@gmail.com)
*/

import * as ts from 'typescript';

const map = {
message: 'getMessage',
lineNumber: 'getLine',
file: 'getFile',
stack: 'getTraceAsString'
};

export default {

emit(hint, node, {helpers, typeChecker}) {

const {
getTextOfNode,
emitExpression,
writeBase
} = helpers;

if (
ts.isPropertyAccessExpression(node)
&& ts.isIdentifier(node.name)
&& map[getTextOfNode(node.name)]
) {
const symbol = typeChecker.getSymbolAtLocation(node.expression);
const declaration = symbol.valueDeclaration;
if (declaration && ts.isCatchClause(declaration.parent)) {
emitExpression(node.expression);
return writeBase(`->${map[getTextOfNode(node.name)]}()`);
}
}

return false;
}
};
4 changes: 3 additions & 1 deletion src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import NumberPlugin from './number';
import ArrayPlugin from './array';
import ConsolePlugin from './console';
import DatePlugin from './date';
import ErrorPlugin from './error';

export default [
StringPligin,
Expand All @@ -22,5 +23,6 @@ export default [
NumberPlugin,
ArrayPlugin,
ConsolePlugin,
DatePlugin
DatePlugin,
ErrorPlugin
];
2 changes: 1 addition & 1 deletion test/features/exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
throw new \Exception("error!");
}
catch (\Exception $e) {
echo "error";
echo "error" . $e->getMessage();
}
$a = "hard";
throw new \Exception("a " . $a . " error!");
Expand Down
2 changes: 1 addition & 1 deletion test/features/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ try {
throw 'error!';
}
catch (e) {
console.log('error');
console.log('error' + e.message);
}

const a = 'hard';
Expand Down

0 comments on commit e5fd213

Please sign in to comment.