aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmllint
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-10-06 12:52:42 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-10-07 10:42:36 +0200
commite7e04656696d144e57c9bec1b6ca7d30d237ee70 (patch)
tree6187a6f8f0701ba39179b668817d7111d679afd5 /tools/qmllint
parentd2c9ecc727688d2519489e4b08013bfde8b0ae1e (diff)
qmllint: Capitalize importer warnings
Refactor the "prefixed message" mechanism into ColorOutput. Change-Id: Ie982e641771f3367406f48a8bcfd2bd3da122b16 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/qmllint')
-rw-r--r--tools/qmllint/checkidentifiers.cpp49
-rw-r--r--tools/qmllint/findwarnings.cpp42
-rw-r--r--tools/qmllint/qcoloroutput.cpp17
-rw-r--r--tools/qmllint/qcoloroutput.h3
4 files changed, 59 insertions, 52 deletions
diff --git a/tools/qmllint/checkidentifiers.cpp b/tools/qmllint/checkidentifiers.cpp
index 06103f073b..6fa26b86ba 100644
--- a/tools/qmllint/checkidentifiers.cpp
+++ b/tools/qmllint/checkidentifiers.cpp
@@ -55,11 +55,6 @@ private:
QStringView m_afterText;
};
-static void writeWarning(ColorOutput *out)
-{
- out->write(QLatin1String("Warning: "), Warning);
-}
-
static const QStringList unknownBuiltins = {
// TODO: "string" should be added to builtins.qmltypes, and the special handling below removed
QStringLiteral("alias"), // TODO: we cannot properly resolve aliases, yet
@@ -124,15 +119,14 @@ bool CheckIdentifiers::checkMemberAccess(const QVector<FieldMember> &members,
QQmlJSScope::ConstPtr scope = outerScope;
for (const FieldMember &access : members) {
if (scope.isNull()) {
- writeWarning(m_colorOut);
- m_colorOut->write(
+ m_colorOut->writePrefixedMessage(
QString::fromLatin1("Type \"%1\" of base \"%2\" not found when accessing member \"%3\" at %4:%5:%6.\n")
.arg(detectedRestrictiveKind)
.arg(detectedRestrictiveName)
.arg(access.m_name)
.arg(m_fileName)
.arg(access.m_location.startLine)
- .arg(access.m_location.startColumn), Normal);
+ .arg(access.m_location.startColumn), Warning);
printContext(m_code, m_colorOut, access.m_location);
return false;
}
@@ -143,15 +137,14 @@ bool CheckIdentifiers::checkMemberAccess(const QVector<FieldMember> &members,
continue;
}
- writeWarning(m_colorOut);
- m_colorOut->write(QString::fromLatin1(
+ m_colorOut->writePrefixedMessage(QString::fromLatin1(
"\"%1\" is a %2. You cannot access \"%3\" on it at %4:%5:%6\n")
.arg(detectedRestrictiveName)
.arg(detectedRestrictiveKind)
.arg(access.m_name)
.arg(m_fileName)
.arg(access.m_location.startLine)
- .arg(access.m_location.startColumn), Normal);
+ .arg(access.m_location.startColumn), Warning);
printContext(m_code, m_colorOut, access.m_location);
return false;
}
@@ -270,15 +263,14 @@ bool CheckIdentifiers::checkMemberAccess(const QVector<FieldMember> &members,
}
}
- writeWarning(m_colorOut);
- m_colorOut->write(QString::fromLatin1(
+ m_colorOut->writePrefixedMessage(QString::fromLatin1(
"Property \"%1\" not found on type \"%2\" at %3:%4:%5\n")
.arg(access.m_name)
.arg(scope->internalName().isEmpty()
? scope->baseTypeName() : scope->internalName())
.arg(m_fileName)
.arg(access.m_location.startLine)
- .arg(access.m_location.startColumn), Normal);
+ .arg(access.m_location.startColumn), Warning);
printContext(m_code, m_colorOut, access.m_location);
return false;
}
@@ -346,13 +338,12 @@ bool CheckIdentifiers::operator()(
continue;
if (!qmlIt->type()) {
- writeWarning(m_colorOut);
- m_colorOut->write(QString::fromLatin1(
+ m_colorOut->writePrefixedMessage(QString::fromLatin1(
"Type of property \"%2\" not found at %3:%4:%5\n")
.arg(memberAccessBase.m_name)
.arg(m_fileName)
.arg(memberAccessBase.m_location.startLine)
- .arg(memberAccessBase.m_location.startColumn), Normal);
+ .arg(memberAccessBase.m_location.startColumn), Warning);
printContext(m_code, m_colorOut, memberAccessBase.m_location);
noUnqualifiedIdentifier = false;
} else if (!checkMemberAccess(memberAccessChain, qmlIt->type(), &*qmlIt)) {
@@ -374,12 +365,11 @@ bool CheckIdentifiers::operator()(
}
noUnqualifiedIdentifier = false;
- writeWarning(m_colorOut);
const auto location = memberAccessBase.m_location;
- m_colorOut->write(QString::fromLatin1("unqualified access at %1:%2:%3\n")
+ m_colorOut->writePrefixedMessage(QString::fromLatin1("unqualified access at %1:%2:%3\n")
.arg(m_fileName)
.arg(location.startLine).arg(location.startColumn),
- Normal);
+ Warning);
printContext(m_code, m_colorOut, location);
@@ -388,12 +378,16 @@ bool CheckIdentifiers::operator()(
if (firstElement->properties().contains(memberAccessBase.m_name)
|| firstElement->methods().contains(memberAccessBase.m_name)
|| firstElement->enums().contains(memberAccessBase.m_name)) {
- m_colorOut->write(QLatin1String("Note: "), Info);
- m_colorOut->write(memberAccessBase.m_name + QLatin1String(" is a member of the root element\n"), Normal );
- m_colorOut->write(QLatin1String(" You can qualify the access with its id to avoid this warning:\n"), Normal);
+ m_colorOut->writePrefixedMessage(
+ memberAccessBase.m_name
+ + QLatin1String(" is a member of the root element\n")
+ + QLatin1String(" You can qualify the access with its id "
+ "to avoid this warning:\n"),
+ Info, QStringLiteral("Note"));
if (rootId == QLatin1String("<id>")) {
- m_colorOut->write(QLatin1String("Note: "), Warning);
- m_colorOut->write(QLatin1String("You first have to give the root element an id\n"));
+ m_colorOut->writePrefixedMessage(
+ QLatin1String("You first have to give the root element an id\n"),
+ Warning, QStringLiteral("Note"));
}
IssueLocationWithContext issueLocationWithContext {m_code, location};
m_colorOut->write(issueLocationWithContext.beforeText().toString(), Normal);
@@ -403,14 +397,13 @@ bool CheckIdentifiers::operator()(
} else if (jsId.has_value()
&& jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
- m_colorOut->write(QLatin1String("Note: "), Info);
- m_colorOut->write(
+ m_colorOut->writePrefixedMessage(
memberAccessBase.m_name + QString::fromLatin1(
" is accessible in this scope because "
"you are handling a signal at %1:%2:%3\n")
.arg(m_fileName)
.arg(id.location.startLine).arg(id.location.startColumn),
- Normal);
+ Info, QStringLiteral("Note"));
m_colorOut->write(QLatin1String("Consider using a function instead\n"), Normal);
IssueLocationWithContext context {m_code, id.location};
m_colorOut->write(context.beforeText() + QLatin1Char(' '));
diff --git a/tools/qmllint/findwarnings.cpp b/tools/qmllint/findwarnings.cpp
index d601b3e910..019901507a 100644
--- a/tools/qmllint/findwarnings.cpp
+++ b/tools/qmllint/findwarnings.cpp
@@ -68,10 +68,10 @@ void FindWarningVisitor::importExportedNames(QQmlJSScope::ConstPtr scope)
}
if (m_warnInheritanceCycle) {
- m_colorOut.write(QLatin1String("Warning: "), Warning);
- m_colorOut.write(QString::fromLatin1("%1 is part of an inheritance cycle: %2\n")
- .arg(scope->internalName())
- .arg(inheritenceCycle));
+ m_colorOut.writePrefixedMessage(
+ QString::fromLatin1("%1 is part of an inheritance cycle: %2\n")
+ .arg(scope->internalName())
+ .arg(inheritenceCycle), Warning);
}
m_unknownImports.insert(scope->internalName());
@@ -92,10 +92,10 @@ void FindWarningVisitor::importExportedNames(QQmlJSScope::ConstPtr scope)
} else if (auto newScope = scope->baseType()) {
scope = newScope;
} else {
- m_colorOut.write(QLatin1String("Warning: "), Warning);
- m_colorOut.write(scope->baseTypeName()
- + QLatin1String(" was not found."
- " Did you add all import paths?\n"));
+ m_colorOut.writePrefixedMessage(
+ scope->baseTypeName()
+ + QLatin1String(" was not found. Did you add all import paths?\n"),
+ Warning);
m_unknownImports.insert(scope->baseTypeName());
m_visitFailed = true;
break;
@@ -118,8 +118,8 @@ void FindWarningVisitor::flushPendingSignalParameters()
void FindWarningVisitor::throwRecursionDepthError()
{
- m_colorOut.write(QStringLiteral("Error"), Error);
- m_colorOut.write(QStringLiteral("Maximum statement or expression depth exceeded"), Error);
+ m_colorOut.writePrefixedMessage(
+ QStringLiteral("Maximum statement or expression depth exceeded"), Error);
m_visitFailed = true;
}
@@ -141,10 +141,8 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiProgram *)
m_rootScopeImports.insert(imported);
const QStringList warnings = m_importer.takeWarnings();
- for (const QString &warning : warnings) {
- m_colorOut.write(QStringLiteral("warning: "), Warning);
- m_colorOut.writeUncolored(warning);
- }
+ for (const QString &warning : warnings)
+ m_colorOut.writePrefixedMessage(warning, Warning);
return true;
}
@@ -258,13 +256,12 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::Catch *)
bool FindWarningVisitor::visit(QQmlJS::AST::WithStatement *withStatement)
{
if (m_warnWithStatement) {
- m_colorOut.write(QString::fromLatin1("Warning: "), Warning);
- m_colorOut.write(QString::fromLatin1(
+ m_colorOut.writePrefixedMessage(QString::fromLatin1(
"%1:%2: with statements are strongly discouraged in QML "
"and might cause false positives when analysing unqalified identifiers\n")
.arg(withStatement->firstSourceLocation().startLine)
.arg(withStatement->firstSourceLocation().startColumn),
- Normal);
+ Warning);
}
enterEnvironment(QQmlJSScope::JSLexicalScope, "with");
@@ -317,11 +314,10 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiScriptBinding *uisb)
if (!m_currentScope->methods().contains(signal) && m_warnUnqualified) {
const auto location = uisb->firstSourceLocation();
- m_colorOut.write(QLatin1String("Warning: "), Warning);
- m_colorOut.write(QString::fromLatin1(
+ m_colorOut.writePrefixedMessage(QString::fromLatin1(
"no matching signal found for handler \"%1\" at %2:%3:%4\n")
.arg(name.toString()).arg(m_filePath).arg(location.startLine)
- .arg(location.startColumn), Normal);
+ .arg(location.startColumn), Warning);
CheckIdentifiers::printContext(m_code, &m_colorOut, location);
return true;
}
@@ -570,10 +566,8 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiImport *import)
m_rootScopeImports.insert(imported);
const QStringList warnings = m_importer.takeWarnings();
- for (const QString &warning : warnings) {
- m_colorOut.write(QStringLiteral("warning: "), Warning);
- m_colorOut.writeUncolored(warning);
- }
+ for (const QString &warning : warnings)
+ m_colorOut.writePrefixedMessage(warning, Warning);
return true;
}
diff --git a/tools/qmllint/qcoloroutput.cpp b/tools/qmllint/qcoloroutput.cpp
index ce0545b1a9..22783ef1b9 100644
--- a/tools/qmllint/qcoloroutput.cpp
+++ b/tools/qmllint/qcoloroutput.cpp
@@ -242,6 +242,23 @@ void ColorOutput::write(const QString &message, int colorID)
d->write(colorify(message, colorID));
}
+void ColorOutput::writePrefixedMessage(const QString &message, MessageColors type,
+ const QString &prefix)
+{
+ static const QStringList prefixes = {
+ QStringLiteral("Error"),
+ QStringLiteral("Warning"),
+ QStringLiteral("Info"),
+ QStringLiteral("Normal"),
+ QStringLiteral("Hint"),
+ };
+
+ Q_ASSERT(prefixes.length() > qsizetype(type));
+ Q_ASSERT(prefix.isEmpty() || prefix.front().isUpper());
+ write((prefix.isEmpty() ? prefixes[type] : prefix) + QStringLiteral(": "), type);
+ writeUncolored(message);
+}
+
/*!
Writes \a message to \c stderr as if for instance
QTextStream would have been used, and adds a line ending at the end.
diff --git a/tools/qmllint/qcoloroutput.h b/tools/qmllint/qcoloroutput.h
index 6a1acfe8b5..41b8751432 100644
--- a/tools/qmllint/qcoloroutput.h
+++ b/tools/qmllint/qcoloroutput.h
@@ -41,6 +41,7 @@
#include <QtCore/qglobal.h>
#include <QtCore/qscopedpointer.h>
+#include <QtCore/qstring.h>
class ColorOutputPrivate;
@@ -104,6 +105,8 @@ public:
void writeUncolored(const QString &message);
void write(const QString &message, int color = -1);
+ void writePrefixedMessage(const QString &message, MessageColors type,
+ const QString &prefix = QString());
QString colorify(const QString &message, int color = -1) const;
private: