forked from qt-creator/qt-creator
qmljsreformatter: support nullish coalescing
qmljsscanner did interpret ?? and ?. as a single ? and thus as the start of a ternary operator, breaking reformatting. Fixes: QTCREATORBUG-27344 Change-Id: I0429d20aed0196743f1c20e6ceac11351d5a7a3b Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -957,10 +957,10 @@ protected:
|
|||||||
accept(ast->left);
|
accept(ast->left);
|
||||||
|
|
||||||
// in general, avoid splitting at the operator
|
// in general, avoid splitting at the operator
|
||||||
// but && and || are ok
|
// but && || and ?? are ok
|
||||||
qreal splitBadness = 30;
|
qreal splitBadness = 30;
|
||||||
if (ast->op == QSOperator::And
|
if (ast->op == QSOperator::And
|
||||||
|| ast->op == QSOperator::Or)
|
|| ast->op == QSOperator::Or || ast->op == QSOperator::Coalesce)
|
||||||
splitBadness = 0;
|
splitBadness = 0;
|
||||||
addPossibleSplit(splitBadness);
|
addPossibleSplit(splitBadness);
|
||||||
|
|
||||||
|
|||||||
@@ -498,6 +498,18 @@ QList<Token> Scanner::operator()(const QString &text, int startState)
|
|||||||
setRegexpMayFollow(&_state, true);
|
setRegexpMayFollow(&_state, true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '?':
|
||||||
|
switch (la.unicode()) {
|
||||||
|
case '?':
|
||||||
|
case '.':
|
||||||
|
tokens.append(Token(index, 2, Token::Delimiter));
|
||||||
|
index += 2;
|
||||||
|
default:
|
||||||
|
tokens.append(Token(index++, 1, Token::Delimiter));
|
||||||
|
}
|
||||||
|
setRegexpMayFollow(&_state, true);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (ch.isSpace()) {
|
if (ch.isSpace()) {
|
||||||
do {
|
do {
|
||||||
|
|||||||
9
tests/auto/qml/reformatter/nullishCoalescing.qml
Normal file
9
tests/auto/qml/reformatter/nullishCoalescing.qml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import QtQuick 2.15
|
||||||
|
import QtQuick.Controls 2.15
|
||||||
|
|
||||||
|
Item {
|
||||||
|
property string otherString: ""
|
||||||
|
property string aProp: otherString ?? "N/A"
|
||||||
|
property string bProp: otherString?.trim()
|
||||||
|
property int unrelatedProp: 10
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user