Comma

[comma] Do not use comma expressions.

This warning category is spelled [comma] by qmllint.

Do not use comma expressions

What happened?

A JavaScript comma expression was used outside of a for loop.

Why is this bad?

Comma expressions reduce readability of the code and obscure side-effects.

Example

To fix this warning, refactor the code to use distinct statements for each operation. This way, each side effect is explicit instead of happening as part of another unrelated operation:

In addition, there are some special considerations for cases where comma expressions appear because a variable is intentionally being captured for a binding, as in the following code, where previewOfFirstPage is a function defined in C++ which internally depends on Config.fontSize:

If you encounter this situation, consider one of the following approaches:

  • If a function such as previewOfFirstPage depends on a property, prefer making this dependency explicit by passing the value as an argument.

  • If changing the function signature is undesirable for API reasons, consider replacing the function with a Q_PROPERTY instead, so that change notifications for it can be emitted when the depenency is modified in C++:

    void Config::setFontSize(int fontSize) {
       if (m_fontSize == fontSize)
          return;
       m_fontSize = fontSize;
       emit fontSizeChanged();
       emit previewOfFirstPageChanged();
    }
    
  • If modifying the C++ implementation or adding QML dependencies is not possible, use a qmllint directive to silence the warning. Include a comment explaining that the intention is to capture the variable in the binding.

    Text {
       // This causes the function to re-run when fontSize changes
       text: Config.fontSize, documentProvider.previewOfFirstPage() // qmllint disable comma
    }