Skip to content

GDIfExpression.FalseExpression is null for multiline parenthesized ternary expressions #26

Description

@dreed-sd

GDShrapt appears to drop the false branch of a GDScript ternary expression when the ternary is parenthesized and split across multiple lines:

var values = []
var x: int = (
    values.back()
    if values.size() > 0
    else 0
)

Proposed regression test:

using System.Linq;
using FluentAssertions;
using GDShrapt.Reader;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GDShrapt.Reader.Tests.Parsing
{
    [TestClass]
    public class IfExpressionMultilineParsingTests
    {
        [TestMethod]
        public void ParseIfExpression_MultilineParenthesized_KeepsFalseExpression()
        {
            var reader = new GDScriptReader();

            var code = @"var values = []
var x: int = (
    values.back()
    if values.size() > 0
    else 0
)
";

            var tree = reader.ParseFileContent(code);

            var x = tree.Variables.First(v => v.Identifier?.ToString() == "x");

            // Depending on the intended AST shape, the initializer may be a
            // GDBracketExpression with a GDIfExpression inside it.
            var bracket = x.Initializer as GDBracketExpression;
            bracket.Should().NotBeNull("the initializer is parenthesized");

            var ternary = bracket!.InnerExpression as GDIfExpression;
            ternary.Should().NotBeNull("the parenthesized expression should contain a ternary GDIfExpression");

            ternary!.TrueExpression.Should().NotBeNull();
            ternary.Condition.Should().NotBeNull();
            ternary.ElseKeyword.Should().NotBeNull();
            ternary.FalseExpression.Should().NotBeNull("the expression after `else` should be parsed");

            ternary.TrueExpression!.ToString().Should().Be("values.back()");
            ternary.Condition!.ToString().Should().Be("values.size() > 0");
            ternary.FalseExpression!.ToString().Should().Be("0");

            AssertHelper.NoInvalidTokens(tree);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions