Skip to content

Commit

Permalink
🦄 refactor: Revise eval() for call expr
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 18, 2024
1 parent eeee122 commit 2d489b4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,14 @@
import com.caoccao.javet.swc4j.span.Swc4jSpan;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.SimpleList;
import com.caoccao.javet.swc4j.utils.SimpleMap;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils)
public class Swc4jAstBinExpr
extends Swc4jAst
implements ISwc4jAstExpr {
protected static final Map<String, String> ARRAY_FUNCTION_STRING_MAP = SimpleMap.immutableOf(
"concat", "",
"copyWithin", "",
"entries", "[object Array Iterator]",
"fill", "",
"flat", "",
"indexOf", "-1",
"includes", "false",
"join", "",
"keys", "[object Array Iterator]",
"lastIndexOf", "-1",
"push", "0",
"reverse", "",
"slice", "",
"sort", "",
"splice", "",
"toReversed", "",
"toSorted", "",
"toSpliced", "",
"toString", "",
"unshift", "0",
"values", "[object Array Iterator]");

@Jni2RustField(box = true)
protected ISwc4jAstExpr left;
protected Swc4jAstBinaryOp op;
Expand Down Expand Up @@ -139,7 +114,7 @@ public Optional<ISwc4jAst> eval() {
if (expr.getType() == Swc4jAstType.Str) {
Swc4jAstStr str = expr.as(Swc4jAstStr.class);
String leftString = left.as(ISwc4jAstCoercionPrimitive.class).asString();
String rightString = ARRAY_FUNCTION_STRING_MAP.getOrDefault(str.getValue(), Swc4jAstIdent.UNDEFINED);
String rightString = Swc4jAstArrayLit.ARRAY_FUNCTION_STRING_MAP.getOrDefault(str.getValue(), Swc4jAstIdent.UNDEFINED);
return Optional.of(Swc4jAstStr.create(leftString + rightString));
}
}
Expand All @@ -155,7 +130,7 @@ public Optional<ISwc4jAst> eval() {
ISwc4jAstExpr expr = computedPropName.getExpr().unParenExpr();
if (expr.getType() == Swc4jAstType.Str) {
Swc4jAstStr str = expr.as(Swc4jAstStr.class);
String leftString = ARRAY_FUNCTION_STRING_MAP.getOrDefault(str.getValue(), Swc4jAstIdent.UNDEFINED);
String leftString = Swc4jAstArrayLit.ARRAY_FUNCTION_STRING_MAP.getOrDefault(str.getValue(), Swc4jAstIdent.UNDEFINED);
String rightString = right.as(ISwc4jAstCoercionPrimitive.class).asString();
return Optional.of(Swc4jAstStr.create(leftString + rightString));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,28 @@ public Optional<ISwc4jAst> eval() {
ISwc4jAstExpr expr = prop.getExpr().unParenExpr();
if (expr instanceof Swc4jAstStr) {
Swc4jAstStr str = expr.as(Swc4jAstStr.class);
if (BUILT_IN_FUNCTION_SET.contains(str.getValue())) {
ISwc4jAstExpr obj = memberExpr.getObj().unParenExpr();
while (obj instanceof Swc4jAstSeqExpr) {
Swc4jAstSeqExpr seqExpr = obj.as(Swc4jAstSeqExpr.class);
if (seqExpr.getExprs().isEmpty()) {
break;
} else {
obj = seqExpr.getExprs().get(seqExpr.getExprs().size() - 1);
}
memberExpr.setProp(Swc4jAstIdent.create(str.getValue()));
}
}
if (memberExpr.getProp() instanceof Swc4jAstIdent) {
Swc4jAstIdent ident = memberExpr.getProp().as(Swc4jAstIdent.class);
if (BUILT_IN_FUNCTION_SET.contains(ident.getSym())) {
ISwc4jAstExpr obj = memberExpr.getObj().unParenExpr();
while (obj instanceof Swc4jAstSeqExpr) {
Swc4jAstSeqExpr seqExpr = obj.as(Swc4jAstSeqExpr.class);
if (seqExpr.getExprs().isEmpty()) {
break;
} else {
obj = seqExpr.getExprs().get(seqExpr.getExprs().size() - 1);
}
if (obj instanceof Swc4jAstStr) {
String objString = obj.as(Swc4jAstStr.class).getValue();
if (FONTCOLOR.equals(str.getValue())) {
String argString = args.isEmpty() ? Swc4jAstIdent.UNDEFINED : args.get(0).toString();
return Optional.of(Swc4jAstStr.create("<font color=\"" + argString + "\">" + objString + "</font>"));
} else if (ITALICS.equals(str.getValue())) {
return Optional.of(Swc4jAstStr.create("<i>" + objString + "</i>"));
}
}
if (obj instanceof Swc4jAstStr) {
String objString = obj.as(Swc4jAstStr.class).getValue();
if (FONTCOLOR.equals(ident.getSym())) {
String argString = args.isEmpty() ? Swc4jAstIdent.UNDEFINED : args.get(0).toString();
return Optional.of(Swc4jAstStr.create("<font color=\"" + argString + "\">" + objString + "</font>"));
} else if (ITALICS.equals(ident.getSym())) {
return Optional.of(Swc4jAstStr.create("<i>" + objString + "</i>"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public Swc4jAstIdent(
setSym(sym);
}

public static Swc4jAstIdent create(String sym) {
return new Swc4jAstIdent(sym, false, Swc4jSpan.DUMMY);
}

public static Swc4jAstIdent createUndefined() {
return new Swc4jAstIdent(UNDEFINED, false, Swc4jSpan.DUMMY);
return create(UNDEFINED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,80 @@
import com.caoccao.javet.swc4j.span.Swc4jSpan;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.SimpleList;
import com.caoccao.javet.swc4j.utils.SimpleMap;
import com.caoccao.javet.swc4j.utils.SimpleSet;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils)
public class Swc4jAstArrayLit
extends Swc4jAst
implements ISwc4jAstExpr, ISwc4jAstCoercionPrimitive {
public static final Set<String> ARRAY_FUNCTION_SET = SimpleSet.immutableOf(
"at",
"concat",
"copyWithin",
"entries",
"every",
"fill",
"filter",
"find",
"findIndex",
"findLast",
"findLastIndex",
"flat",
"flatMap",
"forEach",
"includes",
"indexOf",
"join",
"keys",
"lastIndexOf",
"map",
"pop",
"push",
"reduce",
"reduceRight",
"reverse",
"shift",
"slice",
"some",
"sort",
"splice",
"toLocaleString",
"toReversed",
"toSorted",
"toSpliced",
"toString",
"unshift",
"values",
"with");
public static final Map<String, String> ARRAY_FUNCTION_STRING_MAP = SimpleMap.immutableOf(
"concat", "",
"copyWithin", "",
"entries", "[object Array Iterator]",
"fill", "",
"flat", "",
"indexOf", "-1",
"includes", "false",
"join", "",
"keys", "[object Array Iterator]",
"lastIndexOf", "-1",
"push", "0",
"reverse", "",
"slice", "",
"sort", "",
"splice", "",
"toReversed", "",
"toSorted", "",
"toSpliced", "",
"toString", "",
"unshift", "0",
"values", "[object Array Iterator]");
protected final List<Optional<Swc4jAstExprOrSpread>> elems;

@Jni2RustMethod
Expand Down

0 comments on commit 2d489b4

Please sign in to comment.