Skip to content

Commit

Permalink
🧪 test: Enhance test for function
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 12, 2024
1 parent a8d01be commit fc617b0
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstFnExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstBindingIdent;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstBlockStmt;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstFnDecl;
Expand Down Expand Up @@ -77,4 +78,66 @@ public void testNamedEmptyFunction() throws Swc4jCoreException {
assertTrue(blockStmt.getStmts().isEmpty());
assertSpan(code, script);
}

@Test
public void testNestedFunction() throws Swc4jCoreException {
String code = "function a(x, y) { function b(z) {} }";
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class);
Swc4jAstFnDecl fnDecl = assertAst(
script, script.getBody().get(0).as(Swc4jAstFnDecl.class), Swc4jAstType.FnDecl, 0, 37);
assertFalse(fnDecl.isDeclare());
Swc4jAstIdent ident = assertAst(
fnDecl, fnDecl.getIdent().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 9, 10);
assertEquals("a", ident.getSym());
Swc4jAstFunction function = assertAst(
fnDecl, fnDecl.getFunction(), Swc4jAstType.Function, 0, 37);
assertFalse(function.isAsync());
assertFalse(function.isGenerator());
assertFalse(function.getReturnType().isPresent());
assertFalse(function.getTypeParams().isPresent());
assertTrue(function.getBody().isPresent());
assertEquals(2, function.getParams().size());
Swc4jAstParam param = assertAst(
function, function.getParams().get(0).as(Swc4jAstParam.class), Swc4jAstType.Param, 11, 12);
assertTrue(param.getDecorators().isEmpty());
Swc4jAstBindingIdent bindingIdent = assertAst(
param, param.getPat().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 11, 12);
ident = assertAst(
bindingIdent, bindingIdent.getId().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 11, 12);
assertEquals("x", ident.getSym());
param = assertAst(
function, function.getParams().get(1).as(Swc4jAstParam.class), Swc4jAstType.Param, 14, 15);
assertTrue(param.getDecorators().isEmpty());
bindingIdent = assertAst(
param, param.getPat().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 14, 15);
ident = assertAst(
bindingIdent, bindingIdent.getId().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 14, 15);
assertEquals("y", ident.getSym());
Swc4jAstBlockStmt blockStmt = assertAst(
function, function.getBody().get(), Swc4jAstType.BlockStmt, 17, 37);
fnDecl = assertAst(
blockStmt, blockStmt.getStmts().get(0).as(Swc4jAstFnDecl.class), Swc4jAstType.FnDecl, 19, 35);
assertFalse(fnDecl.isDeclare());
ident = assertAst(
fnDecl, fnDecl.getIdent().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 28, 29);
assertEquals("b", ident.getSym());
function = assertAst(
fnDecl, fnDecl.getFunction(), Swc4jAstType.Function, 19, 35);
assertFalse(function.isAsync());
assertFalse(function.isGenerator());
assertFalse(function.getReturnType().isPresent());
assertFalse(function.getTypeParams().isPresent());
assertTrue(function.getBody().isPresent());
assertEquals(1, function.getParams().size());
param = assertAst(
function, function.getParams().get(0).as(Swc4jAstParam.class), Swc4jAstType.Param, 30, 31);
assertTrue(param.getDecorators().isEmpty());
bindingIdent = assertAst(
param, param.getPat().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 30, 31);
ident = assertAst(
bindingIdent, bindingIdent.getId().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 30, 31);
assertEquals("z", ident.getSym());
assertSpan(code, script);
}
}

0 comments on commit fc617b0

Please sign in to comment.