Skip to content

Commit

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

* Added `find()` to `ISwc4jAst`
* Added `getMinusCount()` to `Swc4jAstNumber`
* Added `getBangCount()` to `Swc4jAstBinExpr`

## 1.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.caoccao.javet.swc4j.ast.Swc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstUnaryOp;
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.Swc4jAstStr;
Expand Down Expand Up @@ -65,6 +66,25 @@ public static Swc4jAstBinExpr create(Swc4jAstBinaryOp op, ISwc4jAstExpr left, IS
return new Swc4jAstBinExpr(op, left, right, Swc4jSpan.DUMMY);
}

protected static int getBangCount(ISwc4jAst ast) {
switch (ast.getType()) {
case BinExpr:
if (ast.as(Swc4jAstBinExpr.class).getOp().isLogicalOperator()) {
return getBangCount(ast.getParent());
}
return 0;
case ParenExpr:
return getBangCount(ast.getParent());
case UnaryExpr:
if (ast.as(Swc4jAstUnaryExpr.class).getOp() == Swc4jAstUnaryOp.Bang) {
return getBangCount(ast.getParent()) + 1;
}
return 0;
default:
return 0;
}
}

@Override
public Optional<ISwc4jAst> eval() {
ISwc4jAstExpr left = this.left.unParenExpr();
Expand Down Expand Up @@ -210,6 +230,10 @@ public Optional<ISwc4jAst> eval() {
return super.eval();
}

public int getBangCount() {
return getBangCount(getParent());
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(left, right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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 All @@ -29,6 +30,7 @@
import com.caoccao.javet.swc4j.utils.SimpleMap;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -78,6 +80,26 @@ public Swc4jAstVisitorResponse visitBinExpr(Swc4jAstBinExpr node) {
assertTransformJs(testCaseMap, pluginHost);
}

@Test
public void testGetBangCount() throws Swc4jCoreException {
Map<String, Integer> testCaseMap = SimpleMap.of(
"a>b", 0,
"!(a>b)", 1,
"!(!(a>b))", 2,
"!(!(!(a>b)))", 3);
for (Map.Entry<String, Integer> entry : testCaseMap.entrySet()) {
String code = entry.getKey();
int bangCount = entry.getValue();
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
List<ISwc4jAst> nodes = output.getProgram().find(Swc4jAstBinExpr.class);
assertEquals(1, nodes.size());
Swc4jAstBinExpr binExpr = nodes.get(0).as(Swc4jAstBinExpr.class);
assertEquals("a", binExpr.getLeft().as(Swc4jAstIdent.class).getSym());
assertEquals("b", binExpr.getRight().as(Swc4jAstIdent.class).getSym());
assertEquals(bangCount, binExpr.getBangCount());
}
}

@Test
public void testOpWithSpace() {
Stream.of(Swc4jAstBinaryOp.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void testFind() throws Swc4jCoreException {
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
IntStream.range(0, 7).forEach(i ->
assertTrue(output.getProgram().find(Swc4jAstNumber.class, i).isEmpty()));
assertEquals(1, output.getProgram().find(Swc4jAstNumber.class, 8).size());
assertEquals(1, output.getProgram().find(Swc4jAstNumber.class, -1).size());
IntStream.of(-2, -1, 8).forEach(i ->
assertEquals(1, output.getProgram().find(Swc4jAstNumber.class, i).size()));
assertEquals(1, output.getProgram().find(Swc4jAstNumber.class).size());
}
}

0 comments on commit 3b24313

Please sign in to comment.