export function buildConditionalExpression()

in packages/codegen-ui-react/lib/react-component-render-helper.ts [329:363]


export function buildConditionalExpression(prop: ConditionalStudioComponentProperty): JsxExpression {
  const { property, field, operand, operator, then } = prop.condition;
  const elseBlock = prop.condition.else;
  const operatorToken = getSyntaxKindToken(operator);

  if (operatorToken === undefined) {
    return factory.createJsxExpression(undefined, undefined);
  }

  const propertyAccess =
    field !== undefined
      ? factory.createPropertyAccessChain(
          factory.createIdentifier(property),
          factory.createToken(SyntaxKind.QuestionDotToken),
          factory.createIdentifier(field),
        )
      : factory.createIdentifier(property);

  return factory.createJsxExpression(
    undefined,
    factory.createConditionalExpression(
      factory.createParenthesizedExpression(
        factory.createBinaryExpression(
          propertyAccess,
          factory.createToken(SyntaxKind.AmpersandAmpersandToken),
          factory.createBinaryExpression(propertyAccess, operatorToken, getConditionalOperandExpression(operand)),
        ),
      ),
      factory.createToken(SyntaxKind.QuestionToken),
      resolvePropToExpression(then),
      factory.createToken(SyntaxKind.ColonToken),
      resolvePropToExpression(elseBlock),
    ),
  );
}