From 5385e8f2ffda05de6e3ac2577a235bbe643ee796 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Wed, 22 May 2024 14:17:21 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20ES2015=20transform=20spread?= =?UTF-8?q?=20v4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Swc4jPluginVisitorEs2015TransformSpread.java | 17 +++++++++-------- ...Swc4jPluginVisitorEs2015TransformSpread.java | 4 ++++ 2 files changed, 13 insertions(+), 8 deletions(-) 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 841ac87d..239bc675 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 @@ -55,9 +55,10 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) { if (optionalExprOrSpread.map(e -> e.getSpread().isPresent()).orElse(false)) { break; } else { - Swc4jAstExprOrSpread elem = Swc4jAstExprOrSpread.create(optionalExprOrSpread.get().getExpr()); - elem.setParent(objArrayLit); - objArrayLit.getElems().add(Optional.of(elem)); + Optional elem = optionalExprOrSpread + .map(e -> Swc4jAstExprOrSpread.create(e.getExpr())); + elem.ifPresent(e -> e.setParent(objArrayLit)); + objArrayLit.getElems().add(elem); startIndex = i + 1; } } @@ -86,12 +87,12 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) { if (objArrayLit == null) { objArrayLit = Swc4jAstArrayLit.create(); } - Swc4jAstExprOrSpread elem = null; - if (optionalExprOrSpread.isPresent()) { - elem = Swc4jAstExprOrSpread.create(optionalExprOrSpread.get().getExpr()); - elem.setParent(objArrayLit); + Optional elem = optionalExprOrSpread + .map(e -> Swc4jAstExprOrSpread.create(e.getExpr())); + if (elem.isPresent()) { + elem.get().setParent(objArrayLit); } - objArrayLit.getElems().add(Optional.ofNullable(elem)); + objArrayLit.getElems().add(elem); } } if (objArrayLit != null) { diff --git a/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java b/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java index 046c3050..b3f79f22 100644 --- a/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java +++ b/src/test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorEs2015TransformSpread.java @@ -49,6 +49,10 @@ public void testArrayLit() { "const a=[1,2];const b=[3,4];JSON.stringify([\"x\",\"y\"].concat(a,b));", "const a = [1,2]; const b = [3,4]; JSON.stringify([...a, ...[...a, ...b], ...b]);", "const a=[1,2];const b=[3,4];JSON.stringify(a.concat(a.concat(b),b));", + "const a = [1,2]; const b = [3,4]; JSON.stringify([,,...a, ...b]);", + "const a=[1,2];const b=[3,4];JSON.stringify([,,].concat(a,b));", + "const a = [1,2]; const b = [3,4]; JSON.stringify([,,...a,,, ...b]);", + "const a=[1,2];const b=[3,4];JSON.stringify([,,].concat(a,[,,],b));", "const a = [1,2]; JSON.stringify([...a]);", "const a=[1,2];JSON.stringify(a);")); }