Skip to content

Commit

Permalink
Merge pull request #1685 from riganti/fix-orand-precedence
Browse files Browse the repository at this point in the history
Fix && / || operator precedence
  • Loading branch information
tomasherceg authored Aug 26, 2023
2 parents 069b871 + 61d789d commit 5872c82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private BindingParserNode ReadAndAlsoExpression()
while (Peek() is BindingToken operatorToken && operatorToken.Type == BindingTokenType.AndAlsoOperator)
{
Read();
var second = ReadOrElseExpression();
var second = ReadOrExpression();
first = CreateNode(new BinaryOperatorBindingParserNode(first, second, BindingTokenType.AndAlsoOperator), startIndex);
}
return first;
Expand Down
8 changes: 8 additions & 0 deletions src/Tests/Binding/BindingCompilationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,14 @@ public void BindingCompiler_Errors_AssigningToType()
StringAssert.Contains(ex.Message, "Expression '123' cannot be assigned into 'System.String'.");
}

[TestMethod]
public void BindingCompiler_LogicalOperatorPrecedence()
{
Assert.AreEqual(true, ExecuteBinding("(false && true) || (true && true)", new TestViewModel()));
Assert.AreEqual(true, ExecuteBinding("false && true || true && true", new TestViewModel()));
Assert.AreEqual(true, ExecuteBinding("true && true || true && false", new TestViewModel()));
}

[TestMethod]
public void BindingCompiler_ExclusiveOrOperator()
{
Expand Down
9 changes: 8 additions & 1 deletion src/Tests/Binding/JavascriptCompilationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,18 @@ public void JsTranslator_IntegerArithmetic()
Assert.AreEqual("(IntProp()/2|0)+((IntProp()+1)/(IntProp()-1)|0)", result);
}

[TestMethod]
public void JsTranslator_BooleanOperatorPrecedence()
{
var result = CompileBinding("IntProp > 0 && IntProp > 1 || IntProp > 2 && IntProp > 3", new [] { typeof(TestViewModel)});
Assert.AreEqual("IntProp()>0&&IntProp()>1||IntProp()>2&&IntProp()>3", result);
}

[TestMethod]
public void JsTranslator_ArrayIndexer()
{
var result = CompileBinding("LongArray[1] == 3 && VmArray[0].MyProperty == 1 && VmArray.Length > 1", new [] { typeof(TestViewModel)});
Assert.AreEqual("LongArray()[1]()==3&&(VmArray()[0]().MyProperty()==1&&VmArray().length>1)", result);
Assert.AreEqual("LongArray()[1]()==3&&VmArray()[0]().MyProperty()==1&&VmArray().length>1", result);
}

[TestMethod]
Expand Down

0 comments on commit 5872c82

Please sign in to comment.