From 69bd7340af72a8634d22d5ace5c5b6407de5b229 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Thu, 23 May 2024 12:36:51 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20test:=20Add=20test=20for=20ast?= =?UTF-8?q?=20opt=20call?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...c4jPluginVisitorEs2015TransformSpread.java | 22 +++--- .../swc4j/ast/miscs/TestSwc4jAstOptCall.java | 71 +++++++++++++++++++ 2 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/caoccao/javet/swc4j/ast/miscs/TestSwc4jAstOptCall.java 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 cf1b1ced..7fd47107 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 @@ -50,6 +50,7 @@ protected ISwc4jAstExpr convertArguments(ISwc4jAstExpr expr) { Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr); callExpr.getArgs().add(Swc4jAstExprOrSpread.create(Swc4jAstNull.create())); callExpr.getArgs().add(Swc4jAstExprOrSpread.create(innerExpr)); + callExpr.updateParent(); return callExpr; } return expr; @@ -128,7 +129,7 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) { if (length == 1) { node.getParent().replaceNode(node, convertArguments(elems.get(0).get().getExpr())); } else { - // Prepare obj + // ident ISwc4jAstExpr obj; Optional optionalExprOrSpread = elems.get(0); int startIndex; @@ -152,11 +153,11 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) { } obj = objArrayLit; } - // Prepare concat() + // ident.concat() Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(obj, Swc4jAstIdent.createConcat()); Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr); Swc4jAstArrayLit objArrayLit = null; - // Prepare args + // ident.concat(...) for (int i = startIndex; i < length; i++) { optionalExprOrSpread = elems.get(i); if (optionalExprOrSpread.map(e -> e.getSpread().isPresent()).orElse(false)) { @@ -195,9 +196,9 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) { @Override public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) { + // super(...arguments) is not supported. if (node.isSpreadPresent() && node.getCallee() instanceof ISwc4jAstExpr) { ISwc4jAstExpr callee = node.getCallee().as(ISwc4jAstExpr.class); - List args = node.getArgs(); Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(callee, Swc4jAstIdent.createApply()); node.setCallee(memberExpr); Swc4jAstExprOrSpread thisArg; @@ -220,10 +221,12 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) { } else { thisArg = Swc4jAstExprOrSpread.create(Swc4jAstNull.create()); } + List args = node.getArgs(); Swc4jAstExprOrSpread arg = getConcatNode(args); args.clear(); args.add(thisArg); args.add(arg); + node.updateParent(); } return super.visitCallExpr(node); } @@ -231,13 +234,14 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) { @Override public Swc4jAstVisitorResponse visitNewExpr(Swc4jAstNewExpr node) { if (node.isSpreadPresent()) { - ISwc4jAstExpr callee = node.getCallee(); - List args = node.getArgs().get(); - Swc4jAstExprOrSpread arg = getConcatNode(args); - Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(callee, Swc4jAstIdent.createApply()); + // 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(arg); + callExpr.getArgs().add(getConcatNode(node.getArgs().get())); + callExpr.updateParent(); node.getParent().replaceNode(node, callExpr); } return super.visitNewExpr(node); diff --git a/src/test/java/com/caoccao/javet/swc4j/ast/miscs/TestSwc4jAstOptCall.java b/src/test/java/com/caoccao/javet/swc4j/ast/miscs/TestSwc4jAstOptCall.java new file mode 100644 index 00000000..2ff15158 --- /dev/null +++ b/src/test/java/com/caoccao/javet/swc4j/ast/miscs/TestSwc4jAstOptCall.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024. caoccao.com Sam Cao + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.caoccao.javet.swc4j.ast.miscs; + +import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst; +import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType; +import com.caoccao.javet.swc4j.ast.expr.Swc4jAstExprOrSpread; +import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent; +import com.caoccao.javet.swc4j.ast.expr.Swc4jAstMemberExpr; +import com.caoccao.javet.swc4j.ast.expr.Swc4jAstOptChainExpr; +import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript; +import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt; +import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException; +import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestSwc4jAstOptCall extends BaseTestSuiteSwc4jAst { + @Test + public void test() throws Swc4jCoreException { + String code = "a?.b(c,d)"; + Swc4jParseOutput output = swc4j.parse(code, jsScriptParseOptions); + Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class); + Swc4jAstExprStmt exprStmt = assertAst( + script, script.getBody().get(0).as(Swc4jAstExprStmt.class), Swc4jAstType.ExprStmt, 0, 9); + Swc4jAstOptChainExpr optChainExpr = assertAst( + exprStmt, exprStmt.getExpr().as(Swc4jAstOptChainExpr.class), Swc4jAstType.OptChainExpr, 0, 9); + assertFalse(optChainExpr.isOptional()); + Swc4jAstOptCall optCall = assertAst( + optChainExpr, optChainExpr.getBase().as(Swc4jAstOptCall.class), Swc4jAstType.OptCall, 0, 9); + assertFalse(optCall.getTypeArgs().isPresent()); + assertEquals(2, optCall.getArgs().size()); + Swc4jAstExprOrSpread exprOrSpread = assertAst( + optCall, optCall.getArgs().get(0), Swc4jAstType.ExprOrSpread, 5, 6); + Swc4jAstIdent ident = assertAst( + exprOrSpread, exprOrSpread.getExpr().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 5, 6); + assertEquals("c", ident.getSym()); + exprOrSpread = assertAst( + optCall, optCall.getArgs().get(1), Swc4jAstType.ExprOrSpread, 7, 8); + ident = assertAst( + exprOrSpread, exprOrSpread.getExpr().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 7, 8); + assertEquals("d", ident.getSym()); + Swc4jAstOptChainExpr childOptChainExpr = assertAst( + optCall, optCall.getCallee().as(Swc4jAstOptChainExpr.class), Swc4jAstType.OptChainExpr, 0, 4); + assertTrue(childOptChainExpr.isOptional()); + Swc4jAstMemberExpr memberExpr = assertAst( + childOptChainExpr, childOptChainExpr.getBase().as(Swc4jAstMemberExpr.class), Swc4jAstType.MemberExpr, 0, 4); + ident = assertAst( + memberExpr, memberExpr.getObj().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 0, 1); + assertEquals("a", ident.getSym()); + ident = assertAst( + memberExpr, memberExpr.getProp().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 3, 4); + assertEquals("b", ident.getSym()); + assertSpan(code, script); + } +}