Skip to content

Commit

Permalink
✨ feat: Support name, toString for member expr, call expr
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 20, 2024
1 parent 9d59ec2 commit bddd300
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstFunction;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstArrayLit;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstRegex;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstStr;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstCallee;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstCoercionPrimitive;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstReturnStmt;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeParamInstantiation;
Expand All @@ -50,6 +52,7 @@ public class Swc4jAstCallExpr
implements ISwc4jAstExpr {
public static final String FONTCOLOR = "fontcolor";
public static final String ITALICS = "italics";
public static final String TO_STRING = "toString";
public static final Set<String> BUILT_IN_FUNCTION_SET = SimpleSet.immutableOf(FONTCOLOR, ITALICS);
protected static final Swc4jParseOptions PARSE_OPTIONS = new Swc4jParseOptions()
.setCaptureAst(true)
Expand Down Expand Up @@ -80,8 +83,8 @@ public Optional<ISwc4jAst> eval() {
Swc4jAstMemberExpr memberExpr = callee.as(Swc4jAstMemberExpr.class);
Optional<String> call = memberExpr.evalAsCall();
if (call.isPresent()) {
ISwc4jAstExpr obj = memberExpr.getObj().unParenExpr();
if (BUILT_IN_FUNCTION_SET.contains(call.get())) {
ISwc4jAstExpr obj = memberExpr.getObj().unParenExpr();
if (obj instanceof Swc4jAstStr) {
String objString = obj.as(Swc4jAstStr.class).getValue();
if (FONTCOLOR.equals(call.get())) {
Expand All @@ -92,7 +95,7 @@ public Optional<ISwc4jAst> eval() {
}
}
} else if (Swc4jAstMemberExpr.CONSTRUCTOR.equals(call.get())) {
if (memberExpr.getObj() instanceof Swc4jAstRegex) {
if (obj instanceof Swc4jAstRegex) {
switch (args.size()) {
case 0:
return Optional.of(Swc4jAstRegex.create());
Expand All @@ -114,6 +117,21 @@ public Optional<ISwc4jAst> eval() {
break;
}
}
} else if (TO_STRING.equals(call.get())) {
if (obj instanceof Swc4jAstNumber) {
int radix = 10;
if (!args.isEmpty()) {
ISwc4jAstExpr arg = args.get(0).getExpr().unParenExpr();
if (arg instanceof Swc4jAstNumber || arg instanceof Swc4jAstStr) {
radix = arg.as(ISwc4jAstCoercionPrimitive.class).asInt();
}
}
if (radix < 2 || radix > 32) {
radix = 10;
}
String value = Integer.toString(obj.as(Swc4jAstNumber.class).asInt(), radix);
return Optional.of(Swc4jAstStr.create(value));
}
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Swc4jAstMemberExpr
extends Swc4jAst
implements ISwc4jAstExpr, ISwc4jAstOptChainBase, ISwc4jAstSimpleAssignTarget {
public static final String CONSTRUCTOR = "constructor";
public static final String NAME = "name";
@Jni2RustField(box = true)
protected ISwc4jAstExpr obj;
protected ISwc4jAstMemberProp prop;
Expand Down Expand Up @@ -79,12 +80,25 @@ public Optional<ISwc4jAst> eval() {
break;
case MemberExpr: {
Optional<String> call = evalAsCall();
if (call.isPresent() && CONSTRUCTOR.equals(call.get())) {
Swc4jAstMemberExpr childMemberExpr = obj.as(Swc4jAstMemberExpr.class);
if (childMemberExpr.getObj() instanceof Swc4jAstArrayLit) {
return childMemberExpr.evalAsCall()
.filter(Swc4jAstArrayLit.ARRAY_FUNCTION_SET::contains)
.map(c -> Swc4jAstIdent.create(Swc4jAstFunction.CONSTRUCTOR));
if (call.isPresent()) {
if (CONSTRUCTOR.equals(call.get())) {
Swc4jAstMemberExpr childMemberExpr = obj.as(Swc4jAstMemberExpr.class);
if (childMemberExpr.getObj() instanceof Swc4jAstArrayLit) {
return childMemberExpr.evalAsCall()
.filter(Swc4jAstArrayLit.ARRAY_FUNCTION_SET::contains)
.map(c -> Swc4jAstIdent.create(Swc4jAstFunction.CONSTRUCTOR));
}
} else if (NAME.equals(call.get())) {
Swc4jAstMemberExpr childMemberExpr = obj.as(Swc4jAstMemberExpr.class);
Optional<String> childCall = childMemberExpr.evalAsCall();
if (childCall.isPresent()) {
if (CONSTRUCTOR.equals(childCall.get())) {
ISwc4jAstExpr childExpr = childMemberExpr.getObj().unParenExpr();
if (childExpr instanceof Swc4jAstStr) {
return Optional.of(Swc4jAstStr.create(Swc4jAstStr.CONSTRUCTOR));
}
}
}
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Swc4jAstStr
extends Swc4jAst
implements ISwc4jAstLit, ISwc4jAstModuleExportName, ISwc4jAstPropName, ISwc4jAstTsModuleName, ISwc4jAstTsLit,
ISwc4jAstTsEnumMemberId, ISwc4jAstCoercionPrimitive {
public static final String CONSTRUCTOR = "String";
@Jni2RustField(componentAtom = true)
protected Optional<String> raw;
@Jni2RustField(atom = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void testEval() throws Swc4jCoreException {
"/a/['constructor']()", "/(?:)/",
"/a/['constructor']('abc')", "/abc/",
"/a/['constructor']('a/b', 'i')", "/a\\/b/i",
"(31)['toString']('32')", "\"v\"",
"Function('return \"abc\"')()", "\"abc\"");
ISwc4jPluginHost pluginHost = program -> {
program.visit(new Swc4jAstVisitor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void testEval() throws Swc4jCoreException {
Map<String, String> testCaseMap = SimpleMap.of(
"'abc'[1]", "\"b\"",
"'abc'['1']", "\"b\"",
"('')['constructor']['name']", "\"String\"",
"[]['at']['constructor']", "Function");
ISwc4jPluginHost pluginHost = program -> {
program.visit(new Swc4jAstVisitor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void test() throws Swc4jCoreException {
testCaseMap.put("(![]+[])[+!+[]]+(+((+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[+[]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+!+[]]])+[])[!+[]+!+[]]+([][(!![]+[])[!+[]+!+[]+!+[]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()+[])[!+[]+!+[]]", "\"a-b\"");
testCaseMap.put("[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(![]+[])[+!+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]]+![]+(![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]])()[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]])+[])[+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]]+([][(!![]+[])[!+[]+!+[]+!+[]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()+[])[!+[]+!+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]])()", "\"a*b\"");
testCaseMap.put("(+[![]]+[][(!![]+[])[!+[]+!+[]+!+[]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]())[+!+[]+[+!+[]]]+(!![]+[])[+!+[]]+(!![]+[])[+!+[]]+(![]+[])[+!+[]]+(+[![]]+[+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]]+[+[]])])[+!+[]+[+[]]]", "\"Array\"");
testCaseMap.put("(!![]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+!+[]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]][([][[]]+[])[+!+[]]+(![]+[])[+!+[]]+((+[])[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]]](!+[]+!+[]+!+[]+[!+[]+!+[]])+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]", "\"eval\"");
ISwc4jPluginHost pluginHost = new Swc4jPluginHostJsFuckDecoder();
jsScriptTransformOptions
.setPluginHost(pluginHost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static void main(String[] args) throws Swc4jCoreException, MalformedURLEx
.setSpecifier(specifier)
.setPluginHost(pluginHost)
.setMediaType(Swc4jMediaType.JavaScript)
.setOmitLastSemi(true)
.setInlineSources(false)
.setSourceMap(Swc4jSourceMapOption.None);
// Transform the code.
Expand Down

0 comments on commit bddd300

Please sign in to comment.