Follow-up on initial TS4 catch param support (#11767)

* refactor: align with AST shape convention

* feat: print catch param type annotations

* test: add test case on transform
This commit is contained in:
Huáng Jùnliàng 2020-06-30 16:42:15 -04:00
parent 8a1d7e41f2
commit 9e6663f125
8 changed files with 43 additions and 25 deletions

View File

@ -174,6 +174,7 @@ export function CatchClause(node: Object) {
if (node.param) {
this.token("(");
this.print(node.param, node);
this.print(node.param.typeAnnotation, node);
this.token(")");
this.space();
}

View File

@ -0,0 +1,2 @@
try {} catch (e: unknown) {}
try {} catch (e: any) {}

View File

@ -0,0 +1,3 @@
try {} catch (e: unknown) {}
try {} catch (e: any) {}

View File

@ -2666,7 +2666,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const type = this.tsTryParseTypeAnnotation();
if (type) {
param.type = type;
param.typeAnnotation = type;
}
return param;

View File

@ -46,16 +46,17 @@
"type": "CatchClause",
"start":28,"end":50,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":29}},
"param": {
"type": {
"type": "Identifier",
"start":35,"end":37,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":16},"identifierName":"ex"},
"name": "ex",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":37,"end":46,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":25}},
"typeAnnotation": {
"type": "TSUnknownKeyword",
"start":39,"end":46,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":25}}
}
},
"start":35,"end":37,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":16},"identifierName":"ex"},
"name": "ex"
}
},
"body": {
"type": "BlockStatement",
@ -79,16 +80,17 @@
"type": "CatchClause",
"start":58,"end":76,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":25}},
"param": {
"type": {
"type": "Identifier",
"start":65,"end":67,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":16},"identifierName":"ex"},
"name": "ex",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":67,"end":72,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start":69,"end":72,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":21}}
}
},
"start":65,"end":67,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":16},"identifierName":"ex"},
"name": "ex"
}
},
"body": {
"type": "BlockStatement",
@ -119,7 +121,10 @@
"type": "CatchClause",
"start":133,"end":149,"loc":{"start":{"line":6,"column":7},"end":{"line":6,"column":23}},
"param": {
"type": {
"type": "Identifier",
"start":140,"end":142,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":16},"identifierName":"ex"},
"name": "ex",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":142,"end":145,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":19}},
"typeAnnotation": {
@ -131,9 +136,7 @@
"name": "A"
}
}
},
"start":140,"end":142,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":16},"identifierName":"ex"},
"name": "ex"
}
},
"body": {
"type": "BlockStatement",
@ -164,7 +167,10 @@
"type": "CatchClause",
"start":157,"end":177,"loc":{"start":{"line":7,"column":7},"end":{"line":7,"column":27}},
"param": {
"type": {
"type": "Identifier",
"start":164,"end":166,"loc":{"start":{"line":7,"column":14},"end":{"line":7,"column":16},"identifierName":"ex"},
"name": "ex",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":166,"end":173,"loc":{"start":{"line":7,"column":16},"end":{"line":7,"column":23}},
"typeAnnotation": {
@ -176,9 +182,7 @@
"name": "Error"
}
}
},
"start":164,"end":166,"loc":{"start":{"line":7,"column":14},"end":{"line":7,"column":16},"identifierName":"ex"},
"name": "ex"
}
},
"body": {
"type": "BlockStatement",
@ -202,16 +206,17 @@
"type": "CatchClause",
"start":185,"end":206,"loc":{"start":{"line":8,"column":7},"end":{"line":8,"column":28}},
"param": {
"type": {
"type": "Identifier",
"start":192,"end":194,"loc":{"start":{"line":8,"column":14},"end":{"line":8,"column":16},"identifierName":"ex"},
"name": "ex",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":194,"end":202,"loc":{"start":{"line":8,"column":16},"end":{"line":8,"column":24}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":196,"end":202,"loc":{"start":{"line":8,"column":18},"end":{"line":8,"column":24}}
}
},
"start":192,"end":194,"loc":{"start":{"line":8,"column":14},"end":{"line":8,"column":16},"identifierName":"ex"},
"name": "ex"
}
},
"body": {
"type": "BlockStatement",
@ -235,7 +240,10 @@
"type": "CatchClause",
"start":214,"end":244,"loc":{"start":{"line":9,"column":7},"end":{"line":9,"column":37}},
"param": {
"type": {
"type": "Identifier",
"start":221,"end":223,"loc":{"start":{"line":9,"column":14},"end":{"line":9,"column":16},"identifierName":"ex"},
"name": "ex",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":223,"end":240,"loc":{"start":{"line":9,"column":16},"end":{"line":9,"column":33}},
"typeAnnotation": {
@ -252,9 +260,7 @@
}
]
}
},
"start":221,"end":223,"loc":{"start":{"line":9,"column":14},"end":{"line":9,"column":16},"identifierName":"ex"},
"name": "ex"
}
},
"body": {
"type": "BlockStatement",

View File

@ -0,0 +1 @@
try {} catch (e: any) {}

View File

@ -0,0 +1,4 @@
{
"plugins": ["transform-typescript"],
"sourceType": "module"
}

View File

@ -0,0 +1 @@
try {} catch (e) {}