diff --git a/packages/babel-generator/src/generators/statements.js b/packages/babel-generator/src/generators/statements.js index 24f9a2dbe5cd..dbeb45bf32b9 100644 --- a/packages/babel-generator/src/generators/statements.js +++ b/packages/babel-generator/src/generators/statements.js @@ -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(); } diff --git a/packages/babel-generator/test/fixtures/typescript/catch-param-type/input.js b/packages/babel-generator/test/fixtures/typescript/catch-param-type/input.js new file mode 100644 index 000000000000..76318908d8b3 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/catch-param-type/input.js @@ -0,0 +1,2 @@ +try {} catch (e: unknown) {} +try {} catch (e: any) {} diff --git a/packages/babel-generator/test/fixtures/typescript/catch-param-type/output.js b/packages/babel-generator/test/fixtures/typescript/catch-param-type/output.js new file mode 100644 index 000000000000..e9e71a8acf93 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/catch-param-type/output.js @@ -0,0 +1,3 @@ +try {} catch (e: unknown) {} + +try {} catch (e: any) {} \ No newline at end of file diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index b2cca834ebfa..7ba2b6abae08 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2666,7 +2666,7 @@ export default (superClass: Class): Class => const type = this.tsTryParseTypeAnnotation(); if (type) { - param.type = type; + param.typeAnnotation = type; } return param; diff --git a/packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/output.json b/packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/output.json index 5232fb1fcdb1..fa244b40b2b2 100644 --- a/packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/output.json +++ b/packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/output.json @@ -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", diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/input.ts new file mode 100644 index 000000000000..0aa11053c6ee --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/input.ts @@ -0,0 +1 @@ +try {} catch (e: any) {} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/options.json new file mode 100644 index 000000000000..d5e8a7e4201b --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["transform-typescript"], + "sourceType": "module" +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/output.mjs new file mode 100644 index 000000000000..0ead89004c5c --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/output.mjs @@ -0,0 +1 @@ +try {} catch (e) {}