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:
parent
8a1d7e41f2
commit
9e6663f125
@ -174,6 +174,7 @@ export function CatchClause(node: Object) {
|
|||||||
if (node.param) {
|
if (node.param) {
|
||||||
this.token("(");
|
this.token("(");
|
||||||
this.print(node.param, node);
|
this.print(node.param, node);
|
||||||
|
this.print(node.param.typeAnnotation, node);
|
||||||
this.token(")");
|
this.token(")");
|
||||||
this.space();
|
this.space();
|
||||||
}
|
}
|
||||||
|
|||||||
2
packages/babel-generator/test/fixtures/typescript/catch-param-type/input.js
vendored
Normal file
2
packages/babel-generator/test/fixtures/typescript/catch-param-type/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
try {} catch (e: unknown) {}
|
||||||
|
try {} catch (e: any) {}
|
||||||
3
packages/babel-generator/test/fixtures/typescript/catch-param-type/output.js
vendored
Normal file
3
packages/babel-generator/test/fixtures/typescript/catch-param-type/output.js
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
try {} catch (e: unknown) {}
|
||||||
|
|
||||||
|
try {} catch (e: any) {}
|
||||||
@ -2666,7 +2666,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
const type = this.tsTryParseTypeAnnotation();
|
const type = this.tsTryParseTypeAnnotation();
|
||||||
|
|
||||||
if (type) {
|
if (type) {
|
||||||
param.type = type;
|
param.typeAnnotation = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
return param;
|
return param;
|
||||||
|
|||||||
@ -46,16 +46,17 @@
|
|||||||
"type": "CatchClause",
|
"type": "CatchClause",
|
||||||
"start":28,"end":50,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":29}},
|
"start":28,"end":50,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":29}},
|
||||||
"param": {
|
"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",
|
"type": "TSTypeAnnotation",
|
||||||
"start":37,"end":46,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":25}},
|
"start":37,"end":46,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":25}},
|
||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
"type": "TSUnknownKeyword",
|
"type": "TSUnknownKeyword",
|
||||||
"start":39,"end":46,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":25}}
|
"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": {
|
"body": {
|
||||||
"type": "BlockStatement",
|
"type": "BlockStatement",
|
||||||
@ -79,16 +80,17 @@
|
|||||||
"type": "CatchClause",
|
"type": "CatchClause",
|
||||||
"start":58,"end":76,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":25}},
|
"start":58,"end":76,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":25}},
|
||||||
"param": {
|
"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",
|
"type": "TSTypeAnnotation",
|
||||||
"start":67,"end":72,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},
|
"start":67,"end":72,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},
|
||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
"type": "TSAnyKeyword",
|
"type": "TSAnyKeyword",
|
||||||
"start":69,"end":72,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":21}}
|
"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": {
|
"body": {
|
||||||
"type": "BlockStatement",
|
"type": "BlockStatement",
|
||||||
@ -119,7 +121,10 @@
|
|||||||
"type": "CatchClause",
|
"type": "CatchClause",
|
||||||
"start":133,"end":149,"loc":{"start":{"line":6,"column":7},"end":{"line":6,"column":23}},
|
"start":133,"end":149,"loc":{"start":{"line":6,"column":7},"end":{"line":6,"column":23}},
|
||||||
"param": {
|
"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",
|
"type": "TSTypeAnnotation",
|
||||||
"start":142,"end":145,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":19}},
|
"start":142,"end":145,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":19}},
|
||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
@ -131,9 +136,7 @@
|
|||||||
"name": "A"
|
"name": "A"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"start":140,"end":142,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":16},"identifierName":"ex"},
|
|
||||||
"name": "ex"
|
|
||||||
},
|
},
|
||||||
"body": {
|
"body": {
|
||||||
"type": "BlockStatement",
|
"type": "BlockStatement",
|
||||||
@ -164,7 +167,10 @@
|
|||||||
"type": "CatchClause",
|
"type": "CatchClause",
|
||||||
"start":157,"end":177,"loc":{"start":{"line":7,"column":7},"end":{"line":7,"column":27}},
|
"start":157,"end":177,"loc":{"start":{"line":7,"column":7},"end":{"line":7,"column":27}},
|
||||||
"param": {
|
"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",
|
"type": "TSTypeAnnotation",
|
||||||
"start":166,"end":173,"loc":{"start":{"line":7,"column":16},"end":{"line":7,"column":23}},
|
"start":166,"end":173,"loc":{"start":{"line":7,"column":16},"end":{"line":7,"column":23}},
|
||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
@ -176,9 +182,7 @@
|
|||||||
"name": "Error"
|
"name": "Error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"start":164,"end":166,"loc":{"start":{"line":7,"column":14},"end":{"line":7,"column":16},"identifierName":"ex"},
|
|
||||||
"name": "ex"
|
|
||||||
},
|
},
|
||||||
"body": {
|
"body": {
|
||||||
"type": "BlockStatement",
|
"type": "BlockStatement",
|
||||||
@ -202,16 +206,17 @@
|
|||||||
"type": "CatchClause",
|
"type": "CatchClause",
|
||||||
"start":185,"end":206,"loc":{"start":{"line":8,"column":7},"end":{"line":8,"column":28}},
|
"start":185,"end":206,"loc":{"start":{"line":8,"column":7},"end":{"line":8,"column":28}},
|
||||||
"param": {
|
"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",
|
"type": "TSTypeAnnotation",
|
||||||
"start":194,"end":202,"loc":{"start":{"line":8,"column":16},"end":{"line":8,"column":24}},
|
"start":194,"end":202,"loc":{"start":{"line":8,"column":16},"end":{"line":8,"column":24}},
|
||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
"type": "TSStringKeyword",
|
"type": "TSStringKeyword",
|
||||||
"start":196,"end":202,"loc":{"start":{"line":8,"column":18},"end":{"line":8,"column":24}}
|
"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": {
|
"body": {
|
||||||
"type": "BlockStatement",
|
"type": "BlockStatement",
|
||||||
@ -235,7 +240,10 @@
|
|||||||
"type": "CatchClause",
|
"type": "CatchClause",
|
||||||
"start":214,"end":244,"loc":{"start":{"line":9,"column":7},"end":{"line":9,"column":37}},
|
"start":214,"end":244,"loc":{"start":{"line":9,"column":7},"end":{"line":9,"column":37}},
|
||||||
"param": {
|
"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",
|
"type": "TSTypeAnnotation",
|
||||||
"start":223,"end":240,"loc":{"start":{"line":9,"column":16},"end":{"line":9,"column":33}},
|
"start":223,"end":240,"loc":{"start":{"line":9,"column":16},"end":{"line":9,"column":33}},
|
||||||
"typeAnnotation": {
|
"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": {
|
"body": {
|
||||||
"type": "BlockStatement",
|
"type": "BlockStatement",
|
||||||
|
|||||||
1
packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/input.ts
vendored
Normal file
1
packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
try {} catch (e: any) {}
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["transform-typescript"],
|
||||||
|
"sourceType": "module"
|
||||||
|
}
|
||||||
1
packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/output.mjs
vendored
Normal file
1
packages/babel-plugin-transform-typescript/test/fixtures/catch-clause/param-type/output.mjs
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
try {} catch (e) {}
|
||||||
Loading…
x
Reference in New Issue
Block a user