TSconfig condition went crazy with ?

The Problem #

Today, I had some fun with conditions in TYPO3's TSconfig. In one of my current projects, I use a statement as it is even shown in the documentation:

# Check if page with uid 2 is inside the root line
[2 in tree.rootLineIds]
    // Your settings go here
[END]

This works pretty fine and the settings apply to all pages inside 2 or the page 2 itself.

But after some time, I realized that the error log was growing with this message:

Variable "tree" is not valid around position 6 for expression `2 in tree.rootLineIds`.

I investigated the issue and realized, this error only occurs, when I want to translate a file meta record inside the files backend module. I already created a bug report for this behavior. But I also wanted to immediately get rid of this error.

Approaches #

Thank god, the Symfony Expression Language, on which the TYPO3 conditions are based, since TYPO3 12 has the null-safe operator and the null-coalescing operator just like PHP has. So I tried several combinations to avoid the error:

[2 in (tree?.rootLineIds ?? [])]

still resulted in the same error but with different character position:

Variable "tree" is not valid around position 7 for expression `2 in (tree?.rootLineIds ?? [])`.

I even tried to split the condition into two single conditions and assumed, the right one would not be evaluated if the left part already would fail:

[(tree ?? null) && 2 in tree.rootLineIds]

But sill the same error.

The Solution #

I finally came up with this solution, which looks pretty weird. 🤪

[2 in ((tree ?? null)?.rootLineIds ?? [])]

Let's assume what happens, if tree is set:

Let's assume what happens, if tree is not set: