Skip to content

Commit

Permalink
🦄 refactor: Revise create() for param, call expr, block stmt, return …
Browse files Browse the repository at this point in the history
…stmt, var decl
  • Loading branch information
caoccao committed May 27, 2024
1 parent e20bcc7 commit 706675c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public Swc4jAstParam(
this.decorators.forEach(node -> node.setParent(this));
}

public static Swc4jAstParam create(ISwc4jAstPat pat) {
return new Swc4jAstParam(SimpleList.of(), pat, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.copyOf(decorators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,15 @@ public Swc4jAstCallExpr(
}

public static Swc4jAstCallExpr create(ISwc4jAstCallee callee) {
return new Swc4jAstCallExpr(callee, SimpleList.of(), null, Swc4jSpan.DUMMY);
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);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ 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);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.copyOf(stmts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public Swc4jAstReturnStmt(
setArg(arg);
}

public static Swc4jAstReturnStmt create(ISwc4jAstExpr arg) {
return new Swc4jAstReturnStmt(arg, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public Optional<ISwc4jAstExpr> getArg() {
return arg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,24 @@ 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);
}

public static Swc4jAstVarDecl create(Swc4jAstVarDeclKind kind, boolean declare) {
return new Swc4jAstVarDecl(kind, declare, SimpleList.of(), Swc4jSpan.DUMMY);
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);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitor;
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitorResponse;
import com.caoccao.javet.swc4j.constants.ISwc4jConstants;
import com.caoccao.javet.swc4j.utils.SimpleList;

import java.util.List;
import java.util.Optional;
Expand All @@ -47,11 +48,10 @@ protected ISwc4jAstExpr convertArguments(ISwc4jAstExpr expr) {
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(
Swc4jAstIdent.createArray(),
Swc4jAstIdent.createApply());
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr);
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(Swc4jAstNull.create()));
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(innerExpr));
callExpr.updateParent();
return callExpr;
return Swc4jAstCallExpr.create(
memberExpr,
Swc4jAstExprOrSpread.create(Swc4jAstNull.create()),
Swc4jAstExprOrSpread.create(innerExpr));
}
return expr;
}
Expand Down Expand Up @@ -87,21 +87,17 @@ protected Swc4jAstExprOrSpread getConcatNode(List<Swc4jAstExprOrSpread> args) {
}
// Prepare concat()
Swc4jAstMemberExpr childMemberExpr = Swc4jAstMemberExpr.create(obj, Swc4jAstIdent.createConcat());
Swc4jAstCallExpr childCallExpr = Swc4jAstCallExpr.create(childMemberExpr);
Swc4jAstArrayLit objArrayLit = null;
// Prepare args
List<Swc4jAstExprOrSpread> childArgs = SimpleList.of();
Swc4jAstArrayLit objArrayLit = null;
for (int i = startIndex; i < length; i++) {
arg = args.get(i);
if (arg.getSpread().isPresent()) {
if (objArrayLit != null) {
Swc4jAstExprOrSpread childArg = Swc4jAstExprOrSpread.create(objArrayLit);
childArg.setParent(childCallExpr);
childCallExpr.getArgs().add(childArg);
childArgs.add(Swc4jAstExprOrSpread.create(objArrayLit));
objArrayLit = null;
}
Swc4jAstExprOrSpread childElem = Swc4jAstExprOrSpread.create(convertArguments(arg.getExpr()));
childElem.setParent(childCallExpr);
childCallExpr.getArgs().add(childElem);
childArgs.add(Swc4jAstExprOrSpread.create(convertArguments(arg.getExpr())));
} else {
if (objArrayLit == null) {
objArrayLit = Swc4jAstArrayLit.create();
Expand All @@ -112,10 +108,9 @@ protected Swc4jAstExprOrSpread getConcatNode(List<Swc4jAstExprOrSpread> args) {
}
}
if (objArrayLit != null) {
Swc4jAstExprOrSpread childArg = Swc4jAstExprOrSpread.create(objArrayLit);
childArg.setParent(childCallExpr);
childCallExpr.getArgs().add(childArg);
childArgs.add(Swc4jAstExprOrSpread.create(objArrayLit));
}
Swc4jAstCallExpr childCallExpr = Swc4jAstCallExpr.create(childMemberExpr, childArgs);
arg = Swc4jAstExprOrSpread.create(childCallExpr);
}
return arg;
Expand Down Expand Up @@ -205,16 +200,11 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) {
if (callee instanceof Swc4jAstMemberExpr) {
Swc4jAstMemberExpr childMemberExpr = callee.as(Swc4jAstMemberExpr.class);
ISwc4jAstStmt stmt = node.getParent(ISwc4jAstStmt.class);
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create();
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var);
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
varDecl.getDecls().add(varDeclarator);
varDecl.updateParent();
blockStmt.getStmts().add(varDecl);
blockStmt.getStmts().add(Swc4jAstExprStmt.create(node));
blockStmt.updateParent();
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var, varDeclarator);
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create(varDecl, Swc4jAstExprStmt.create(node));
stmt.getParent().replaceNode(stmt, blockStmt);
childMemberExpr.setObj(Swc4jAstIdent.createDummy());
thisArg = Swc4jAstExprOrSpread.create(Swc4jAstIdent.createDummy());
Expand All @@ -224,9 +214,10 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) {
List<Swc4jAstExprOrSpread> args = node.getArgs();
Swc4jAstExprOrSpread arg = getConcatNode(args);
args.clear();
thisArg.setParent(node);
arg.setParent(node);
args.add(thisArg);
args.add(arg);
node.updateParent();
}
return super.visitCallExpr(node);
}
Expand All @@ -236,12 +227,11 @@ public Swc4jAstVisitorResponse visitNewExpr(Swc4jAstNewExpr node) {
if (node.isSpreadPresent()) {
// ident.apply
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(node.getCallee(), Swc4jAstIdent.createApply());
// ident.apply()
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr);
// ident.apply(null, arg)
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(Swc4jAstNull.create()));
callExpr.getArgs().add(getConcatNode(node.getArgs().get()));
callExpr.updateParent();
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(
memberExpr,
Swc4jAstExprOrSpread.create(Swc4jAstNull.create()),
getConcatNode(node.getArgs().get()));
node.getParent().replaceNode(node, callExpr);
}
return super.visitNewExpr(node);
Expand All @@ -256,24 +246,22 @@ public Swc4jAstVisitorResponse visitOptCall(Swc4jAstOptCall node) {
node.setCallee(memberExpr);
Swc4jAstMemberExpr childMemberExpr = callee.getBase().as(Swc4jAstMemberExpr.class);
ISwc4jAstStmt stmt = node.getParent(ISwc4jAstStmt.class);
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create();
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var);
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
varDecl.getDecls().add(varDeclarator);
varDecl.updateParent();
blockStmt.getStmts().add(varDecl);
blockStmt.getStmts().add(Swc4jAstExprStmt.create(node.getParent().as(ISwc4jAstExpr.class)));
blockStmt.updateParent();
Swc4jAstVarDecl varDecl = Swc4jAstVarDecl.create(Swc4jAstVarDeclKind.Var, varDeclarator);
Swc4jAstBlockStmt blockStmt = Swc4jAstBlockStmt.create(
varDecl, Swc4jAstExprStmt.create(node.getParent().as(ISwc4jAstExpr.class)));
stmt.getParent().replaceNode(stmt, blockStmt);
childMemberExpr.setObj(Swc4jAstIdent.createDummy());
List<Swc4jAstExprOrSpread> args = node.getArgs();
Swc4jAstExprOrSpread arg = getConcatNode(args);
args.clear();
args.add(Swc4jAstExprOrSpread.create(Swc4jAstIdent.createDummy()));
Swc4jAstExprOrSpread dummyExprOrSpread = Swc4jAstExprOrSpread.create(Swc4jAstIdent.createDummy());
dummyExprOrSpread.setParent(node);
arg.setParent(node);
args.add(dummyExprOrSpread);
args.add(arg);
node.updateParent();
}
}
return super.visitOptCall(node);
Expand Down

0 comments on commit 706675c

Please sign in to comment.