qmllint: Print fix suggestions for pragma ComponentBehavior

Pick-to: 6.5
Fixes: QTBUG-104576
Fixes: QTBUG-104632
Change-Id: I9e0919feb04798fb4c5d0c8c0ed2f5cbc7a0b552
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2023-01-16 12:37:23 +01:00
parent 47e111800e
commit e3cb23d34b
4 changed files with 33 additions and 0 deletions

View File

@ -25,6 +25,7 @@ QT_BEGIN_NAMESPACE
class QQmlJSScopesById
{
public:
bool componentsAreBound() const { return m_componentsAreBound; }
void setComponentsAreBound(bool bound) { m_componentsAreBound = bound; }
void setSignaturesAreEnforced(bool enforced) { m_signaturesAreEnforced = enforced; }

View File

@ -393,6 +393,18 @@ void QQmlJSTypePropagator::handleUnqualifiedAccess(const QString &name, bool isM
}
}
if (!suggestion.has_value() && !m_function->addressableScopes.componentsAreBound()
&& m_function->addressableScopes.existsAnywhereInDocument(name)) {
FixSuggestion::Fix bindComponents;
const QLatin1String replacement = "pragma ComponentBehavior: Bound"_L1;
bindComponents.replacementString = replacement + '\n'_L1;
bindComponents.message = "Set \"%1\" in order to use IDs "
"from outer components in nested components."_L1.arg(replacement);
bindComponents.cutLocation = QQmlJS::SourceLocation(0, 0, 1, 1);
bindComponents.isHint = false;
suggestion = FixSuggestion {{ bindComponents }};
}
if (!suggestion.has_value()) {
if (auto didYouMean =
QQmlJSUtils::didYouMean(name,

View File

@ -0,0 +1,10 @@
import QtQuick
Item {
id: barsty
property int fooInt: 42
Repeater {
model: 5
Text { text: "Foo=" + barsty.fooInt }
}
}

View File

@ -1021,6 +1021,16 @@ expression: \${expr} \${expr} \\\${expr} \\\${expr}`)",
QTest::newRow("duplicatedSignalName")
<< QStringLiteral("duplicatedPropertyName.qml")
<< Result{ { Message{ QStringLiteral("Duplicated signal name \"clicked\"."), 8, 5 } } };
QTest::newRow("missingComponentBehaviorBound")
<< QStringLiteral("missingComponentBehaviorBound.qml")
<< Result {
{ Message{ QStringLiteral("Unqualified access"), 8, 31 } },
{},
{ Message{ QStringLiteral("Set \"pragma ComponentBehavior: Bound\" in "
"order to use IDs from outer components "
"in nested components."), 0, 0, QtInfoMsg } },
Result::AutoFixable
};
}
void TestQmllint::dirtyQmlCode()