27 QQmlJSTypePropagator::generate_Ret();
29 if (m_function->isSignalHandler) {
31 }
else if (m_state.accumulatorIn().contains(m_typeResolver->voidType())) {
33 }
else if (!m_returnType.isValid() && m_state.accumulatorIn().isValid()) {
34 if (m_function->isFullyTyped) {
36 m_logger->log(u"Function without return type annotation returns %1"_s.arg(
37 m_state.accumulatorIn().containedTypeName()),
38 qmlIncompatibleType, currentFunctionSourceLocation());
40 }
else if (!canConvertFromTo(m_state.accumulatorIn(), m_returnType)) {
41 m_logger->log(u"Cannot assign binding of type %1 to %2"_s.arg(
42 m_state.accumulatorIn().containedTypeName(),
43 m_returnType.containedTypeName()),
44 qmlIncompatibleType, currentFunctionSourceLocation());
47 const QQmlJS::SourceLocation location = m_function->isProperty
48 ? currentFunctionSourceLocation()
49 : currentNonEmptySourceLocation();
50 QQmlSA::PassManagerPrivate::get(m_passManager)
52 QQmlJSScope::createQQmlSAElement(m_function->qmlScope.containedType()),
53 QQmlJSScope::createQQmlSAElement(m_state.accumulatorIn().containedType()),
54 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(location));
59 QQmlJSTypePropagator::generate_LoadQmlContextPropertyLookup(index);
61 Q_ASSERT(m_idMemberShadows);
63 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
64 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
66 const auto qmlScope = m_function->qmlScope.containedType();
67 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
68 QQmlJSScope::createQQmlSAElement(qmlScope), name,
69 QQmlJSScope::createQQmlSAElement(qmlScope),
70 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
71 currentNonEmptySourceLocation()));
73 const auto &accumulatorOut = m_state.accumulatorOut();
74 if (!accumulatorOut.isValid())
78 if (accumulatorOut.variant() == QQmlJSRegisterContent::Attachment
79 || accumulatorOut.variant() == QQmlJSRegisterContent::MetaType) {
80 m_context.renamedComponents.handleRenamedType(accumulatorOut.scopeType(), name,
81 currentNonEmptySourceLocation(), m_logger);
84 const QQmlJSScope::ConstPtr scope = accumulatorOut.scopeType();
85 const QQmlJSScope::ConstPtr idScope = m_context.scopesById.scope(name, scope);
86 if (!idScope.isNull()) {
87 const auto log = [&](
const auto &memberType,
const auto &memberOwnerScope) {
91 if (m_idMemberShadows->contains(idMemberShadow))
94 m_idMemberShadows->insert(std::move(idMemberShadow));
95 const auto useLoc = currentSourceLocation();
96 m_logger->log(
"Id for object %1 shadows %2 \"%3\". Rename one or the other."_L1
97 .arg(idScope->baseTypeName(), memberType, name),
98 qmlIdShadowsMember, useLoc);
99 m_logger->log(
"Note: Id defined here"_L1, qmlIdShadowsMember,
100 idScope->idSourceLocation(),
true,
true, {}, useLoc.startLine);
103 if (scope->hasProperty(name)) {
104 log(
"property"_L1, scope->ownerOfProperty(scope, name).scope);
105 }
else if (scope->hasMethod(name)) {
106 const auto methods = scope->methods(name);
107 const auto &method = methods[0];
108 if (method.methodType() == QQmlSA::MethodType::Method)
109 log(
"method"_L1, scope->ownerOfMethod(scope, name).scope);
110 else if (method.methodType() == QQmlSA::MethodType::Signal)
111 log(
"signal"_L1, scope->ownerOfMethod(scope, name).scope);
136 QQmlJSTypePropagator::generate_StoreProperty(nameIndex, base);
138 auto callBase = m_state.registers[base].content;
139 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
140 const bool isAttached = callBase.variant() == QQmlJSRegisterContent::Attachment;
142 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
143 QQmlJSScope::createQQmlSAElement(callBase.containedType()),
145 QQmlJSScope::createQQmlSAElement(
146 m_state.accumulatorIn().containedType()),
147 QQmlJSScope::createQQmlSAElement(isAttached
148 ? callBase.attachee().containedType()
149 : m_function->qmlScope.containedType()),
150 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
151 currentNonEmptySourceLocation()));
156 QQmlJSTypePropagator::generate_CallProperty(nameIndex, base, argc, argv);
158 const auto saCheck = [&](
const QString &propertyName,
const QQmlJSScope::ConstPtr &baseType) {
159 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(baseType) };
160 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
161 m_function->qmlScope.containedType()) };
162 const QQmlSA::SourceLocation saLocation{
163 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
166 QQmlSA::PassManagerPrivate::get(m_passManager)
167 ->analyzeRead(saBaseType, propertyName, saContainedType, saLocation);
168 QQmlSA::PassManagerPrivate::get(m_passManager)
169 ->analyzeCall(saBaseType, propertyName, saContainedType, saLocation);
172 const auto callBase = m_state.registers[base].content;
173 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
174 const auto member = m_typeResolver->memberType(callBase, propertyName);
176 const bool isLoggingMethod = QQmlJSTypePropagator::isLoggingMethod(propertyName);
177 if (callBase.contains(m_typeResolver->mathObject()))
178 saCheck(propertyName, callBase.containedType());
179 else if (callBase.contains(m_typeResolver->consoleObject()) && isLoggingMethod)
180 saCheck(propertyName, callBase.containedType());
181 else if (!member.isMethod()) {
182 if (callBase.contains(m_typeResolver->jsValueType())
183 || callBase.contains(m_typeResolver->varType())) {
184 saCheck(propertyName, callBase.containedType());
234 QQmlJSTypePropagator::handleUnqualifiedAccess(name, isMethod);
236 auto location = currentSourceLocation();
238 const auto qmlScopeContained = m_function->qmlScope.containedType();
239 if (qmlScopeContained->isInCustomParserParent()) {
241 if (qmlScopeContained->baseType().isNull()
242 || qmlScopeContained->baseType()->internalName() != u"QQmlConnections"_s)
247 if (isCallingProperty(qmlScopeContained, name))
249 }
else if (propertyResolution(qmlScopeContained, name) != PropertyMissing) {
253 std::optional<QQmlJSFixSuggestion> suggestion;
255 const auto childScopes = m_function->qmlScope.containedType()->childScopes();
256 for (qsizetype i = 0, end = childScopes.size(); i < end; i++) {
257 auto &scope = childScopes[i];
258 if (location.offset > scope->sourceLocation().offset) {
260 && childScopes.at(i + 1)->sourceLocation().offset < location.offset)
262 if (scope->childScopes().size() == 0)
265 const auto jsId = scope->childScopes().first()->jsIdentifier(name);
267 if (jsId.has_value() && jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
268 const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
270 QQmlJS::SourceLocation fixLocation = id.location;
271 Q_UNUSED(fixLocation)
272 fixLocation.length = 0;
274 const auto handler = m_typeResolver->signalHandlers()[id.location];
276 QString fixString = handler.isMultiline ? u"function("_s : u"("_s;
277 const auto parameters = handler.signalParameters;
278 for (
int numParams = parameters.size(); numParams > 0; --numParams) {
279 fixString += parameters.at(parameters.size() - numParams);
281 fixString += u", "_s;
284 fixString += handler.isMultiline ? u") "_s : u") => "_s;
285 const auto msg = u"\"%1\" is ambiguous. Use a function instead: %2%3"_s.arg(
286 name, fixString, handler.isMultiline ?
"{ ... }"_L1 :
"..."_L1);
287 QQmlJSDocumentEdit documentEdit{ m_logger->filePath(), fixLocation, fixString };
288 suggestion = {{ msg, fixLocation, documentEdit }};
289 suggestion->setAutoApplicable();
299 const auto qmlScope = m_function->qmlScope.containedType();
300 if (name == u"model" || name == u"index") {
301 if (
const QQmlJSScope::ConstPtr parent = qmlScope->parentScope(); !parent.isNull()) {
302 const auto bindings = parent->ownPropertyBindings(u"delegate"_s);
304 for (
auto it = bindings.first; it != bindings.second; it++) {
305 if (!it->hasObject())
307 if (it->objectType() == qmlScope) {
308 suggestion = QQmlJSFixSuggestion {
309 "'%1' is implicitly injected into this delegate. "
310 "Add a required property '%1' to the delegate instead."_L1
312 qmlScope->sourceLocation()
321 if (!suggestion.has_value()) {
322 for (QQmlJSScope::ConstPtr scope = qmlScope; !scope.isNull(); scope = scope->parentScope()) {
323 if (scope->hasProperty(name)) {
324 QQmlJSScopesById::MostLikelyCallback<QString> id;
325 m_function->addressableScopes.possibleIds(scope, qmlScope,
326 QQmlJSScopesByIdOption::Default, id);
328 QQmlJS::SourceLocation fixLocation = location;
329 fixLocation.length = 0;
330 QString m =
"%1 is a member of a parent element.\n You can qualify the "
331 "access with its id to avoid this warning%2.\n"_L1.arg(name);
332 m = m.arg(id.result.isEmpty() ?
" (You first have to give the element an id)"_L1 :
""_L1);
334 suggestion = QQmlJSFixSuggestion{
335 m, fixLocation, { m_logger->filePath(), fixLocation,
336 (id.result.isEmpty() ? u"<id>."_s : (id.result + u'.')) }
339 if (!id.result.isEmpty())
340 suggestion->setAutoApplicable();
345 if (!suggestion.has_value() && !m_function->addressableScopes.componentsAreBound()
346 && m_function->addressableScopes.existsAnywhereInDocument(name)) {
347 const QLatin1String replacement =
"pragma ComponentBehavior: Bound"_L1;
348 QQmlJSFixSuggestion bindComponents {
349 "Set \"%1\" in order to use IDs from outer components in nested components."_L1
351 QQmlJS::s_documentOrigin,
352 QQmlJSDocumentEdit{ m_logger->filePath(), QQmlJS::s_documentOrigin, replacement + u'\n' }
354 bindComponents.setAutoApplicable();
355 suggestion = std::move(bindComponents);
358 if (!suggestion.has_value()) {
359 if (
auto didYouMean = QQmlJSUtils::didYouMean(
360 name, qmlScope->properties().keys() + qmlScope->methods().keys(),
361 m_logger->filePath(), location);
362 didYouMean.has_value()) {
363 suggestion = std::move(didYouMean);
367 m_logger->log(QLatin1String(
"Unqualified access"), qmlUnqualified, location,
true,
true,
386 const QString &name,
bool isMethod)
const
388 QQmlJSTypePropagator::handleUnqualifiedAccessAndContextProperties(name, isMethod);
390 if (m_context.userContextProperties.isUnqualifiedAccessDisabled(name))
393 const auto warningMessage = [&name,
this]() {
395 "Potential context property access detected."
396 " Context properties are discouraged in QML: use normal, required, or singleton properties instead."_L1;
398 if (shouldMentionRequiredProperties(m_function->qmlScope.containedType())) {
400 "\nNote: '%1' assumed to be a potential context property because it is not declared as required property."_L1
406 if (m_context.userContextProperties.isOnUsageWarned(name)) {
407 m_logger->log(warningMessage(), qmlContextProperties, currentSourceLocation());
412 handleUnqualifiedAccess(name, isMethod);
414 const QList<QQmlJS::HeuristicContextProperty> definitions =
415 m_context.heuristicContextProperties.definitionsForName(name);
416 if (definitions.isEmpty())
418 QString warning = warningMessage();
419 for (
const auto &candidate : definitions) {
420 warning.append(
"\nNote: candidate context property declaration '%1' at %2:%3:%4"_L1.arg(
421 name, QDir::cleanPath(candidate.filename),
422 QString::number(candidate.location.startLine),
423 QString::number(candidate.location.startColumn)));
425 m_logger->log(warning, qmlContextProperties, currentSourceLocation());
431 QQmlJSTypePropagator::checkDeprecated(scope, name, isMethod);
433 Q_ASSERT(!scope.isNull());
434 auto qmlScope = QQmlJSScope::findCurrentQMLScope(scope);
435 if (qmlScope.isNull())
438 QList<QQmlJSAnnotation> annotations;
440 QQmlJSMetaMethod method;
443 const QList<QQmlJSMetaMethod> methods = qmlScope->methods(name);
444 if (methods.isEmpty())
446 method = methods.constFirst();
447 annotations = method.annotations();
449 QQmlJSMetaProperty property = qmlScope->property(name);
450 if (!property.isValid())
452 annotations = property.annotations();
455 auto deprecationAnn = std::find_if(
456 annotations.constBegin(), annotations.constEnd(),
457 [](
const QQmlJSAnnotation &annotation) {
return annotation.isDeprecation(); });
459 if (deprecationAnn == annotations.constEnd())
462 QQQmlJSDeprecation deprecation = deprecationAnn->deprecation();
464 QString descriptor = name;
466 descriptor += u'(' + method.parameterNames().join(u", "_s) + u')';
468 QString message =
"%1 \"%2\" is deprecated"_L1
469 .arg(isMethod ? u"Method"_s : u"Property"_s, descriptor);
471 if (!deprecation.reason.isEmpty())
472 message.append(QStringLiteral(
" (Reason: %1)").arg(deprecation.reason));
474 m_logger->log(message, qmlDeprecated, currentSourceLocation());
478 const QString &name)
const
480 const bool res = QQmlJSTypePropagator::isCallingProperty(scope, name);
482 if (
const auto property = scope->property(name); property.isValid()) {
484 if (property.type() == m_typeResolver->varType()) {
485 errorType = u"a var property. It may or may not be a method. "_s
486 u"Use a regular function instead."_s;
487 }
else if (property.type() == m_typeResolver->jsValueType()) {
488 errorType = u"a QJSValue property. It may or may not be a method. "_s
489 u"Use a regular Q_INVOKABLE instead."_s;
491 errorType = u"not a method"_s;
494 m_logger->log(u"Property \"%1\" is %2"_s.arg(name, errorType),
495 qmlUseProperFunction, currentSourceLocation(),
true,
true, {});
538 QQmlJSTypePropagator::handleLookupError(propertyName);
540 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
541 const QString typeName = accumulatorIn.containedTypeName();
543 if (typeName == u"QVariant")
545 if (accumulatorIn.isList() && propertyName == u"length")
548 auto baseType = accumulatorIn.containedType();
551 if (propertyResolution(baseType, propertyName) != PropertyMissing)
554 if (baseType->isScript())
557 std::optional<QQmlJSFixSuggestion> fixSuggestion;
559 if (
auto suggestion = QQmlJSUtils::didYouMean(propertyName, baseType->properties().keys(),
560 m_logger->filePath(), currentSourceLocation());
561 suggestion.has_value()) {
562 fixSuggestion = std::move(suggestion);
565 if (!fixSuggestion.has_value()
566 && accumulatorIn.variant() == QQmlJSRegisterContent::MetaType) {
568 const QQmlJSScope::ConstPtr scopeType = accumulatorIn.scopeType();
569 const auto metaEnums = scopeType->enumerations();
570 const bool enforcesScoped = scopeType->enforcesScopedEnums();
572 QStringList enumKeys;
573 for (
const QQmlJSMetaEnum &metaEnum : metaEnums) {
574 if (!enforcesScoped || !metaEnum.isScoped())
575 enumKeys << metaEnum.keys();
578 if (
auto suggestion = QQmlJSUtils::didYouMean(
579 propertyName, enumKeys, m_logger->filePath(), currentSourceLocation());
580 suggestion.has_value()) {
581 fixSuggestion = std::move(suggestion);
585 if (checkTypeResolved(baseType)) {
586 m_logger->log(u"Member \"%1\" not found on type \"%2\""_s.arg(propertyName, typeName),
587 qmlMissingProperty, currentSourceLocation(),
true,
true, fixSuggestion);
613 QQmlJSTypePropagator::generate_StoreNameCommon(nameIndex);
615 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
616 const QQmlJSRegisterContent in = m_state.accumulatorIn();
617 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
619 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
620 QQmlJSScope::createQQmlSAElement(
621 m_state.accumulatorIn().containedType()),
623 QQmlJSScope::createQQmlSAElement(isAttached
624 ? in.attachee().containedType()
625 : m_function->qmlScope.containedType()),
626 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
627 currentNonEmptySourceLocation()));
629 const auto qmlScope = m_function->qmlScope.containedType();
630 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
631 QQmlJSScope::createQQmlSAElement(qmlScope), name,
632 QQmlJSScope::createQQmlSAElement(in.containedType()),
633 QQmlJSScope::createQQmlSAElement(qmlScope),
634 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
635 currentNonEmptySourceLocation()));
657 QQmlJSRegisterContent scope)
659 QQmlJSTypePropagator::propagateCall(methods, argc, argv, scope);
662 const QQmlJSMetaMethod match = bestMatchForCall(methods, argc, argv, &errors);
663 if (!match.isValid())
666 const QQmlSA::Element saBaseType = QQmlJSScope::createQQmlSAElement(scope.containedType());
667 const QQmlSA::SourceLocation saLocation{
668 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
670 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
671 m_function->qmlScope.containedType()) };
673 QQmlSA::PassManagerPrivate::get(m_passManager)
674 ->analyzeCall(saBaseType, match.methodName(), saContainedType, saLocation);