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([])));
fork icon46
star icon35
watch icon0

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([])));
fork icon46
star icon35
watch icon0

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]);
    },
  },
};
fork icon2
star icon12
watch icon1

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'),
fork icon0
star icon2
watch icon1

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
);
fork icon0
star icon1
watch icon0

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)
fork icon0
star icon0
watch icon0

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)