From c63940d9cc339e221dafd202fd4f47ad822c61a8 Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Tue, 21 May 2024 13:09:05 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20concat=20to=20array?= =?UTF-8?q?=20lit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../swc4j/ast/expr/Swc4jAstCallExpr.java | 17 +++++++++-- .../swc4j/ast/expr/lit/Swc4jAstArrayLit.java | 29 +++++++++++++++++-- .../TestSwc4jPluginHostJsFuckDecoder.java | 3 ++ .../swc4j/tutorials/Tutorial07Decode.java | 2 +- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java index 93f2f664..67c7ed6b 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstCallExpr.java @@ -56,6 +56,7 @@ public class Swc4jAstCallExpr public static final String TO_STRING = "toString"; public static final Set BUILT_IN_FUNCTION_SET = SimpleSet.immutableOf(FONTCOLOR, ITALICS); public static final String SLICE = "slice"; + public static final String CONCAT = "concat"; protected static final Swc4jParseOptions PARSE_OPTIONS = new Swc4jParseOptions() .setCaptureAst(true) .setMediaType(Swc4jMediaType.JavaScript); @@ -115,8 +116,20 @@ public Optional eval() { } } } else if (obj instanceof Swc4jAstArrayLit) { - return Optional.of(Swc4jAstStr.create(Swc4jAstArrayLit.ARRAY_FUNCTION_STRING_MAP - .getOrDefault(call.get(), Swc4jAstIdent.UNDEFINED))); + if (CONCAT.equals(call.get())) { + if (!args.isEmpty()) { + ISwc4jAstExpr expr = args.get(0).getExpr().unParenExpr(); + if (expr instanceof Swc4jAstArrayLit) { + Swc4jAstArrayLit leftArrayLit = obj.as(Swc4jAstArrayLit.class); + Swc4jAstArrayLit rightArrayLit = expr.as(Swc4jAstArrayLit.class); + leftArrayLit.concat(rightArrayLit); + return Optional.of(leftArrayLit); + } + } + } else { + return Optional.of(Swc4jAstStr.create(Swc4jAstArrayLit.ARRAY_FUNCTION_STRING_MAP + .getOrDefault(call.get(), Swc4jAstIdent.UNDEFINED))); + } } else if (Swc4jAstMemberExpr.CONSTRUCTOR.equals(call.get())) { if (obj instanceof Swc4jAstRegex) { switch (args.size()) { diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstArrayLit.java b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstArrayLit.java index 4e69d4f2..6d773b8c 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstArrayLit.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstArrayLit.java @@ -170,6 +170,15 @@ public String asString() { return toString(); } + public void concat(Swc4jAstArrayLit arrayLit) { + if (!AssertionUtils.notNull(arrayLit, "Array lit").getElems().isEmpty()) { + arrayLit.getElems().forEach(elem -> { + elem.ifPresent(e -> e.setParent(this)); + elems.add(elem); + }); + } + } + @Override public List getChildNodes() { List childNodes = SimpleList.of(); @@ -196,8 +205,15 @@ public boolean isAllPrimitive() { .map(Optional::get) .map(Swc4jAstExprOrSpread::getExpr) .map(ISwc4jAstExpr::unParenExpr) - .map(ISwc4jAst::getType) - .allMatch(Swc4jAstType::isPrimitive); + .allMatch(elem -> { + if (elem.getType().isPrimitive() || elem.isUndefined() || elem.isInfinity() || elem.isNaN()) { + return true; + } + if (elem instanceof Swc4jAstArrayLit) { + return elem.as(Swc4jAstArrayLit.class).isAllPrimitive(); + } + return false; + }); } @Override @@ -221,7 +237,14 @@ public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) { public String toString() { return elems.stream() .map(optionalElem -> optionalElem - .map(Swc4jAstExprOrSpread::toString) + .map(elem -> { + ISwc4jAstExpr expr = elem.as(Swc4jAstExprOrSpread.class).getExpr().unParenExpr(); + if (expr.isUndefined()) { + return ""; + } else { + return expr.toString(); + } + }) .orElse("")) .collect(Collectors.joining(",")); } diff --git a/src/test/java/com/caoccao/javet/swc4j/plugins/jsfuck/TestSwc4jPluginHostJsFuckDecoder.java b/src/test/java/com/caoccao/javet/swc4j/plugins/jsfuck/TestSwc4jPluginHostJsFuckDecoder.java index 694fa599..ccafd565 100644 --- a/src/test/java/com/caoccao/javet/swc4j/plugins/jsfuck/TestSwc4jPluginHostJsFuckDecoder.java +++ b/src/test/java/com/caoccao/javet/swc4j/plugins/jsfuck/TestSwc4jPluginHostJsFuckDecoder.java @@ -44,6 +44,9 @@ public void test() throws Swc4jCoreException { testCaseMap.put("'a'+true", "\"atrue\""); testCaseMap.put("[][[]]", "undefined"); testCaseMap.put("[][[]]+[]", "\"undefined\""); + testCaseMap.put("[[],[]]+''", "\",\""); + testCaseMap.put("[[],undefined]+''", "\",\""); + testCaseMap.put("[[]][\"concat\"]([[]])+[]", "\",\""); testCaseMap.put("''+[]['entries']()", "\"[object Array Iterator]\""); testCaseMap.put("([![]]+[][[]])[+!+[]+[+[]]]", "\"i\""); testCaseMap.put("true+[][\"flat\"]", "\"truefunction flat() { [native code] }\""); diff --git a/src/test/java/com/caoccao/javet/swc4j/tutorials/Tutorial07Decode.java b/src/test/java/com/caoccao/javet/swc4j/tutorials/Tutorial07Decode.java index 4a39c6d1..fb26765e 100644 --- a/src/test/java/com/caoccao/javet/swc4j/tutorials/Tutorial07Decode.java +++ b/src/test/java/com/caoccao/javet/swc4j/tutorials/Tutorial07Decode.java @@ -40,7 +40,7 @@ public static void main(String[] args) throws Swc4jCoreException, MalformedURLEx // Prepare a script name. URL specifier = new URL("file:///abc.ts"); // Create a plugin host. - ISwc4jPluginHost pluginHost = new Swc4jPluginHostJsFuckDecoder(); + ISwc4jPluginHost pluginHost = new Swc4jPluginHostJsFuckDecoder(10); Swc4jTransformOptions options = new Swc4jTransformOptions() .setSpecifier(specifier) .setPluginHost(pluginHost)