Skip to content

Commit

Permalink
✨ feat: Add getParentBinExpr() to Swc4jAstBinExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 20, 2024
1 parent eef6998 commit a08cdd9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* Added `find()` to `ISwc4jAst`
* Added `getMinusCount()` to `Swc4jAstNumber`
* Added `getBangCount()`, `getLogicalOperatorCount()` to `Swc4jAstBinExpr`
* Added `getBangCount()`, `getLogicalOperatorCount()`, `getParentBinExpr()` to `Swc4jAstBinExpr`
* Added `getOppositeOperator()`, `isLogicalOperator()`, `isLogicalCompareOperator()`, `isLogicalConditionOperator()` to `Swc4jAstBinaryOp`

## 1.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ public Swc4jAstBinaryOp getOp() {
return op;
}

public Swc4jAstBinExpr getParentBinExpr() {
return getParentBinExpr(getParent());
}

protected Swc4jAstBinExpr getParentBinExpr(ISwc4jAst ast) {
switch (ast.getType()) {
case BinExpr:
return ast.as(Swc4jAstBinExpr.class);
case ParenExpr:
return getParentBinExpr(ast.getParent());
default:
return null;
}
}

@Jni2RustMethod
public ISwc4jAstExpr getRight() {
return right;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
import java.util.Map;
import java.util.stream.Stream;

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

public class TestSwc4jAstBinExpr extends BaseTestSuiteSwc4jAst {
@Test
Expand Down Expand Up @@ -135,6 +134,16 @@ public void testGetLogicalOperatorCount() throws Swc4jCoreException {
}
}

@Test
public void testGetParentBinExpr() throws Swc4jCoreException {
Swc4jParseOutput output = swc4j.parse("a==b&&(c==d)", tsScriptParseOptions);
List<Swc4jAstBinExpr> nodes = output.getProgram().find(Swc4jAstBinExpr.class);
assertEquals(3, nodes.size());
assertNull(nodes.get(0).getParentBinExpr());
assertEquals(nodes.get(0), nodes.get(1).getParentBinExpr());
assertEquals(nodes.get(0), nodes.get(2).getParentBinExpr());
}

@Test
public void testOpWithSpace() {
Stream.of(Swc4jAstBinaryOp.values())
Expand Down

0 comments on commit a08cdd9

Please sign in to comment.