From bea4c993cb469ab7f26aa4096bc8189d7944c328 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Wed, 29 May 2024 12:11:17 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20create()=20v4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../swc4j/ast/expr/Swc4jAstArrowExpr.java | 33 +++++++++++++++++++ .../swc4j/ast/expr/Swc4jAstAssignExpr.java | 7 ++++ .../swc4j/ast/expr/Swc4jAstAwaitExpr.java | 4 +++ .../swc4j/ast/expr/Swc4jAstCallExpr.java | 9 ++++- .../swc4j/ast/expr/Swc4jAstClassExpr.java | 4 +++ .../swc4j/ast/expr/Swc4jAstCondExpr.java | 4 +++ .../swc4j/ast/expr/Swc4jAstExprOrSpread.java | 9 +++-- .../swc4j/ast/expr/lit/Swc4jAstBigInt.java | 7 +++- .../swc4j/ast/expr/lit/Swc4jAstJsxText.java | 14 +++++--- .../swc4j/ast/expr/lit/Swc4jAstObjectLit.java | 4 +++ .../swc4j/ast/expr/lit/Swc4jAstRegex.java | 3 +- .../javet/swc4j/ast/expr/lit/Swc4jAstStr.java | 6 +++- 12 files changed, 93 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstArrowExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstArrowExpr.java index 14806443..bc7fcb89 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstArrowExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstArrowExpr.java @@ -69,6 +69,39 @@ public Swc4jAstArrowExpr( this.params.forEach(node -> node.setParent(this)); } + public static Swc4jAstArrowExpr create( + List params, + ISwc4jAstBlockStmtOrExpr body) { + return create(params, body, false, false); + } + + public static Swc4jAstArrowExpr create( + List params, + ISwc4jAstBlockStmtOrExpr body, + boolean _async, + boolean generator) { + return create(params, body, _async, generator, null); + } + + public static Swc4jAstArrowExpr create( + List params, + ISwc4jAstBlockStmtOrExpr body, + boolean _async, + boolean generator, + Swc4jAstTsTypeParamDecl typeParams) { + return create(params, body, _async, generator, typeParams, null); + } + + public static Swc4jAstArrowExpr create( + List params, + ISwc4jAstBlockStmtOrExpr body, + boolean _async, + boolean generator, + Swc4jAstTsTypeParamDecl typeParams, + Swc4jAstTsTypeAnn returnType) { + return new Swc4jAstArrowExpr(params, body, _async, generator, typeParams, returnType, Swc4jSpan.DUMMY); + } + @Jni2RustMethod public ISwc4jAstBlockStmtOrExpr getBody() { return body; diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAssignExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAssignExpr.java index 6e570e92..b582b2d7 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAssignExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAssignExpr.java @@ -55,6 +55,13 @@ public Swc4jAstAssignExpr( setRight(right); } + public static Swc4jAstAssignExpr create( + Swc4jAstAssignOp op, + ISwc4jAstAssignTarget left, + ISwc4jAstExpr right) { + return new Swc4jAstAssignExpr(op, left, right, Swc4jSpan.DUMMY); + } + @Override public List getChildNodes() { return SimpleList.of(left, right); diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAwaitExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAwaitExpr.java index a4e8abc7..de75b74d 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAwaitExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstAwaitExpr.java @@ -47,6 +47,10 @@ public Swc4jAstAwaitExpr( setArg(arg); } + public static Swc4jAstAwaitExpr create(ISwc4jAstExpr arg) { + return new Swc4jAstAwaitExpr(arg, Swc4jSpan.DUMMY); + } + @Jni2RustMethod public ISwc4jAstExpr getArg() { return arg; diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java index d387a6c6..4bfa125a 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java @@ -76,7 +76,14 @@ public static Swc4jAstCallExpr create(ISwc4jAstCallee callee) { } public static Swc4jAstCallExpr create(ISwc4jAstCallee callee, List args) { - return new Swc4jAstCallExpr(callee, args, null, Swc4jSpan.DUMMY); + return create(callee, args, null); + } + + public static Swc4jAstCallExpr create( + ISwc4jAstCallee callee, + List args, + Swc4jAstTsTypeParamInstantiation typeArgs) { + return new Swc4jAstCallExpr(callee, args, typeArgs, Swc4jSpan.DUMMY); } @Override diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstClassExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstClassExpr.java index 126e1dfb..e3996127 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstClassExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstClassExpr.java @@ -50,6 +50,10 @@ public Swc4jAstClassExpr( setIdent(ident); } + public static Swc4jAstClassExpr create(Swc4jAstIdent ident, Swc4jAstClass clazz) { + return new Swc4jAstClassExpr(ident, clazz, Swc4jSpan.DUMMY); + } + @Override public List getChildNodes() { List childNodes = SimpleList.of(clazz); diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCondExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCondExpr.java index ad6a7db8..ec5b5d83 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCondExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCondExpr.java @@ -55,6 +55,10 @@ public Swc4jAstCondExpr( setTest(test); } + public static Swc4jAstCondExpr create(ISwc4jAstExpr test, ISwc4jAstExpr cons, ISwc4jAstExpr alt) { + return new Swc4jAstCondExpr(test, cons, alt, Swc4jSpan.DUMMY); + } + @Jni2RustMethod public ISwc4jAstExpr getAlt() { return alt; diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstExprOrSpread.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstExprOrSpread.java index e2f2351f..b88e24cc 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstExprOrSpread.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstExprOrSpread.java @@ -26,6 +26,7 @@ import com.caoccao.javet.swc4j.span.Swc4jSpan; import com.caoccao.javet.swc4j.utils.AssertionUtils; import com.caoccao.javet.swc4j.utils.SimpleList; +import com.caoccao.javet.swc4j.utils.StringUtils; import java.util.List; import java.util.Optional; @@ -48,7 +49,11 @@ public Swc4jAstExprOrSpread( } public static Swc4jAstExprOrSpread create(ISwc4jAstExpr expr) { - return new Swc4jAstExprOrSpread(null, expr, Swc4jSpan.DUMMY); + return create(null, expr); + } + + public static Swc4jAstExprOrSpread create(Swc4jSpan spread, ISwc4jAstExpr expr) { + return new Swc4jAstExprOrSpread(spread, expr, Swc4jSpan.DUMMY); } @Override @@ -93,7 +98,7 @@ public Swc4jAstExprOrSpread setSpread(Swc4jSpan spread) { @Override public String toString() { - String str = spread.map(span -> "...").orElse(""); + String str = spread.map(span -> "...").orElse(StringUtils.EMPTY); str += expr.toString(); return str; } diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstBigInt.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstBigInt.java index 0798d116..f1297918 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstBigInt.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstBigInt.java @@ -156,7 +156,12 @@ public Swc4jAstBigInt setValue(BigInteger value) { @Override public String toString() { - return raw.orElse((sign == Swc4jAstBigIntSign.Minus ? "-" : "") + value.toString()); + return raw.orElseGet(() -> { + String signString = sign == Swc4jAstBigIntSign.Minus + ? Swc4jAstBigIntSign.Minus.getName() + : Swc4jAstBigIntSign.NoSign.getName(); + return signString + value; + }); } @Override diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstJsxText.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstJsxText.java index a51a309b..c80bf714 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstJsxText.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstJsxText.java @@ -51,6 +51,10 @@ public Swc4jAstJsxText( setValue(value); } + public static Swc4jAstJsxText create(String value, String raw) { + return new Swc4jAstJsxText(value, raw, Swc4jSpan.DUMMY); + } + @Override public List getChildNodes() { return EMPTY_CHILD_NODES; @@ -66,16 +70,16 @@ public Swc4jAstType getType() { return Swc4jAstType.JsxText; } - @Override - public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) { - return false; - } - @Jni2RustMethod public String getValue() { return value; } + @Override + public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) { + return false; + } + public Swc4jAstJsxText setRaw(String raw) { this.raw = AssertionUtils.notNull(raw, "Raw"); return this; diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstObjectLit.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstObjectLit.java index c77fb524..c6ca9f38 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstObjectLit.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstObjectLit.java @@ -48,6 +48,10 @@ public Swc4jAstObjectLit( props.forEach(node -> node.setParent(this)); } + public static Swc4jAstObjectLit create(List props) { + return new Swc4jAstObjectLit(props, Swc4jSpan.DUMMY); + } + @Override public boolean asBoolean() { return true; diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstRegex.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstRegex.java index b162ba95..85bd5880 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstRegex.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstRegex.java @@ -29,6 +29,7 @@ import com.caoccao.javet.swc4j.jni2rust.Jni2RustMethod; import com.caoccao.javet.swc4j.span.Swc4jSpan; import com.caoccao.javet.swc4j.utils.AssertionUtils; +import com.caoccao.javet.swc4j.utils.StringUtils; import java.util.List; @@ -56,7 +57,7 @@ public static Swc4jAstRegex create() { } public static Swc4jAstRegex create(String exp) { - return create(exp, ""); + return create(exp, StringUtils.EMPTY); } public static Swc4jAstRegex create(String exp, String flags) { diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstStr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstStr.java index 984cac9f..ecdc7d64 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstStr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstStr.java @@ -50,7 +50,11 @@ public Swc4jAstStr( } public static Swc4jAstStr create(String value) { - return new Swc4jAstStr(value, "\"" + value + "\"", Swc4jSpan.DUMMY); + return create(value, "\"" + value + "\""); + } + + public static Swc4jAstStr create(String value, String raw) { + return new Swc4jAstStr(value, raw, Swc4jSpan.DUMMY); } @Override