How to use the callExpression function from @babel/types

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

@babel/types.callExpression is a method used to determine if a given AST node represents a function call expression in a JavaScript program.

1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
  if (name.startsWith("super")) {
    path.node.arguments.unshift(thisArgument);
  } else {
    path.node.arguments[0] = thisArgument;
  }
  let callExpr = types.callExpression(expr, path.node.arguments);
  path.replaceWith(callExpr);
} else if (
  name == "this.base.apply" ||
  name == "this.base.call"
fork icon257
star icon757
watch icon76

+ 1854 other calls in file

1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
  path: NodePath<>,
  dependency: InternalDependency,
  state: State,
): void {
  path.replaceWith(
    t.callExpression(t.identifier('require'), [
      createModuleIDExpression(dependency, state),
      t.stringLiteral(dependency.name),
    ]),
  );
fork icon615
star icon0
watch icon99

+ 667 other calls in file

How does @babel/types.callExpression work?

@babel/types.callExpression is a function provided by the Babel library that creates an AST (Abstract Syntax Tree) node representing a function call expression. The function call expression is defined by two components: the callee, which is the function being called, and the arguments, which are the values passed to the function.

The @babel/types.callExpression function takes in two arguments: the callee argument, which is an expression node representing the function being called, and the args argument, which is an array of expression nodes representing the values passed to the function.

The resulting AST node represents the function call expression and can be used in further processing of JavaScript code by Babel or other tools.

74
75
76
77
78
79
80
81
82
83
    false,
    true,
  );
  let objectPattern = t.objectPattern([objectProperty]);
  let callee = t.identifier(USE_TRANSLATION_FUN);
  let useTranslationCall = t.callExpression(callee, []);
  let tDeclarator = t.variableDeclarator(objectPattern, useTranslationCall);
  const node = t.variableDeclaration('const', [tDeclarator]);
  return node;
};
fork icon79
star icon190
watch icon15

+ 63 other calls in file

245
246
247
248
249
250
251
252
253
254
255
256
  ];


  args = args.map(_unwrapExpressionStatement);
  args = args.concat(randomArguments(path));


  return babelTypes.callExpression(
      babelTypes.identifier('__callRandomFunction'),
      args);
}

fork icon35
star icon92
watch icon29

+ 21 other calls in file

Ai Example

1
2
3
4
5
6
const t = require("@babel/types");

// Create an AST node representing the call expression 'foo(42)'
const callExpressionNode = t.callExpression(t.identifier("foo"), [
  t.numericLiteral(42),
]);

In this example, we are using @babel/types to create a call expression that invokes a function called foo with a single argument of the numeric literal 42.

362
363
364
365
366
367
368
369
370
371
  itemExpression = getInputJsxExpression(item)
}

