How to use the conditionalExpression function from @babel/types
Find comprehensive JavaScript @babel/types.conditionalExpression code examples handpicked from public code repositorys.
@babel/types.conditionalExpression is a function in the Babel library that creates an AST (abstract syntax tree) node representing a ternary conditional expression in JavaScript.
GitHub: egoist/styled-vue
89 90 91 92 93 94 95 96 97 98
t.stringLiteral(`--${variableId}`), mustBeFunction ? getValue(true) : mustNotBeFunction ? getValue(false) : t.conditionalExpression( t.binaryExpression( '===', t.unaryExpression('typeof', id), t.stringLiteral('function')
+ 15 other calls in file
53 54 55 56 57 58 59 60 61 62
.map(prop => getObjectsProp(classObject, prop)) .map(classes => getConditionalArgs(args, classes)) .filter(conditionalArgs => conditionalArgs.length) .map(conditionalArgs => conditionalArgs.reduceRight((acc, prop) => t.conditionalExpression(prop.test, prop.value, acc) ) ); const simplifiedConditionals = [];
How does @babel/types.conditionalExpression work?
@babel/types.conditionalExpression is a method provided by the Babel package @babel/types that generates an abstract syntax tree (AST) node representing a conditional expression in JavaScript, which evaluates a condition and returns one of two values depending on the result of the condition. The AST node generated by this method has three properties: test represents the condition to be evaluated, consequent represents the value to be returned if the condition is true, and alternate represents the value to be returned if the condition is false.
GitHub: icai/vue-to-react
14 15 16 17 18 19 20 21 22 23
t.identifier(value) ); parentPath.replaceWith( t.jSXExpressionContainer( t.conditionalExpression( test, parentPath.node, nextElement ? nextElement : t.nullLiteral() )
+ 3 other calls in file
49 50 51 52 53 54 55 56 57 58
function transformIfBinding(path, ifBinding) { const { attrBinding, index, node } = ifBinding; removeAttrASTByIndex(node, index); const targetAST = t.conditionalExpression( attrBinding.value.expression, node, t.nullLiteral() );
+ 5 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9
const t = require("@babel/types"); const node = t.conditionalExpression( t.identifier("a"), t.identifier("b"), t.identifier("c") ); console.log(node);
This code will generate an AST node for the following code: css Copy code
99 100 101 102 103 104 105 106 107 108
const conditionalArgs = getConditionalArgs(args, classes); if (!conditionalArgs.length) return; return conditionalArgs.reduceRight((acc, prop) => { return t.conditionalExpression(prop.test, prop.value, acc); }); }).filter(Boolean); const additions = conditionals.reduceRight((acc, expr) => {
+ 3 other calls in file
137 138 139 140 141 142 143 144 145 146
curr.path.node, t.nullLiteral() ); } } else { expression = t.conditionalExpression( testExpr, curr.path.node, prev );
+ 5 other calls in file
GitHub: 2833844911/cy_jsvmp
54 55 56 57 58 59 60 61 62 63
var hu,ko,kop, kop2; try{ hu = tee.conditionalExpression(path.node.test, tee.sequenceExpression(shuj2), tee.sequenceExpression(shuj)) path.replaceInline(hu) }catch (e){
GitHub: msojocs/wcc.js
205 206 207 208 209 210 211 212 213 214
path.stop(); return; } else if (!path.node._wcc_handled) { let thisNode = babelTypes.expressionStatement( babelTypes.conditionalExpression( babelTypes.binaryExpression( "===", babelTypes.memberExpression( path.node,
+ 5 other calls in file
GitHub: suchipi/tofu
185 186 187 188 189 190 191 192 193 194
if (isExpressionOrBlock && key == '?') { this.updateCode({ ast: produce(ast, ast => { getNodeFromPath(ast, path.slice(0, -1))[ path[path.length - 1] ] = t.conditionalExpression( getNodeFromPath(ast, path), t.nullLiteral(), t.nullLiteral() );
+ 3 other calls in file
167 168 169 170 171 172 173 174 175 176
if (argumentsPaths.length > 0) { const argumentsBinding = getBinding(thisEnvFn, "arguments", () => { const args = () => t.identifier("arguments"); if (thisEnvFn.scope.path.isProgram()) { return t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", args()), t.stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args()); } else { return args(); } });
+ 40 other calls in file
147 148 149 150 151 152 153 154 155 156
); } else if (ifConditions.length === 2) { const { block: elseBlock } = ifConditions[1]; const elseElement = initTemplate(elseBlock, null, attrsCollector); wrappedElement = t.jSXExpressionContainer( t.conditionalExpression(t.identifier(ifConditions[0].exp), element, elseElement) ); } else { let ifStatement = []; for (const item of ifConditions) {
+ 89 other calls in file
GitHub: harby9/tphone-core
4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514
// 把 { key: value } 转换成 [ value ? 'key' : '' ] var objectVisitor = { ObjectExpression: function(path) { var elements = path.node.properties.map(function(propertyItem) { return t.conditionalExpression( propertyItem.value, getStrByNode(propertyItem.key), t.stringLiteral("") )
+ 201 other calls in file
@babel/types.identifier is the most popular function in @babel/types (20936 examples)