diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java b/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java index e512d2a2..15c9f4bb 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java @@ -117,12 +117,15 @@ default List find(Class clazz, int depth) { * @since 0.2.0 */ @SuppressWarnings("unchecked") - default T getParent(Class clazz) { + default Optional getParent(Class 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); } /** diff --git a/src/main/java/com/caoccao/javet/swc4j/plugins/es2015/Swc4jPluginVisitorEs2015TransformSpread.java b/src/main/java/com/caoccao/javet/swc4j/plugins/es2015/Swc4jPluginVisitorEs2015TransformSpread.java index 89e84deb..c0023872 100644 --- a/src/main/java/com/caoccao/javet/swc4j/plugins/es2015/Swc4jPluginVisitorEs2015TransformSpread.java +++ b/src/main/java/com/caoccao/javet/swc4j/plugins/es2015/Swc4jPluginVisitorEs2015TransformSpread.java @@ -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 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()); } @@ -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 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 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 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);