How to use the isIfStatement function from @babel/types
Find comprehensive JavaScript @babel/types.isIfStatement code examples handpicked from public code repositorys.
@babel/types.isIfStatement is a function provided by the @babel/types library that checks whether a given AST node represents an if statement.
GitHub: lin/cs
17 18 19 20 21 22 23 24 25 26
this.print(node.test, node); this.token(")"); this.space(); const needsBlock = node.alternate && t.isIfStatement(this.getLastStatement(node.consequent)); if (needsBlock) { this.token("{"); this.newline(); this.indent();
+ 40 other calls in file
GitHub: xcodebuild/rax
76 77 78 79 80 81 82 83 84 85
* 1. parentPath is IfStatement * 2. parentNode's alternate start & end is same as current path start & end */ if ( nodePath.parentPath.isIfStatement() && t.isIfStatement(parentPathAlternate) && parentPathAlternate.start === start && parentPathAlternate.end === end ) { testAttrName = adapter.elseif;
+ 3 other calls in file
How does @babel/types.isIfStatement work?
@babel/types.isIfStatement is a function provided by the @babel/types library that checks whether a given AST node represents an if statement. The function takes in one argument, the AST node to test. When called, isIfStatement checks whether the type of the node matches the IfStatement type, which represents an if statement in the AST. If the node matches the IfStatement type, isIfStatement returns true; otherwise, it returns false. Overall, @babel/types.isIfStatement provides a way to test whether a given AST node represents an if statement in a JavaScript program, which can be useful when manipulating or analyzing the program's syntax tree.
GitHub: liuhahi/rax
79 80 81 82 83 84 85 86 87 88
* Condition: * 1. parentPath is IfStatement * 2. parentNode's alternate start & end is same as current path start & end */ if (t.isIfStatement(nodePath.parentPath) && t.isIfStatement(parentPathAlternate) && parentPathAlternate.start === start && parentPathAlternate.end === end) { testAttrName = parserAdapter.elseif; }
77 78 79 80 81 82 83 84 85 86
// .find({ type: "IfStatement" }).each(function (item) { // sequenceExpressionHandle(item.node.test, item) // }).root() // .find({ type: "SequenceExpression" }).each(function (item) { // var parentPath = item[0].nodePath.parentPath.node // if (t.isReturnStatement(parentPath) || t.isIfStatement(parentPath)) return // sequenceExpressionHandle(item.node, item, true) // }).root() // return $ast // }
Ai Example
1 2 3 4 5 6 7 8 9
const t = require("@babel/types"); const node = t.ifStatement( t.identifier("x"), t.blockStatement([]), t.blockStatement([]) ); console.log(t.isIfStatement(node)); // true
In this example, we use @babel/types.isIfStatement to check whether an AST node represents an if statement. We first import the @babel/types library using the require function and assign it to the variable t. We create a new ifStatement AST node using the t.ifStatement method, passing in three arguments: the test condition (t.identifier('x')), the consequent block statement (t.blockStatement([])), and the alternate block statement (t.blockStatement([])). We call t.isIfStatement and pass in the node variable, which contains our newly created AST node. The call returns true, indicating that the node variable represents an if statement. Note that in order to use @babel/types.isIfStatement, you need to have the @babel/types library installed and imported in your application.
37 38 39 40 41 42 43 44 45 46
this.space(); this.token("("); this.print(node.test, node); this.token(")"); this.space(); const needsBlock = node.alternate && t.isIfStatement(getLastStatement(node.consequent)); if (needsBlock) { this.token("{"); this.newline();
GitHub: Alanhays/ast-decode
237 238 239 240 241 242 243 244 245 246
if (result instanceof Object) { bodyList.push(result); } } // 返回node为return或If类型则结束循环 if (types.isReturnStatement(result) || types.isIfStatement(result)) break; scope.slice(-1)[0]['index']++; } // 退出当前作用域 if (scope.length > 1) scope.pop();
+ 8 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)