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 QJsonArray documentEdits;
494 for (const auto &documentEdit : suggestion->documentEdits()) {
495 QJsonObject location;
496 convertLocation(documentEdit.m_location, &location);
497 QJsonObject edit {
498 { "filename"_L1, documentEdit.m_filename },
499 { "location"_L1, location },
500 { "replacement"_L1, documentEdit.m_replacement }
501 };
502 documentEdits.append(edit);
503 }
504
505 QJsonObject jsonFix {
506 { "message"_L1, suggestion->description() },
507 { "documentEdits"_L1, documentEdits },
508 { "isAutoApplicable"_L1, suggestion->isAutoApplicable() },
509 };
510 convertLocation(suggestion->location(), &jsonFix);
511 const QString filename = suggestion->filename();
512 if (!filename.isEmpty())
513 jsonFix.insert("fileName"_L1, filename);
514 suggestions << jsonFix;
515 }
516 jsonMessage[u"suggestions"] = suggestions;
517
518 warnings << jsonMessage;
519}
520
521void QQmlJSLinter::processMessages(QJsonArray &warnings)
522{
523 m_logger->iterateAllMessages([&](const Message &message) {
524 addJsonWarning(warnings, message, message.id, message.fixSuggestion);
525 });
526}
527
528ContextPropertyInfo QQmlJSLinter::contextPropertiesFor(
529 const QString &filename, QQmlJSResourceFileMapper *mapper,
530 const QQmlJS::HeuristicContextProperties &heuristicContextProperties)
531{
532 ContextPropertyInfo result;
533 if (m_userContextPropertySettings.search(filename).isValid()) {
534 result.userContextProperties =
535 QQmlJS::UserContextProperties{ m_userContextPropertySettings };
536 }
537
538 if (heuristicContextProperties.isValid()) {
539 result.heuristicContextProperties = heuristicContextProperties;
540 return result;
541 }
542
543#if QT_CONFIG(qmlcontextpropertydump)
544 const QString buildPath = QQmlJSUtils::qmlBuildPathFromSourcePath(mapper, filename);
545 if (const auto searchResult = m_heuristicContextPropertySearcher.search(buildPath);
546 searchResult.isValid()) {
547 QSettings settings(searchResult.iniFilePath, QSettings::IniFormat);
548 result.heuristicContextProperties =
549 QQmlJS::HeuristicContextProperties::collectFrom(&settings);
550 }
551#else
552 Q_UNUSED(mapper);
553#endif
554 return result;
555}
556
558QQmlJSLinter::lintFile(const QString &filename, const QString *fileContents, const bool silent,
559 QJsonArray *json, const QStringList &qmlImportPaths,
560 const QStringList &qmldirFiles, const QStringList &resourceFiles,
561 const QList<QQmlJS::LoggerCategory> &categories,
562 const QQmlJS::HeuristicContextProperties &heuristicContextProperties)
563{
564 const LintResult lintResult =
565 lintFileImpl(filename, fileContents, silent, json, qmlImportPaths, qmldirFiles,
566 resourceFiles, categories, heuristicContextProperties);
567 if (!json)
568 return lintResult;
569
570 QJsonArray warnings;
571 processMessages(warnings);
572
573 QJsonObject result;
574 result[u"filename"_s] = QFileInfo(filename).absoluteFilePath();
575 result[u"warnings"] = warnings;
576 result[u"success"] = lintResult == LintSuccess;
577
578 json->append(result);
579 return lintResult;
580}
581
582void QQmlJSLinter::setupLoggingCategoriesInLogger(const QList<QQmlJS::LoggerCategory> &categories)
583{
584 if (m_enablePlugins) {
585 for (const Plugin &plugin : m_plugins) {
586 for (const QQmlJS::LoggerCategory &category : plugin.categories())
587 m_logger->registerCategory(category);
588 }
589 }
590
591 for (auto it = categories.cbegin(); it != categories.cend(); ++it) {
592 if (auto logger = *it; !QQmlJS::LoggerCategoryPrivate::get(&logger)->hasChanged())
593 continue;
594
595 m_logger->setCategorySeverity(it->id(), it->severity());
596 }
597}
598
600QQmlJSLinter::lintFileImpl(const QString &filename, const QString *fileContents, const bool silent,
601 QJsonArray *json, const QStringList &qmlImportPaths,
602 const QStringList &qmldirFiles, const QStringList &resourceFiles,
603 const QList<QQmlJS::LoggerCategory> &categories,
604 const QQmlJS::HeuristicContextProperties &heuristicContextProperties)
605{
606 QString code;
607
608 QFileInfo info(filename);
609 const QString lowerSuffix = info.suffix().toLower();
610 const bool isESModule = lowerSuffix == QLatin1String("mjs");
611 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
612
613 m_logger.reset(new QQmlJSLogger);
614 m_logger->setFilePath(m_useAbsolutePath ? info.absoluteFilePath() : filename);
615 m_logger->setSilent(silent || json);
616 setupLoggingCategoriesInLogger(categories);
617
618 if (fileContents == nullptr) {
619 QFile file(filename);
620 if (!file.open(QFile::ReadOnly)) {
621 m_logger->log("Failed to open file %1: %2"_L1.arg(filename, file.errorString()),
622 qmlImport, QQmlJS::SourceLocation());
623 return FailedToOpen;
624 }
625
626 code = QString::fromUtf8(file.readAll());
627 file.close();
628 } else {
629 code = *fileContents;
630 }
631
632 m_fileContents = code;
633 m_logger->setCode(code);
634
635 QQmlJS::Engine engine;
636 QQmlJS::Lexer lexer(&engine);
637
638 lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
639 QQmlJS::Parser parser(&engine);
640
641 const bool parseSuccess = isJavaScript
642 ? (isESModule ? parser.parseModule() : parser.parseProgram())
643 : parser.parse();
644 const auto diagnosticMessages = parser.diagnosticMessages();
645 for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages)
646 m_logger->log(m.message, qmlSyntax, m.loc);
647
648 if (!parseSuccess)
649 return FailedToParse;
650
651 if (isJavaScript)
652 return LintSuccess;
653
654 m_importer.setImportPaths(qmlImportPaths);
655
656 std::optional<QQmlJSResourceFileMapper> mapper;
657 if (!resourceFiles.isEmpty())
658 mapper.emplace(resourceFiles);
659 m_importer.setResourceFileMapper(mapper.has_value() ? &*mapper : nullptr);
660
661 QQmlJS::LinterVisitor v{ &m_importer, m_logger.get(),
662 QQmlJSImportVisitor::implicitImportDirectory(
663 m_logger->filePath(), m_importer.resourceFileMapper()),
664 qmldirFiles, &engine };
665
666 parseComments(m_logger.get(), engine.comments());
667
668 QQmlJSTypeResolver typeResolver(&m_importer);
669
670 // Type resolving is using document parent mode here so that it produces fewer false
671 // positives on the "parent" property of QQuickItem. It does produce a few false
672 // negatives this way because items can be reparented. Furthermore, even if items
673 // are not reparented, the document parent may indeed not be their visual parent.
674 // See QTBUG-95530. Eventually, we'll need cleverer logic to deal with this.
675 typeResolver.setParentMode(QQmlJSTypeResolver::UseDocumentParent);
676 // We don't need to create tracked types and such as we are just linting the code
677 // here and not actually compiling it. The duplicated scopes would cause issues
678 // during linting.
679 typeResolver.setCloneMode(QQmlJSTypeResolver::DoNotCloneTypes);
680
681 typeResolver.init(&v, parser.rootNode());
682
683 const QStringList resourcePaths = mapper
684 ? mapper->resourcePaths(QQmlJSResourceFileMapper::localFileFilter(filename))
685 : QStringList();
686 const QString resolvedPath =
687 (resourcePaths.size() == 1) ? u':' + resourcePaths.first() : filename;
688
689 QQmlJSLinterCodegen codegen{ &m_importer, resolvedPath, qmldirFiles, m_logger.get(),
690 contextPropertiesFor(filename, mapper ? &*mapper : nullptr,
691 heuristicContextProperties) };
692 codegen.setTypeResolver(std::move(typeResolver));
693 codegen.setScopesById(v.addressableScopes());
694 codegen.setRenamedComponents(&v.renamedComponents());
695 codegen.setKnownUnresolvedTypes(v.knownUnresolvedTypes());
696
697 using PassManagerPtr =
698 std::unique_ptr<QQmlSA::PassManager,
699 decltype(&QQmlSA::PassManagerPrivate::deletePassManager)>;
700 PassManagerPtr passMan(
701 QQmlSA::PassManagerPrivate::createPassManager(&v, codegen.typeResolver()),
702 &QQmlSA::PassManagerPrivate::deletePassManager);
703 QQmlJSLinterPasses::registerDefaultPasses(passMan.get());
704
705 if (m_enablePlugins) {
706 for (const Plugin &plugin : m_plugins) {
707 if (!plugin.isValid() || !plugin.isEnabled())
708 continue;
709
710 QQmlSA::LintPlugin *instance = plugin.m_instance;
711 Q_ASSERT(instance);
712 instance->registerPasses(passMan.get(), QQmlJSScope::createQQmlSAElement(v.result()));
713 }
714 }
715 passMan->analyze(QQmlJSScope::createQQmlSAElement(v.result()));
716
717 if (m_logger->hasErrors())
718 return HasErrors;
719
720 // passMan now has a pointer to the moved from type resolver
721 // we fix this in setPassManager
722 codegen.setPassManager(passMan.get());
723
724 QQmlJSSaveFunction saveFunction = [](const QV4::CompiledData::SaveableUnitPointer &,
725 const QQmlJSAotFunctionMap &, QString *) { return true; };
726
727 QQmlJSCompileError error;
728
729 QLoggingCategory::setFilterRules(u"qt.qml.compiler=false"_s);
730
731 CodegenWarningInterface warningInterface(m_logger.get());
732 qCompileQmlFile(filename, saveFunction, &codegen, &error, true, &warningInterface,
733 fileContents);
734
735 QList<QQmlJS::DiagnosticMessage> globalWarnings = m_importer.takeGlobalWarnings();
736
737 if (!globalWarnings.isEmpty()) {
738 m_logger->log(QStringLiteral("Type warnings occurred while evaluating file:"), qmlImport,
739 QQmlJS::SourceLocation());
740 m_logger->processMessages(globalWarnings, qmlImport);
741 }
742
743 if (m_logger->hasErrors())
744 return HasErrors;
745 if (m_logger->hasWarnings())
746 return HasWarnings;
747
748 return LintSuccess;
749}
750
752 const QString &module, const bool silent, QJsonArray *json,
753 const QStringList &qmlImportPaths, const QStringList &resourceFiles)
754{
755 const LintResult lintResult = lintModuleImpl(module, silent, json, qmlImportPaths, resourceFiles);
756 if (!json)
757 return lintResult;
758
759 QJsonArray warnings;
760 processMessages(warnings);
761
762 QJsonObject result;
763 result[u"module"_s] = module;
764 result[u"warnings"] = warnings;
765 result[u"success"] = lintResult == LintSuccess;
766
767 json->append(result);
768 return lintResult;
769}
770
771QQmlJSLinter::LintResult QQmlJSLinter::lintModuleImpl(
772 const QString &module, const bool silent, QJsonArray *json,
773 const QStringList &qmlImportPaths, const QStringList &resourceFiles)
774{
775 // Make sure that we don't expose an old logger if we return before a new one is created.
776 m_logger.reset();
777
778 // We can't lint properly if a module has already been pre-cached
779 m_importer.clearCache();
780
781 // We don't support file selectors during module linting currently
782 const QQmlJSImporterFlags oldFlags = m_importer.flags();
783 QQmlJSImporterFlags newFlags = oldFlags;
784 newFlags.setFlag(TolerateFileSelectors, false);
785 m_importer.setFlags(newFlags);
786 auto flagGuard = qScopeGuard([this, oldFlags]() { m_importer.setFlags(oldFlags); });
787 m_importer.setImportPaths(qmlImportPaths);
788
789 QQmlJSResourceFileMapper mapper(resourceFiles);
790 if (!resourceFiles.isEmpty())
791 m_importer.setResourceFileMapper(&mapper);
792 else
793 m_importer.setResourceFileMapper(nullptr);
794
795 m_logger.reset(new QQmlJSLogger);
796 m_logger->setFilePath(module);
797 m_logger->setCode(u""_s);
798 m_logger->setSilent(silent || json);
799
800 const QQmlJSImporter::ImportedTypes types =
801 m_importer.importModule(module, QQmlJS::PrecedenceValues::Default);
802
803 QList<QQmlJS::DiagnosticMessage> importWarnings =
804 m_importer.takeGlobalWarnings() + types.warnings();
805
806 if (!importWarnings.isEmpty()) {
807 m_logger->log(QStringLiteral("Warnings occurred while importing module:"), qmlImport,
808 QQmlJS::SourceLocation());
809 m_logger->processMessages(importWarnings, qmlImport);
810 }
811
812 QMap<QString, QSet<QString>> missingTypes;
813 QMap<QString, QSet<QString>> partiallyResolvedTypes;
814
815 const QString modulePrefix = u"$module$."_s;
816 const QString internalPrefix = u"$internal$."_s;
817
818 for (auto &&[typeName, importedScope] : types.types().asKeyValueRange()) {
819 QString name = typeName;
820 const QQmlJSScope::ConstPtr scope = importedScope.scope;
821
822 if (name.startsWith(modulePrefix))
823 continue;
824
825 if (name.startsWith(internalPrefix)) {
826 name = name.mid(internalPrefix.size());
827 }
828
829 if (scope.isNull()) {
830 if (!missingTypes.contains(name))
831 missingTypes[name] = {};
832 continue;
833 }
834
835 if (!scope->isFullyResolved()) {
836 if (!partiallyResolvedTypes.contains(name))
837 partiallyResolvedTypes[name] = {};
838 }
839 const auto &ownProperties = scope->ownProperties();
840 for (const auto &property : ownProperties) {
841 if (property.typeName().isEmpty()) {
842 // If the type name is empty, then it's an intentional vaguery i.e. for some
843 // builtins
844 continue;
845 }
846 if (property.type().isNull()) {
847 missingTypes[property.typeName()]
848 << scope->internalName() + u'.' + property.propertyName();
849 continue;
850 }
851 if (!property.type()->isFullyResolved()) {
852 partiallyResolvedTypes[property.typeName()]
853 << scope->internalName() + u'.' + property.propertyName();
854 }
855 }
856 if (scope->attachedType() && !scope->attachedType()->isFullyResolved()) {
857 m_logger->log(u"Attached type of \"%1\" not fully resolved"_s.arg(name),
858 qmlUnresolvedType, scope->sourceLocation());
859 }
860
861 const auto &ownMethods = scope->ownMethods();
862 for (const auto &method : ownMethods) {
863 if (method.returnTypeName().isEmpty())
864 continue;
865 if (method.returnType().isNull()) {
866 missingTypes[method.returnTypeName()] << u"return type of "_s
867 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
868 } else if (!method.returnType()->isFullyResolved()) {
869 partiallyResolvedTypes[method.returnTypeName()] << u"return type of "_s
870 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
871 }
872
873 const auto parameters = method.parameters();
874 for (qsizetype i = 0; i < parameters.size(); i++) {
875 auto &parameter = parameters[i];
876 const QString typeName = parameter.typeName();
877 const QSharedPointer<const QQmlJSScope> type = parameter.type();
878 if (typeName.isEmpty())
879 continue;
880 if (type.isNull()) {
881 missingTypes[typeName] << u"parameter %1 of "_s.arg(i + 1)
882 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
883 continue;
884 }
885 if (!type->isFullyResolved()) {
886 partiallyResolvedTypes[typeName] << u"parameter %1 of "_s.arg(i + 1)
887 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
888 continue;
889 }
890 }
891 }
892 }
893
894 for (auto &&[name, uses] : missingTypes.asKeyValueRange()) {
895 QString message = u"Type \"%1\" not found"_s.arg(name);
896
897 if (!uses.isEmpty()) {
898 const QStringList usesList = QStringList(uses.begin(), uses.end());
899 message += u". Used in %1"_s.arg(usesList.join(u", "_s));
900 }
901
902 m_logger->log(message, qmlUnresolvedType, QQmlJS::SourceLocation());
903 }
904
905 for (auto &&[name, uses] : partiallyResolvedTypes.asKeyValueRange()) {
906 QString message = u"Type \"%1\" is not fully resolved"_s.arg(name);
907
908 if (!uses.isEmpty()) {
909 const QStringList usesList = QStringList(uses.begin(), uses.end());
910 message += u". Used in %1"_s.arg(usesList.join(u", "_s));
911 }
912
913 m_logger->log(message, qmlUnresolvedType, QQmlJS::SourceLocation());
914 }
915
916 return (m_logger->hasWarnings() || m_logger->hasErrors()) ? HasWarnings : LintSuccess;
917}
918
919QQmlJSLinter::FixResult QQmlJSLinter::applyFixes(QString *fixedCode, bool silent)
920{
921 Q_ASSERT(fixedCode != nullptr);
922
923 // This means that the necessary analysis for applying fixes hasn't run for some reason
924 // (because it was JS file, a syntax error etc.). We can't procede without it and if an error
925 // has occurred that has to be handled by the caller of lintFile(). Just say that there is
926 // nothing to fix.
927 if (m_logger == nullptr)
928 return NothingToFix;
929
930 QString code = m_fileContents;
931
932 QList<QQmlJSFixSuggestion> fixesToApply;
933
934 QFileInfo info(m_logger->filePath());
935 const QString currentFileAbsolutePath = info.absoluteFilePath();
936
937 const QString lowerSuffix = info.suffix().toLower();
938 const bool isESModule = lowerSuffix == QLatin1String("mjs");
939 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
940
941 if (isESModule || isJavaScript)
942 return NothingToFix;
943
944 m_logger->iterateAllMessages([&](const Message &msg) {
945 if (!msg.fixSuggestion.has_value() || !msg.fixSuggestion->isAutoApplicable())
946 return;
947
948 // Ignore fix suggestions for other files
949 const QString filename = msg.fixSuggestion->filename();
950 if (!filename.isEmpty()
951 && QFileInfo(filename).absoluteFilePath() != currentFileAbsolutePath) {
952 return;
953 }
954
955 fixesToApply << msg.fixSuggestion.value();
956 });
957
958 if (fixesToApply.isEmpty())
959 return NothingToFix;
960
961 QList<QQmlJSDocumentEdit> documentEdits;
962 for (const auto &fixToApply : std::as_const(fixesToApply)) {
963 const auto &fixDocumentEdits = fixToApply.documentEdits();
964 for (const auto &documentEdit : fixDocumentEdits) {
965 // TODO also apply documentEdits in other files
966 if (documentEdit.m_filename == m_logger->filePath())
967 documentEdits << documentEdit;
968 }
969 }
970
971 std::sort(documentEdits.begin(), documentEdits.end(),
972 [](const QQmlJSDocumentEdit &a, const QQmlJSDocumentEdit &b) {
973 return a.m_location.offset < b.m_location.offset;
974 });
975
976 const auto dupes = std::unique(documentEdits.begin(), documentEdits.end());
977 documentEdits.erase(dupes, documentEdits.end());
978
979 for (auto it = documentEdits.begin(); it + 1 != documentEdits.end(); it++) {
980 const QQmlJS::SourceLocation srcLocA = it->m_location;
981 const QQmlJS::SourceLocation srcLocB = (it + 1)->m_location;
982 if (srcLocA.offset + srcLocA.length > srcLocB.offset) {
983 if (!silent)
984 qWarning() << "Document edits for warning fixes are overlapping, aborting. "
985 "Please file a bug report if this is a Qt warning";
986 return FixError;
987 }
988 }
989
990 int offsetEdit = 0;
991
992 for (const auto &edit : std::as_const(documentEdits)) {
993 const QQmlJS::SourceLocation fixLocation = edit.m_location;
994 qsizetype cutLocation = fixLocation.offset + offsetEdit;
995 const QString before = code.left(cutLocation);
996 const QString after = code.mid(cutLocation + fixLocation.length);
997
998 const QString replacement = edit.m_replacement;
999 code = before + replacement + after;
1000 offsetEdit += replacement.size() - fixLocation.length;
1001 }
1002
1003 QQmlJS::Engine engine;
1004 QQmlJS::Lexer lexer(&engine);
1005
1006 lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
1007 QQmlJS::Parser parser(&engine);
1008
1009 bool success = parser.parse();
1010
1011 if (!success) {
1012 const auto diagnosticMessages = parser.diagnosticMessages();
1013
1014 if (!silent) {
1015 qDebug() << "File became unparseable after suggestions were applied. Please file a bug "
1016 "report.";
1017 } else {
1018 return FixError;
1019 }
1020
1021 for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
1022 qWarning().noquote() << QString::fromLatin1("%1:%2:%3: %4")
1023 .arg(m_logger->filePath())
1024 .arg(m.loc.startLine)
1025 .arg(m.loc.startColumn)
1026 .arg(m.message);
1027 }
1028 return FixError;
1029 }
1030
1031 *fixedCode = code;
1032 return FixSuccess;
1033}
1034
1035QT_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={})