Skip to content

Commit

Permalink
Fixed more problems and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Nov 15, 2024
1 parent be75d14 commit 492ab45
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
4 changes: 2 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,6 @@ private static boolean isNotValidSimpleAssignmentTarget(AstNode pn) {
private AstNode condExpr() throws IOException {
AstNode pn = nullishCoalescingExpr();
if (matchToken(Token.HOOK, true)) {
int line = lineNumber();
int qmarkPos = ts.tokenBeg, colonPos = -1;
/*
* Always accept the 'in' operator in the middle clause of a ternary,
Expand All @@ -2559,7 +2558,8 @@ private AstNode condExpr() throws IOException {
AstNode ifFalse = assignExpr();
int beg = pn.getPosition(), len = getNodeEnd(ifFalse) - beg;
ConditionalExpression ce = new ConditionalExpression(beg, len);
ce.setLineno(line);
ce.setLineno(pn.getLineno());
ce.setColumn(pn.getColumn());
ce.setTestExpression(pn);
ce.setTrueExpression(ifTrue);
ce.setFalseExpression(ifFalse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Node;
import org.mozilla.javascript.ast.Assignment;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.BigIntLiteral;
import org.mozilla.javascript.ast.Block;
import org.mozilla.javascript.ast.BreakStatement;
import org.mozilla.javascript.ast.CatchClause;
import org.mozilla.javascript.ast.ConditionalExpression;
import org.mozilla.javascript.ast.ContinueStatement;
import org.mozilla.javascript.ast.DoLoop;
import org.mozilla.javascript.ast.ElementGet;
Expand All @@ -27,6 +29,7 @@
import org.mozilla.javascript.ast.FunctionCall;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.IfStatement;
import org.mozilla.javascript.ast.InfixExpression;
import org.mozilla.javascript.ast.KeywordLiteral;
import org.mozilla.javascript.ast.LabeledStatement;
import org.mozilla.javascript.ast.Name;
Expand Down Expand Up @@ -607,10 +610,53 @@ void expressionPropertyElem() {
assertLineColumnAre(property, 0, 5);
}

@Test
void expressionInfixOperator() {
AstRoot root = myParse(" a + /*comment*/ 1");
ExpressionStatement firstExpr =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(firstExpr, 0, 2);
InfixExpression infix = assertInstanceOf(InfixExpression.class, firstExpr.getExpression());
assertLineColumnAre(infix, 0, 2);
Name left = assertInstanceOf(Name.class, infix.getLeft());
assertLineColumnAre(left, 0, 2);
NumberLiteral right = assertInstanceOf(NumberLiteral.class, infix.getRight());
assertLineColumnAre(right, 0, 18);
}

@Test
void statementAssignment() {
AstRoot root = myParse(" a |= 1");
ExpressionStatement firstExpr =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(firstExpr, 0, 2);
Assignment assignment = assertInstanceOf(Assignment.class, firstExpr.getExpression());
assertLineColumnAre(assignment, 0, 2);
Name target = assertInstanceOf(Name.class, assignment.getLeft());
assertLineColumnAre(target, 0, 2);
NumberLiteral right = assertInstanceOf(NumberLiteral.class, assignment.getRight());
assertLineColumnAre(right, 0, 7);
}

@Test
void expressionHook() {
AstRoot root = myParse(" a ? b : 1");
ExpressionStatement firstExpr =
assertInstanceOf(ExpressionStatement.class, root.getFirstChild());
assertLineColumnAre(firstExpr, 0, 2);
ConditionalExpression hook =
assertInstanceOf(ConditionalExpression.class, firstExpr.getExpression());
assertLineColumnAre(hook, 0, 2);
Name testExpression = assertInstanceOf(Name.class, hook.getTestExpression());
assertLineColumnAre(testExpression, 0, 2);
Name trueExpression = assertInstanceOf(Name.class, hook.getTrueExpression());
assertLineColumnAre(trueExpression, 0, 6);
NumberLiteral falseExpression =
assertInstanceOf(NumberLiteral.class, hook.getFalseExpression());
assertLineColumnAre(falseExpression, 0, 10);
}

// TODO expressions:
// infix operators
// assignment operators such as +=
// nullish coalesce, hook
// parenthesis
// square brackets
// object literals
Expand Down

0 comments on commit 492ab45

Please sign in to comment.