[K/JS] Add support of compilation with ES-classes

This commit is contained in:
Artem Kobzar
2023-01-17 18:14:17 +00:00
committed by Space Team
parent 8d728d4f53
commit 71486a321c
239 changed files with 57567 additions and 1658 deletions
@@ -145,6 +145,11 @@ class JsPrecedenceVisitor extends JsVisitor {
answer = 17; // primary
}
@Override
public void visitSuper(@NotNull JsSuperRef x) {
answer = 17; // primary
}
@Override
protected void visitElement(@NotNull JsNode node) {
throw new RuntimeException("Only expressions have precedence.");
@@ -36,6 +36,9 @@ public class JsToStringGenerationVisitor extends JsVisitor {
private static final char[] CHARS_FINALLY = "finally".toCharArray();
private static final char[] CHARS_FOR = "for".toCharArray();
private static final char[] CHARS_FUNCTION = "function".toCharArray();
private static final char[] CHARS_STATIC = "static".toCharArray();
private static final char[] CHARS_GET = "get".toCharArray();
private static final char[] CHARS_SET = "set".toCharArray();
private static final char[] CHARS_IF = "if".toCharArray();
private static final char[] CHARS_IN = "in".toCharArray();
private static final char[] CHARS_NEW = "new".toCharArray();
@@ -43,6 +46,8 @@ public class JsToStringGenerationVisitor extends JsVisitor {
private static final char[] CHARS_RETURN = "return".toCharArray();
private static final char[] CHARS_SWITCH = "switch".toCharArray();
private static final char[] CHARS_THIS = "this".toCharArray();
private static final char[] CHARS_SUPER = "super".toCharArray();
private static final char[] CHARS_THROW = "throw".toCharArray();
private static final char[] CHARS_TRUE = "true".toCharArray();
private static final char[] CHARS_TRY = "try".toCharArray();
@@ -664,8 +669,21 @@ public class JsToStringGenerationVisitor extends JsVisitor {
printCommentsAfterNode(x);
}
// name(<params>) { <body> }
// [static?] [get|set?] name(<params>) { <body> }
private void printFunction(@NotNull JsFunction x) {
if (x.isStatic()) {
p.print(CHARS_STATIC);
space();
}
if (x.isGetter()) {
p.print(CHARS_GET);
space();
} else if (x.isSetter()) {
p.print(CHARS_SET);
space();
}
if (x.getName() != null) {
nameOf(x);
}
@@ -1128,6 +1146,17 @@ public class JsToStringGenerationVisitor extends JsVisitor {
popSourceInfo();
}
@Override
public void visitSuper(@NotNull JsSuperRef x) {
pushSourceInfo(x.getSource());
printCommentsBeforeNode(x);
p.print(CHARS_SUPER);
printCommentsAfterNode(x);
popSourceInfo();
}
@Override
public void visitThrow(@NotNull JsThrow x) {
pushSourceInfo(x.getSource());
@@ -30,11 +30,14 @@ class JsClass(
}
override fun acceptChildren(visitor: JsVisitor) {
visitor.accept(baseClass)
visitor.accept(constructor)
visitor.acceptList(members)
}
override fun traverse(v: JsVisitorWithContext, ctx: JsContext<*>) {
if (v.visit(this, ctx)) {
baseClass = v.accept(baseClass)
constructor = v.accept(constructor)
v.acceptList(members)
}
@@ -10,15 +10,18 @@ import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.*;
public final class JsFunction extends JsLiteral implements HasName {
public enum Modifier { STATIC, GET, SET }
@NotNull
private JsBlock body;
private List<JsParameter> params;
@NotNull
private final JsFunctionScope scope;
private JsName name;
private Set<Modifier> modifiers;
public JsFunction(@NotNull JsScope parentScope, @NotNull String description) {
this(parentScope, description, null);
@@ -29,7 +32,11 @@ public final class JsFunction extends JsLiteral implements HasName {
this.body = body;
}
private JsFunction(@NotNull JsScope parentScope, @NotNull String description, @Nullable JsName name) {
private JsFunction(
@NotNull JsScope parentScope,
@NotNull String description,
@Nullable JsName name
) {
this.name = name;
scope = new JsFunctionScope(parentScope, name == null ? description : name.getIdent());
}
@@ -62,6 +69,26 @@ public final class JsFunction extends JsLiteral implements HasName {
return scope;
}
public boolean isStatic() {
return modifiers != null && modifiers.contains(Modifier.STATIC);
}
public boolean isGetter() {
return modifiers != null && modifiers.contains(Modifier.GET);
}
public boolean isSetter() {
return modifiers != null && modifiers.contains(Modifier.SET);
}
@NotNull
public Set<Modifier> getModifiers() {
if (modifiers == null) {
modifiers = EnumSet.noneOf(Modifier.class);
}
return modifiers;
}
public void setBody(@NotNull JsBlock body) {
this.body = body;
}
@@ -98,6 +125,7 @@ public final class JsFunction extends JsLiteral implements HasName {
functionCopy.getScope().copyOwnNames(scope);
functionCopy.setBody(body.deepCopy());
functionCopy.params = AstUtil.deepCopy(params);
functionCopy.modifiers = modifiers == null ? null : EnumSet.copyOf(modifiers);
return functionCopy.withMetadataFrom(this);
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2017 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.backend.ast;
import org.jetbrains.annotations.NotNull;
public final class JsSuperRef extends JsLiteral.JsValueLiteral {
@Override
public void accept(JsVisitor v) {
v.visitSuper(this);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsSuperRef deepCopy() {
return new JsSuperRef().withMetadataFrom(this);
}
}
@@ -144,6 +144,9 @@ abstract class JsVisitor {
open fun visitThis(x: JsThisRef): Unit =
visitElement(x)
open fun visitSuper(x: JsSuperRef): Unit =
visitElement(x)
open fun visitThrow(x: JsThrow): Unit =
visitElement(x)
@@ -199,6 +199,9 @@ public abstract class JsVisitorWithContext {
endVisit((JsExpression) x, ctx);
}
public void endVisit(@NotNull JsSuperRef x, @NotNull JsContext ctx) {
endVisit((JsExpression) x, ctx);
}
public void endVisit(@NotNull JsThrow x, @NotNull JsContext ctx) {
}
@@ -383,6 +386,9 @@ public abstract class JsVisitorWithContext {
return true;
}
public boolean visit(@NotNull JsSuperRef x, @NotNull JsContext ctx) {
return true;
}
public boolean visit(@NotNull JsThrow x, @NotNull JsContext ctx) {
return true;
}