Skip to content

Commit

Permalink
🧪 test: Add test for ast opt call
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 23, 2024
1 parent bdca67a commit 69bd734
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected ISwc4jAstExpr convertArguments(ISwc4jAstExpr expr) {
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr);
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(Swc4jAstNull.create()));
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(innerExpr));
callExpr.updateParent();
return callExpr;
}
return expr;
Expand Down Expand Up @@ -128,7 +129,7 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) {
if (length == 1) {
node.getParent().replaceNode(node, convertArguments(elems.get(0).get().getExpr()));
} else {
// Prepare obj
// ident
ISwc4jAstExpr obj;
Optional<Swc4jAstExprOrSpread> optionalExprOrSpread = elems.get(0);
int startIndex;
Expand All @@ -152,11 +153,11 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) {
}
obj = objArrayLit;
}
// Prepare concat()
// ident.concat()
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(obj, Swc4jAstIdent.createConcat());
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr);
Swc4jAstArrayLit objArrayLit = null;
// Prepare args
// ident.concat(...)
for (int i = startIndex; i < length; i++) {
optionalExprOrSpread = elems.get(i);
if (optionalExprOrSpread.map(e -> e.getSpread().isPresent()).orElse(false)) {
Expand Down Expand Up @@ -195,9 +196,9 @@ public Swc4jAstVisitorResponse visitArrayLit(Swc4jAstArrayLit node) {

@Override
public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) {
// super(...arguments) is not supported.
if (node.isSpreadPresent() && node.getCallee() instanceof ISwc4jAstExpr) {
ISwc4jAstExpr callee = node.getCallee().as(ISwc4jAstExpr.class);
List<Swc4jAstExprOrSpread> args = node.getArgs();
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(callee, Swc4jAstIdent.createApply());
node.setCallee(memberExpr);
Swc4jAstExprOrSpread thisArg;
Expand All @@ -220,24 +221,27 @@ public Swc4jAstVisitorResponse visitCallExpr(Swc4jAstCallExpr node) {
} else {
thisArg = Swc4jAstExprOrSpread.create(Swc4jAstNull.create());
}
List<Swc4jAstExprOrSpread> args = node.getArgs();
Swc4jAstExprOrSpread arg = getConcatNode(args);
args.clear();
args.add(thisArg);
args.add(arg);
node.updateParent();
}
return super.visitCallExpr(node);
}

@Override
public Swc4jAstVisitorResponse visitNewExpr(Swc4jAstNewExpr node) {
if (node.isSpreadPresent()) {
ISwc4jAstExpr callee = node.getCallee();
List<Swc4jAstExprOrSpread> args = node.getArgs().get();
Swc4jAstExprOrSpread arg = getConcatNode(args);
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(callee, Swc4jAstIdent.createApply());
// ident.apply
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(node.getCallee(), Swc4jAstIdent.createApply());
// ident.apply()
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr);
// ident.apply(null, arg)
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(Swc4jAstNull.create()));
callExpr.getArgs().add(arg);
callExpr.getArgs().add(getConcatNode(node.getArgs().get()));
callExpr.updateParent();
node.getParent().replaceNode(node, callExpr);
}
return super.visitNewExpr(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.swc4j.ast.miscs;

import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstExprOrSpread;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstMemberExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstOptChainExpr;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class TestSwc4jAstOptCall extends BaseTestSuiteSwc4jAst {
@Test
public void test() throws Swc4jCoreException {
String code = "a?.b(c,d)";
Swc4jParseOutput output = swc4j.parse(code, jsScriptParseOptions);
Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class);
Swc4jAstExprStmt exprStmt = assertAst(
script, script.getBody().get(0).as(Swc4jAstExprStmt.class), Swc4jAstType.ExprStmt, 0, 9);
Swc4jAstOptChainExpr optChainExpr = assertAst(
exprStmt, exprStmt.getExpr().as(Swc4jAstOptChainExpr.class), Swc4jAstType.OptChainExpr, 0, 9);
assertFalse(optChainExpr.isOptional());
Swc4jAstOptCall optCall = assertAst(
optChainExpr, optChainExpr.getBase().as(Swc4jAstOptCall.class), Swc4jAstType.OptCall, 0, 9);
assertFalse(optCall.getTypeArgs().isPresent());
assertEquals(2, optCall.getArgs().size());
Swc4jAstExprOrSpread exprOrSpread = assertAst(
optCall, optCall.getArgs().get(0), Swc4jAstType.ExprOrSpread, 5, 6);
Swc4jAstIdent ident = assertAst(
exprOrSpread, exprOrSpread.getExpr().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 5, 6);
assertEquals("c", ident.getSym());
exprOrSpread = assertAst(
optCall, optCall.getArgs().get(1), Swc4jAstType.ExprOrSpread, 7, 8);
ident = assertAst(
exprOrSpread, exprOrSpread.getExpr().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 7, 8);
assertEquals("d", ident.getSym());
Swc4jAstOptChainExpr childOptChainExpr = assertAst(
optCall, optCall.getCallee().as(Swc4jAstOptChainExpr.class), Swc4jAstType.OptChainExpr, 0, 4);
assertTrue(childOptChainExpr.isOptional());
Swc4jAstMemberExpr memberExpr = assertAst(
childOptChainExpr, childOptChainExpr.getBase().as(Swc4jAstMemberExpr.class), Swc4jAstType.MemberExpr, 0, 4);
ident = assertAst(
memberExpr, memberExpr.getObj().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 0, 1);
assertEquals("a", ident.getSym());
ident = assertAst(
memberExpr, memberExpr.getProp().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 3, 4);
assertEquals("b", ident.getSym());
assertSpan(code, script);
}
}

0 comments on commit 69bd734

Please sign in to comment.