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.

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')
fork icon27
star icon429
watch icon6

+ 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 = [];
fork icon26
star icon525
watch icon6

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.

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()
        )
fork icon65
star icon1
watch icon1

+ 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()
    );
fork icon3
star icon11
watch icon2

+ 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) => {
fork icon26
star icon0
watch icon0

+ 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
  );
fork icon13
star icon88
watch icon4

+ 5 other calls in file

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){
fork icon18
star icon41
watch icon0

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,
fork icon10
star icon0
watch icon0

+ 5 other calls in file

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()
      );
fork icon4
star icon0
watch icon1

+ 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();
    }
  });
fork icon0
star icon1
watch icon1

+ 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) {
fork icon0
star icon0
watch icon1

+ 89 other calls in file

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("")
            )
fork icon0
star icon0
watch icon1

+ 201 other calls in file

Other functions in @babel/types

Sorted by popularity

function icon

@babel/types.identifier is the most popular function in @babel/types (20936 examples)