How to use the isCallExpression function from @babel/types

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

@babel/types.isCallExpression is a function in the Babel Types package that checks if a node in an Abstract Syntax Tree (AST) represents a call expression.

16
17
18
19
20
21
22
23
24
const chainedRegular = 4;

function match(path, ctx, explicitOnly) {
    const node = path.node;
    const isMethodCall = (
        t.isCallExpression(node) &&
            t.isMemberExpression(node.callee) &&
            node.callee.computed === false
        );
fork icon156
star icon244
watch icon6

75
76
77
78
79
80
81
82
83
84
};


function isAopBlock(path, block, AOP, cb) {
    t.isExpressionStatement(block)
    && t.isCallExpression(block.expression)
    && t.isMemberExpression(get(block, 'expression.callee'))
    && get(block, 'expression.callee.property.name') === 'after'
    && cb(path, block, AOP);
}
fork icon17
star icon123
watch icon10

+ 7 other calls in file

How does @babel/types.isCallExpression work?

@babel/types.isCallExpression is a function in the @babel/types package that takes a node object as input and checks whether it represents a CallExpression node in an Abstract Syntax Tree (AST) by comparing its type with the CallExpression node type.

1
2
3
4
5
6
7
8
9
10
var moduleDetails = require('./get-module-function');
var nodeResolve = require('resolve');
var browserResolve = require('browser-resolve');

function isRequireResolve (path) {
  return t.isCallExpression(path.node) &&
    t.isMemberExpression(path.node.callee) &&
    path.node.callee.object.name === 'require' &&
    path.node.callee.property.name === 'resolve';
}
fork icon11
star icon33
watch icon8

+ 19 other calls in file

126
127
128
129
130
131
132
133
134
135
    return argsMap.get(componentNode)
  }
}

const isExtended = props => (
  t.isCallExpression(props) && (props.callee.name === '_extends' || props.callee.name === '_objectSpread')
)

