aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2022-02-24 10:38:47 +0100
committerMaximilian Goldstein <max.goldstein@qt.io>2022-03-03 11:19:54 +0100
commit8dd7201994c41a5adb8f26a43509fd0dab031bf7 (patch)
tree69c9d6e4ea4f6f1d06136133a1f4658dc9e7dcdc
parent195af3ce69600076782224d9fe74f3ca597ad70f (diff)
qmlcompiler: Improve required property warnings
When a property is required after declaration, now show the scope where it happens including full location information and context. Change-Id: Iddd2d0e543406ab479b4af1a5e74682328998d74 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qmlcompiler/qqmljsimportvisitor.cpp35
-rw-r--r--tests/auto/qml/qmllint/tst_qmllint.cpp4
2 files changed, 31 insertions, 8 deletions
diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp
index 8ebdb0bed5..c482b6597c 100644
--- a/src/qmlcompiler/qqmljsimportvisitor.cpp
+++ b/src/qmlcompiler/qqmljsimportvisitor.cpp
@@ -758,24 +758,45 @@ void QQmlJSImportVisitor::checkRequiredProperties()
if (propertyUsedInRootAlias)
continue;
- const QString propertyScopeName = scopesToSearch.length() > 1
- ? getScopeName(scopesToSearch.at(scopesToSearch.length() - 2),
- QQmlJSScope::QMLScope)
+ const QQmlJSScope::ConstPtr propertyScope = scopesToSearch.length() > 1
+ ? scopesToSearch.at(scopesToSearch.length() - 2)
+ : QQmlJSScope::ConstPtr();
+
+ const QString propertyScopeName = !propertyScope.isNull()
+ ? getScopeName(propertyScope, QQmlJSScope::QMLScope)
: u"here"_qs;
+
const QString requiredScopeName = prevRequiredScope
? getScopeName(prevRequiredScope, QQmlJSScope::QMLScope)
: u"here"_qs;
+ std::optional<FixSuggestion> suggestion;
+
QString message =
QStringLiteral(
"Component is missing required property %1 from %2")
.arg(propName)
.arg(propertyScopeName);
- if (requiredScope != scope)
- message += QStringLiteral(" (marked as required by %3)")
- .arg(requiredScopeName);
+ if (requiredScope != scope) {
+ if (!prevRequiredScope.isNull()) {
+ auto sourceScope = prevRequiredScope->baseType();
+ suggestion = FixSuggestion {
+ { { u"%1:%2:%3: Property marked as required in %4"_qs
+ .arg(sourceScope->filePath())
+ .arg(sourceScope->sourceLocation().startLine)
+ .arg(sourceScope->sourceLocation().startColumn)
+ .arg(requiredScopeName),
+ sourceScope->sourceLocation(), QString(),
+ sourceScope->filePath() } }
+ };
+ } else {
+ message += QStringLiteral(" (marked as required by %1)")
+ .arg(requiredScopeName);
+ }
+ }
- m_logger->log(message, Log_Required, defScope->sourceLocation());
+ m_logger->log(message, Log_Required, defScope->sourceLocation(), true,
+ true, suggestion);
}
}
prevRequiredScope = requiredScope;
diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp
index 4d41fa70a4..14ed6a496f 100644
--- a/tests/auto/qml/qmllint/tst_qmllint.cpp
+++ b/tests/auto/qml/qmllint/tst_qmllint.cpp
@@ -1378,8 +1378,10 @@ void TestQmllint::requiredProperty()
searchWarnings(
warnings,
QStringLiteral("Component is missing required property required_later_string from "
- "Base (marked as required by Derived)"),
+ "Base"),
"requiredPropertyBindingsLater.qml");
+ searchWarnings(warnings, QStringLiteral("Property marked as required in Derived"),
+ "requiredPropertyBindingsLater.qml");
searchWarnings(
warnings,
QStringLiteral("Component is missing required property required_even_later_string "