Пример #1
0
 @NotNull
 private static JsStatement translateEntryExpression(
     @NotNull KtWhenEntry entry, @NotNull TranslationContext context, @NotNull JsBlock block) {
   KtExpression expressionToExecute = entry.getExpression();
   assert expressionToExecute != null : "WhenEntry should have whenExpression to execute.";
   return Translation.translateAsStatement(expressionToExecute, context, block);
 }
Пример #2
0
  private JsStatement translate() {
    if (expressionToMatch != null && JsAstUtils.isEmptyExpression(expressionToMatch)) {
      return JsEmpty.INSTANCE;
    }

    JsIf currentIf = null;
    JsIf resultIf = null;
    for (KtWhenEntry entry : whenExpression.getEntries()) {
      JsBlock statementBlock = new JsBlock();
      JsStatement statement = translateEntryExpression(entry, context(), statementBlock);

      if (resultIf == null && entry.isElse()) {
        context().addStatementsToCurrentBlockFrom(statementBlock);
        return statement;
      }
      statement = JsAstUtils.mergeStatementInBlockIfNeeded(statement, statementBlock);

      if (resultIf == null) {
        currentIf = JsAstUtils.newJsIf(translateConditions(entry, context()), statement);
        resultIf = currentIf;
      } else {
        if (entry.isElse()) {
          currentIf.setElseStatement(statement);
          return resultIf;
        }
        JsBlock conditionsBlock = new JsBlock();
        JsIf nextIf =
            JsAstUtils.newJsIf(
                translateConditions(entry, context().innerBlock(conditionsBlock)), statement);
        JsStatement statementToAdd =
            JsAstUtils.mergeStatementInBlockIfNeeded(nextIf, conditionsBlock);
        currentIf.setElseStatement(statementToAdd);
        currentIf = nextIf;
      }
    }
    return resultIf;
  }
Пример #3
0
  @NotNull
  private JsExpression translateConditions(
      @NotNull KtWhenEntry entry, @NotNull TranslationContext context) {
    KtWhenCondition[] conditions = entry.getConditions();

    assert conditions.length > 0 : "When entry (not else) should have at least one condition";

    if (conditions.length == 1) {
      return translateCondition(conditions[0], context);
    }

    JsExpression result = translateCondition(conditions[0], context);
    for (int i = 1; i < conditions.length; i++) {
      result = translateOrCondition(result, conditions[i], context);
    }

    return result;
  }