Skip to content

Commit

Permalink
🦄 refactor: Revise return type of find() for ISwc4jAst
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 18, 2024
1 parent 3b24313 commit 0017881
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ default Optional<ISwc4jAst> eval() {
* @return the list of AST nodes
* @since 1.3.0
*/
default <T extends ISwc4jAst> List<ISwc4jAst> find(Class<T> clazz) {
default <T extends ISwc4jAst> List<T> find(Class<T> clazz) {
return find(clazz, -1);
}

Expand All @@ -77,12 +77,13 @@ default <T extends ISwc4jAst> List<ISwc4jAst> find(Class<T> clazz) {
* @return the list of AST nodes
* @since 1.3.0
*/
default <T extends ISwc4jAst> List<ISwc4jAst> find(Class<T> clazz, int depth) {
@SuppressWarnings("unchecked")
default <T extends ISwc4jAst> List<T> find(Class<T> clazz, int depth) {
AssertionUtils.notNull(clazz, "Class");
List<ISwc4jAst> nodes = SimpleList.of();
List<T> nodes = SimpleList.of();
getChildNodes().forEach((childNode) -> {
if (clazz.isAssignableFrom(childNode.getClass())) {
nodes.add(childNode);
nodes.add((T) childNode);
}
if (depth != 0) {
final int newDepth = depth > 0 ? depth - 1 : depth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt;
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitor;
Expand Down Expand Up @@ -91,9 +90,9 @@ public void testGetBangCount() throws Swc4jCoreException {
String code = entry.getKey();
int bangCount = entry.getValue();
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
List<ISwc4jAst> nodes = output.getProgram().find(Swc4jAstBinExpr.class);
List<Swc4jAstBinExpr> nodes = output.getProgram().find(Swc4jAstBinExpr.class);
assertEquals(1, nodes.size());
Swc4jAstBinExpr binExpr = nodes.get(0).as(Swc4jAstBinExpr.class);
Swc4jAstBinExpr binExpr = nodes.get(0);
assertEquals("a", binExpr.getLeft().as(Swc4jAstIdent.class).getSym());
assertEquals("b", binExpr.getRight().as(Swc4jAstIdent.class).getSym());
assertEquals(bangCount, binExpr.getBangCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
Expand Down Expand Up @@ -88,9 +87,9 @@ public void testGetMinusCount() throws Swc4jCoreException {
String code = entry.getKey();
int minusCount = entry.getValue();
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
List<ISwc4jAst> nodes = output.getProgram().find(Swc4jAstNumber.class);
List<Swc4jAstNumber> nodes = output.getProgram().find(Swc4jAstNumber.class);
assertEquals(1, nodes.size());
Swc4jAstNumber number = nodes.get(0).as(Swc4jAstNumber.class);
Swc4jAstNumber number = nodes.get(0);
assertEquals(12345, number.asInt());
assertEquals("12345", number.getRaw().get());
assertEquals(minusCount, number.getMinusCount());
Expand Down

0 comments on commit 0017881

Please sign in to comment.