Skip to content

Commit

Permalink
✨ feat: Add create() v4
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 29, 2024
1 parent af12068 commit bea4c99
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ public Swc4jAstArrowExpr(
this.params.forEach(node -> node.setParent(this));
}

public static Swc4jAstArrowExpr create(
List<ISwc4jAstPat> params,
ISwc4jAstBlockStmtOrExpr body) {
return create(params, body, false, false);
}

public static Swc4jAstArrowExpr create(
List<ISwc4jAstPat> params,
ISwc4jAstBlockStmtOrExpr body,
boolean _async,
boolean generator) {
return create(params, body, _async, generator, null);
}

public static Swc4jAstArrowExpr create(
List<ISwc4jAstPat> params,
ISwc4jAstBlockStmtOrExpr body,
boolean _async,
boolean generator,
Swc4jAstTsTypeParamDecl typeParams) {
return create(params, body, _async, generator, typeParams, null);
}

public static Swc4jAstArrowExpr create(
List<ISwc4jAstPat> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISwc4jAst> getChildNodes() {
return SimpleList.of(left, right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public static Swc4jAstCallExpr create(ISwc4jAstCallee callee) {
}

public static Swc4jAstCallExpr create(ISwc4jAstCallee callee, List<Swc4jAstExprOrSpread> args) {
return new Swc4jAstCallExpr(callee, args, null, Swc4jSpan.DUMMY);
return create(callee, args, null);
}

public static Swc4jAstCallExpr create(
ISwc4jAstCallee callee,
List<Swc4jAstExprOrSpread> args,
Swc4jAstTsTypeParamInstantiation typeArgs) {
return new Swc4jAstCallExpr(callee, args, typeArgs, Swc4jSpan.DUMMY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.of(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISwc4jAst> getChildNodes() {
return EMPTY_CHILD_NODES;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public Swc4jAstObjectLit(
props.forEach(node -> node.setParent(this));
}

public static Swc4jAstObjectLit create(List<ISwc4jAstPropOrSpread> props) {
return new Swc4jAstObjectLit(props, Swc4jSpan.DUMMY);
}

@Override
public boolean asBoolean() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bea4c99

Please sign in to comment.