How to use the functionExpression function from @babel/types

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

1557
1558
1559
1560
1561
1562
1563
1564
1565
).program.body;
inject.push(tmp[0]);
let block = types.blockStatement(inject);
let rootFn = types.expressionStatement(
  types.callExpression(
    types.functionExpression(null, [], block),
    []
  )
);
fork icon257
star icon758
watch icon0

+ 21 other calls in file

179
180
181
182
183
184
185
186
187
188
  }
}

if (vars.length > 0) {
  ref.replaceWith(
    t.functionExpression(
      null,
      [
        t.identifier('vm'),
        !isGlobal && t.identifier('existing') // Global vars are handled differently
fork icon27
star icon429
watch icon6

+ 15 other calls in file

36
37
38
39
40
41
42
43
44
          babelTypes.blockStatement([])));
}

function wrapTryCatchInFunction(node) {
  const ret = wrapTryCatch(babelTypes.returnStatement(node));
  const anonymousFun = babelTypes.functionExpression(
      null, [], babelTypes.blockStatement([ret]));
  return babelTypes.callExpression(anonymousFun, []);
}
fork icon3
star icon25
watch icon0

60
61
62
63
64
65
66
67
68
69
    let { body } = node;
    // 判断是不是 blockStatement,不是的话让他变成 blockStatement
    if (!t.isBlockStatement(body)) {
      body = t.blockStatement([body]);
    }
    const functionExpression = t.functionExpression(null, params, body);
    // 替换原来的函数
    path.replaceWith(functionExpression);
  },
},
fork icon7
star icon16
watch icon1

+ 11 other calls in file

319
320
321
322
323
324
325
326
327
    let id = path.parent.id;
    let params = node.params;
    let body= types.blockStatement([
      types.returnStatement(node.body)
    ]);
    let functionExpression = types.functionExpression(id,params,body,false,false);
    path.replaceWith(functionExpression);
  }
})
fork icon3
star icon14
watch icon7

+ 9 other calls in file

54
55
56
57
58
59
60
61
62
63
if (replacer) {
  replacer.replaceWith(
    t.variableDeclaration('const', [
      t.variableDeclarator(
        t.identifier(EXPORTED_DEF),
        t.functionExpression(id, params, body, generator, async)
      )
    ])
  );
}
fork icon663
star icon0
watch icon1

+ 19 other calls in file

