How to use the tryStatement function from babel-types

Find comprehensive JavaScript babel-types.tryStatement code examples handpicked from public code repositorys.

167
168
169
170
171
172
173
174
175
```js
let visitor = {
  'ArrowFunctionExpression|FunctionExpression|FunctionDeclaration': function(path) {
      let body = path.node.body
      // ...此处省略catch的生成
      let tryStatement = types.tryStatement(body, catchClause);
  }
}
```
fork icon6
star icon14
watch icon2

+ 47 other calls in file

79
80
81
82
83
84
85
86
87
88
if (i != -1) startOfTryBlock = splitPoints[i] + 1;
let endOfTryBlock = block.length;
if (i < splitPoints.length - 1) endOfTryBlock = splitPoints[i + 1];
if (i != -1) newBlock.push(block[splitPoints[i]]);
newBlock.push(
  t.tryStatement(
    t.blockStatement(block.slice(startOfTryBlock, endOfTryBlock).concat(auxBlock), []),
    catchClause,
    null,
  ),
fork icon2
star icon5
watch icon3

+ 7 other calls in file

27
28
29
30
31
32
33
34
35
36
//把一个JS字符串转成AST语法树节点 CallExpression
let catchStatement = template.statement('console.log(error)')();
let catchClause = types.catchClause(types.identifier('error'),
types.blockStatement([catchStatement]));
//node.body就是原来的函数里的语句,现在要放在try里面
let tryStatement = types.tryStatement(node.body,catchClause);
//新的函数方法名不变sum,参数不变a,b
var func = types.functionExpression(id,node.params,types.blockStatement([
    tryStatement
]),node.generator,node.async)
fork icon0
star icon0
watch icon0

29
30
31
32
33
34
35
36
37
38

// 把一个JS字符串转成一个AST节点
let catchStatement = template.statement('console.log(error)')();
let catchClause = types.catchClause(types.identifier('error'), types.blockStatement([catchStatement]));
// node.body就是原来的函数里的语句,现在要放到try里面
let tryStatement = types.tryStatement(node.body, catchClause);
// 新的函数方法名不变sum,参数不变 a,b
var func = types.functionDeclaration(id, node.params, types.blockStatement([
  tryStatement
]), node.generator, node.async);
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 (4076 examples)