Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljslinterrenamedcomponents.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
8
9namespace QQmlJS {
10
11using namespace Qt::StringLiterals;
12
13static QStringView baseNameOf(const QString &path)
14{
15 if (!path.endsWith(".qml"_L1))
16 return {};
17
18 QStringView result(path);
19 result.chop(".qml"_L1.size());
20 if (result.endsWith(".ui"_L1))
21 result.chop(".ui"_L1.size());
22 const qsizetype start = path.lastIndexOf("/"_L1);
23 result.slice(start == -1 ? 0 : start + 1);
24 return result;
25}
26
27void LinterRenamedComponents::handleRenamedType(const QQmlJSScope::ConstPtr &scope,
28 const QStringView name,
29 const QQmlJS::SourceLocation &location,
30 QQmlJSLogger *logger) const
31{
32 if (scope.isNull())
33 return;
34
35 auto [start, end] = m_scopeToName->equal_range(scope);
36 if (start == end || std::next(start) == end)
37 return;
38
39 const QString oldName = baseNameOf(scope->filePath()).toString();
40 if (oldName != name)
41 return;
42
43 QStringList names(start, end);
44 names.erase(std::remove(names.begin(), names.end(), oldName), names.end());
45 std::sort(names.begin(), names.end());
46
47 logger->log(
48 "\"%2\" is explicitly renamed to \"%1\" via a qmldir entry or QT_QML_SOURCE_TYPENAME CMake property, use \"%1\" instead."_L1
49 .arg(names.join("\", \""_L1), name),
50 qmlRenamedType, location);
51}
52
53bool LinterRenamedComponents::isTypeRenamedTo(const QQmlJSScope::ConstPtr &scope,
54 const QString &alias) const
55{
56 const auto [start, end] = m_scopeToName->equal_range(scope);
57 if (start == end || std::next(start) == end)
58 return false;
59
60 const QString filePath = scope->filePath();
61 const QStringView oldName = baseNameOf(filePath);
62 if (oldName == alias)
63 return false;
64 return std::find(start, end, alias) != end;
65}
66} // namespace QQmlJS
67
68QT_END_NAMESPACE
void handleRenamedType(const QQmlJSScope::ConstPtr &scope, const QStringView name, const QQmlJS::SourceLocation &location, QQmlJSLogger *logger) const
bool isTypeRenamedTo(const QQmlJSScope::ConstPtr &scope, const QString &alias) const
static QStringView baseNameOf(const QString &path)
Combined button and popup list for selecting options.