org.jetbrains.kotlin.js.backend.JsFirstExpressionVisitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package org.jetbrains.kotlin.js.backend;
import org.jetbrains.kotlin.js.backend.ast.*;
import org.jetbrains.kotlin.js.backend.ast.JsExpressionStatement;
import org.jetbrains.annotations.NotNull;
/**
* Determines if an expression statement needs to be surrounded by parentheses.
*
* The statement or the left-most expression needs to be surrounded by
* parentheses if the left-most expression is an object literal or a function
* object. Function declarations do not need parentheses.
*
* For example the following require parentheses:
*
* - { key : 'value'}
* - { key : 'value'}.key
* - function () {return 1;}()
* - function () {return 1;}.prototype
*
*
* The following do not require parentheses:
*
* - var x = { key : 'value'}
* - "string" + { key : 'value'}.key
* - function func() {}
* - function() {}
*
*/
public class JsFirstExpressionVisitor extends RecursiveJsVisitor {
public static boolean exec(JsExpressionStatement statement) {
JsExpression expression = statement.getExpression();
// Pure function declarations do not need parentheses
if (expression instanceof JsFunction || expression instanceof JsClass) {
return false;
}
JsFirstExpressionVisitor visitor = new JsFirstExpressionVisitor();
visitor.accept(statement.getExpression());
return visitor.needsParentheses;
}
private boolean needsParentheses = false;
private JsFirstExpressionVisitor() {
}
@Override
public void visitArrayAccess(@NotNull JsArrayAccess x) {
accept(x.getArrayExpression());
}
@Override
public void visitArray(@NotNull JsArrayLiteral x) {
}
@Override
public void visitBinaryExpression(@NotNull JsBinaryOperation x) {
accept(x.getArg1());
}
@Override
public void visitConditional(@NotNull JsConditional x) {
accept(x.getTestExpression());
}
@Override
public void visitFunction(@NotNull JsFunction x) {
needsParentheses = true;
}
@Override
public void visitInvocation(@NotNull JsInvocation invocation) {
accept(invocation.getQualifier());
}
@Override
public void visitNameRef(@NotNull JsNameRef nameRef) {
if (!nameRef.isLeaf()) {
accept(nameRef.getQualifier());
}
}
@Override
public void visitNew(@NotNull JsNew x) {
}
@Override
public void visitObjectLiteral(@NotNull JsObjectLiteral x) {
needsParentheses = true;
}
@Override
public void visitPostfixOperation(@NotNull JsPostfixOperation x) {
accept(x.getArg());
}
@Override
public void visitPrefixOperation(@NotNull JsPrefixOperation x) {
}
}