Skip to content

Commit

Permalink
✨ feat: Add concat to array lit
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 21, 2024
1 parent 9156ef0 commit c63940d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class Swc4jAstCallExpr
public static final String TO_STRING = "toString";
public static final Set<String> 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);
Expand Down Expand Up @@ -115,8 +116,20 @@ public Optional<ISwc4jAst> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.of();
Expand All @@ -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
Expand All @@ -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(","));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] }\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c63940d

Please sign in to comment.