259
260
261
262
263
264
265
266
267
268
  let body = node.body
  // 判断是不是 blockStatement,不是的话让他变成 blockStatement
  if (!t.isBlockStatement(body)) {
    body = t.blockStatement([body])
  }
  const functionExpression = t.functionExpression(null, params, body)
  // 替换原来的函数
  path.replaceWith(functionExpression)
}
```
fork icon2
star icon0
watch icon1

+ 103 other calls in file

214
215
216
217
218
219
220
221
    btypes.numericLiteral(0)
  )
}

static setterOverride (sname, pi, rwcode) {
  return btypes.functionExpression(
    null,
    [pi],
fork icon1
star icon1
watch icon2

+ 11 other calls in file

49
50
51
52
53
54
55
56
57
58
const program = t.program([
  ...nodes,
  t.expressionStatement(
    t.callExpression(
      t.memberExpression(
        t.functionExpression(null, [], t.blockStatement(inner)),
        t.identifier("call")
      ),
      [t.identifier("module.exports")]
    )
fork icon0
star icon1
watch icon1

+ 58 other calls in file

216
217
218
219
220
221
} else if (/created|attached|ready|moved|detached/.test(methodName)) {
  // Component life cycle fn.
  if (!isComponent()) return
  const fn = t.objectProperty(
    t.identifier(methodName),
    t.functionExpression(null, path.node.params, path.node.body, path.node.generator, path.node.async)
fork icon12
star icon84
watch icon1

658
659
660
661
662
663
664
665
666
667
668
 * @param {*} path
 * @returns
 */
function objectMethod2FunctionExpression (path) {
    if (!path) global.log("objectMethod2FunctionExpression 参数错误")
    return t.functionExpression(path.id, path.params, path.body)
}


/**
 * 获取components ast里面的props列表
fork icon221
star icon0
watch icon0

+ 5 other calls in file

127
128
129
130
131
132
133
134
135
136
    })
    var me = t.memberExpression(t.thisExpression(), t.identifier(newFunName))
    var callExp = t.callExpression(me, args)
    var expStatement = t.expressionStatement(callExp)
    var blockStatement = t.blockStatement([expStatement])
    var funExp = t.functionExpression(null, args, blockStatement)
    onLoadFunNode.value = funExp
} else if (t.isObjectMethod(onLoadFunNode)) {
    //onLoad(a){}
    var args = onLoadFunNode.params
fork icon221
star icon0
watch icon0

51
52
53
54
55
56
57
58
59
60
            }
        }
    },
    MemberExpression: function (path, state) {
        if (isConsoleLog(path) && path.parentKey === 'init') {
            var myFun = t.functionExpression(null, [], t.blockStatement([]));
            path.replaceWith(myFun);
        }
    },
};
fork icon0
star icon1
watch icon1

+ 35 other calls in file

64
65
66
67
68
69
70
71
72
73
74


function functionFromProgram(
program,
parameters)
{
  return t.functionExpression(
  t.identifier(''),
  parameters.map(makeIdentifier),
  t.blockStatement(program.body, program.directives));

fork icon1
star icon0
watch icon1

161
162
163
164
165
166
167
168
169
170
    params[0] = t.identifier(p0.name);
}
const isArrow = t.isArrowFunctionExpression(fn);
const fnExp = isArrow
    ? t.arrowFunctionExpression(params, fn.body, fn.async)
    : t.functionExpression(undefined, params, fn.body, fn.generator, fn.async);
// fnExp.end = fn.end;
fnExp.extra = fn.extra;
fnExp.generator = fn.generator;
// fnExp.innerComments = fn.innerComments;
fork icon0
star icon0
watch icon1

+ 25 other calls in file

133
134
135
136
137
138
139
140
141
142
  );
  nodes.push(constructorFn);
} else if (item.kind === 'method') {
  let prototypeMem = t.memberExpression(className, t.identifier('prototype'));
  let methodMem = t.memberExpression(prototypeMem, item.key);
  let memberFunction = t.functionExpression(
    item.key,
    item.params,
    item.body,
    item.generator,
fork icon0
star icon0
watch icon1

44
45
46
47
48
49
50
51
52
53
54
`);


function classOrObjectMethod(path, callId) {
  const node = path.node;
  const body = node.body;
  const container = t.functionExpression(null, [], t.blockStatement(body.body), true);
  body.body = [t.returnStatement(t.callExpression(t.callExpression(callId, [container]), []))];
  node.async = false;
  node.generator = false;
  path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
fork icon0
star icon0
watch icon0

15
16
17
18
19
20
21
22
23
24
let retSta2 = t.returnStatement(t.binaryExpression("+", binExpr2, t.numericLiteral(1000)));
let retSta3 = t.returnStatement(t.binaryExpression("+", binExpr3, t.numericLiteral(1000)));
let bloSta2 = t.blockStatement([retSta2]);
let bloSta3 = t.blockStatement([retSta3]);
let funcExpr2 = t.functionExpression(null, [a, b], bloSta2);
let funcExpr3 = t.functionExpression(null, [a, b], bloSta3);
let objProp1 = t.objectProperty(t.identifier('name'), t.stringLiteral('xiaojianbang'));
let objProp2 = t.objectProperty(t.identifier('add'), funcExpr2);
let objProp3 = t.objectProperty(t.identifier('mul'), funcExpr3);
let objExpr = t.objectExpression([objProp1, objProp2, objProp3]);
fork icon0
star icon0
watch icon0

61
62
63
64
65
66
67
68
69
70
}

if (t.isProperty(node)) {
  value = node.value;
} else if (t.isObjectMethod(node) || t.isClassMethod(node)) {
  value = t.functionExpression(null, node.params, node.body, node.generator, node.async);
  value.returnType = node.returnType;
}

const inheritedKind = toKind(node);
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)