Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-132280

qmlformat: The optional chaining operator (JavaScript) is incorrectly removed when followed by an array access

    XMLWordPrintable

Details

    • caa553414 (dev), db9ebe66e (6.9), c6c39d9f5 (6.8)

    Description

      Take the following example code:

      import QtQuick
      import QtQuick.Controls
      import QtQuick.Layouts
      
      Container {
          id: root
          readonly property var backendObject
      
          contentItem: ColumnLayout {
              Repeater {
                  model: ["red", "green", "blue"]
                  Text {
                      Layout.fillWidth: true
                      text: {
                          const notAvailable = "N/A";
                          return root.backendObject.results?.[modelData].text ?? notAvailable;
                      }
                  }
              }
          }
      }
      

      After running `qmlformat` the line

      return root.backendObject.results?.[modelData].text ?? notAvailable; 

      is changed to

      return root.backendObject.results[modelData].text ?? notAvailable;
      

      Note how the optional chaining (`?.`) is completely dropped.

      In the likely case that this object is not available from the backend, this change would result in the error `Uncaught TypeError cannot read properties of null` instead of just returning `"N/A"`.

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            olivier.decanniere Olivier De Cannière
            bkarpati B Karpati
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes