Skip to content

Commit e5fd213

Browse files
committed
feat: support error property
1 parent 533d704 commit e5fd213

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/features/error.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @file Exception
3+
* @author cxtom(cxtom2008@gmail.com)
4+
*/
5+
6+
import * as ts from 'typescript';
7+
8+
const map = {
9+
message: 'getMessage',
10+
lineNumber: 'getLine',
11+
file: 'getFile',
12+
stack: 'getTraceAsString'
13+
};
14+
15+
export default {
16+
17+
emit(hint, node, {helpers, typeChecker}) {
18+
19+
const {
20+
getTextOfNode,
21+
emitExpression,
22+
writeBase
23+
} = helpers;
24+
25+
if (
26+
ts.isPropertyAccessExpression(node)
27+
&& ts.isIdentifier(node.name)
28+
&& map[getTextOfNode(node.name)]
29+
) {
30+
const symbol = typeChecker.getSymbolAtLocation(node.expression);
31+
const declaration = symbol.valueDeclaration;
32+
if (declaration && ts.isCatchClause(declaration.parent)) {
33+
emitExpression(node.expression);
34+
return writeBase(`->${map[getTextOfNode(node.name)]}()`);
35+
}
36+
}
37+
38+
return false;
39+
}
40+
};

src/features/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import NumberPlugin from './number';
1212
import ArrayPlugin from './array';
1313
import ConsolePlugin from './console';
1414
import DatePlugin from './date';
15+
import ErrorPlugin from './error';
1516

1617
export default [
1718
StringPligin,
@@ -22,5 +23,6 @@ export default [
2223
NumberPlugin,
2324
ArrayPlugin,
2425
ConsolePlugin,
25-
DatePlugin
26+
DatePlugin,
27+
ErrorPlugin
2628
];

test/features/exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
throw new \Exception("error!");
55
}
66
catch (\Exception $e) {
7-
echo "error";
7+
echo "error" . $e->getMessage();
88
}
99
$a = "hard";
1010
throw new \Exception("a " . $a . " error!");

test/features/exception.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ try {
33
throw 'error!';
44
}
55
catch (e) {
6-
console.log('error');
6+
console.log('error' + e.message);
77
}
88

99
const a = 'hard';

0 commit comments

Comments
 (0)