How to use the isBlock function from @babel/types

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

@babel/types.isBlock is a method that checks whether a given object is a block node in the abstract syntax tree (AST).

96
97
98
99
100
101
102
103
104
105
if (Array.isArray(node)) {
  return node.some(ancestor => hasBinding(ancestor, name));
} else if (
  types.isProgram(node) ||
  types.isBlockStatement(node) ||
  types.isBlock(node)
) {
  return node.body.some(statement => hasBinding(statement, name));
} else if (
  types.isFunctionDeclaration(node) ||
fork icon0
star icon2
watch icon2

+ 3 other calls in file

How does @babel/types.isBlock work?

@babel/types.isBlock is a function in the @babel/types package that determines whether a given object is a block statement node in the Abstract Syntax Tree (AST) of a JavaScript program by checking its type and properties. Specifically, it checks whether the object has a type property set to "BlockStatement", indicating that it represents a block of code enclosed in curly braces.

Ai Example

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

const block = t.blockStatement([
  t.variableDeclaration("const", [
    t.variableDeclarator(
      t.identifier("x"),
      t.binaryExpression("+", t.numericLiteral(1), t.numericLiteral(2))
    ),
  ]),
]);

console.log(t.isBlock(block)); // true
console.log(t.isBlock(block.body[0].declarations[0].init)); // false

In this example, @babel/types.isBlock is used to check whether a given block is a block statement or not. The first console.log statement outputs true, since block is indeed a block statement. The second console.log statement outputs false, since the init property of the first declaration inside block is a binary expression, not a block statement.

Other functions in @babel/types

Sorted by popularity

function icon

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