2014-07-09 12:28:23 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** Copyright (C) 2016 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sergio Martins <sergio.martins@kdab.com>
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-07-09 12:28:23 +00:00
|
|
|
**
|
|
|
|
** This file is part of the plugins of the Qt Toolkit.
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2014-07-09 12:28:23 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 11:23:05 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2014-07-09 12:28:23 +00:00
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2014-07-09 12:28:23 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include "findunqualified.h"
|
|
|
|
|
|
|
|
#include <QtQml/private/qqmljslexer_p.h>
|
|
|
|
#include <QtQml/private/qqmljsparser_p.h>
|
|
|
|
#include <QtQml/private/qqmljsengine_p.h>
|
|
|
|
#include <QtQml/private/qqmljsastvisitor_p.h>
|
|
|
|
#include <QtQml/private/qqmljsast_p.h>
|
|
|
|
|
|
|
|
#include <QtCore/qdebug.h>
|
|
|
|
#include <QtCore/qfile.h>
|
|
|
|
#include <QtCore/qfileinfo.h>
|
|
|
|
#include <QtCore/qcoreapplication.h>
|
|
|
|
|
2017-01-23 08:01:22 +00:00
|
|
|
#if QT_CONFIG(commandlineparser)
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qcommandlineparser.h>
|
2017-01-23 08:01:22 +00:00
|
|
|
#endif
|
2014-07-09 12:28:23 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
#ifndef QT_BOOTSTRAPPED
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qlibraryinfo.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
static bool lint_file(const QString &filename, const bool silent, const bool warnUnqualied,
|
|
|
|
const QStringList &qmltypeDirs)
|
2014-07-09 12:28:23 +00:00
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
2019-09-19 09:15:47 +00:00
|
|
|
if (!silent)
|
|
|
|
qWarning() << "Failed to open file" << filename << file.error();
|
2014-07-09 12:28:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
|
|
|
|
|
|
|
QFileInfo info(filename);
|
2019-10-15 08:39:40 +00:00
|
|
|
const QString lowerSuffix = info.suffix().toLower();
|
|
|
|
const bool isESModule = lowerSuffix == QLatin1String("mjs");
|
2019-11-11 17:18:04 +00:00
|
|
|
const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
|
|
|
|
|
|
|
|
lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/ !isJavaScript);
|
2014-07-09 12:28:23 +00:00
|
|
|
QQmlJS::Parser parser(&engine);
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
bool success = isJavaScript ? (isESModule ? parser.parseModule() : parser.parseProgram())
|
|
|
|
: parser.parse();
|
2014-07-09 12:28:23 +00:00
|
|
|
|
|
|
|
if (!success && !silent) {
|
2016-08-11 10:37:27 +00:00
|
|
|
const auto diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
|
2019-11-11 17:18:04 +00:00
|
|
|
qWarning().noquote() << QString::fromLatin1("%1:%2 : %3")
|
2020-02-27 09:49:14 +00:00
|
|
|
.arg(filename).arg(m.loc.startLine).arg(m.message);
|
2014-07-09 12:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
if (success && !isJavaScript && warnUnqualied) {
|
|
|
|
auto root = parser.rootNode();
|
2019-09-19 09:15:47 +00:00
|
|
|
FindUnqualifiedIDVisitor v { qmltypeDirs, code, filename, silent };
|
2019-06-14 12:21:25 +00:00
|
|
|
root->accept(&v);
|
|
|
|
success = v.check();
|
|
|
|
}
|
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argv, char *argc[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argv, argc);
|
|
|
|
QCoreApplication::setApplicationName("qmllint");
|
|
|
|
QCoreApplication::setApplicationVersion("1.0");
|
2017-01-23 08:01:22 +00:00
|
|
|
#if QT_CONFIG(commandlineparser)
|
2014-07-09 12:28:23 +00:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription(QLatin1String("QML syntax verifier"));
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
QCommandLineOption silentOption(QStringList() << "s" << "silent",
|
|
|
|
QLatin1String("Don't output syntax errors"));
|
2014-07-09 12:28:23 +00:00
|
|
|
parser.addOption(silentOption);
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
QCommandLineOption checkUnqualified(QStringList() << "U" << "check-unqualified",
|
|
|
|
QLatin1String("Warn about unqualified identifiers"));
|
2019-06-14 12:21:25 +00:00
|
|
|
parser.addOption(checkUnqualified);
|
|
|
|
|
|
|
|
QCommandLineOption qmltypesDirsOption(
|
|
|
|
QStringList() << "I"
|
|
|
|
<< "qmldirs",
|
|
|
|
QLatin1String("Look for qmltypes files in specified directory"),
|
|
|
|
QLatin1String("directory"));
|
|
|
|
parser.addOption(qmltypesDirsOption);
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
parser.addPositionalArgument(QLatin1String("files"),
|
|
|
|
QLatin1String("list of qml or js files to verify"));
|
2014-07-09 12:28:23 +00:00
|
|
|
|
|
|
|
parser.process(app);
|
|
|
|
|
2016-08-11 10:37:27 +00:00
|
|
|
const auto positionalArguments = parser.positionalArguments();
|
|
|
|
if (positionalArguments.isEmpty()) {
|
2014-07-09 12:28:23 +00:00
|
|
|
parser.showHelp(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool silent = parser.isSet(silentOption);
|
2019-06-14 12:21:25 +00:00
|
|
|
bool warnUnqualified = parser.isSet(checkUnqualified);
|
|
|
|
// use host qml import path as a sane default if nothing else has been provided
|
2019-11-11 17:18:04 +00:00
|
|
|
QStringList qmltypeDirs = parser.isSet(qmltypesDirsOption)
|
|
|
|
? parser.values(qmltypesDirsOption)
|
|
|
|
# ifndef QT_BOOTSTRAPPED
|
|
|
|
: QStringList { QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath),
|
|
|
|
QLatin1String(".") };
|
|
|
|
# else
|
|
|
|
: QStringList { QLatin1String(".") };
|
|
|
|
# endif
|
2017-01-23 08:01:22 +00:00
|
|
|
#else
|
|
|
|
bool silent = false;
|
2019-06-14 12:21:25 +00:00
|
|
|
bool warnUnqualified = false;
|
|
|
|
QStringList qmltypeDirs {};
|
2017-01-23 08:01:22 +00:00
|
|
|
#endif
|
2014-07-09 12:28:23 +00:00
|
|
|
bool success = true;
|
2017-01-23 08:01:22 +00:00
|
|
|
#if QT_CONFIG(commandlineparser)
|
2016-08-11 10:37:27 +00:00
|
|
|
for (const QString &filename : positionalArguments)
|
2017-01-23 08:01:22 +00:00
|
|
|
#else
|
2017-01-25 23:34:21 +00:00
|
|
|
const auto arguments = app.arguments();
|
|
|
|
for (const QString &filename : arguments)
|
2017-01-23 08:01:22 +00:00
|
|
|
#endif
|
2019-06-14 12:21:25 +00:00
|
|
|
success &= lint_file(filename, silent, warnUnqualified, qmltypeDirs);
|
2014-07-09 12:28:23 +00:00
|
|
|
|
|
|
|
return success ? 0 : -1;
|
|
|
|
}
|