How to use the isUnaryExpression function from @babel/types
Find comprehensive JavaScript @babel/types.isUnaryExpression code examples handpicked from public code repositorys.
@babel/types.isUnaryExpression is a function provided by the Babel Types library that determines if a given node is a UnaryExpression node.
GitHub: youtube/cobalt
71 72 73 74 75 76 77 78 79
return; } // We handle negative unary expressions separately to replace the whole // expression below. E.g. -5 is UnaryExpression(-, NumericLiteral(5)). if (path.parent && babelTypes.isUnaryExpression(path.parent) && path.parent.operator === '-') { return; }
33 34 35 36 37 38 39 40 41 42
// Visitor for constant folding const constantFold = { "BinaryExpression|UnaryExpression"(path) { const { node } = path; if ( t.isUnaryExpression(node) && (node.operator == "-" || node.operator == "void") ) return; let { confident, value } = path.evaluate(); // Evaluate the binary expression
+ 2 other calls in file
How does @babel/types.isUnaryExpression work?
@babel/types.isUnaryExpression is a function provided by the Babel Types library that determines if a given node is a UnaryExpression node. When called, @babel/types.isUnaryExpression takes a single argument, which should be a Babel node. It checks whether the type of the node is "UnaryExpression" and returns a boolean value indicating whether or not the node is a UnaryExpression node. A UnaryExpression node represents an expression that is preceded by a unary operator, such as ! (logical NOT), - (negation), or typeof (type-of operator). The node has a single operand, which can be any valid expression. By using @babel/types.isUnaryExpression, you can easily check whether a given node is a UnaryExpression node in your Babel plugin or transformation code. Overall, @babel/types.isUnaryExpression provides a convenient way to test whether a given node is a UnaryExpression node, allowing you to write more powerful and flexible Babel transformations.
GitHub: Alanhays/ast-decode
79 80 81 82 83 84 85 86 87 88
function isBaseLiteral(node) { if (types.isLiteral(node)) { return true; } if (types.isUnaryExpression(node, {operator: "-"}) || types.isUnaryExpression(node, {operator: "+"})) { return isBaseLiteral(node.argument); } return false; }
+ 136 other calls in file
GitHub: wwhtrbbtt/GPJSD
127 128 129 130 131 132 133 134 135 136
if (p.node.operator != '!') return; // get every '!' if ( t.isUnaryExpression(p.node.argument) && p.node.argument.operator == '!' ) { // get every '!!' if (t.isArrayExpression(p.node.argument.argument)) {
+ 713 other calls in file
Ai Example
1 2 3 4 5 6
const t = require("@babel/types"); const node = t.unaryExpression("!", t.booleanLiteral(true)); const isUnary = t.isUnaryExpression(node); console.log(isUnary); // true
In this example, we use @babel/types.isUnaryExpression to check whether a given node is a UnaryExpression node. We first import the Babel Types library using the require function and assign it to the variable t. We then create a new UnaryExpression node using t.unaryExpression, which represents the expression !true. This node has a unary operator of ! and an operand of true. We call t.isUnaryExpression and pass in the node variable as the argument. This checks whether the node variable is a UnaryExpression node and returns a boolean value indicating whether or not it is. Finally, we log the result of isUnary to the console, which in this case will be true, since the node variable is indeed a UnaryExpression node. Note that in order to use @babel/types.isUnaryExpression, you need to have the Babel Types library installed and imported in your application.
105 106 107 108 109 110 111 112 113
let { confident, value } = path.evaluate(); // Evaluate the binary expression if (!confident || value == Infinity || value == -Infinity) return; // Skip if not confident const newNode = t.valueToNode(value); if (t.isBinaryExpression(newNode) || t.isUnaryExpression(newNode)) return; path.replaceWith(newNode); // Replace the BinaryExpression with a new node of inferred type }, };
+ 11 other calls in file
88 89 90 91 92 93 94 95 96 97
// No handling for arrays right now } else if ( t.isBinaryExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isFunctionExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isUnaryExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isConditionalExpression(path.node.init) ){ // No handling for ^^ right now } else if ( t.isLogicalExpression(path.node.init) ){
246 247 248 249 250 251 252 253 254 255
// return { // WhileStatement: { // exit: [ // function (path: NodePath<t.WhileStatement>) { // var node = path.node // if (!(t.isBooleanLiteral(node.test) || t.isUnaryExpression(node.test))) return // if (!(node.test.prefix || node.test.value)) return // if (!t.isBlockStatement(node.body)) return // var body = node.body.body // if (!t.isSwitchStatement(body[0]) || !t.isMemberExpression(body[0].discriminant) || !t.isBreakStatement(body[1])) return
122 123 124 125 126 127 128 129 130 131
getExtraDataPath(getCode(processMemberExpression(element, state)), methodName) )) } return t.stringLiteral('$' + (extraArrayElements.length - 1)) } else if ( // +1=>1 t.isUnaryExpression(element) && element.operator === '+' && t.isNumericLiteral(element.argument) ) { element = t.numericLiteral(element.argument.value)
+ 3 other calls in file
35 36 37 38 39 40 41 42 43 44
{ return isNodeLiteral(node.left) && isNodeLiteral(node.right); } if (types.isUnaryExpression(node, { "operator": "-" }) || types.isUnaryExpression(node, { "operator": "+" })) { return isNodeLiteral(node.argument); }
+ 74 other calls in file
44 45 46 47 48 49 50 51 52 53
} return null; } if (t.isTSLiteralType(node)) { // field: "A"|1|false if (t.isTemplateLiteral(node.literal) || t.isUnaryExpression(node.literal)) { return null; } const value = node.literal.value; const type = typeof value;
+ 25 other calls in file
135 136 137 138 139 140 141 142 143 144 145 146 147
return false; } function isVoidExpression(path) { return t.isUnaryExpression(path) && path.node.operator === 'void'; } function isInteropRequireCall(path) { var expression = findExpression(path);
+ 4 other calls in file
GitHub: redexp/adv-parser
14 15 16 17 18 19 20 21 22 23
function isPure(root) { return ( t.isUnaryExpression(root) && root.operator === '!' && t.isUnaryExpression(root.argument) && root.argument.operator === '!' && t.isObjectExpression(root.argument.argument) ); }
+ 11 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)