How to use the blockStatement function from @babel/types
Find comprehensive JavaScript @babel/types.blockStatement code examples handpicked from public code repositorys.
@babel/types.blockStatement is a function in the Babel library that generates an AST node representing a block of code in JavaScript.
GitHub: qooxdoo/qooxdoo
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
path.node.body.forEach(node => inject.push(node)); tmp = babylon.parse( t.__className + ".$$dbClassInfo = $$dbClassInfo;" ).program.body; inject.push(tmp[0]); let block = types.blockStatement(inject); let rootFn = types.expressionStatement( types.callExpression( types.functionExpression(null, [], block), []
+ 21 other calls in file
208 209 210 211 212 213 214 215 216 217
// let a = t.Identifier('a') // let decryptFunc = t.functionDeclaration( // t.identifier(decrypt.name), // [a], // t.blockStatement([t.returnStatement( // t.callExpression( // t.identifier(decrypt), // [a]) // )]));
+ 23 other calls in file
How does @babel/types.blockStatement work?
@babel/types.blockStatement is a function that creates an AST node representing a block statement in JavaScript. It takes an array of body nodes as its argument, where each body node represents a statement inside the block. The @babel/types module is a collection of functions that generate Abstract Syntax Tree (AST) nodes for various constructs in the JavaScript programming language. An AST is a tree representation of the source code that allows you to perform programmatic operations on the code. A block statement in JavaScript is a set of statements enclosed in curly braces ({}) that are treated as a single unit of code. It is often used to group multiple statements together, for example, inside an if statement or a function. The @babel/types.blockStatement function generates an AST node for such a block of code. The generated AST node has the following structure: css Copy code {{{{{{{ class="!whitespace-pre hljs language-css">{ type: 'BlockStatement', body: [ /* an array of body nodes representing the statements inside the block */ ], directives: [] /* an array of directives, if any */ } Here, type indicates the type of the AST node (BlockStatement), body is an array of AST nodes representing the statements inside the block, and directives is an array of directives (e.g., 'use strict') that are used to enable certain features or enforce certain behavior. Overall, the @babel/types.blockStatement function is a building block that can be used to generate AST nodes representing blocks of code in JavaScript, which can then be manipulated or transformed using other functions in the Babel library.
GitHub: raxjs/miniapp
80 81 82 83 84 85 86 87 88 89
const forNode = handleParentListReturn(node, iterValue, parentList, dynamicValue, code); if (!t.isBlockStatement(body)) { // create a block return for inline return body = t.blockStatement([t.returnStatement(body)]); mapCallbackFnBodyPath.replaceWith(body); } // __jsxlist
+ 13 other calls in file
GitHub: egoist/styled-vue
185 186 187 188 189 190 191 192 193 194
null, [ t.identifier('vm'), !isGlobal && t.identifier('existing') // Global vars are handled differently ].filter(Boolean), t.blockStatement([ ...varDeclarations, t.returnStatement( isGlobal ? t.objectExpression(vars)
+ 15 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
const t = require("@babel/types"); const statements = [ t.expressionStatement( t.assignmentExpression( "=", t.identifier("x"), t.binaryExpression("+", t.identifier("y"), t.identifier("z")) ) ), t.returnStatement(t.identifier("x")), ]; const block = t.blockStatement(statements); console.log(block);
In this example, @babel/types.blockStatement is used to create a block statement node that contains two expression statements and a return statement. The resulting AST node can then be used by a code generator to output the corresponding JavaScript code.
79 80 81 82 83 84 85 86 87 88
// 防止命名重复的问题,暂时用var _self.componentBody.unshift( t.variableDeclaration('var', [t.variableDeclarator(decl, call)]) ) }) const blockStatements = t.blockStatement(_self.componentBody) path.replaceWith( t.functionDeclaration( path.node.id, [t.identifier('props')],
+ 11 other calls in file
122 123 124 125 126 127 128 129 130
statements.unshift(printHash()); const statement = babelTypes.tryStatement( babelTypes.blockStatement(statements), babelTypes.catchClause( babelTypes.identifier('e'), babelTypes.blockStatement([]))); this.annotate(statement, 'Print variables and exceptions from section'); return statement; }
+ 3 other calls in file
GitHub: 2833844911/cy_jsvmp
62 63 64 65 66 67 68 69 70 71
if (path.node.body.type == "BlockStatement"){ body = body.concat(path.node.body.body) }else{ body.push(path.node.body) } bodyOfmyfor = tee.blockStatement( body ) var forme = tee.forStatement(initOfmyfor, testOfmyfor, upOfmyfor, bodyOfmyfor)
+ 7 other calls in file
GitHub: nwjs/v8
37 38 39 40 41 42 43 44 45 46 47 48
} function wrapTryCatchInFunction(node) { const ret = wrapTryCatch(babelTypes.returnStatement(node)); const anonymousFun = babelTypes.functionExpression( null, [], babelTypes.blockStatement([ret])); return babelTypes.callExpression(anonymousFun, []); } // Wrap particular member expressions after `new` that are known to appear
+ 5 other calls in file
98 99 100 101 102 103 104 105 106 107
buildReference(type/*: any*/)/*: any*/ { return bt.genericTypeAnnotation(bt.identifier(type)) } buildModule(types/*: mixed[]*/)/*: mixed[]*/ { return [bt.declareModule(bt.stringLiteral('telegram-typings'), bt.blockStatement(types))] } buildProgram(body/*: any*/) { return generate(bt.program(body, [], 'module'))
+ 11 other calls in file
GitHub: omarjuice/algo-viz
128 129 130 131 132 133 134 135 136 137
} if (!t.isReturnStatement(block.body[block.body.length - 1])) { block.body.push(t.returnStatement(t.identifier('undefined'))) } } else { path.node.body = t.blockStatement([ t.expressionStatement( newNode), ...params.filter(p => p), t.returnStatement(path.node.body)
+ 21 other calls in file
GitHub: baidu/wx2
119 120 121 122 123 124 125 126 127 128
temp[item] && temp[item].forEach((cont, index) => { arg.properties.forEach(obj => { if (get(obj, 'key.name') !== get(cont, 'key.name')) { return; } const blockBody = t.blockStatement(get(cont, 'body.body')); get(obj, 'body.body').push(blockBody); temp[item].splice(index, 1); }); });
+ 3 other calls in file
GitHub: ringcrl/cs-notes
58 59 60 61 62 63 64 65 66 67
// 拿到函数的参数 const { params } = node; let { body } = node; // 判断是不是 blockStatement,不是的话让他变成 blockStatement if (!t.isBlockStatement(body)) { body = t.blockStatement([body]); } const functionExpression = t.functionExpression(null, params, body); // 替换 原来的函数 path.replaceWith(functionExpression);
+ 11 other calls in file
244 245 246 247 248 249 250 251 252 253
traverse(ast, { ArrowFunctionExpression: (path, state) => { let node = path.node; let id = path.parent.id; let params = node.params; let body= types.blockStatement([ types.returnStatement(node.body) ]); let functionExpression = types.functionExpression(id,params,body,false,false); path.replaceWith(functionExpression);
+ 13 other calls in file
GitHub: alijk-fe/motor-rax
207 208 209 210 211 212 213 214 215 216
// Transform x-for forNode to map function const properties = [ t.objectProperty(params[0], params[0]), t.objectProperty(params[1], params[1]) ]; const loopFnBody = t.blockStatement([ t.returnStatement( t.objectExpression(properties) ) ]);
+ 7 other calls in file
GitHub: fecym/ast-share
23 24 25 26 27 28 29 30 31 32
} else { // 如果只是表达式的话,path.parentPath.node 就是 blockStatement 参数 replacePath = path.parentPath; } // console.log("AwaitExpression -> replacePath", replacePath) const tryBlock = t.blockStatement([replacePath.node]); // 生成 catch --> new Error(e) const paramsE = t.identifier('e'); const throwStatement = t.throwStatement(t.newExpression(t.identifier('Error'), [paramsE])); const catchClause = t.catchClause(paramsE, t.blockStatement([throwStatement]));
GitHub: suchipi/tofu
31 32 33 34 35 36 37 38 39 40
canWrapStatement: boolean; hidden?: boolean; }[] = [ { name: 'if', create: (child = t.blockStatement([t.emptyStatement()])) => t.ifStatement(t.identifier('someCondition'), child), getInitialCursor: (ast, path) => selectName(getNodeFromPath(ast, [...path, 'test'])), canWrapStatement: true
+ 23 other calls in file
GitHub: groupon/swagql
67 68 69 70 71 72 73 74 75 76
t.objectProperty(t.identifier('name'), t.stringLiteral(graphqlName)), t.objectMethod( 'method', t.identifier('serialize'), [t.identifier('value')], t.blockStatement([t.returnStatement(t.identifier('value'))]) ), ]), ]) ),
GitHub: dchan3/enigma-cms
15 16 17 18 19 20 21 22 23 24
} else if (path.node.arguments[0].params.length === 2) { path.parentPath.replaceWith(t.forInStatement( t.variableDeclaration('let', [t.variableDeclarator(t.identifier(path.node.arguments[0].params[1].name))]), path.node.callee.object, t.blockStatement([t.ifStatement(t.binaryExpression('!==', t.identifier(path.node.arguments[0].params[1].name), t.stringLiteral('length')), t.blockStatement([t.variableDeclaration('let', [t.variableDeclarator(path.node.arguments[0].params[0], t.memberExpression(t.identifier(path.node.callee.object.name), t.identifier(path.node.arguments[0].params[1].name), true))] ), ...path.node.arguments[0].body.body])
+ 51 other calls in file
257 258 259 260 261 262 263 264 265 266
// 拿到函数的参数 const params = node.params let body = node.body // 判断是不是 blockStatement,不是的话让他变成 blockStatement if (!t.isBlockStatement(body)) { body = t.blockStatement([body]) } const functionExpression = t.functionExpression(null, params, body) // 替换原来的函数 path.replaceWith(functionExpression)
+ 51 other calls in file
122 123 124 125 126 127 128 129 130 131
state.vForVars[index.trim()] = true; const member = handleExpression(state, right.trim()); const body = !showIfExp ? dom : t.blockStatement([ t.ifStatement( handleExpression(state, showIfExp), t.blockStatement([t.returnStatement(dom)]) )
+ 25 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)