qmllint: warn when passing a directory to -i option
It should be a file. Change-Id: I52f4ebcf8bdd8ddd1fedd66ceffe9a060139c1d9 Fixes: QTBUG-83861 Pick-to: 5.15 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
parent
afd6c6b905
commit
aa470fa92d
|
@ -50,8 +50,10 @@ private Q_SLOTS:
|
|||
|
||||
void testUnknownCausesFail();
|
||||
|
||||
void directoryPassedAsQmlTypesFile();
|
||||
|
||||
private:
|
||||
QString runQmllint(const QString &fileToLint, bool shouldSucceed);
|
||||
QString runQmllint(const QString &fileToLint, bool shouldSucceed, const QStringList &extraArgs = QStringList());
|
||||
|
||||
QString m_qmllintPath;
|
||||
};
|
||||
|
@ -114,6 +116,15 @@ void TestQmllint::testUnknownCausesFail()
|
|||
QStringLiteral("warning: Unknown was not found. Did you add all import paths?")));
|
||||
}
|
||||
|
||||
void TestQmllint::directoryPassedAsQmlTypesFile()
|
||||
{
|
||||
const QStringList iArg = QStringList() << QStringLiteral("-i") << dataDirectory();
|
||||
const QString errorMessages = runQmllint("unknownElement.qml", false, iArg);
|
||||
const QString expectedError = QStringLiteral("warning: QML types file cannot be a directory: ") + dataDirectory();
|
||||
QVERIFY2(errorMessages.contains(expectedError), qPrintable(QString::fromLatin1(
|
||||
"Expected error to contain \"%1\", but it didn't: %2").arg(expectedError, errorMessages)));
|
||||
}
|
||||
|
||||
void TestQmllint::dirtyQmlCode_data()
|
||||
{
|
||||
QTest::addColumn<QString>("filename");
|
||||
|
@ -214,14 +225,15 @@ void TestQmllint::cleanQmlCode()
|
|||
QVERIFY(warnings.isEmpty());
|
||||
}
|
||||
|
||||
QString TestQmllint::runQmllint(const QString &fileToLint, bool shouldSucceed)
|
||||
QString TestQmllint::runQmllint(const QString &fileToLint, bool shouldSucceed, const QStringList &extraArgs)
|
||||
{
|
||||
auto qmlImportDir = QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath);
|
||||
QStringList args;
|
||||
args << testFile(fileToLint)
|
||||
<< QStringLiteral("-I") << qmlImportDir
|
||||
<< QStringLiteral("-I") << dataDirectory()
|
||||
<< QStringLiteral("--silent");
|
||||
<< QStringLiteral("-I") << dataDirectory();
|
||||
args << extraArgs;
|
||||
args << QStringLiteral("--silent");
|
||||
QString errors;
|
||||
auto verify = [&](bool isSilent) {
|
||||
QProcess process;
|
||||
|
|
|
@ -57,14 +57,6 @@ static QQmlDirParser createQmldirParserForFile(const QString &filename)
|
|||
return parser;
|
||||
}
|
||||
|
||||
static TypeDescriptionReader createQmltypesReaderForFile(const QString &filename)
|
||||
{
|
||||
QFile f(filename);
|
||||
f.open(QFile::ReadOnly);
|
||||
TypeDescriptionReader reader { filename, f.readAll() };
|
||||
return reader;
|
||||
}
|
||||
|
||||
void FindWarningVisitor::enterEnvironment(ScopeType type, const QString &name)
|
||||
{
|
||||
m_currentScope = ScopeTree::create(type, name, m_currentScope);
|
||||
|
@ -186,10 +178,25 @@ static const QLatin1String SlashQmldir = QLatin1String("/qmldir");
|
|||
static const QLatin1String SlashPluginsDotQmltypes = QLatin1String("/plugins.qmltypes");
|
||||
|
||||
void FindWarningVisitor::readQmltypes(const QString &filename,
|
||||
FindWarningVisitor::Import &result)
|
||||
QHash<QString, ScopeTree::ConstPtr> *objects, QStringList *dependencies)
|
||||
{
|
||||
auto reader = createQmltypesReaderForFile(filename);
|
||||
auto succ = reader(&result.objects, &result.dependencies);
|
||||
const QFileInfo fileInfo(filename);
|
||||
if (!fileInfo.exists()) {
|
||||
m_colorOut.write(QLatin1String("warning: "), Warning);
|
||||
m_colorOut.writeUncolored(QLatin1String("QML types file does not exist: ") + filename);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fileInfo.isDir()) {
|
||||
m_colorOut.write(QLatin1String("warning: "), Warning);
|
||||
m_colorOut.writeUncolored(QLatin1String("QML types file cannot be a directory: ") + filename);
|
||||
return;
|
||||
}
|
||||
|
||||
QFile file(filename);
|
||||
file.open(QFile::ReadOnly);
|
||||
TypeDescriptionReader reader { filename, file.readAll() };
|
||||
auto succ = reader(objects, dependencies);
|
||||
if (!succ)
|
||||
m_colorOut.writeUncolored(reader.errorMessage());
|
||||
}
|
||||
|
@ -226,7 +233,7 @@ FindWarningVisitor::Import FindWarningVisitor::readQmldir(const QString &path)
|
|||
result.objects.insert( it.key(), ScopeTree::ConstPtr(it.value()));
|
||||
|
||||
if (!reader.plugins().isEmpty() && QFile::exists(path + SlashPluginsDotQmltypes))
|
||||
readQmltypes(path + SlashPluginsDotQmltypes, result);
|
||||
readQmltypes(path + SlashPluginsDotQmltypes, &result.objects, &result.dependencies);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -293,7 +300,7 @@ void FindWarningVisitor::importHelper(const QString &module, const QString &pref
|
|||
QDirIterator it { qmltypesPath, QStringList() << QLatin1String("*.qmltypes"), QDir::Files };
|
||||
|
||||
while (it.hasNext())
|
||||
readQmltypes(it.next(), result);
|
||||
readQmltypes(it.next(), &result.objects, &result.dependencies);
|
||||
|
||||
processImport(prefix, result);
|
||||
}
|
||||
|
@ -304,7 +311,7 @@ void FindWarningVisitor::importHelper(const QString &module, const QString &pref
|
|||
Import result;
|
||||
|
||||
for (const auto &qmltypeFile : m_qmltypeFiles)
|
||||
readQmltypes(qmltypeFile, result);
|
||||
readQmltypes(qmltypeFile, &result.objects, &result.dependencies);
|
||||
|
||||
processImport("", result);
|
||||
}
|
||||
|
@ -442,20 +449,14 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiProgram *)
|
|||
QDirIterator it { dir, QStringList() << QLatin1String("builtins.qmltypes"), QDir::NoFilter,
|
||||
QDirIterator::Subdirectories };
|
||||
while (it.hasNext()) {
|
||||
auto reader = createQmltypesReaderForFile(it.next());
|
||||
auto succ = reader(&objects, &dependencies);
|
||||
if (!succ)
|
||||
m_colorOut.writeUncolored(reader.errorMessage());
|
||||
readQmltypes(it.next(), &objects, &dependencies);
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_qmltypeFiles.isEmpty())
|
||||
{
|
||||
for (const auto &qmltypeFile : m_qmltypeFiles) {
|
||||
auto reader = createQmltypesReaderForFile(qmltypeFile);
|
||||
auto succ = reader(&objects, &dependencies);
|
||||
if (!succ)
|
||||
m_colorOut.writeUncolored(reader.errorMessage());
|
||||
readQmltypes(qmltypeFile, &objects, &dependencies);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,8 @@ private:
|
|||
void importHelper(const QString &module, const QString &prefix = QString(),
|
||||
QTypeRevision version = QTypeRevision());
|
||||
|
||||
void readQmltypes(const QString &filename, Import &result);
|
||||
void readQmltypes(const QString &filename, QHash<QString, ScopeTree::ConstPtr> *objects,
|
||||
QStringList *dependencies);
|
||||
Import readQmldir(const QString &dirname);
|
||||
void processImport(const QString &prefix, const Import &import);
|
||||
|
||||
|
|
Loading…
Reference in New Issue