Skip to content

Commit

Permalink
✨ feat: Add create() v1
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 27, 2024
1 parent 706675c commit 068d8d2
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public Swc4jAstAssignProp(
setValue(value);
}

public static Swc4jAstAssignProp create(Swc4jAstIdent key, ISwc4jAstExpr value) {
return new Swc4jAstAssignProp(key, value, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public Swc4jAstAutoAccessor(
this.decorators.forEach(node -> node.setParent(this));
}

public static Swc4jAstAutoAccessor create(ISwc4jAstKey key) {
return create(key, null);
}

public static Swc4jAstAutoAccessor create(ISwc4jAstKey key, ISwc4jAstExpr value) {
return new Swc4jAstAutoAccessor(
key, value, null, false, SimpleList.of(),
null, false, false, false, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public Optional<Swc4jAstAccessibility> getAccessibility() {
return accessibility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public Swc4jAstClass(
this.decorators.forEach(node -> node.setParent(this));
}

public static Swc4jAstClass create(List<ISwc4jAstClassMember> body) {
return new Swc4jAstClass(
SimpleList.of(), body, null, false, null,
null, SimpleList.of(), Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public List<ISwc4jAstClassMember> getBody() {
return body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstClassMember;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPropName;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeParamDecl;
import com.caoccao.javet.swc4j.ast.visitors.ISwc4jAstVisitor;
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitorResponse;
import com.caoccao.javet.swc4j.jni2rust.*;
Expand Down Expand Up @@ -74,6 +73,15 @@ public Swc4jAstClassMethod(
setStatic(_static);
}

public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind) {
return new Swc4jAstClassMethod(
key, function, kind, false, null,
false, false, false, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public Optional<Swc4jAstAccessibility> getAccessibility() {
return accessibility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ public Swc4jAstClassProp(
this.decorators.forEach(node -> node.setParent(this));
}

public static Swc4jAstClassProp create(ISwc4jAstPropName key) {
return create(key, null);
}

public static Swc4jAstClassProp create(ISwc4jAstPropName key, ISwc4jAstExpr value) {
return new Swc4jAstClassProp(
key, value, null, false, SimpleList.of(),
null, false, false, false, false,
false, false, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public Optional<Swc4jAstAccessibility> getAccessibility() {
return accessibility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public Swc4jAstComputedPropName(
setExpr(expr);
}

public static Swc4jAstComputedPropName create(ISwc4jAstExpr expr) {
return new Swc4jAstComputedPropName(expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ public Swc4jAstConstructor(
this.params.forEach(node -> node.setParent(this));
}

public static Swc4jAstConstructor create(ISwc4jAstPropName key) {
return create(key, SimpleList.of());
}

public static Swc4jAstConstructor create(
ISwc4jAstPropName key,
List<ISwc4jAstParamOrTsParamProp> params) {
return create(key, params, null);
}

public static Swc4jAstConstructor create(
ISwc4jAstPropName key,
List<ISwc4jAstParamOrTsParamProp> params,
Swc4jAstBlockStmt body) {
return new Swc4jAstConstructor(key, params, body, null, false, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public Optional<Swc4jAstAccessibility> getAccessibility() {
return accessibility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public static Swc4jAstCallExpr create(ISwc4jAstCallee callee) {
return create(callee, SimpleList.of());
}

public static Swc4jAstCallExpr create(ISwc4jAstCallee callee, Swc4jAstExprOrSpread... args) {
return create(callee, SimpleList.of(args));
}

public static Swc4jAstCallExpr create(ISwc4jAstCallee callee, List<Swc4jAstExprOrSpread> args) {
return new Swc4jAstCallExpr(callee, args, null, Swc4jSpan.DUMMY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static Swc4jAstBlockStmt create() {
return new Swc4jAstBlockStmt(SimpleList.of(), Swc4jSpan.DUMMY);
}

public static Swc4jAstBlockStmt create(ISwc4jAstStmt... stmts) {
return new Swc4jAstBlockStmt(SimpleList.of(stmts), Swc4jSpan.DUMMY);
public static Swc4jAstBlockStmt create(List<ISwc4jAstStmt> stmts) {
return new Swc4jAstBlockStmt(stmts, Swc4jSpan.DUMMY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind) {
return create(kind, false);
}

public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind, Swc4jAstVarDeclarator... decls) {
return create(kind, false, SimpleList.of(decls));
}

public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind, List<Swc4jAstVarDeclarator> decls) {
return create(kind, false, decls);
}
Expand All @@ -71,10 +67,6 @@ public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind, boolean declare)
return create(kind, declare, SimpleList.of());
}

public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind, boolean declare, Swc4jAstVarDeclarator... decls) {
return create(kind, declare, SimpleList.of(decls));
}

public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind, boolean declare, List<Swc4jAstVarDeclarator> decls) {
return new Swc4jAstVarDecl(kind, declare, decls, Swc4jSpan.DUMMY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ protected ISwc4jAstExpr convertArguments(ISwc4jAstExpr expr) {
Swc4jAstIdent.createApply());
return Swc4jAstCallExpr.create(
memberExpr,
Swc4jAstExprOrSpread.create(Swc4jAstNull.create()),
Swc4jAstExprOrSpread.create(innerExpr));
SimpleList.of(
Swc4jAstExprOrSpread.create(Swc4jAstNull.create()),
Swc4jAstExprOrSpread.create(innerExpr)));
}
return expr;
}
Expand Down Expand Up @@ -203,8 +204,8 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) {
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var, varDeclarator);
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create(varDecl, Swc4jAstExprStmt.create(node));
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var, SimpleList.of(varDeclarator));
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create(SimpleList.of(varDecl, Swc4jAstExprStmt.create(node)));
stmt.getParent().replaceNode(stmt, blockStmt);
childMemberExpr.setObj(Swc4jAstIdent.createDummy());
thisArg = Swc4jAstExprOrSpread.create(Swc4jAstIdent.createDummy());
Expand All @@ -230,8 +231,9 @@ public Swc4jAstVisitorResponse visitNewExpr(Swc4jAstNewExpr node) {
// ident.apply(null, arg)
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(
memberExpr,
Swc4jAstExprOrSpread.create(Swc4jAstNull.create()),
getConcatNode(node.getArgs().get()));
SimpleList.of(
Swc4jAstExprOrSpread.create(Swc4jAstNull.create()),
getConcatNode(node.getArgs().get())));
node.getParent().replaceNode(node, callExpr);
}
return super.visitNewExpr(node);
Expand All @@ -249,9 +251,9 @@ public Swc4jAstVisitorResponse visitOptCall(Swc4jAstOptCall node) {
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var, varDeclarator);
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create(
varDecl, Swc4jAstExprStmt.create(node.getParent().as(ISwc4jAstExpr.class)));
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var, SimpleList.of(varDeclarator));
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create(SimpleList.of(
varDecl, Swc4jAstExprStmt.create(node.getParent().as(ISwc4jAstExpr.class))));
stmt.getParent().replaceNode(stmt, blockStmt);
childMemberExpr.setObj(Swc4jAstIdent.createDummy());
List<Swc4jAstExprOrSpread> args = node.getArgs();
Expand Down

0 comments on commit 068d8d2

Please sign in to comment.