How to use the isBlockStatement function from @babel/types
Find comprehensive JavaScript @babel/types.isBlockStatement code examples handpicked from public code repositorys.
@babel/types.isBlockStatement is a function that checks whether a given object is a block statement node in the abstract syntax tree of a JavaScript program.
GitHub: liuhahi/rax
154 155 156 157 158 159 160 161 162 163
let childNode = null; let itemName = 'item'; let indexName = 'index'; if (t.isFunction(args[0])) { // { foo.map(() => {}) } const returnEl = t.isBlockStatement(args[0].body) // () => { return xxx } ? findReturnElement(args[0].body).node // () => (<jsx></jsx) : args[0].body;
+ 3 other calls in file
91 92 93 94 95 96 97 98 99
path.traverse({ ThisExpression (thisPath) { 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'));
How does @babel/types.isBlockStatement work?
@babel/types.isBlockStatement
works by taking an object and checking whether it matches the structure of a block statement node in the AST (Abstract Syntax Tree) of a JavaScript program.
This involves checking whether the object has a specific type (BlockStatement
) and whether it has certain properties that are expected for a block statement node, such as body
, which is an array of statements enclosed in curly braces.
If the object meets these criteria, @babel/types.isBlockStatement
returns true
, indicating that the object is a block statement node. Otherwise, it returns false
.
GitHub: suchipi/tofu
183 184 185 186 187 188 189 190 191 192
) { return node.end - 1; } const shouldEndBlock = t.isBlockStatement(node) && ((isRight && start !== node.end) || recursionDepth > 0); if (t.isBlockStatement(node) && node.body.length == 0) { const blockStart = node.start + 1;
+ 13 other calls in file
95 96 97 98 99 100 101 102 103 104
function hasBinding(node, name) { 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 (
+ 3 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const t = require("@babel/types"); const blockStatementNode = t.blockStatement([ t.variableDeclaration("const", [ t.variableDeclarator(t.identifier("x"), t.numericLiteral(42)), ]), ]); console.log(t.isBlockStatement(blockStatementNode)); // true const variableDeclarationNode = t.variableDeclaration("const", [ t.variableDeclarator(t.identifier("y"), t.numericLiteral(123)), ]); console.log(t.isBlockStatement(variableDeclarationNode)); // false
In this example, we use @babel/types to create an AST node that represents a block statement containing a variable declaration. We then pass this node to t.isBlockStatement to check whether it is a block statement node, which returns true. Next, we create another AST node representing a variable declaration, and pass it to t.isBlockStatement, which returns false since it is not a block statement node.
66 67 68 69 70 71 72 73 74
if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) { return false; } if (this.isExpression()) { return t.isBlockStatement(replacement); } else if (this.isBlockStatement()) { return t.isExpression(replacement); }
94 95 96 97 98 99 100 101 102 103 104
}; function hasBinding(node, name) { 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) || types.isFunctionExpression(node) || types.isArrowFunctionExpression(node)) { return node.id && node.id.name === name || node.params.some(param => types.isIdentifier(param) && param.name === name); } else if (types.isVariableDeclaration(node)) {
54 55 56 57 58 59 60 61 62 63
// 递归向上找异步函数的 node 节点 while (path && path.node) { let parentPath = path.parentPath; if ( // 找到 async Function t.isBlockStatement(path.node) && isAsyncFuncNode(parentPath.node) ) { let tryCatchAst = t.tryStatement( path.node,
87 88 89 90 91 92 93 94 95 96
*/ if( astNode.init && t.isArrowFunctionExpression(astNode.init) && astNode.init.body && t.isBlockStatement(astNode.init.body) ) { astNode.init.body = addFunctionsCore(astNode.init.body, apiUrl); astNode.init.body = addDataHooksToNodeCore(astNode.init.body);
@babel/types.identifier is the most popular function in @babel/types (20936 examples)