qmllint: Allow assigning to readonly properties in same scope

If the property lives in the same scope where it's assigned we can
assign it even if it's readonly. That's the whole point of it being
readonly.

Pick-to: 6.2
Change-Id: I172d5b997641490201a0216fd7ebb5a3de30a63b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2021-10-04 16:44:46 +02:00
parent b66db710e5
commit d2507f2383
4 changed files with 12 additions and 4 deletions

View File

@ -532,7 +532,7 @@ void QQmlJSTypePropagator::generate_StoreNameSloppy(int nameIndex)
return;
}
if (!type.isWritable()) {
if (!type.isWritable() && !m_currentScope->hasOwnProperty(name)) {
setError(u"Can't assign to read-only property %1"_qs.arg(name));
m_logger->logWarning(u"Cannot assign to read-only property %1"_qs.arg(name), Log_Property,

View File

@ -135,9 +135,11 @@ void QQmlJSTypeResolver::init(QQmlJSImportVisitor &visitor)
if (!binding.isLiteralBinding())
continue;
if (QQmlJSMetaProperty property = scope->property(binding.propertyName());
property.isValid()) {
if (!property.isWritable()) {
const QQmlJSMetaProperty property = scope->property(binding.propertyName());
if (property.isValid()) {
// If the property is defined in the same scope where it is set,
// we are in fact allowed to set it, even if it's not writable.
if (!property.isWritable() && !scope->hasOwnProperty(binding.propertyName())) {
m_logger->logWarning(u"Cannot assign to read-only property %1"_qs
.arg(binding.propertyName()),
Log_Type, binding.sourceLocation());

View File

@ -0,0 +1,5 @@
import QtQml
QtObject {
readonly property int i: 14
}

View File

@ -853,6 +853,7 @@ void TestQmllint::cleanQmlCode_data()
QTest::newRow("GoodModulePrefix") << QStringLiteral("goodModulePrefix.qml");
QTest::newRow("required property in Component") << QStringLiteral("requiredPropertyInComponent.qml");
QTest::newRow("bytearray") << QStringLiteral("bytearray.qml");
QTest::newRow("initReadonly") << QStringLiteral("initReadonly.qml");
}
void TestQmllint::cleanQmlCode()