How to use the tryStatement function from @babel/types
Find comprehensive JavaScript @babel/types.tryStatement code examples handpicked from public code repositorys.
118 119 120 121 122 123 124 125 126 127
getSectionFooter(path) { const variables = common.availableVariables(path); const statements = variables.map(prettyPrintStatement); statements.unshift(printCaught()); statements.unshift(printHash()); const statement = babelTypes.tryStatement( babelTypes.blockStatement(statements), babelTypes.catchClause( babelTypes.identifier('e'), babelTypes.blockStatement([])));
GitHub: nwjs/v8
27 28 29 30 31 32 33 34 35 36 37
['WebAssembly', 'Module'], ['WebAssembly', 'Instantiate'], ]; function wrapTryCatch(node) { return babelTypes.tryStatement( babelTypes.blockStatement([node]), babelTypes.catchClause( babelTypes.identifier('e'), babelTypes.blockStatement([])));
GitHub: fecym/ast-share
![fecym profile picture](/fecym.png?size=40)
28 29 30 31 32 33 34 35 36 37
const tryBlock = t.blockStatement([replacePath.node]); // 生成 catch --> new Error(e) const paramsE = t.identifier('e'); const throwStatement = t.throwStatement(t.newExpression(t.identifier('Error'), [paramsE])); const catchClause = t.catchClause(paramsE, t.blockStatement([throwStatement])); const tryStatement = t.tryStatement(tryBlock, catchClause); replacePath.replaceWithMultiple([tryStatement]); }, }, };
101 102 103 104 105 106 107 108 109 110
// redefine the body of the function as a new blockStatement containing a tryStatement // which catches errors and forwards them to reject() when caught ast.body = babelTypes.blockStatement([ // try { babelTypes.tryStatement( block, // catch (error) { babelTypes.catchClause( babelTypes.identifier('error'),
104 105 106 107 108 109 110 111 112 113
} if (catchBody.length > 1) { this.mapIfAlternates(catchBody); } return t.tryStatement( t.blockStatement(tryBody), t.catchClause(expIdentifier, t.blockStatement(flatten([catchBody[0]]))), finallyBody ? t.blockStatement(finallyBody) : undefined );
57 58 59 60 61 62 63 64 65 66
if ( // 找到 async Function t.isBlockStatement(path.node) && isAsyncFuncNode(parentPath.node) ) { let tryCatchAst = t.tryStatement( path.node, t.catchClause( t.identifier(options.identifier), t.blockStatement(catchNode)
@babel/types.identifier is the most popular function in @babel/types (20936 examples)