All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.js.translate.utils.astUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.js.translate.utils.jsAstUtils

import com.google.dart.compiler.backend.js.ast.*
import org.jetbrains.kotlin.js.translate.context.Namer

public fun JsFunction.addStatement(stmt: JsStatement) {
    getBody().getStatements().add(stmt)
}

public fun JsFunction.addParameter(identifier: String, index: Int? = null): JsParameter {
    val name = getScope().declareFreshName(identifier)
    val parameter = JsParameter(name)

    if (index == null) {
        getParameters().add(parameter)
    } else {
        getParameters().add(index, parameter)
    }

    return parameter
}

/**
 * Tests, if any node containing in receiver's AST matches, [predicate].
 */
public fun JsNode.any(predicate: (JsNode) -> Boolean): Boolean {
    val visitor = object : RecursiveJsVisitor() {
        public var matched: Boolean = false

        override fun visitElement(node: JsNode) {
            matched = matched || predicate(node)

            if (!matched) {
                super.visitElement(node)
            }
        }
    }

    visitor.accept(this)
    return visitor.matched
}

fun JsExpression.toInvocationWith(thisExpr: JsExpression): JsExpression {
    val qualifier: JsExpression
    val arguments: MutableList

    when (this) {
        is JsNew -> {
            qualifier = Namer.getFunctionCallRef(getConstructorExpression())
            arguments = getArguments()
            // `new A(a, b, c)` -> `A.call($this, a, b, c)`
            return JsInvocation(qualifier, listOf(thisExpr) + arguments)
        }
        is JsInvocation -> {
            qualifier = getQualifier()
            arguments = getArguments()
            // `A(a, b, c)` -> `A(a, b, c, $this)`
            return JsInvocation(qualifier, arguments + thisExpr)
        }
        else -> throw IllegalStateException("Unexpected node type: " + javaClass)
    }
}

public var JsWhile.test: JsExpression
    get() = getCondition()
    set(value) = setCondition(value)

public var JsArrayAccess.index: JsExpression
    get() = getIndexExpression()
    set(value) = setIndexExpression(value)

public var JsArrayAccess.array: JsExpression
    get() = getArrayExpression()
    set(value) = setArrayExpression(value)

public var JsConditional.test: JsExpression
    get() = getTestExpression()
    set(value) = setTestExpression(value)

public var JsConditional.then: JsExpression
    get() = getThenExpression()
    set(value) = setThenExpression(value)

public var JsConditional.otherwise: JsExpression
    get() = getElseExpression()
    set(value) = setElseExpression(value)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy