Skip to content

Commit

Permalink
✨ feat: Revise return type of getParent()
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 20, 2024
1 parent a08cdd9 commit c4b05f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ default <T extends ISwc4jAst> List<T> find(Class<T> clazz, int depth) {
* @since 0.2.0
*/
@SuppressWarnings("unchecked")
default <T extends ISwc4jAst> T getParent(Class<T> clazz) {
default <T extends ISwc4jAst> Optional<T> getParent(Class<T> clazz) {
ISwc4jAst parent = getParent();
while (parent != null && !clazz.isAssignableFrom(parent.getClass())) {
parent = parent.getParent();
if (parent == null) {
return Optional.empty();
}
return (T) parent;
if (clazz.isAssignableFrom(parent.getClass())) {
return Optional.of((T) parent);
}
return parent.getParent(clazz);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,20 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) {
Swc4jAstExprOrSpread thisArg;
if (callee instanceof Swc4jAstMemberExpr) {
Swc4jAstMemberExpr childMemberExpr = callee.as(Swc4jAstMemberExpr.class);
ISwc4jAstStmt stmt = node.getParent(ISwc4jAstStmt.class);
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
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());
Optional<ISwc4jAstStmt> optionalStmt = node.getParent(ISwc4jAstStmt.class);
if (optionalStmt.isPresent()) {
ISwc4jAstStmt stmt = optionalStmt.get();
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
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());
} else {
throw new IllegalArgumentException("Parent statement not found.");
}
} else {
thisArg = Swc4jAstExprOrSpread.create(Swc4jAstNull.create());
}
Expand Down Expand Up @@ -247,23 +252,28 @@ public Swc4jAstVisitorResponse visitOptCall(Swc4jAstOptCall node) {
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(callee, Swc4jAstIdentName.createApply());
node.setCallee(memberExpr);
Swc4jAstMemberExpr childMemberExpr = callee.getBase().as(Swc4jAstMemberExpr.class);
ISwc4jAstStmt stmt = node.getParent(ISwc4jAstStmt.class);
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
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();
Swc4jAstExprOrSpread arg = getConcatNode(args);
args.clear();
Swc4jAstExprOrSpread dummyExprOrSpread = Swc4jAstExprOrSpread.create(Swc4jAstIdent.createDummy());
dummyExprOrSpread.setParent(node);
arg.setParent(node);
args.add(dummyExprOrSpread);
args.add(arg);
Optional<ISwc4jAstStmt> optionalStmt = node.getParent(ISwc4jAstStmt.class);
if (optionalStmt.isPresent()) {
ISwc4jAstStmt stmt = optionalStmt.get();
Swc4jAstVarDeclarator varDeclarator = Swc4jAstVarDeclarator.create(
Swc4jAstIdent.createDummy(),
childMemberExpr.getObj());
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();
Swc4jAstExprOrSpread arg = getConcatNode(args);
args.clear();
Swc4jAstExprOrSpread dummyExprOrSpread = Swc4jAstExprOrSpread.create(Swc4jAstIdent.createDummy());
dummyExprOrSpread.setParent(node);
arg.setParent(node);
args.add(dummyExprOrSpread);
args.add(arg);
} else {
throw new IllegalArgumentException("Parent statement not found.");
}
}
}
return super.visitOptCall(node);
Expand Down

0 comments on commit c4b05f4

Please sign in to comment.