Skip to content

Commit

Permalink
Follow-up on initial TS4 catch param support (#11767)
Browse files Browse the repository at this point in the history
* refactor: align with AST shape convention

* feat: print catch param type annotations

* test: add test case on transform
  • Loading branch information
JLHwung committed Jul 29, 2020
1 parent 8a1d7e4 commit 9e6663f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/babel-generator/src/generators/statements.js
Expand Up @@ -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();
}
Expand Down
@@ -0,0 +1,2 @@
try {} catch (e: unknown) {}
try {} catch (e: any) {}
@@ -0,0 +1,3 @@
try {} catch (e: unknown) {}

try {} catch (e: any) {}
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -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;
Expand Down
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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": {
Expand All @@ -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",
Expand Down
@@ -0,0 +1 @@
try {} catch (e: any) {}
@@ -0,0 +1,4 @@
{
"plugins": ["transform-typescript"],
"sourceType": "module"
}
@@ -0,0 +1 @@
try {} catch (e) {}

0 comments on commit 9e6663f

Please sign in to comment.