How to use the functionExpression function from babel-types

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

40
41
42
43
44
45
46
47
48
49
  
}else{
    // 替他情况就是原型上的方法
    let protoObj = t.memberExpression(className,t.identifier('prototype'));
    let left = t.memberExpression(protoObj,t.identifier(item.key.name));
    let right = t.functionExpression(null,[],body,false,false);
    let assign = t.assignmentExpression('=',left,right);
    // 多个原型上的方法
    es5Func.push(assign);
}
fork icon57
star icon56
watch icon4

+ 3 other calls in file

24
25
26
27
28
29
30
31
32
33
            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 icon4

+ 9 other calls in file

91
92
93
94
95
96
97
98
99
100
if (path.node.declarations &&
  t.isVariableDeclarator(path.node.declarations[0]) &&
  t.isNewExpression(path.node.declarations[0].init) &&
  t.isIdentifier(path.node.declarations[0].init.callee, { name: 'Vue' })
) {
  const fnExpression = t.functionExpression(
    null,
    [],
    t.blockStatement(
      [
fork icon49
star icon47
watch icon12

+ 43 other calls in file

176
177
178
179
180
181
182
183
184
185
const astProgramBody = codeAst.program.body;
const codeBlock = types.BlockStatement(astProgramBody);
if (renderReturn) {
  astProgramBody.push(renderReturn);
}
const coceFunction = types.functionExpression(
  types.Identifier('jsonmlReactLoader'),
  [],
);
path.replaceWith(coceFunction);
fork icon11
star icon102
watch icon3

+ 19 other calls in file

9
10
11
12
13
14
15
16
17
18
      let {
        node
      } = path;
      let body = node.body;
      let params = node.params;
      let r = t.functionExpression(null, params, body, false, false);
      path.replaceWith(r);
    }
  }
}
fork icon12
star icon27
watch icon1

+ 11 other calls in file

273
274
275
276
277
278
279
280
281
let func = null;
// 区分是函数申明还是函数表达式
if (types.isFunctionDeclaration(node)) {
    func = types.functionDeclaration(node.id, node.params, types.BlockStatement([tryStatement]), node.generator, node.async);
} else {
    func = types.functionExpression(node.id, node.params, types.BlockStatement([tryStatement]), node.generator, node.async);
}
path.replaceWith(func);
```
fork icon6
star icon14
watch icon2

+ 23 other calls in file

7
8
9
10
11
12
13
14
15
  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

272
273
274
275
276
277
278
279
280
281
    if (method.kind == 'constructor') {
        constructorFunction = t.functionDeclaration(id,method.params,method.body,false,false);
        functions.push(constructorFunction);
    } else {
        let memberObj=t.memberExpression(t.memberExpression(id,t.identifier('prototype')),method.key);
        let memberFunction = t.functionExpression(id,method.params,method.body,false,false);
        let assignment = t.assignmentExpression('=',memberObj,memberFunction);
        functions.push(assignment);
    }
});
fork icon6
star icon12
watch icon2

+ 35 other calls in file

20
21
22
23
24
25
26
27
28
29
'var',
[
  t.variableDeclarator(
    t.identifier(className),
    t.callExpression(
      t.functionExpression(
        null,
        [],
        body
      ),
fork icon0
star icon45
watch icon2

+ 8 other calls in file

181
182
183
184
185
186
187
188
189
190
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)
  //替换当前箭头函数节点
  path.replaceWith(func)
},
VariableDeclaration(path) { // 在visitor中变量声明
fork icon4
star icon12
watch icon0

+ 21 other calls in file

249
250
251
252
253
254
255
256
257
258
    funs = t.functionDeclaration(className, [params], body, false, false);
    path.replaceWith(funs);
}else {
    let protoObj = t.memberExpression(className, t.identifier('prototype'));
    let left = t.memberExpression(protoObj, t.identifier(item.key.name));
    let right = t.functionExpression(null, [], body, false, false);

    let assign = t.assignmentExpression('=', left, right);
    es5func.push(assign);
}
fork icon1
star icon9
watch icon1

+ 65 other calls in file

76
77
78
79
80
81
82
83
84
85
// ReactDOM.render always at the last of preview method
if (renderReturn) {
  astProgramBody.push(renderReturn);
}
//program.body is an array, so we can push returnStatement to the end of array
var coceFunction = types.functionExpression(types.Identifier('jsonmlReactLoader'),
//here is an Identifier of function
[], codeBlock
//Even though `import` or 'ReactDOM.render' are removed, but
// <program><codeBlock></codeBlock><returnStatement><returnStatement/></program>
fork icon8
star icon15
watch icon2

+ 5 other calls in file

43
44
45
46
47
48
49
50
51
52
    body = node.body,
    container;

  node.async = false;

  container = t.functionExpression(null, [], t.blockStatement(body.body), true);
  container.shadow = true;

  body.body = [t.returnStatement(t.callExpression(t.callExpression(callId, [container]), []))];
}
fork icon0
star icon2
watch icon1

+ 11 other calls in file

204
205
206
207
208
209
210
211
212
213
// Examples
const intoExampleFunction = (source, ast, options) => {
  const body = ast.program.body;

  return new Raw(withMeta({
    OBJECT: t.functionExpression(
      null,   // id
      [],     // params
      t.blockStatement(body),
      false,  // generator
fork icon111
star icon0
watch icon2

+ 11 other calls in file

149
150
151
152
153
154
155
156
157
158

const key = c.KEY_I(branch.iterSuffix);

const iter = t.callExpression(c.ITER, [
  Guard(branch.subject.path),
  t.functionExpression(c.EACH, [
    key,
    c.INDEX,
    c.LENGTH,
  ], t.blockStatement([
fork icon17
star icon0
watch icon5

20
21
22
23
24
25
26
27
28
29
        // console.log(path)
        let params = path.node.params
        let body = types.blockStatement([
            types.returnStatement(path.node.body)
        ])
        let fn = types.functionExpression(null, params, body, false, false)
        path.replaceWith(fn)
    }
}
let arrayPlugin = { visitor }
fork icon7
star icon6
watch icon3

83
84
85
86
87
88
89
90
91
92
      // ReactDOM.render always at the last of preview method
      if (renderReturn) {
        astProgramBody.push(renderReturn);
      }

      const coceFunction = types.functionExpression(null, [], codeBlock);
      path.replaceWith(coceFunction);
    }
  },
});
fork icon6
star icon0
watch icon2

+ 4 other calls in file

21
22
23
24
25
26
27
28
29
30
            // 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);
        }
    });
    path.replaceWith(func);
}
fork icon2
star icon6
watch icon2

+ 3 other calls in file

162
163
164
165
166
167
168
169
170
171
}

createSelfInvoke = function (expr) { return t.parenthesizedExpression(expr); };

// wrapping up the self-invoke function
funcExpr = t.functionExpression(null, [], t.blockStatement(funcLines));
funcCallExpr = t.callExpression(
  t.memberExpression(funcExpr, t.identifier('apply')),
  [t.thisExpression()]
);
fork icon47
star icon0
watch icon36

23
24
25
26
27
28
29
30
31
32
        // 不是代码块,兼容箭头函数的问题 简写模式
        let returnStatement =t.returnStatement(body) // 创造return代码块
        body = t.blockStatement([returnStatement]) // 
      }
      // 生成一个普通函数表达式
      let funcs = t.functionExpression(null,params,body,false,false)
      path.replaceWith(funcs)
    }
  }
}
fork icon4
star icon2
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)