From 66b1a12c6f4d59c243b39bdf86182d513bb34023 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Wed, 22 May 2024 12:45:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20ES2015=20transform=20spread?= =?UTF-8?q?=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Swc4jPluginVisitorEs2015TransformSpread.java | 17 ++++++++++++----- ...wc4jPluginVisitorEs2015TransformSpread.java} | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) rename src/test/java/com/caoccao/javet/swc4j/plugins/es2015/{TestSwc4jPluginVisitorJsFuckDecoder.java => TestSwc4jPluginVisitorEs2015TransformSpread.java} (95%) 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 0626006f..052a57ca 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 @@ -39,23 +39,26 @@ public Swc4jAstVisitorResponse visitExprOrSpread(Swc4jAstExprOrSpread node) { switch (node.getParent().getType()) { case ArrayLit: Swc4jAstArrayLit arrayLit = node.getParent().as(Swc4jAstArrayLit.class); - if (arrayLit.getElems().size() == 1) { + final int length = arrayLit.getElems().size(); + if (length == 1) { arrayLit.getParent().replaceNode(arrayLit, node.getExpr()); } else { - Swc4jAstIdent identConcat = Swc4jAstIdent.create(Swc4jAstArrayLit.CONCAT); int index = arrayLit.indexOf(node); if (index == 0) { - Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(node.getExpr(), identConcat); + Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create( + node.getExpr(), + Swc4jAstIdent.create(Swc4jAstArrayLit.CONCAT)); Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr); ISwc4jAst newNode = callExpr; - final int length = arrayLit.getElems().size(); for (int i = 1; i < length; i++) { Optional elem = arrayLit.getElems().get(i); if (elem.isPresent()) { if (elem.get().getSpread().isPresent()) { callExpr.getArgs().add(Swc4jAstExprOrSpread.create(elem.get().getExpr())); newNode = callExpr; - memberExpr = Swc4jAstMemberExpr.create(callExpr, identConcat); + memberExpr = Swc4jAstMemberExpr.create( + callExpr, + Swc4jAstIdent.create(Swc4jAstArrayLit.CONCAT)); callExpr = Swc4jAstCallExpr.create(memberExpr); } else { // TODO @@ -65,6 +68,10 @@ public Swc4jAstVisitorResponse visitExprOrSpread(Swc4jAstExprOrSpread node) { } } arrayLit.getParent().replaceNode(arrayLit, newNode); + } else if (index == length - 1) { + // TODO + } else { + // TODO } } break; diff --git a/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorJsFuckDecoder.java b/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java similarity index 95% rename from src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorJsFuckDecoder.java rename to src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java index 14e85abc..d171ba7c 100644 --- a/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorJsFuckDecoder.java +++ b/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java @@ -23,7 +23,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class TestSwc4jPluginVisitorJsFuckDecoder extends BaseTestSuiteSwc4jPlugin { +public class TestSwc4jPluginVisitorEs2015TransformSpread extends BaseTestSuiteSwc4jPlugin { @BeforeEach @Override