module.exports = () => {
  const addDataProp = createAddDataProp()
fork icon10
star icon44
watch icon10

+ 9 other calls in file

Ai Example

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

const node = t.callExpression(t.identifier("foo"), [t.stringLiteral("bar")]);

if (t.isCallExpression(node)) {
  console.log("Node is a CallExpression!");
} else {
  console.log("Node is not a CallExpression!");
}

In this example, the t.isCallExpression function checks whether the node variable is a CallExpression, which it is. If it were not, the else branch would execute instead.

45
46
47
48
49
50
51
52
53
54

const isReturned = (p) => {
  const { parent } = p

  if (
    t.isCallExpression(parent) ||
    t.isConditionalExpression(parent) ||
    t.isSequenceExpression(parent) ||
    t.isLogicalExpression(parent)
  ) {
fork icon10
star icon44
watch icon10

+ 17 other calls in file

213
214
215
216
217
218
219
    `Only EventHandlers are supported in Mini Program, eg: onClick/onChange, instead of "${attributeName}".`,
  );
const callExp = expression.body;
const args = callExp.arguments;
const { attributes } = parentPath.parentPath.node;
const fnExpression = t.isCallExpression(callExp) ? callExp.callee : expression;
const name = dynamicEvents.add({
fork icon663
star icon1
watch icon1

+ 3 other calls in file

197
198
199
200
201
202
203
204
205
206
 * 1. 对象成员访问 var a = this.x;
 * 2. 函数调用参数 console.log(a, b, c, this)
 * 合法的场景,需要把this替换为(this.constructor === Window ? {} : this)
 */
if (path.isThisExpression()) {
  if (!(babelTypes.isMemberExpression(path.parent) || babelTypes.isCallExpression(path.parent))) {
    let message = `${filePath}:${path.node.loc.start.line + startLine - 1}:${path.node.loc.start.column}: SyntaxError: invalid usage of identifier \`this\`\n`;
    wccError = new error.WccError(error.CODE.INVALID_THIS, message);
    path.stop();
    return;
fork icon10
star icon0
watch icon0

+ 5 other calls in file

113
114
115
116
117
118
119
120
121
122
  return { moduleName, bindings: [] };
}

function isRequire(declarator) {
  return (
    t.isCallExpression(declarator.init) && t.isIdentifier(declarator.init.callee) && declarator.init.callee.name === 'require'
  );
}

module.exports = {
fork icon3
star icon13
watch icon21

93
94
95
96
97
98
99
100
101
102
  };

case isValue(node):
  return { type: 'value' };

case types.isCallExpression(node):
  return createDescriptor({
    node: await getReturnValue({ node: node.callee, ast, cwd }),
    ast,
    cwd,
fork icon2
star icon11
watch icon222

+ 7 other calls in file

15
16
17
18
19
20
21
22
23
24
  });
},
ExpressionStatement(path) {
  const expression = path.node.expression;
  if (
    t.isCallExpression(expression) &&
    !t.isThisExpression(expression.callee.object)
  ) {
    path.traverse(
      {
fork icon1
star icon8
watch icon2

97
98
99
100
101
102
103
104
105
106
} else if ( t.isLogicalExpression(path.node.init) ){
  // No handling for ^^ right now
} else if ( t.isObjectExpression(path.node.init) ) {
  // No handling for ^^ right now - i think this is fine generally actually - if objects
  // are being built up, leaving this will be clearer
} else if ( t.isCallExpression(path.node.init) ) {
  // No handling for ^^ right now
  // One example is var U2 = document.createElement("canvas");
  // could be good to make that clearer later
} else if ( t.isMemberExpression(path.node.init) ){
fork icon1
star icon6
watch icon7

276
277
278
279
280
281
282
283
284
285
  t.isMemberExpression(node.left) &&
  (node.left.property.value || node.left.property.name) &&
  node.right.body.body.length === 1 &&
  t.isReturnStatement(node.right.body.body[0]) &&
  (t.isBinaryExpression(node.right.body.body[0].argument) ||
    t.isCallExpression(node.right.body.body[0].argument))
) {
  // We find proxy function and fill in scopeIdToBinaryOpPath(scopeUID -> Map<key, pathToBinaryNode>).
  // Key is created using the ID name and the property value(n['xyz'] = function ... -> key = n_xyz).
  // In this way we get a structure where each scope is mapped to a bindings.
fork icon0
star icon6
watch icon1

+ 36 other calls in file

8
9
10
11
12
13
14
15
16
17
/**
 * todo text解析模板都在js里面处理,在js里去合并?统一babel parse入口
 */
function prevent$tRecursive(fn) {
  return (path) => {
    if (path.parent && t.isCallExpression(path.parent) && t.isIdentifier(path.parent.callee, { name: '$t' })) {
      const firstArgNode = path.parent.arguments[0];
      if (firstArgNode.type === 'StringLiteral') {
        addI18nKeys(firstArgNode.value);
      } else if (firstArgNode.type === 'TemplateLiteral') {
fork icon0
star icon3
watch icon2

93
94
95
96
97
98
99
100
101
102
const parentNode = thisPath.parentPath.parentPath.parent;
const isValid = t.isExpressionStatement(parentNode) || 
    t.isVariableDeclaration(parentNode) ||
    t.isBlockStatement(parentNode) || 
    t.isJSXElement(parentNode) || 
    t.isCallExpression(parentNode) || 
    (t.isJSXAttribute(parentNode) && !parentNode.name.name.startsWith('on'));

if (isValid) {
    // prop
fork icon65
star icon1
watch icon1

261
262
263
264
265
266
267
268
269
270

/**
 * Returns the Babel node from this FbtNode only if it's a BabelNodeCallExpression
 */
getCallNode(): ?BabelNodeCallExpression {
  return isCallExpression(this.node) ? this.node : null;
}

/**
 * Returns the list of BabelNode arguments of this fbt node
fork icon186
star icon0
watch icon1

251
252
253
254
255
256
257
258
259
260
        referencePath.replaceInline(variableValue);
    } catch (e) {
        debugger
    }
    referencePath.scope.crawl();
} else if (types.isFunction(variableValue) && types.isCallExpression(referencePath.parentPath)) {
    // 函数里的表达式只有一个return, 直接替换
    const callPath = referencePath.parentPath;
    const returnStatement = variableValue.body.body[0].argument;
    const nodeArguments = callPath.node.arguments;
fork icon2
star icon1
watch icon1

+ 82 other calls in file

89
90
91
92
93
94
95
96
97
98

if (t.isStringLiteral(node) || t.isPlaceholder(node, {
  expectedNode: "StringLiteral"
})) {
  type = "string";
} else if (t.isNewExpression(parent) && key === "arguments" || t.isCallExpression(parent) && key === "arguments" || t.isFunction(parent) && key === "params") {
  type = "param";
} else if (t.isExpressionStatement(parent) && !t.isPlaceholder(node)) {
  type = "statement";
  ancestors = ancestors.slice(0, -1);
fork icon0
star icon1
watch icon1

+ 26 other calls in file

450
451
452
453
454
455
456
457
458
459
  }
}

const children = traverseExpr(forReturnStatementArgument, state)
// 支付宝小程序在 block 标签上使用 key 时顺序不能保障
if (state.options.platform.name === 'mp-alipay' && t.isCallExpression(forReturnStatementArgument) && children &&
  children.type) {
  children.attr = children.attr || {}
  Object.assign(children.attr, attr)
  return children
fork icon0
star icon1
watch icon1

+ 11 other calls in file

229
230
231
232
233
234
235
236
237
238
        }
      }
      break
  }
} else if (
  t.isCallExpression(callee) &&
  t.isIdentifier(callee.callee) &&
  callee.callee.name === METHOD_RESOLVE_FILTER
) {
  // multi filter
fork icon0
star icon1
watch icon0

277
278
279
280
281
282
283
284
285
286
287
288
const isExpect = path =>
  btypes.isMemberExpression(path.node.callee) &&
  btypes.isIdentifier(path.node.callee.object.callee, isIdExpectOpts)


const isRequire = declaration =>
  btypes.isCallExpression(declaration.init) &&
  declaration.init.callee.name === 'require'


const argZeroEquals = (args, shouldEq) =>
  args ? args[0].value === shouldEq : false
fork icon0
star icon15
watch icon1

+ 4 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)