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
qqmljslinter.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
7
8#include <private/qqmljsimporter_p.h>
9#include <private/qqmljsimportvisitor_p.h>
10#include <private/qqmljslinterpasses_p.h>
11#include <private/qqmljslintervisitor_p.h>
12#include <private/qqmljsliteralbindingcheck_p.h>
13#include <private/qqmljsloggingutils_p.h>
14#include <private/qqmljsutils_p.h>
15#include <private/qqmlsa_p.h>
16
17#include <QtCore/qjsonobject.h>
18#include <QtCore/qfileinfo.h>
19#include <QtCore/qloggingcategory.h>
20#include <QtCore/qpluginloader.h>
21#include <QtCore/qlibraryinfo.h>
22#include <QtCore/qdir.h>
23#include <QtCore/private/qduplicatetracker_p.h>
24#include <QtCore/qscopedpointer.h>
25
26
27#if QT_CONFIG(library)
28# include <QtCore/qdiriterator.h>
29# include <QtCore/qlibrary.h>
30#endif
31
32#if QT_CONFIG(qmlcontextpropertydump)
33# include <QtCore/qsettings.h>
34#endif
35
36#include <QtQml/private/qqmljslexer_p.h>
37#include <QtQml/private/qqmljsparser_p.h>
38#include <QtQml/private/qqmljsengine_p.h>
39#include <QtQml/private/qqmljsastvisitor_p.h>
40#include <QtQml/private/qqmljsast_p.h>
41#include <QtQml/private/qqmljsdiagnosticmessage_p.h>
42
43
45
46using namespace Qt::StringLiterals;
47
48class HasFunctionDefinitionVisitor final : public QQmlJS::AST::Visitor
49{
50public:
51 bool visit(QQmlJS::AST::FunctionDeclaration *functionDeclaration) override
52 {
53 m_result = !functionDeclaration->name.isEmpty();
54 return false;
55 }
56
58 bool result() const { return m_result; }
59 void reset() { m_result = false; }
60
61private:
62 bool m_result = false;
63};
64
65class UnreachableVisitor final : public QQmlJS::AST::Visitor
66{
67public:
68 UnreachableVisitor(QQmlJSLogger *logger) : m_logger(logger) { }
69
70 bool containsFunctionDeclaration(QQmlJS::AST::Node *node)
71 {
72 m_hasFunctionDefinition.reset();
73 node->accept(&m_hasFunctionDefinition);
74 return m_hasFunctionDefinition.result();
75 }
76
77 bool visit(QQmlJS::AST::StatementList *unreachable) override
78 {
79 QQmlJS::SourceLocation location;
80 auto report = [this, &location]() {
81 if (location.isValid()) {
82 m_logger->log(u"Unreachable code"_s, qmlUnreachableCode, location);
83 }
84 location = QQmlJS::SourceLocation{};
85 };
86
87 for (auto it = unreachable; it && it->statement; it = it->next) {
88 if (containsFunctionDeclaration(it->statement)) {
89 report();
90 continue; // don't warn about the location of the function declaration
91 }
92 location = combine(location,
93 combine(it->statement->firstSourceLocation(),
94 it->statement->lastSourceLocation()));
95 }
96 report();
97 return false;
98 }
100
101private:
102 QQmlJSLogger *m_logger = nullptr;
103 HasFunctionDefinitionVisitor m_hasFunctionDefinition;
104};
105
106class CodegenWarningInterface final : public QV4::Compiler::CodegenWarningInterface
107{
108public:
110 {
111 }
112
113 void reportVarUsedBeforeDeclaration(const QString &name, const QString &fileName,
114 QQmlJS::SourceLocation declarationLocation,
115 QQmlJS::SourceLocation accessLocation) override
116 {
117 Q_UNUSED(fileName)
118
119 m_logger->log("Identifier '%1' is used here before its declaration."_L1.arg(name),
120 qmlVarUsedBeforeDeclaration, accessLocation);
121 m_logger->log("Note: declaration of '%1' here"_L1.arg(name), qmlVarUsedBeforeDeclaration,
122 declarationLocation, true, true, {}, accessLocation.startLine);
123 }
124
125 void reportFunctionUsedBeforeDeclaration(const QString &name, const QString &fileName,
126 QQmlJS::SourceLocation declarationLocation,
127 QQmlJS::SourceLocation accessLocation) override
128 {
129 Q_UNUSED(fileName)
130
131 m_logger->log("Function '%1' is used here before its declaration."_L1.arg(name),
132 qmlFunctionUsedBeforeDeclaration, accessLocation);
133 m_logger->log("Note: declaration of '%1' here"_L1.arg(name),
134 qmlFunctionUsedBeforeDeclaration, declarationLocation);
135 }
136
137 UnreachableVisitor *unreachableVisitor() override { return &m_unreachableVisitor; }
138
139private:
140 QQmlJSLogger *m_logger;
141 UnreachableVisitor m_unreachableVisitor;
142};
143
144QQmlJSLinter::QQmlJSLinter(const QStringList &importPaths, const QStringList &extraPluginPaths,
145 bool useAbsolutePath)
146 : m_useAbsolutePath(useAbsolutePath),
147 m_enablePlugins(true),
148 m_importer(importPaths, nullptr,
151{
152 m_plugins = loadPlugins(extraPluginPaths);
153}
154
161 , m_instance(std::move(plugin.m_instance))
163 , m_isInternal(std::move(plugin.m_isInternal))
164 , m_isValid(std::move(plugin.m_isValid))
165{
166 // Mark the old Plugin as invalid and make sure it doesn't delete the loader
167 Q_ASSERT(!plugin.m_loader);
168 plugin.m_instance = nullptr;
169 plugin.m_isValid = false;
170}
171
172#if QT_CONFIG(library)
173QQmlJSLinter::Plugin::Plugin(QString path)
174{
175 m_loader = std::make_unique<QPluginLoader>(path);
176 if (!parseMetaData(m_loader->metaData(), path))
177 return;
178
179 QObject *object = m_loader->instance();
180 if (!object)
181 return;
182
183 m_instance = qobject_cast<QQmlSA::LintPlugin *>(object);
184 if (!m_instance)
185 return;
186
187 m_isValid = true;
188}
189#endif
190
191QQmlJSLinter::Plugin::Plugin(const QStaticPlugin &staticPlugin)
192{
193 if (!parseMetaData(staticPlugin.metaData(), u"built-in"_s))
194 return;
195
196 m_instance = qobject_cast<QQmlSA::LintPlugin *>(staticPlugin.instance());
197 if (!m_instance)
198 return;
199
200 m_isValid = true;
201}
202
204{
205#if QT_CONFIG(library)
206 if (m_loader != nullptr) {
207 m_loader->unload();
208 m_loader->deleteLater();
209 }
210#endif
211}
212
213bool QQmlJSLinter::Plugin::parseMetaData(const QJsonObject &metaData, QString pluginName)
214{
215 const QString pluginIID = QStringLiteral(QmlLintPluginInterface_iid);
216
217 if (metaData[u"IID"].toString() != pluginIID)
218 return false;
219
220 QJsonObject pluginMetaData = metaData[u"MetaData"].toObject();
221
222 for (const QString &requiredKey :
223 { u"name"_s, u"version"_s, u"author"_s, u"loggingCategories"_s }) {
224 if (!pluginMetaData.contains(requiredKey)) {
225 qWarning() << pluginName << "is missing the required " << requiredKey
226 << "metadata, skipping";
227 return false;
228 }
229 }
230
231 m_name = pluginMetaData[u"name"].toString();
232 m_author = pluginMetaData[u"author"].toString();
233 m_version = pluginMetaData[u"version"].toString();
234 m_description = pluginMetaData[u"description"].toString(u"-/-"_s);
235 m_isInternal = pluginMetaData[u"isInternal"].toBool(false);
236
237 if (!pluginMetaData[u"loggingCategories"].isArray()) {
238 qWarning() << pluginName << "has loggingCategories which are not an array, skipping";
239 return false;
240 }
241
242 const QJsonArray categories = pluginMetaData[u"loggingCategories"].toArray();
243 for (const QJsonValue &value : categories) {
244 if (!value.isObject()) {
245 qWarning() << pluginName << "has invalid loggingCategories entries, skipping";
246 return false;
247 }
248
249 const QJsonObject object = value.toObject();
250
251 for (const QString &requiredKey : { u"name"_s, u"description"_s }) {
252 if (!object.contains(requiredKey)) {
253 qWarning() << pluginName << " logging category is missing the required "
254 << requiredKey << "metadata, skipping";
255 return false;
256 }
257 }
258
259 const QString prefix = (m_isInternal ? u""_s : u"Plugin."_s).append(m_name).append(u'.');
260 const QString categoryId =
261 prefix + object[u"name"].toString();
262 const auto settingsNameIt = object.constFind(u"settingsName");
263 const QString settingsName = (settingsNameIt == object.constEnd())
264 ? categoryId
265 : prefix + settingsNameIt->toString(categoryId);
266 m_categories << QQmlJS::LoggerCategory{ categoryId, settingsName,
267 object["description"_L1].toString(),
268 QQmlJS::WarningSeverity::Warning };
269 const auto itSeverity = object.find("defaultSeverity"_L1);
270 if (itSeverity == object.end())
271 continue;
272
273 const QString severityName = itSeverity->toString();
274 const auto severity = QQmlJS::LoggingUtils::severityFromString(severityName);
275 if (!severity.has_value()) {
276 qWarning() << "Invalid logging severity" << severityName << "provided for"
277 << m_categories.last().id().name().toString()
278 << "(allowed are: disable, info, warning, error) found in plugin metadata.";
279 continue;
280 }
281
282 m_categories.last().setSeverity(severity.value());
283 }
284
285 return true;
286}
287
288std::vector<QQmlJSLinter::Plugin> QQmlJSLinter::loadPlugins(QStringList extraPluginPaths)
289{
290 std::vector<Plugin> plugins;
291
292 QDuplicateTracker<QString> seenPlugins;
293
294 const auto &staticPlugins = QPluginLoader::staticPlugins();
295 for (const QStaticPlugin &staticPlugin : staticPlugins) {
296 Plugin plugin(staticPlugin);
297 if (!plugin.isValid())
298 continue;
299
300 if (seenPlugins.hasSeen(plugin.name().toLower())) {
301 qWarning() << "Two plugins named" << plugin.name()
302 << "present, make sure no plugins are duplicated. The second plugin will "
303 "not be loaded.";
304 continue;
305 }
306
307 plugins.push_back(std::move(plugin));
308 }
309
310#if QT_CONFIG(library)
311 const QStringList paths = [&extraPluginPaths]() {
312 QStringList result{ extraPluginPaths };
313 const QStringList libraryPaths = QCoreApplication::libraryPaths();
314 for (const auto &path : libraryPaths) {
315 result.append(path + u"/qmllint"_s);
316 }
317 return result;
318 }();
319 for (const QString &pluginDir : paths) {
320 QDirIterator it{ pluginDir, QDir::Files };
321
322 while (it.hasNext()) {
323 auto potentialPlugin = it.next();
324
325 if (!QLibrary::isLibrary(potentialPlugin))
326 continue;
327
328 Plugin plugin(potentialPlugin);
329
330 if (!plugin.isValid())
331 continue;
332
333 if (seenPlugins.hasSeen(plugin.name().toLower())) {
334 qWarning() << "Two plugins named" << plugin.name()
335 << "present, make sure no plugins are duplicated. The second plugin "
336 "will not be loaded.";
337 continue;
338 }
339
340 plugins.push_back(std::move(plugin));
341 }
342 }
343#endif
344 Q_UNUSED(extraPluginPaths)
345 return plugins;
346}
347
348void QQmlJSLinter::parseComments(QQmlJSLogger *logger,
349 const QList<QQmlJS::SourceLocation> &comments)
350{
351 QHash<int, QSet<QString>> disablesPerLine;
352 QHash<int, QSet<QString>> enablesPerLine;
353 QHash<int, QSet<QString>> oneLineDisablesPerLine;
354
355 struct PostponedWarning
356 {
357 QString message;
358 QQmlSA::LoggerWarningId category;
359 QQmlJS::SourceLocation location;
360 };
361
362 std::vector<PostponedWarning> postponedWarnings;
363 auto guard = qScopeGuard([&postponedWarnings, &logger]() {
364 // only log messages after processing the logger->ignoreWarnings() calls, so that the
365 // qmlInvalidLintDirective warnings can be disabled if needed.
366 for (const auto &warning : postponedWarnings)
367 logger->log(warning.message, warning.category, warning.location);
368 });
369
370 const QString code = logger->code();
371 const QStringList lines = code.split(u'\n');
372 const auto loggerCategories = logger->categories();
373
374 for (const auto &loc : comments) {
375 const QString comment = code.mid(loc.offset, loc.length);
376 if (!comment.startsWith(u" qmllint ") && !comment.startsWith(u"qmllint "))
377 continue;
378
379 QStringList words = comment.split(u' ', Qt::SkipEmptyParts);
380 if (words.size() < 2)
381 continue;
382
383 QSet<QString> categories;
384 for (qsizetype i = 2; i < words.size(); i++) {
385 const QString category = words.at(i);
386 const auto categoryExists = std::any_of(
387 loggerCategories.cbegin(), loggerCategories.cend(),
388 [&](const QQmlJS::LoggerCategory &cat) { return cat.id().name() == category; });
389
390 if (categoryExists)
391 categories << category;
392 else {
393 postponedWarnings.push_back(
394 { u"qmllint directive on unknown category \"%1\""_s.arg(category),
395 qmlInvalidLintDirective, loc });
396 }
397 }
398
399 if (words.size() == 2) {
400 const auto &loggerCategories = logger->categories();
401 for (const auto &option : loggerCategories)
402 categories << option.id().name().toString();
403 }
404
405 const QString command = words.at(1);
406 if (command == u"disable"_s) {
407 if (const qsizetype lineIndex = loc.startLine - 1; lineIndex < lines.size()) {
408 const QString line = lines[lineIndex];
409 const QString preComment = line.left(line.indexOf(comment) - 2);
410
411 bool lineHasContent = false;
412 for (qsizetype i = 0; i < preComment.size(); i++) {
413 if (!preComment[i].isSpace()) {
414 lineHasContent = true;
415 break;
416 }
417 }
418
419 if (lineHasContent)
420 oneLineDisablesPerLine[loc.startLine] |= categories;
421 else
422 disablesPerLine[loc.startLine] |= categories;
423 }
424 } else if (command == u"enable"_s) {
425 enablesPerLine[loc.startLine + 1] |= categories;
426 } else {
427 postponedWarnings.push_back(
428 { u"Invalid qmllint directive \"%1\" provided"_s.arg(command),
429 qmlInvalidLintDirective, loc });
430 }
431 }
432
433 if (disablesPerLine.isEmpty() && oneLineDisablesPerLine.isEmpty())
434 return;
435
436 QSet<QString> currentlyDisabled;
437 for (qsizetype i = 1; i <= lines.size(); i++) {
438 currentlyDisabled.unite(disablesPerLine[i]).subtract(enablesPerLine[i]);
439
440 currentlyDisabled.unite(oneLineDisablesPerLine[i]);
441
442 if (!currentlyDisabled.isEmpty())
443 logger->ignoreWarnings(i, currentlyDisabled);
444
445 currentlyDisabled.subtract(oneLineDisablesPerLine[i]);
446 }
447}
448
449static void addJsonWarning(QJsonArray &warnings, const QQmlJS::DiagnosticMessage &message,
450 QAnyStringView id, const std::optional<QQmlJSFixSuggestion> &suggestion = {})
451{
452 QJsonObject jsonMessage;
453
454 QString type;
455 switch (message.type) {
456 case QtDebugMsg:
457 type = u"debug"_s;
458 break;
459 case QtWarningMsg:
460 type = u"warning"_s;
461 break;
462 case QtCriticalMsg:
463 type = u"critical"_s;
464 break;
465 case QtFatalMsg:
466 type = u"fatal"_s;
467 break;
468 case QtInfoMsg:
469 type = u"info"_s;
470 break;
471 default:
472 type = u"unknown"_s;
473 break;
474 }
475
476 jsonMessage[u"type"_s] = type;
477 jsonMessage[u"id"_s] = id.toString();
478
479 const auto convertLocation = [](const QQmlJS::SourceLocation &source, QJsonObject *target) {
480 target->insert("line"_L1, int(source.startLine));
481 target->insert("column"_L1, int(source.startColumn));
482 target->insert("charOffset"_L1, int(source.offset));
483 target->insert("length"_L1, int(source.length));
484 };
485
486 if (message.loc.isValid())
487 convertLocation(message.loc, &jsonMessage);
488
489 jsonMessage[u"message"_s] = message.message;
490
491 QJsonArray suggestions;
492 if (suggestion.has_value()) {
493 QJsonObject jsonFix {
494 { "message"_L1, suggestion->description() },
495 { "replacement"_L1, suggestion->replacement() },
496 { "isAutoApplicable"_L1, suggestion->isAutoApplicable() }
497 };
498 convertLocation(suggestion->location(), &jsonFix);
499 const QString filename = suggestion->filename();
500 if (!filename.isEmpty())
501 jsonFix.insert("fileName"_L1, filename);
502 suggestions << jsonFix;
503 }
504 jsonMessage[u"suggestions"] = suggestions;
505
506 warnings << jsonMessage;
507}
508
509void QQmlJSLinter::processMessages(QJsonArray &warnings)
510{
511 m_logger->iterateAllMessages([&](const Message &message) {
512 addJsonWarning(warnings, message, message.id, message.fixSuggestion);
513 });
514}
515
516ContextPropertyInfo QQmlJSLinter::contextPropertiesFor(
517 const QString &filename, QQmlJSResourceFileMapper *mapper,
518 const QQmlJS::HeuristicContextProperties &heuristicContextProperties)
519{
520 ContextPropertyInfo result;
521 if (m_userContextPropertySettings.search(filename).isValid()) {
522 result.userContextProperties =
523 QQmlJS::UserContextProperties{ m_userContextPropertySettings };
524 }
525
526 if (heuristicContextProperties.isValid()) {
527 result.heuristicContextProperties = heuristicContextProperties;
528 return result;
529 }
530
531#if QT_CONFIG(qmlcontextpropertydump)
532 const QString buildPath = QQmlJSUtils::qmlBuildPathFromSourcePath(mapper, filename);
533 if (const auto searchResult = m_heuristicContextPropertySearcher.search(buildPath);
534 searchResult.isValid()) {
535 QSettings settings(searchResult.iniFilePath, QSettings::IniFormat);
536 result.heuristicContextProperties =
537 QQmlJS::HeuristicContextProperties::collectFrom(&settings);
538 }
539#else
540 Q_UNUSED(mapper);
541#endif
542 return result;
543}
544
546QQmlJSLinter::lintFile(const QString &filename, const QString *fileContents, const bool silent,
547 QJsonArray *json, const QStringList &qmlImportPaths,
548 const QStringList &qmldirFiles, const QStringList &resourceFiles,
549 const QList<QQmlJS::LoggerCategory> &categories,
550 const QQmlJS::HeuristicContextProperties &heuristicContextProperties)
551{
552 const LintResult lintResult =
553 lintFileImpl(filename, fileContents, silent, json, qmlImportPaths, qmldirFiles,
554 resourceFiles, categories, heuristicContextProperties);
555 if (!json)
556 return lintResult;
557
558 QJsonArray warnings;
559 processMessages(warnings);
560
561 QJsonObject result;
562 result[u"filename"_s] = QFileInfo(filename).absoluteFilePath();
563 result[u"warnings"] = warnings;
564 result[u"success"] = lintResult == LintSuccess;
565
566 json->append(result);
567 return lintResult;
568}
569
570void QQmlJSLinter::setupLoggingCategoriesInLogger(const QList<QQmlJS::LoggerCategory> &categories)
571{
572 if (m_enablePlugins) {
573 for (const Plugin &plugin : m_plugins) {
574 for (const QQmlJS::LoggerCategory &category : plugin.categories())
575 m_logger->registerCategory(category);
576 }
577 }
578
579 for (auto it = categories.cbegin(); it != categories.cend(); ++it) {
580 if (auto logger = *it; !QQmlJS::LoggerCategoryPrivate::get(&logger)->hasChanged())
581 continue;
582
583 m_logger->setCategorySeverity(it->id(), it->severity());
584 }
585}
586
588QQmlJSLinter::lintFileImpl(const QString &filename, const QString *fileContents, const bool silent,
589 QJsonArray *json, const QStringList &qmlImportPaths,
590 const QStringList &qmldirFiles, const QStringList &resourceFiles,
591 const QList<QQmlJS::LoggerCategory> &categories,
592 const QQmlJS::HeuristicContextProperties &heuristicContextProperties)
593{
594 QString code;
595
596 QFileInfo info(filename);
597 const QString lowerSuffix = info.suffix().toLower();
598 const bool isESModule = lowerSuffix == QLatin1String("mjs");
599 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
600
601 m_logger.reset(new QQmlJSLogger);
602 m_logger->setFilePath(m_useAbsolutePath ? info.absoluteFilePath() : filename);
603 m_logger->setSilent(silent || json);
604 setupLoggingCategoriesInLogger(categories);
605
606 if (fileContents == nullptr) {
607 QFile file(filename);
608 if (!file.open(QFile::ReadOnly)) {
609 m_logger->log("Failed to open file %1: %2"_L1.arg(filename, file.errorString()),
610 qmlImport, QQmlJS::SourceLocation());
611 return FailedToOpen;
612 }
613
614 code = QString::fromUtf8(file.readAll());
615 file.close();
616 } else {
617 code = *fileContents;
618 }
619
620 m_fileContents = code;
621 m_logger->setCode(code);
622
623 QQmlJS::Engine engine;
624 QQmlJS::Lexer lexer(&engine);
625
626 lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
627 QQmlJS::Parser parser(&engine);
628
629 const bool parseSuccess = isJavaScript
630 ? (isESModule ? parser.parseModule() : parser.parseProgram())
631 : parser.parse();
632 const auto diagnosticMessages = parser.diagnosticMessages();
633 for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages)
634 m_logger->log(m.message, qmlSyntax, m.loc);
635
636 if (!parseSuccess)
637 return FailedToParse;
638
639 if (isJavaScript)
640 return LintSuccess;
641
642 m_importer.setImportPaths(qmlImportPaths);
643
644 std::optional<QQmlJSResourceFileMapper> mapper;
645 if (!resourceFiles.isEmpty())
646 mapper.emplace(resourceFiles);
647 m_importer.setResourceFileMapper(mapper.has_value() ? &*mapper : nullptr);
648
649 QQmlJS::LinterVisitor v{ &m_importer, m_logger.get(),
650 QQmlJSImportVisitor::implicitImportDirectory(
651 m_logger->filePath(), m_importer.resourceFileMapper()),
652 qmldirFiles, &engine };
653
654 parseComments(m_logger.get(), engine.comments());
655
656 QQmlJSTypeResolver typeResolver(&m_importer);
657
658 // Type resolving is using document parent mode here so that it produces fewer false
659 // positives on the "parent" property of QQuickItem. It does produce a few false
660 // negatives this way because items can be reparented. Furthermore, even if items
661 // are not reparented, the document parent may indeed not be their visual parent.
662 // See QTBUG-95530. Eventually, we'll need cleverer logic to deal with this.
663 typeResolver.setParentMode(QQmlJSTypeResolver::UseDocumentParent);
664 // We don't need to create tracked types and such as we are just linting the code
665 // here and not actually compiling it. The duplicated scopes would cause issues
666 // during linting.
667 typeResolver.setCloneMode(QQmlJSTypeResolver::DoNotCloneTypes);
668
669 typeResolver.init(&v, parser.rootNode());
670
671 const QStringList resourcePaths = mapper
672 ? mapper->resourcePaths(QQmlJSResourceFileMapper::localFileFilter(filename))
673 : QStringList();
674 const QString resolvedPath =
675 (resourcePaths.size() == 1) ? u':' + resourcePaths.first() : filename;
676
677 QQmlJSLinterCodegen codegen{ &m_importer, resolvedPath, qmldirFiles, m_logger.get(),
678 contextPropertiesFor(filename, mapper ? &*mapper : nullptr,
679 heuristicContextProperties) };
680 codegen.setTypeResolver(std::move(typeResolver));
681 codegen.setScopesById(v.addressableScopes());
682 codegen.setRenamedComponents(&v.renamedComponents());
683
684 using PassManagerPtr =
685 std::unique_ptr<QQmlSA::PassManager,
686 decltype(&QQmlSA::PassManagerPrivate::deletePassManager)>;
687 PassManagerPtr passMan(
688 QQmlSA::PassManagerPrivate::createPassManager(&v, codegen.typeResolver()),
689 &QQmlSA::PassManagerPrivate::deletePassManager);
690 QQmlJSLinterPasses::registerDefaultPasses(passMan.get());
691
692 if (m_enablePlugins) {
693 for (const Plugin &plugin : m_plugins) {
694 if (!plugin.isValid() || !plugin.isEnabled())
695 continue;
696
697 QQmlSA::LintPlugin *instance = plugin.m_instance;
698 Q_ASSERT(instance);
699 instance->registerPasses(passMan.get(), QQmlJSScope::createQQmlSAElement(v.result()));
700 }
701 }
702 passMan->analyze(QQmlJSScope::createQQmlSAElement(v.result()));
703
704 if (m_logger->hasErrors())
705 return HasErrors;
706
707 // passMan now has a pointer to the moved from type resolver
708 // we fix this in setPassManager
709 codegen.setPassManager(passMan.get());
710
711 QQmlJSSaveFunction saveFunction = [](const QV4::CompiledData::SaveableUnitPointer &,
712 const QQmlJSAotFunctionMap &, QString *) { return true; };
713
714 QQmlJSCompileError error;
715
716 QLoggingCategory::setFilterRules(u"qt.qml.compiler=false"_s);
717
718 CodegenWarningInterface warningInterface(m_logger.get());
719 qCompileQmlFile(filename, saveFunction, &codegen, &error, true, &warningInterface,
720 fileContents);
721
722 QList<QQmlJS::DiagnosticMessage> globalWarnings = m_importer.takeGlobalWarnings();
723
724 if (!globalWarnings.isEmpty()) {
725 m_logger->log(QStringLiteral("Type warnings occurred while evaluating file:"), qmlImport,
726 QQmlJS::SourceLocation());
727 m_logger->processMessages(globalWarnings, qmlImport);
728 }
729
730 if (m_logger->hasErrors())
731 return HasErrors;
732 if (m_logger->hasWarnings())
733 return HasWarnings;
734
735 return LintSuccess;
736}
737
739 const QString &module, const bool silent, QJsonArray *json,
740 const QStringList &qmlImportPaths, const QStringList &resourceFiles)
741{
742 const LintResult lintResult = lintModuleImpl(module, silent, json, qmlImportPaths, resourceFiles);
743 if (!json)
744 return lintResult;
745
746 QJsonArray warnings;
747 processMessages(warnings);
748
749 QJsonObject result;
750 result[u"module"_s] = module;
751 result[u"warnings"] = warnings;
752 result[u"success"] = lintResult == LintSuccess;
753
754 json->append(result);
755 return lintResult;
756}
757
758QQmlJSLinter::LintResult QQmlJSLinter::lintModuleImpl(
759 const QString &module, const bool silent, QJsonArray *json,
760 const QStringList &qmlImportPaths, const QStringList &resourceFiles)
761{
762 // Make sure that we don't expose an old logger if we return before a new one is created.
763 m_logger.reset();
764
765 // We can't lint properly if a module has already been pre-cached
766 m_importer.clearCache();
767 m_importer.setImportPaths(qmlImportPaths);
768
769 QQmlJSResourceFileMapper mapper(resourceFiles);
770 if (!resourceFiles.isEmpty())
771 m_importer.setResourceFileMapper(&mapper);
772 else
773 m_importer.setResourceFileMapper(nullptr);
774
775 m_logger.reset(new QQmlJSLogger);
776 m_logger->setFilePath(module);
777 m_logger->setCode(u""_s);
778 m_logger->setSilent(silent || json);
779
780 const QQmlJSImporter::ImportedTypes types =
781 m_importer.importModule(module, QQmlJS::PrecedenceValues::Default);
782
783 QList<QQmlJS::DiagnosticMessage> importWarnings =
784 m_importer.takeGlobalWarnings() + types.warnings();
785
786 if (!importWarnings.isEmpty()) {
787 m_logger->log(QStringLiteral("Warnings occurred while importing module:"), qmlImport,
788 QQmlJS::SourceLocation());
789 m_logger->processMessages(importWarnings, qmlImport);
790 }
791
792 QMap<QString, QSet<QString>> missingTypes;
793 QMap<QString, QSet<QString>> partiallyResolvedTypes;
794
795 const QString modulePrefix = u"$module$."_s;
796 const QString internalPrefix = u"$internal$."_s;
797
798 for (auto &&[typeName, importedScope] : types.types().asKeyValueRange()) {
799 QString name = typeName;
800 const QQmlJSScope::ConstPtr scope = importedScope.scope;
801
802 if (name.startsWith(modulePrefix))
803 continue;
804
805 if (name.startsWith(internalPrefix)) {
806 name = name.mid(internalPrefix.size());
807 }
808
809 if (scope.isNull()) {
810 if (!missingTypes.contains(name))
811 missingTypes[name] = {};
812 continue;
813 }
814
815 if (!scope->isFullyResolved()) {
816 if (!partiallyResolvedTypes.contains(name))
817 partiallyResolvedTypes[name] = {};
818 }
819 const auto &ownProperties = scope->ownProperties();
820 for (const auto &property : ownProperties) {
821 if (property.typeName().isEmpty()) {
822 // If the type name is empty, then it's an intentional vaguery i.e. for some
823 // builtins
824 continue;
825 }
826 if (property.type().isNull()) {
827 missingTypes[property.typeName()]
828 << scope->internalName() + u'.' + property.propertyName();
829 continue;
830 }
831 if (!property.type()->isFullyResolved()) {
832 partiallyResolvedTypes[property.typeName()]
833 << scope->internalName() + u'.' + property.propertyName();
834 }
835 }
836 if (scope->attachedType() && !scope->attachedType()->isFullyResolved()) {
837 m_logger->log(u"Attached type of \"%1\" not fully resolved"_s.arg(name),
838 qmlUnresolvedType, scope->sourceLocation());
839 }
840
841 const auto &ownMethods = scope->ownMethods();
842 for (const auto &method : ownMethods) {
843 if (method.returnTypeName().isEmpty())
844 continue;
845 if (method.returnType().isNull()) {
846 missingTypes[method.returnTypeName()] << u"return type of "_s
847 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
848 } else if (!method.returnType()->isFullyResolved()) {
849 partiallyResolvedTypes[method.returnTypeName()] << u"return type of "_s
850 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
851 }
852
853 const auto parameters = method.parameters();
854 for (qsizetype i = 0; i < parameters.size(); i++) {
855 auto &parameter = parameters[i];
856 const QString typeName = parameter.typeName();
857 const QSharedPointer<const QQmlJSScope> type = parameter.type();
858 if (typeName.isEmpty())
859 continue;
860 if (type.isNull()) {
861 missingTypes[typeName] << u"parameter %1 of "_s.arg(i + 1)
862 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
863 continue;
864 }
865 if (!type->isFullyResolved()) {
866 partiallyResolvedTypes[typeName] << u"parameter %1 of "_s.arg(i + 1)
867 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
868 continue;
869 }
870 }
871 }
872 }
873
874 for (auto &&[name, uses] : missingTypes.asKeyValueRange()) {
875 QString message = u"Type \"%1\" not found"_s.arg(name);
876
877 if (!uses.isEmpty()) {
878 const QStringList usesList = QStringList(uses.begin(), uses.end());
879 message += u". Used in %1"_s.arg(usesList.join(u", "_s));
880 }
881
882 m_logger->log(message, qmlUnresolvedType, QQmlJS::SourceLocation());
883 }
884
885 for (auto &&[name, uses] : partiallyResolvedTypes.asKeyValueRange()) {
886 QString message = u"Type \"%1\" is not fully resolved"_s.arg(name);
887
888 if (!uses.isEmpty()) {
889 const QStringList usesList = QStringList(uses.begin(), uses.end());
890 message += u". Used in %1"_s.arg(usesList.join(u", "_s));
891 }
892
893 m_logger->log(message, qmlUnresolvedType, QQmlJS::SourceLocation());
894 }
895
896 return (m_logger->hasWarnings() || m_logger->hasErrors()) ? HasWarnings : LintSuccess;
897}
898
899QQmlJSLinter::FixResult QQmlJSLinter::applyFixes(QString *fixedCode, bool silent)
900{
901 Q_ASSERT(fixedCode != nullptr);
902
903 // This means that the necessary analysis for applying fixes hasn't run for some reason
904 // (because it was JS file, a syntax error etc.). We can't procede without it and if an error
905 // has occurred that has to be handled by the caller of lintFile(). Just say that there is
906 // nothing to fix.
907 if (m_logger == nullptr)
908 return NothingToFix;
909
910 QString code = m_fileContents;
911
912 QList<QQmlJSFixSuggestion> fixesToApply;
913
914 QFileInfo info(m_logger->filePath());
915 const QString currentFileAbsolutePath = info.absoluteFilePath();
916
917 const QString lowerSuffix = info.suffix().toLower();
918 const bool isESModule = lowerSuffix == QLatin1String("mjs");
919 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
920
921 if (isESModule || isJavaScript)
922 return NothingToFix;
923
924 m_logger->iterateAllMessages([&](const Message &msg) {
925 if (!msg.fixSuggestion.has_value() || !msg.fixSuggestion->isAutoApplicable())
926 return;
927
928 // Ignore fix suggestions for other files
929 const QString filename = msg.fixSuggestion->filename();
930 if (!filename.isEmpty()
931 && QFileInfo(filename).absoluteFilePath() != currentFileAbsolutePath) {
932 return;
933 }
934
935 fixesToApply << msg.fixSuggestion.value();
936 });
937
938 if (fixesToApply.isEmpty())
939 return NothingToFix;
940
941 std::sort(fixesToApply.begin(), fixesToApply.end(),
942 [](const QQmlJSFixSuggestion &a, const QQmlJSFixSuggestion &b) {
943 return a.location().offset < b.location().offset;
944 });
945
946 const auto dupes = std::unique(fixesToApply.begin(), fixesToApply.end());
947 fixesToApply.erase(dupes, fixesToApply.end());
948
949 for (auto it = fixesToApply.begin(); it + 1 != fixesToApply.end(); it++) {
950 const QQmlJS::SourceLocation srcLocA = it->location();
951 const QQmlJS::SourceLocation srcLocB = (it + 1)->location();
952 if (srcLocA.offset + srcLocA.length > srcLocB.offset) {
953 if (!silent)
954 qWarning() << "Fixes for two warnings are overlapping, aborting. Please file a bug "
955 "report.";
956 return FixError;
957 }
958 }
959
960 int offsetChange = 0;
961
962 for (const auto &fix : std::as_const(fixesToApply)) {
963 const QQmlJS::SourceLocation fixLocation = fix.location();
964 qsizetype cutLocation = fixLocation.offset + offsetChange;
965 const QString before = code.left(cutLocation);
966 const QString after = code.mid(cutLocation + fixLocation.length);
967
968 const QString replacement = fix.replacement();
969 code = before + replacement + after;
970 offsetChange += replacement.size() - fixLocation.length;
971 }
972
973 QQmlJS::Engine engine;
974 QQmlJS::Lexer lexer(&engine);
975
976 lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
977 QQmlJS::Parser parser(&engine);
978
979 bool success = parser.parse();
980
981 if (!success) {
982 const auto diagnosticMessages = parser.diagnosticMessages();
983
984 if (!silent) {
985 qDebug() << "File became unparseable after suggestions were applied. Please file a bug "
986 "report.";
987 } else {
988 return FixError;
989 }
990
991 for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
992 qWarning().noquote() << QString::fromLatin1("%1:%2:%3: %4")
993 .arg(m_logger->filePath())
994 .arg(m.loc.startLine)
995 .arg(m.loc.startColumn)
996 .arg(m.message);
997 }
998 return FixError;
999 }
1000
1001 *fixedCode = code;
1002 return FixSuccess;
1003}
1004
1005QT_END_NAMESPACE
void reportVarUsedBeforeDeclaration(const QString &name, const QString &fileName, QQmlJS::SourceLocation declarationLocation, QQmlJS::SourceLocation accessLocation) override
void reportFunctionUsedBeforeDeclaration(const QString &name, const QString &fileName, QQmlJS::SourceLocation declarationLocation, QQmlJS::SourceLocation accessLocation) override
UnreachableVisitor * unreachableVisitor() override
CodegenWarningInterface(QQmlJSLogger *logger)
bool visit(QQmlJS::AST::FunctionDeclaration *functionDeclaration) override
void throwRecursionDepthError() override
void setPassManager(QQmlSA::PassManager *passManager)
Plugin(Plugin &&plugin) noexcept
Plugin(const QStaticPlugin &plugin)
FixResult applyFixes(QString *fixedCode, bool silent)
QQmlJSLinter(const QStringList &importPaths, const QStringList &extraPluginPaths={}, bool useAbsolutePath=false)
LintResult lintModule(const QString &uri, const bool silent, QJsonArray *json, const QStringList &qmlImportPaths, const QStringList &resourceFiles)
LintResult lintFile(const QString &filename, const QString *fileContents, const bool silent, QJsonArray *json, const QStringList &qmlImportPaths, const QStringList &qmldirFiles, const QStringList &resourceFiles, const QList< QQmlJS::LoggerCategory > &categories, const QQmlJS::HeuristicContextProperties &contextProperties={})
UnreachableVisitor(QQmlJSLogger *logger)
void throwRecursionDepthError() override
bool containsFunctionDeclaration(QQmlJS::AST::Node *node)
bool visit(QQmlJS::AST::StatementList *unreachable) override
\inmodule QtQmlCompiler
Combined button and popup list for selecting options.
static void addJsonWarning(QJsonArray &warnings, const QQmlJS::DiagnosticMessage &message, QAnyStringView id, const std::optional< QQmlJSFixSuggestion > &suggestion={})