How to use the isExpressionStatement function from @babel/types
Find comprehensive JavaScript @babel/types.isExpressionStatement code examples handpicked from public code repositorys.
@babel/types.isExpressionStatement is a function that checks if a given AST node is an ExpressionStatement.
GitHub: baidu/wx2
74 75 76 77 78 79 80 81 82 83
return generateResult.code; }; function isAopBlock(path, block, AOP, cb) { t.isExpressionStatement(block) && t.isCallExpression(block.expression) && t.isMemberExpression(get(block, 'expression.callee')) && get(block, 'expression.callee.property.name') === 'after' && cb(path, block, AOP);
+ 7 other calls in file
89 90 91 92 93 94 95 96 97 98
`); } 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) ||
How does @babel/types.isExpressionStatement work?
@babel/types.isExpressionStatement is a function that takes an AST node and returns true if the node is an ExpressionStatement or false otherwise. The function first checks if the node is an object and has a type property, which is a string indicating the type of the node. If the type property is "ExpressionStatement", then the function returns true. If the type property is not "ExpressionStatement", then the function returns false. The purpose of this function is to allow developers to easily check whether a given node in an abstract syntax tree (AST) represents an expression statement, which is a statement that consists of an expression that is evaluated for its side effects, such as assigning a value to a variable or calling a function.
148 149 150 151 152 153 154 155 156 157
//当前case下的所有节点 var targetBody = case_list[i].consequent; // 删除break节点,和break节点的上一个节点的一些无用代码 if (t.isBreakStatement(targetBody[targetBody.length - 1]) && t.isExpressionStatement(targetBody[targetBody.length - 2]) && targetBody[targetBody.length - 2].expression.right.object.object.callee.object.name == "khCTZ") { // 提取break节点的上一个节点AJgjJ.EMf()后面的两个索引值 var change_arg_f = targetBody[targetBody.length - 2].expression.right.object.property.value;
GitHub: suchipi/tofu
8 9 10 11 12 13 14 15 16
t.isTemplateLiteral, t.isTemplateElement, t.isIdentifier, t.isNumericLiteral, t.isCallExpression, t.isExpressionStatement ].some(check => check(node)); export type Cursor = [number, number];
+ 13 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 ast = t.file( t.program([ t.expressionStatement( t.callExpression(t.identifier("console.log"), [ t.stringLiteral("Hello World!"), ]) ), ]) ); const node = ast.program.body[0]; console.log(t.isExpressionStatement(node)); // true console.log(t.isVariableDeclaration(node)); // false
In the above example, we create an abstract syntax tree (AST) using @babel/types, and then we use t.isExpressionStatement to check whether a node inside the AST is an expression statement. The function returns true because the node is indeed an expression statement.
187 188 189 190 191 192 193 194 195 196
simple1: function (ast) { // 对象合并 traverse(ast, { VariableDeclarator(path) { if (types.isObjectExpression(path.node.init)) { // path.getAllNextSiblings().filter(s => types.isExpressionStatement(s)); for (let expressionStatementPath of path.parentPath.getAllNextSiblings().filter(s => types.isExpressionStatement(s))) { let expression = expressionStatementPath.node.expression; if (types.isAssignmentExpression(expression) && expression.operator === '=') { if (types.isMemberExpression(expression.left) && expression.left.object.name === path.node.id.name && types.isIdentifier(expression.left.property)) {
+ 248 other calls in file
279 280 281 282 283 284 285 286 287 288
// "click":function($event) {click1(item);click2(item);} const body = funcPath.node.body && funcPath.node.body.body const funcParams = funcPath.node.params if (body && body.length && funcParams && funcParams.length === 1) { const exprStatements = body.filter(node => { return t.isExpressionStatement(node) && t.isCallExpression(node.expression) }) if (exprStatements.length === body.length) { const paramPath = funcPath.get('params')[0] const paramName = paramPath.node.name
+ 3 other calls in file
495 496 497 498 499 500 501 502 503 504 505
function initAnnotationParser(annotation) { var params = annotation.match(/(?<=(\()).*(?=(\)))/g)[0].split(","); params = params.length === 1 && params[0] === "" ? [] : params; params = params.filter(function (item, index) { return types.isExpressionStatement(template(item)()); }).map(function (item) { return template(item)().expression; }); // console.log("PARAMETERS TO BE PARSED: ", params);
GitHub: c323fff25/Spider
291 292 293 294 295 296 297 298 299 300
AssignmentExpression(path){ let obj_name = path.get('right.callee') if (!t.isMemberExpression(obj_name)) return; if (!t.isStringLiteral(obj_name.node.object) && !t.isStringLiteral(obj_name.node.property, {value: 'split'})) return; let var_path = path.findParent(function (p){ return t.isExpressionStatement(p) }) let while_path = var_path.getNextSibling() let my_arr = [] while_path.node.body.body[0].cases.map(function(p){
+ 2 other calls in file
255 256 257 258 259 260 261 262 263 264
let node = printStack[i]; i--; let parent = printStack[i]; while (i >= 0) { if (expressionStatement && t.isExpressionStatement(parent, { expression: node }) || exportDefault && t.isExportDefaultDeclaration(parent, { declaration: node }) || arrowBody && t.isArrowFunctionExpression(parent, {
+ 2 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)