How to use the isLogicalExpression function from @babel/types

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

@babel/types.isLogicalExpression is a function provided by the Babel library that checks whether a given abstract syntax tree (AST) node represents a logical expression in JavaScript.

48
49
50
51
52
53
54
55
56

if (
  t.isCallExpression(parent) ||
  t.isConditionalExpression(parent) ||
  t.isSequenceExpression(parent) ||
  t.isLogicalExpression(parent)
) {
  return isReturned(p.parentPath)
}
fork icon10
star icon44
watch icon10

+ 17 other calls in file

161
162
163
164
165
166
167
168
169
170
},
isLoopMap(astPath: any) {
    if (
        t.isJSXExpressionContainer(astPath.parentPath) ||
        t.isConditionalExpression(astPath.parentPath) ||
        t.isLogicalExpression(astPath.parentPath)
    ) {
        var callee = astPath.node.callee;
        return callee.type == 'MemberExpression' && callee.property.name === 'map';
    }
fork icon330
star icon0
watch icon0

+ 3 other calls in file

How does @babel/types.isLogicalExpression work?

@babel/types.isLogicalExpression is a function provided by the Babel library that takes in an AST node object and checks whether it represents a logical expression in JavaScript. To determine whether the node is a logical expression, the function checks the node's type property to ensure it is set to 'LogicalExpression', and checks its operator property to ensure it is set to one of the valid logical operators ('&&', '||', or '??'). If both checks pass, the function returns true, indicating that the node represents a logical expression. Otherwise, it returns false. This function can be useful when working with AST nodes in Babel plugins or other code transformation tools, allowing you to easily determine whether a given node represents a logical expression and take appropriate action based on that information.

503
504
505
506
507
508
509
510
511
512
513
function logicalExpressionHandle (ast) {
    traverse(ast, {
        noScope: true,
        LogicalExpression (path) {
            let parentPath = path.parentPath
            if (t.isIfStatement(parentPath) || t.isLogicalExpression(parentPath)) {
                let left = path.get("left")
                let right = path.get("right")


                if (t.isBinaryExpression(left) && t.isBinaryExpression(right)) {
fork icon220
star icon0
watch icon0

199
200
201
202
203
204
205
206
207
208
    return;
  }
}

if (
  (t.isBinaryExpression(node) || t.isLogicalExpression(node)) &&
  key.length == 1
) {
  let newOperator = key;
  if (key == '=') {
fork icon4
star icon0
watch icon1

+ 3 other calls in file

Ai Example

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

const myNode = t.logicalExpression("||", t.identifier("x"), t.identifier("y"));

console.log(t.isLogicalExpression(myNode)); // true
console.log(t.isLogicalExpression(t.identifier("z"))); // false

In this example, we use @babel/types to create an AST node object representing a logical OR expression with two identifier operands, 'x' and 'y'. We then use @babel/types.isLogicalExpression to check whether this node represents a logical expression, which returns true since it is a valid logical OR expression. We also use @babel/types.isLogicalExpression to check whether a different node representing an identifier, 'z', represents a logical expression, which returns false since it is not a valid logical expression.

107
108
109
110
111
112
113
114
115
116
      node: await getMemberProperty({ node: node, ast, cwd }),
      ast,
      cwd,
    });

  case types.isLogicalExpression(node):
    return await createDescriptor({ node: node.right, ast, cwd });
}

throw `Cannot resolve arguments for ${node.type}`;
fork icon2
star icon11
watch icon222

+ 3 other calls in file

92
93
94
95
96
97
98
99
100
101
  // No handling for ^^ right now
} else if ( t.isUnaryExpression(path.node.init) ){
  // No handling for ^^ right now
} else if ( t.isConditionalExpression(path.node.init) ){
  // No handling for ^^ right now
} 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
fork icon1
star icon6
watch icon7

216
217
218
219
220
221
222
223
224
225
226
227
      return t.isLogicalExpression(parent, {
        operator: "??"
      });


    case "??":
      return t.isLogicalExpression(parent) && parent.operator !== "??";
  }
}


function Identifier(node, parent, printStack) {
fork icon0
star icon0
watch icon0

+ 5 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)