Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 24
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 2, 2024
1 parent b8dab3e commit 1061c76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.caoccao.javet.buddy.ts2java.ast.interfaces.*;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.ast.ts.Ts2JavaAstTsTypeAnn;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstBindingIdent;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;
Expand All @@ -42,12 +41,8 @@ public Ts2JavaAstBindingIdent(
Swc4jAstBindingIdent ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
this.typeAnn = ast.getTypeAnn().map(typeAnn -> new Ts2JavaAstTsTypeAnn(this, typeAnn, memo));
if (typeAnn.isPresent()) {
type = typeAnn.get().getType();
} else {
throw new Ts2JavaAstException(ast, "Binding ident type ann is missing.");
}
typeAnn = ast.getTypeAnn().map(typeAnn -> new Ts2JavaAstTsTypeAnn(this, typeAnn, memo));
typeAnn.ifPresent(ts2JavaAstTsTypeAnn -> type = ts2JavaAstTsTypeAnn.getType());
id = new Ts2JavaAstIdent(this, ast.getId(), memo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public void compile() {
SimpleFreeMarkerFormat.format("Var declarator name type ${type} is not supported.",
SimpleMap.of("type", name.getAst().getType().name())));
}
if (type == null && init.isPresent()) {
type = init.get().getType();
}
offset = memo.getNextOffset();
localVariable = new JavaLocalVariable(variableName, type);
memo.addLocalVariable(localVariable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.caoccao.javet.buddy.ts2java.TsMethodArgument;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

public class TestTs2JavaAstVarDeclarator extends BaseTestTs2Java {
/*
Expand Down Expand Up @@ -111,12 +111,23 @@ public void testAssignAndCast() throws Exception {
public void testAssignConst() throws Exception {
assertEquals(105L, assignConst(1, 2L));
TsClass tsClass = new TsClass(
"const c: int = 100;\n" +
"const c = 100;\n" +
"const d: long = 2;\n" +
"return a + b + c + d;",
long.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", long.class));
assertEquals(105L, tsClass.invoke(1, 2L));
}

@Test
public void testBoolean() throws Exception {
TsClass tsClass = new TsClass(
"const b = a;\n" +
"return b;",
boolean.class,
TsMethodArgument.of("a", boolean.class));
assertTrue((boolean) tsClass.invoke(true));
assertFalse((boolean) tsClass.invoke(false));
}
}

0 comments on commit 1061c76

Please sign in to comment.