From 492ab45531762386a6e8efd67abd0f2def39108e Mon Sep 17 00:00:00 2001 From: "andrea.bergia" Date: Fri, 15 Nov 2024 16:33:49 +0100 Subject: [PATCH] Fixed more problems and added more tests --- .../java/org/mozilla/javascript/Parser.java | 4 +- .../tests/ParserLineColumnNumberTest.java | 52 +++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/Parser.java b/rhino/src/main/java/org/mozilla/javascript/Parser.java index 98109ef042..705f0d5887 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Parser.java +++ b/rhino/src/main/java/org/mozilla/javascript/Parser.java @@ -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, @@ -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); diff --git a/tests/src/test/java/org/mozilla/javascript/tests/ParserLineColumnNumberTest.java b/tests/src/test/java/org/mozilla/javascript/tests/ParserLineColumnNumberTest.java index b170370791..916d412eb3 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/ParserLineColumnNumberTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/ParserLineColumnNumberTest.java @@ -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; @@ -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; @@ -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