How to use the returnStatement function from babel-types

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

163
164
165
166
167
168
169
170
171
172
CallPathNode.callee.object &&
CallPathNode.callee.object.name === 'ReactDOM' &&
CallPathNode.callee.property &&
CallPathNode.callee.property.name === 'render') {
//we focus on ReactDOM.render method
renderReturn = types.returnStatement(
   CallPathNode.arguments[0]
);
//we focus on first parameter of ReactDOM.render method
CallPath.remove();
fork icon11
star icon102
watch icon3

+ 19 other calls in file

21
22
23
24
25
26
27
28
29
30
let params = node.params;
let body = node.body;
// 生成一个函数表达式
if(!t.isBlockStatement(body)){
    // 不是代码块
    let returnStatement = t.returnStatement(body);
    body = t.blockStatement([returnStatement]);
}
let funcs = t.functionExpression(null,params,body,false,false);
path.replaceWith(funcs);
fork icon57
star icon56
watch icon0

+ 9 other calls in file

96
97
98
99
100
101
102
103
104
105
const fnExpression = t.functionExpression(
  null,
  [],
  t.blockStatement(
    [
      t.returnStatement(
        t.newExpression(
          path.node.declarations[0].init.callee,
          path.node.declarations[0].init.arguments
        )
fork icon49
star icon47
watch icon12

+ 43 other calls in file

60
61
62
63
64
65
66
67
68
69
//ExpressionStatement->CallExpression->MemberExpression. Detail in https://astexplorer.net/#/tSIO7NIclp/2%E8%A7%A3%E6%9E%90%E5%87%BA%E6%9D%A5%E7%9A%84%E5%B0%B1%E6%98%AF%E6%88%91%E4%BB%AC%E7%9A%84program.body%E9%83%A8%E5%88%86%EF%BC%8C%E4%B9%9F%E5%B0%B1%E6%98%AF%E5%A6%82%E4%B8%8B%E7%9A%84%E5%86%85%E5%AE%B9%EF%BC%9A
CallExpression: function CallExpression(CallPath) {
  var CallPathNode = CallPath.node;
  if (CallPathNode.callee && CallPathNode.callee.object && CallPathNode.callee.object.name === 'ReactDOM' && CallPathNode.callee.property && CallPathNode.callee.property.name === 'render') {
    //we focus on ReactDOM.render method
    renderReturn = types.returnStatement(CallPathNode.arguments[0]);
    //we focus on first parameter of ReactDOM.render method
    CallPath.remove();
  }
}
fork icon8
star icon15
watch icon2

+ 5 other calls in file

144
145
146
147
148
149
150
151
152
153
ArrowFunctionExpression: (path, state) => {
    let node = path.node;
    let id = path.parent.id;
    let params = node.params;
    let body=t.blockStatement([
        t.returnStatement(node.body)
    ]);
    let functionExpression = t.functionExpression(id,params,body,false,false);
    path.replaceWith(functionExpression);
}
fork icon6
star icon12
watch icon2

+ 17 other calls in file

178
179
180
181
182
183
184
185
186
187
const types = require('babel-types'); // types就是用来构造一个新的node节点的

const visitor = {
  ArrowFunctionExpression(path) { // 在visitor中拦截箭头函数
    let params = path.node.params // 获取函数参数
    const returnStatement = types.returnStatement(path.node.body) //构建一个return表达式
    const blockStatement = types.blockStatement([returnStatement]) // 构建一个blockStatement
    // babel-types的functionExpression构造成一个新的ES function语法的函数
    let func = types.functionExpression(null, params, blockStatement, false, false)
    //替换当前箭头函数节点
fork icon4
star icon12
watch icon2

+ 21 other calls in file

5
6
7
8
9
10
11
12
13
14
// 这个访问者可以对特定的类型的节点进行处理
let visitor = {
  ArrowFunctionExpression(path){
    let params = path.node.params;
    let blockStatement = types.blockStatement([
        types.returnStatement(path.node.body)
    ]);
    let func = types.functionExpression(null, params, blockStatement, false, false);
    path.replaceWith(func);
  }
fork icon6
star icon12
watch icon2

70
71
72
73
74
75
76
77
78
79
exportBody() {
  return t.blockStatement([
    this.modelDefinition(),
    this.associate(),
    this.datFileGetter(),
    t.returnStatement(t.identifier('model')),
  ]);
}

modelDefinition() {
fork icon1
star icon5
watch icon2

+ 3 other calls in file

135
136
137
138
139
140
141
142
143
    if (t.isReturnStatement(node)) return node.argument;
    return transformNonNullConsequentOrAlternate(node);
}

function transformAlternate(node) {
    if (node == null) return t.returnStatement(t.nullLiteral());
    if (t.isReturnStatement(node)) return node.argument;
    return transformNonNullConsequentOrAlternate(node);
}
fork icon330
star icon0
watch icon0

+ 3 other calls in file

17
18
19
20
21
22
23
24
25
26
// console.log(classExpression)
let func;
body.body.forEach(body => {
    if (body.kind === 'constructor') {
        // console.log(body)
        // const statement = types.returnStatement(body.body);
        // console.log(statement)
        const block = types.BlockStatement([body.body]);
        // console.log(block)
        func = types.functionExpression(id, body.params, block, false, false);
fork icon2
star icon6
watch icon2

+ 3 other calls in file

73
74
75
76
77
78
79
80
81
82
        t.expressionStatement(t.assignmentExpression('=', key, c.INDEX)),
        t.expressionStatement(t.assignmentExpression('=', c.KEY, c.INDEX)),
        t.expressionStatement(t.assignmentExpression('+=', output, ConcatStringList(compile(branch.body)))),
      ]),
    ),
    t.returnStatement(output),
  ])), []);
}

const transforms = {
fork icon17
star icon0
watch icon5

+ 5 other calls in file

78
79
80
81
82
83
84
85
86
87
// console.log('=====loop=====');
// console.log(loop);
// 箭头函数
if(t.isJSXExpressionContainer(childNode)){
  // console.log('childNode',childNode)
  let s =  t.returnStatement(childNode.expression);
  childNode = t.blockStatement([s])
}

let arrow = t.ArrowFunctionExpression(loopArgs.map(item=>t.Identifier(item)),childNode);
fork icon0
star icon1
watch icon0

+ 4 other calls in file

97
98
99
100
101
102
103
104
105
} else {
  funcNode = t.functionExpression(
    null,
    [],
    t.blockStatement([
      t.returnStatement(path.node)
    ])
  );
}
fork icon0
star icon0
watch icon3

Other functions in babel-types

Sorted by popularity

function icon

babel-types.identifier is the most popular function in babel-types (4076 examples)