const expression = t.jsxExpressionContainer(
  t.callExpression(
    t.callExpression(t.identifier('getFieldDecorator'), [
      t.stringLiteral(key),
      t.objectExpression(configArray)
    ]),
    itemExpression
fork icon1
star icon25
watch icon0

+ 7 other calls in file

48
49
50
51
52
53
54
55
56
57
const {code} = generate(
  t.variableDeclaration('const', [
    t.variableDeclarator(
      t.identifier(name),
      t.addComment(
        t.callExpression(t.identifier('createIconComponent'), [
          // The name of the generated icon
          t.stringLiteral(name),
          // The className used on the underlying <svg> element
          t.stringLiteral(`octicon octicon-${key}`),
fork icon912
star icon0
watch icon0

+ 3 other calls in file

145
146
147
148
149
150
151
152
153
154
}

if (fnName) {
  // AST of `{foo('baz')}`
  attribute.value = t.jSXExpressionContainer(
    t.callExpression(t.identifier(fnName), [
      t.stringLiteral(nameSpacedId),
    ]),
  );
}
fork icon6
star icon20
watch icon2

260
261
262
263
264
265
266
267
268
269
    return generateObjProperty('handler', step.handler);
  }
  addDepModule(REQUIRE_FROM_STRING_VAR, REQUIRE_FROM_STRING_MODULE, scope);
  return t.objectProperty(
    t.identifier('handler'),
    t.callExpression(t.identifier(REQUIRE_FROM_STRING_VAR), [
      t.stringLiteral(step.handler)
    ])
  );
}
fork icon1
star icon2
watch icon1

+ 521 other calls in file

206
207
208
209
210
211
212
213
214
215
}

static indexOfWhatEq0 (id, what) {
  return btypes.binaryExpression(
    '===',
    btypes.callExpression(EH.memberExpression(id, 'indexOf'), [
      EH.makeStringLiteral(what)
    ]),
    btypes.numericLiteral(0)
  )
fork icon1
star icon1
watch icon2

+ 95 other calls in file

105
106
107
108
109
110
111
112
113
114
    value: node.arguments[0].quasis[0].value.raw,
  };
} else if (options.synchronousImport) {
  const str = node.arguments[0];
  const newNode = t.expressionStatement(
    t.callExpression(t.import(), [str])
  );

  // @ts-ignore
  newNode.__replaced = true;
fork icon0
star icon1
watch icon1

+ 21 other calls in file

214
215
216
217
218
219
220
221
222
223
if (isAssignment) {
  const value = superProp.parentPath.node.right;
  args.push(value);
}

const call = t.callExpression(t.identifier(superBinding), args);

if (isCall) {
  superProp.parentPath.unshiftContainer("arguments", t.thisExpression());
  superProp.replaceWith(t.memberExpression(call, t.identifier("call")));
fork icon0
star icon1
watch icon1

+ 40 other calls in file

72
73
74
75
76
77
78
79
80
81
  console.log(generator(node, genOpts).code)
}

const expectToAva = {
  toBeGolden: (actual, filePath) =>
    btypes.callExpression(
      btypes.memberExpression(
        btypes.memberExpression(
          btypes.identifier('t'),
          btypes.identifier('context')
fork icon0
star icon15
watch icon1

+ 431 other calls in file

1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
                    return types.callExpression(newCallee, newArguments);
                }
            } else if (destNode.callee.type === 'MemberExpression'
                && destNode.callee.object.type === 'Identifier'
                && destNode.callee.property.type === 'Identifier') {
                return types.callExpression(destNode.callee, arguments_);
            }
        }
    }
},
fork icon0
star icon2
watch icon1

+ 23 other calls in file

198
199
200
201
202
203
204
205
206
207
208
    });
    `
};


const buildReexportsFromMeta = (meta, metadata, constantReexports) => {
  const namespace = metadata.lazy ? t.callExpression(t.identifier(metadata.name), []) : t.identifier(metadata.name);
  const {
    stringSpecifiers
  } = meta;
  return Array.from(metadata.reexports, ([exportName, importName]) => {
fork icon0
star icon1
watch icon1

+ 87 other calls in file

499
500
501
502
503
504
505
506
507
508
  if (arrayLikeIsIterable) {
    args.unshift(this.hub.addHelper(helperName));
    helperName = "maybeArrayLike";
  }

  return t.callExpression(this.hub.addHelper(helperName), args);
}

hasLabel(name) {
  return !!this.getLabel(name);
fork icon0
star icon1
watch icon1

+ 33 other calls in file

464
465
466
467
468
469
470
471
472
473
// When normalization is not needed we need to manually compile text into vNodes
for (var j = 0; j < vChildren.elements.length; j++) {
  var aChild = vChildren.elements[j];

  if (aChild.type === 'StringLiteral') {
    vChildren.elements[j] = t.callExpression(
      t.identifier(opts.pragmaTextVNode || 'createTextVNode'),
      [aChild]
    );
  }
fork icon27
star icon77
watch icon14

+ 13 other calls in file

105
106
107
108
109
110
111
112
113
114
  )
}
function converJSX(path) {
  if (path.isJSXElement()) {
    const tagName = path.node.openingElement.name.name
    return t.callExpression(t.identifier('h'), [
      tagName.charCodeAt(0) < 96 ? t.identifier(tagName) : t.stringLiteral(tagName),
      convertAttribute(path.node.openingElement.attributes),
      t.ArrayExpression(
        path
fork icon2
star icon39
watch icon0

106
107
108
109
110
111
112
113
114
115
116
            ifAttr.value.expression,
            t.returnStatement(ifNode),
            getAlternteAST(elseIfBindings, elseBinding)
        )
    ]));
    const targetAST = t.callExpression(callee, []);
    path.replaceWith(targetAST);
}


function getAlternteAST(elseIfBindings, elseBinding, index=0) {
fork icon3
star icon11
watch icon0

26
27
28
29
30
31
32
33
34
35
                return expr;
            }
            break;
    }
    // Pass it to insulate.
    return t.callExpression(t.identifier(insulateFunction), [expr]);
};

const makeSafeImport = (local) => {
    if (local.type === 'Identifier' &&
fork icon7
star icon31
watch icon5

+ 11 other calls in file

124
125
126
127
128
129
130
131
132
133
    if (t.isAssignmentPattern(obj)) {
        args[i] = obj.left
    }
})
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
fork icon221
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)