How to use the isReturnStatement function from @babel/types

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

@babel/types.isReturnStatement is a method provided by the Babel Types package that checks if a given AST (Abstract Syntax Tree) node is a return statement.

122
123
124
125
126
127
128
129
130
131
if (fnExp) {
  const { params, body } = fnExp;
  let jsxEl = body;
  if (t.isBlockStatement(body)) {
    const returnEl = body.body.filter(el =>
      t.isReturnStatement(el),
    )[0];
    if (returnEl) jsxEl = returnEl.argument;
  }
  const {
fork icon663
star icon11
watch icon0

+ 17 other calls in file

53
54
55
56
57
58
59
60
61
    t.isLogicalExpression(parent)
  ) {
    return isReturned(p.parentPath)
  }

  return t.isReturnStatement(parent) || t.isArrowFunctionExpression(parent)
}

const projectPath = process.cwd().toLowerCase()
fork icon10
star icon44
watch icon10

+ 17 other calls in file

How does @babel/types.isReturnStatement work?

The @babel/types.isReturnStatement method is used to determine if a given object is a Babel abstract syntax tree (AST) node representing a return statement. It checks if the object has a type property with the value ReturnStatement.

65
66
67
68
69
70
71
72
73
  this.state.props[this.childKey].value = body;
} else if (t.isBlockStatement(body)) {
  // Object/Block array
  var childNodes = body.body;

  if (childNodes.length === 1 && t.isReturnStatement(childNodes[0])) {
    this.state.props[this.childKey].value = childNodes[0].argument;
  }
} // validator
fork icon1
star icon8
watch icon2

+ 5 other calls in file

106
107
108
109
110
111
112
113
114
115
function get_real_func(tmp_node){
    if (t.isFunctionExpression(tmp_node)){
        var node_ = tmp_node;

        var func_body = node_.body.body;
        if (func_body.length == 1  && t.isReturnStatement(func_body[0])){
            var body_return = func_body[0];

            if (t.isCallExpression(body_return.argument) && t.isMemberExpression(body_return.argument.callee)){
                let tmp_obj_name = body_return.argument.callee.object.name;
fork icon1
star icon4
watch icon1

+ 41 other calls in file

Ai Example

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

const node = t.returnStatement(
  t.binaryExpression("+", t.identifier("a"), t.identifier("b"))
);

console.log(t.isReturnStatement(node)); // true

In this example, we create a ReturnStatement node using the t.returnStatement() function from @babel/types. We then pass this node to t.isReturnStatement() and log the result to the console, which will output true since the node variable holds a ReturnStatement.

174
175
176
177
178
179
180
181
182
183
) {
  return start;
}

if (
  t.isReturnStatement(node) &&
  recursionDepth > 0 &&
  !node.argument &&
  (isRight || start == node.end - 1)
) {
fork icon4
star icon0
watch icon1

+ 13 other calls in file

156
157
158
159
160
161
162
163
164
165
        },
    });
    break;
}
case "FunctionExpression": {
    if (t.isBlockStatement(property.value.body) && property.value.body.body != null && property.value.body.body.length == 1 && t.isReturnStatement(property.value.body.body[0])) {
        let returnNode = property.value.body.body[0].argument

        // 根据return语句的类型 分别进行替换
        switch (returnNode.type) {
fork icon189
star icon313
watch icon0

360
361
362
363
364
365
366
367
368
369
if (body.length) {
  if (body.length === 1) {
    const [firstNode] = body
    return this.getNodeValue(firstNode)
  } else {
    const hadReturn = body.find(item => t.isReturnStatement(item))
    if (hadReturn) {
      // TODO
      this.errorLog('getBlockStatementValue not cover hadReturn', node)
    } else {
fork icon3
star icon0
watch icon0

274
275
276
277
278
279
280
281
282
283
if (
  t.isFunctionExpression(node.right) &&
  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>).
fork icon0
star icon6
watch icon1

+ 36 other calls in file

118
119
120
121
122
123
124
125
126
127
    const name = p.node.id.name;
    const body = p.node.body.body;
    return body.length === 3 &&
        types.isVariableDeclaration(body[0]) && body[0].declarations.length === 1 && (types.isArrayExpression(body[0].declarations[0].init) || types.isCallExpression(body[0].declarations[0].init)) &&
        types.isExpressionStatement(body[1]) && types.isAssignmentExpression(body[1].expression) && body[1].expression.left.name === name &&
        types.isReturnStatement(body[2]);
}
if (!checkFunction(path)) {
    return;
}
fork icon2
star icon1
watch icon1

+ 82 other calls in file

261
262
263
264
265
266
267
268
269
270
if (types.isStringLiteral(propValue)) {
	newMap.set(propKey, propValue);
}
else if (types.isFunctionExpression(propValue)) {
	let retState = propValue.body.body;
	if (retState.length == 1 && types.isReturnStatement(retState[0])) {
		let argument = retState[0].argument;
		if (types.isCallExpression(argument)) {
			newMap.set(propKey, "Call");
		}
fork icon0
star icon0
watch icon1

+ 71 other calls in file

539
540
541
542
543
544
545
546
547
548
console.log(" *********** putting data into use effect node *********** ");
var node = template("\n      React.useEffect(()=>{ INIT_NODES; }, [])\n      ")({
  INIT_NODES: childNode
});
parentBlockExp.body = parentBlockExp.body.reduce(function (cum, item) {
  return types.isReturnStatement(item) ? [].concat(_toConsumableArray(cum), [node, item]) : [].concat(_toConsumableArray(cum), [item]);
}, []);
useEffectNode = node; // } else {
//   useEffectNode.expression.arguments[0].body.body.push(childNode);
// }
fork icon0
star icon0
watch icon1

Other functions in @babel/types

Sorted by popularity

function icon

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