2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-12-13 15:10:46 +00:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QFile>
|
2020-06-18 08:25:30 +00:00
|
|
|
#include <QTextStream>
|
2019-12-13 15:10:46 +00:00
|
|
|
|
|
|
|
#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>
|
2021-04-19 14:43:21 +00:00
|
|
|
#include <QtQmlDom/private/qqmldomitem_p.h>
|
|
|
|
#include <QtQmlDom/private/qqmldomexternalitems_p.h>
|
|
|
|
#include <QtQmlDom/private/qqmldomtop_p.h>
|
|
|
|
#include <QtQmlDom/private/qqmldomoutwriter_p.h>
|
2024-11-08 15:57:22 +00:00
|
|
|
#include <QtQmlDom/private/qqmldomlinewriterfactory_p.h>
|
2019-12-13 15:10:46 +00:00
|
|
|
|
|
|
|
#if QT_CONFIG(commandlineparser)
|
2021-04-19 14:43:21 +00:00
|
|
|
# include <QCommandLineParser>
|
2019-12-13 15:10:46 +00:00
|
|
|
#endif
|
|
|
|
|
2023-01-24 09:45:21 +00:00
|
|
|
#include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h>
|
2024-09-13 17:18:28 +00:00
|
|
|
#include <QtQmlFormat/private/qqmlformatsettings_p.h>
|
|
|
|
#include <QtQmlFormat/private/qqmlformatoptions_p.h>
|
2023-01-24 09:45:21 +00:00
|
|
|
|
2021-04-19 14:43:21 +00:00
|
|
|
using namespace QQmlJS::Dom;
|
2019-12-13 15:10:46 +00:00
|
|
|
|
2023-12-05 09:38:43 +00:00
|
|
|
static void logParsingErrors(const DomItem &fileItem, const QString &filename)
|
|
|
|
{
|
|
|
|
fileItem.iterateErrors(
|
|
|
|
[](const DomItem &, const ErrorMessage &msg) {
|
|
|
|
errorToQDebug(msg);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
true);
|
|
|
|
qWarning().noquote() << "Failed to parse" << filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// refactor this workaround. ExternalOWningItem is not recognized as an owning type
|
|
|
|
// in ownerAs.
|
|
|
|
static std::shared_ptr<ExternalOwningItem> getFileItemOwner(const DomItem &fileItem)
|
|
|
|
{
|
|
|
|
std::shared_ptr<ExternalOwningItem> filePtr = nullptr;
|
|
|
|
switch (fileItem.internalKind()) {
|
|
|
|
case DomType::JsFile:
|
|
|
|
filePtr = fileItem.ownerAs<JsFile>();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
filePtr = fileItem.ownerAs<QmlFile>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return filePtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO refactor
|
|
|
|
// Introduce better encapsulation and separation of concerns and move to DOM API
|
|
|
|
// returns a DomItem corresponding to the loaded file and bool indicating the validity of the file
|
|
|
|
static std::pair<DomItem, bool> parse(const QString &filename)
|
|
|
|
{
|
|
|
|
auto envPtr =
|
|
|
|
DomEnvironment::create(QStringList(),
|
|
|
|
QQmlJS::Dom::DomEnvironment::Option::SingleThreaded
|
|
|
|
| QQmlJS::Dom::DomEnvironment::Option::NoDependencies);
|
|
|
|
// placeholder for a node
|
|
|
|
// containing metadata (ExternalItemInfo) about the loaded file
|
|
|
|
DomItem fMetadataItem;
|
2024-01-24 13:39:30 +00:00
|
|
|
envPtr->loadFile(FileToLoad::fromFileSystem(envPtr, filename),
|
|
|
|
// callback called when everything is loaded that receives the
|
|
|
|
// loaded external file pair (path, oldValue, newValue)
|
|
|
|
[&fMetadataItem](Path, const DomItem &, const DomItem &extItemInfo) {
|
|
|
|
fMetadataItem = extItemInfo;
|
|
|
|
});
|
2023-12-05 09:38:43 +00:00
|
|
|
auto fItem = fMetadataItem.fileObject();
|
|
|
|
auto filePtr = getFileItemOwner(fItem);
|
|
|
|
return { fItem, filePtr && filePtr->isValid() };
|
|
|
|
}
|
|
|
|
|
2024-09-13 17:18:28 +00:00
|
|
|
static bool parseFile(const QString &filename, const QQmlFormatOptions &options)
|
2023-12-05 09:38:43 +00:00
|
|
|
{
|
|
|
|
const auto [fileItem, validFile] = parse(filename);
|
|
|
|
if (!validFile) {
|
|
|
|
logParsingErrors(fileItem, filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn AST back into source code
|
2024-09-13 17:18:28 +00:00
|
|
|
if (options.isVerbose())
|
2023-12-05 09:38:43 +00:00
|
|
|
qWarning().noquote() << "Dumping" << filename;
|
|
|
|
|
2024-01-29 12:50:32 +00:00
|
|
|
const auto &code = getFileItemOwner(fileItem)->code();
|
2024-09-13 17:18:28 +00:00
|
|
|
auto lwOptions = options.optionsForCode(code);
|
2023-12-05 09:38:43 +00:00
|
|
|
WriteOutChecks checks = WriteOutCheck::Default;
|
|
|
|
//Disable writeOutChecks for some usecases
|
2024-12-18 14:15:50 +00:00
|
|
|
if (options.sortImports() || options.forceEnabled() || options.isMaxColumnWidthSet() || code.size() > 32000
|
2024-11-08 15:57:22 +00:00
|
|
|
|| fileItem.internalKind() == DomType::JsFile) {
|
2023-12-05 09:38:43 +00:00
|
|
|
checks = WriteOutCheck::None;
|
|
|
|
}
|
2022-11-06 20:40:50 +00:00
|
|
|
|
QQmlJS::Dom::OutWriter. Refactoring
The refactoring consists of:
- Changing writeOut & writeOutForFile API
to return boolean instead of MutableDomItem, which better reflects
the existing usecases improving consistency of the data model*
Moreover, previous API was exposing DomItem, which was not "committed
to base" (MutableDomItem.commitToBase()), meaning it was exposing the
"unmerged" Item alongside with the "temporary environment"
- Refactoring & renaming OutWriter::updatedFile
breaking it into smaller chunks preserving
only necessary functionality
- Adding some comments / documentation
Before this commit, the writeOut API was "exposing",so called,
"updatedFile", which is basically the copy of the original fileItem +
renewed scriptExpressions which were modified during the writeOut of
the original fileItem.
The idea behind the "mutating" Dom API is that one has to create a
MutableDomItem, do some changes to it and then "commit" them.
This process is also facilitated by the creation of separate Env.
(git analogy might be handy here:
We create a separate branch, where all the mutation will happen and then
we "merge" this branch)
However, in the writeOutForFile usecase this "updatedFile" was needed
only for the verifying of the consistency of the "writtenOut" DOM,
however the API was exposing it further back to the caller sites,
without "committing".
The potential issue here is inconsistency of the data Model.
On one side we have an original File Item owned by the Base Env,
on the other side we have an "updatedFile" which is owned by another Env.
Taking into account that there are no usecases requiring "exposing"
"updatedFile", but also no need for "committing" the changes,
It's arguably better to keep that temporary "updatedFile" locally,
not exposing it outside the writeOutForFile function. Thereby improving
consistency of the data model.
Change-Id: If45eca4b4d6d703e2a76d0580f124d0292af5ed8
Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
2023-12-05 09:42:23 +00:00
|
|
|
bool res = false;
|
2024-09-13 17:18:28 +00:00
|
|
|
if (options.isInplace()) {
|
|
|
|
if (options.isVerbose())
|
2021-04-19 14:43:21 +00:00
|
|
|
qWarning().noquote() << "Writing to file" << filename;
|
|
|
|
FileWriter fw;
|
2022-10-19 10:50:31 +00:00
|
|
|
const unsigned numberOfBackupFiles = 0;
|
2023-12-05 09:38:43 +00:00
|
|
|
res = fileItem.writeOut(filename, numberOfBackupFiles, lwOptions, &fw, checks);
|
2021-04-19 14:43:21 +00:00
|
|
|
} else {
|
|
|
|
QFile out;
|
2024-03-23 12:44:46 +00:00
|
|
|
if (out.open(stdout, QIODevice::WriteOnly)) {
|
2024-11-08 15:57:22 +00:00
|
|
|
auto lw = createLineWriter([&out](QStringView s) { out.write(s.toUtf8()); }, filename,
|
|
|
|
lwOptions);
|
|
|
|
OutWriter ow(*lw);
|
2024-03-23 12:44:46 +00:00
|
|
|
res = fileItem.writeOutForFile(ow, checks);
|
|
|
|
ow.flush();
|
|
|
|
} else {
|
|
|
|
res = false;
|
|
|
|
}
|
2021-04-19 14:43:21 +00:00
|
|
|
}
|
QQmlJS::Dom::OutWriter. Refactoring
The refactoring consists of:
- Changing writeOut & writeOutForFile API
to return boolean instead of MutableDomItem, which better reflects
the existing usecases improving consistency of the data model*
Moreover, previous API was exposing DomItem, which was not "committed
to base" (MutableDomItem.commitToBase()), meaning it was exposing the
"unmerged" Item alongside with the "temporary environment"
- Refactoring & renaming OutWriter::updatedFile
breaking it into smaller chunks preserving
only necessary functionality
- Adding some comments / documentation
Before this commit, the writeOut API was "exposing",so called,
"updatedFile", which is basically the copy of the original fileItem +
renewed scriptExpressions which were modified during the writeOut of
the original fileItem.
The idea behind the "mutating" Dom API is that one has to create a
MutableDomItem, do some changes to it and then "commit" them.
This process is also facilitated by the creation of separate Env.
(git analogy might be handy here:
We create a separate branch, where all the mutation will happen and then
we "merge" this branch)
However, in the writeOutForFile usecase this "updatedFile" was needed
only for the verifying of the consistency of the "writtenOut" DOM,
however the API was exposing it further back to the caller sites,
without "committing".
The potential issue here is inconsistency of the data Model.
On one side we have an original File Item owned by the Base Env,
on the other side we have an "updatedFile" which is owned by another Env.
Taking into account that there are no usecases requiring "exposing"
"updatedFile", but also no need for "committing" the changes,
It's arguably better to keep that temporary "updatedFile" locally,
not exposing it outside the writeOutForFile function. Thereby improving
consistency of the data model.
Change-Id: If45eca4b4d6d703e2a76d0580f124d0292af5ed8
Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
2023-12-05 09:42:23 +00:00
|
|
|
return res;
|
2019-12-13 15:10:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 22:07:04 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
QCoreApplication::setApplicationName("qmlformat");
|
2021-06-22 08:19:46 +00:00
|
|
|
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
|
2021-02-07 22:07:04 +00:00
|
|
|
|
2024-09-13 17:18:28 +00:00
|
|
|
QQmlFormatSettings settings(QLatin1String("qmlformat"));
|
2025-01-27 11:06:07 +00:00
|
|
|
const auto &options = QQmlFormatOptions::buildCommandLineOptions(app.arguments());
|
2024-09-13 17:18:28 +00:00
|
|
|
if (!options.isValid()) {
|
|
|
|
for (const auto &error : options.errors()) {
|
2021-02-07 22:07:04 +00:00
|
|
|
qWarning().noquote() << error;
|
|
|
|
}
|
2020-09-02 10:34:29 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2025-08-11 20:28:47 +00:00
|
|
|
if (options.dryRun()) {
|
|
|
|
settings.reportConfigForFiles(options.arguments());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-13 17:18:28 +00:00
|
|
|
if (options.writeDefaultSettingsEnabled())
|
2021-10-25 16:30:26 +00:00
|
|
|
return settings.writeDefaults() ? 0 : -1;
|
|
|
|
|
2021-02-07 22:07:04 +00:00
|
|
|
bool success = true;
|
2024-09-13 17:18:28 +00:00
|
|
|
if (!options.files().isEmpty()) {
|
|
|
|
if (!options.arguments().isEmpty())
|
2020-06-18 08:25:30 +00:00
|
|
|
qWarning() << "Warning: Positional arguments are ignored when -F is used";
|
|
|
|
|
2024-09-13 17:18:28 +00:00
|
|
|
for (const QString &file : options.files()) {
|
2021-02-07 22:07:04 +00:00
|
|
|
Q_ASSERT(!file.isEmpty());
|
2020-06-18 08:25:30 +00:00
|
|
|
|
2025-01-27 11:06:07 +00:00
|
|
|
if (!parseFile(file, options.optionsForFile(file, &settings)))
|
2020-06-18 08:25:30 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-13 17:18:28 +00:00
|
|
|
for (const QString &file : options.arguments()) {
|
2025-01-27 11:06:07 +00:00
|
|
|
if (!parseFile(file, options.optionsForFile(file, &settings)))
|
2020-06-18 08:25:30 +00:00
|
|
|
success = false;
|
|
|
|
}
|
2019-12-13 15:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success ? 0 : 1;
|
|
|
|
}
|