How to use the functionDeclaration function from babel-types

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

34
35
36
37
38
39
40
41
42
43
let body = classList[index].body;
if(item.kind === 'constructor'){
    //如果是够赞函数那就生成新的函数将默认的空函数替换掉
    let params = item.params.length?item.params.map(item=>item.name) : [];
    params = t.identifier(params);
    funcs = t.functionDeclaration(className,[params],body,false,false);
  
}else{
    // 替他情况就是原型上的方法
    let protoObj = t.memberExpression(className,t.identifier('prototype'));
fork icon57
star icon56
watch icon4

+ 7 other calls in file

271
272
273
274
275
276
277
278
279
280

```js
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

294
295
296
297
298
299
300
301
302
303
  astProgramBody.push(renderReturn);
}
const codeBlock = types.BlockStatement(astProgramBody);
//将astProgramBody转化为BlockStatement
//t.functionDeclaration(id, params, body, generator, async)
const previewFunction = types.functionDeclaration(
  types.Identifier('functionName'),
  [],
  codeBlock
);
fork icon3
star icon23
watch icon2

+ 3 other calls in file

263
264
265
266
267
268
269
270
271
272
let ClassPlugin={
    visitor: {
        ClassDeclaration(path) {
            let node=path.node;
            let id=node.id;
            let constructorFunction = t.functionDeclaration(id,[],t.blockStatement([]),false,false);
            let methods=node.body.body;
            let functions = [];
            methods.forEach(method => {
                if (method.kind == 'constructor') {
fork icon6
star icon12
watch icon2

+ 35 other calls in file

191
192
193
194
195
196
197
198
199
200
        ClassDeclaration(path){
            let {node} = path;
            let className = node.id.name;
            className = t.identifier(className);
            //console.log(className);
            let funs = t.functionDeclaration(className, [], t.blockStatement([]), false, false);
            path.replaceWith(funs);
        }
    }
}
fork icon1
star icon9
watch icon1

+ 65 other calls in file

204
205
206
207
208
209
210
211
212
 */
function compiler(parsed, opts) {
  options = opts;
  const compiled = compile(parsed);

  return t.functionDeclaration(c.COMPILED, c.runtimeParams, t.blockStatement([
    t.returnStatement(ConcatStringList(compiled)),
  ]));
}
fork icon17
star icon0
watch icon5

35
36
37
38
39
40
41
42
43
44
if (methodName !== 'constructor') {
    var fn = helpers.method(astPath, methodName);
    modules.thisMethods.push(fn);
} else {
    var node = astPath.node;
    modules.ctorFn = t.functionDeclaration(
        t.identifier(modules.className),
        node.params,
        node.body,
        node.generator,
fork icon330
star icon0
watch icon0

30
31
32
33
34
35
36
37
38
39
let body = [];
classMethods.forEach(method => {
  console.log(method)
  if (method.kind === 'constructor') { // 如果方法的类型是构造函数的话
    // Person [name] this.name=name(body复用) 
    let construcorFunction = types.functionDeclaration(id, method.params, method.body, method.generator, method.async);
    body.push(construcorFunction)
  } else {// 其他的函数属于普通函数,需要放在原型上的
    // method.key=getName
    // Person.prototype.getName
fork icon0
star icon0
watch icon0

31
32
33
34
35
36
37
38
39
40
  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);
  nodePath.replaceWith(func);
}
fork icon0
star icon0
watch icon0

122
123
124
125
126
127
128
129
130
131
        params,
        types.BlockStatement([tryStatement]),
        node.computed
    );
} else if (types.isFunctionDeclaration(node)) {
    func = types.functionDeclaration(
        node.id,
        params,
        types.BlockStatement([tryStatement]),
        isGenerator,
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)