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 &,
726 const LookupSignatures &,
727 const QString *) { return true; };
728
729 QQmlJSCompileError error;
730
731 QLoggingCategory::setFilterRules(u"qt.qml.compiler=false"_s);
732
733 CodegenWarningInterface warningInterface(m_logger.get());
734 qCompileQmlFile(filename, saveFunction, &codegen, &error, true, &warningInterface,
735 fileContents);
736
737 QList<QQmlJS::DiagnosticMessage> globalWarnings = m_importer.takeGlobalWarnings();
738
739 if (!globalWarnings.isEmpty()) {
740 m_logger->log(QStringLiteral("Type warnings occurred while evaluating file:"), qmlImport,
741 QQmlJS::SourceLocation());
742 m_logger->processMessages(globalWarnings, qmlImport);
743 }
744
745 if (m_logger->hasErrors())
746 return HasErrors;
747 if (m_logger->hasWarnings())
748 return HasWarnings;
749
750 return LintSuccess;
751}
752
754 const QString &module, const bool silent, QJsonArray *json,
755 const QStringList &qmlImportPaths, const QStringList &resourceFiles)
756{
757 const LintResult lintResult = lintModuleImpl(module, silent, json, qmlImportPaths, resourceFiles);
758 if (!json)
759 return lintResult;
760
761 QJsonArray warnings;
762 processMessages(warnings);
763
764 QJsonObject result;
765 result[u"module"_s] = module;
766 result[u"warnings"] = warnings;
767 result[u"success"] = lintResult == LintSuccess;
768
769 json->append(result);
770 return lintResult;
771}
772
773QQmlJSLinter::LintResult QQmlJSLinter::lintModuleImpl(
774 const QString &module, const bool silent, QJsonArray *json,
775 const QStringList &qmlImportPaths, const QStringList &resourceFiles)
776{
777 // Make sure that we don't expose an old logger if we return before a new one is created.
778 m_logger.reset();
779
780 // We can't lint properly if a module has already been pre-cached
781 m_importer.clearCache();
782
783 // We don't support file selectors during module linting currently
784 const QQmlJSImporterFlags oldFlags = m_importer.flags();
785 QQmlJSImporterFlags newFlags = oldFlags;
786 newFlags.setFlag(TolerateFileSelectors, false);
787 m_importer.setFlags(newFlags);
788 auto flagGuard = qScopeGuard([this, oldFlags]() { m_importer.setFlags(oldFlags); });
789 m_importer.setImportPaths(qmlImportPaths);
790
791 QQmlJSResourceFileMapper mapper(resourceFiles);
792 if (!resourceFiles.isEmpty())
793 m_importer.setResourceFileMapper(&mapper);
794 else
795 m_importer.setResourceFileMapper(nullptr);
796
797 m_logger.reset(new QQmlJSLogger);
798 m_logger->setFilePath(module);
799 m_logger->setCode(u""_s);
800 m_logger->setSilent(silent || json);
801
802 const QQmlJSImporter::ImportedTypes types =
803 m_importer.importModule(module, quint8(QQmlJS::PrecedenceValues::Default));
804
805 QList<QQmlJS::DiagnosticMessage> importWarnings =
806 m_importer.takeGlobalWarnings() + types.warnings();
807
808 if (!importWarnings.isEmpty()) {
809 m_logger->log(QStringLiteral("Warnings occurred while importing module:"), qmlImport,
810 QQmlJS::SourceLocation());
811 m_logger->processMessages(importWarnings, qmlImport);
812 }
813
814 QMap<QString, QSet<QString>> missingTypes;
815 QMap<QString, QSet<QString>> partiallyResolvedTypes;
816
817 const QString modulePrefix = u"$module$."_s;
818 const QString internalPrefix = u"$internal$."_s;
819
820 for (auto &&[typeName, importedScope] : types.types().asKeyValueRange()) {
821 QString name = typeName;
822 const QQmlJSScope::ConstPtr scope = importedScope.scope;
823
824 if (name.startsWith(modulePrefix))
825 continue;
826
827 if (name.startsWith(internalPrefix)) {
828 name = name.mid(internalPrefix.size());
829 }
830
831 if (scope.isNull()) {
832 if (!missingTypes.contains(name))
833 missingTypes[name] = {};
834 continue;
835 }
836
837 if (!scope->isFullyResolved()) {
838 if (!partiallyResolvedTypes.contains(name))
839 partiallyResolvedTypes[name] = {};
840 }
841 const auto &ownProperties = scope->ownProperties();
842 for (const auto &property : ownProperties) {
843 if (property.typeName().isEmpty()) {
844 // If the type name is empty, then it's an intentional vaguery i.e. for some
845 // builtins
846 continue;
847 }
848 if (property.type().isNull()) {
849 missingTypes[property.typeName()]
850 << scope->internalName() + u'.' + property.propertyName();
851 continue;
852 }
853 if (!property.type()->isFullyResolved()) {
854 partiallyResolvedTypes[property.typeName()]
855 << scope->internalName() + u'.' + property.propertyName();
856 }
857 }
858 if (scope->attachedType() && !scope->attachedType()->isFullyResolved()) {
859 m_logger->log(u"Attached type of \"%1\" not fully resolved"_s.arg(name),
860 qmlUnresolvedType, scope->sourceLocation());
861 }
862
863 const auto &ownMethods = scope->ownMethods();
864 for (const auto &method : ownMethods) {
865 if (method.returnTypeName().isEmpty())
866 continue;
867 if (method.returnType().isNull()) {
868 missingTypes[method.returnTypeName()] << u"return type of "_s
869 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
870 } else if (!method.returnType()->isFullyResolved()) {
871 partiallyResolvedTypes[method.returnTypeName()] << u"return type of "_s
872 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
873 }
874
875 const auto parameters = method.parameters();
876 for (qsizetype i = 0; i < parameters.size(); i++) {
877 auto &parameter = parameters[i];
878 const QString typeName = parameter.typeName();
879 const QSharedPointer<const QQmlJSScope> type = parameter.type();
880 if (typeName.isEmpty())
881 continue;
882 if (type.isNull()) {
883 missingTypes[typeName] << u"parameter %1 of "_s.arg(i + 1)
884 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
885 continue;
886 }
887 if (!type->isFullyResolved()) {
888 partiallyResolvedTypes[typeName] << u"parameter %1 of "_s.arg(i + 1)
889 + scope->internalName() + u'.' + method.methodName() + u"()"_s;
890 continue;
891 }
892 }
893 }
894 }
895
896 for (auto &&[name, uses] : missingTypes.asKeyValueRange()) {
897 QString message = u"Type \"%1\" not found"_s.arg(name);
898
899 if (!uses.isEmpty()) {
900 const QStringList usesList = QStringList(uses.begin(), uses.end());
901 message += u". Used in %1"_s.arg(usesList.join(u", "_s));
902 }
903
904 m_logger->log(message, qmlUnresolvedType, QQmlJS::SourceLocation());
905 }
906
907 for (auto &&[name, uses] : partiallyResolvedTypes.asKeyValueRange()) {
908 QString message = u"Type \"%1\" is not fully resolved"_s.arg(name);
909
910 if (!uses.isEmpty()) {
911 const QStringList usesList = QStringList(uses.begin(), uses.end());
912 message += u". Used in %1"_s.arg(usesList.join(u", "_s));
913 }
914
915 m_logger->log(message, qmlUnresolvedType, QQmlJS::SourceLocation());
916 }
917
918 return (m_logger->hasWarnings() || m_logger->hasErrors()) ? HasWarnings : LintSuccess;
919}
920
921QQmlJSLinter::FixResult QQmlJSLinter::applyFixes(QString *fixedCode, bool silent)
922{
923 Q_ASSERT(fixedCode != nullptr);
924
925 // This means that the necessary analysis for applying fixes hasn't run for some reason
926 // (because it was JS file, a syntax error etc.). We can't procede without it and if an error
927 // has occurred that has to be handled by the caller of lintFile(). Just say that there is
928 // nothing to fix.
929 if (m_logger == nullptr)
930 return NothingToFix;
931
932 QString code = m_fileContents;
933
934 QList<QQmlJSFixSuggestion> fixesToApply;
935
936 QFileInfo info(m_logger->filePath());
937 const QString currentFileAbsolutePath = info.absoluteFilePath();
938
939 const QString lowerSuffix = info.suffix().toLower();
940 const bool isESModule = lowerSuffix == QLatin1String("mjs");
941 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
942
943 if (isESModule || isJavaScript)
944 return NothingToFix;
945
946 m_logger->iterateAllMessages([&](const Message &msg) {
947 if (!msg.fixSuggestion.has_value() || !msg.fixSuggestion->isAutoApplicable())
948 return;
949
950 // Ignore fix suggestions for other files
951 const QString filename = msg.fixSuggestion->filename();
952 if (!filename.isEmpty()
953 && QFileInfo(filename).absoluteFilePath() != currentFileAbsolutePath) {
954 return;
955 }
956
957 fixesToApply << msg.fixSuggestion.value();
958 });
959
960 if (fixesToApply.isEmpty())
961 return NothingToFix;
962
963 QList<QQmlJSDocumentEdit> documentEdits;
964 for (const auto &fixToApply : std::as_const(fixesToApply)) {
965 const auto &fixDocumentEdits = fixToApply.documentEdits();
966 for (const auto &documentEdit : fixDocumentEdits) {
967 // TODO also apply documentEdits in other files
968 if (documentEdit.m_filename == m_logger->filePath())
969 documentEdits << documentEdit;
970 }
971 }
972
973 std::sort(documentEdits.begin(), documentEdits.end(),
974 [](const QQmlJSDocumentEdit &a, const QQmlJSDocumentEdit &b) {
975 return a.m_location.offset < b.m_location.offset;
976 });
977
978 const auto dupes = std::unique(documentEdits.begin(), documentEdits.end());
979 documentEdits.erase(dupes, documentEdits.end());
980
981 for (auto it = documentEdits.begin(); it + 1 != documentEdits.end(); it++) {
982 const QQmlJS::SourceLocation srcLocA = it->m_location;
983 const QQmlJS::SourceLocation srcLocB = (it + 1)->m_location;
984 if (srcLocA.offset + srcLocA.length > srcLocB.offset) {
985 if (!silent)
986 qWarning() << "Document edits for warning fixes are overlapping, aborting. "
987 "Please file a bug report if this is a Qt warning";
988 return FixError;
989 }
990 }
991
992 int offsetEdit = 0;
993
994 for (const auto &edit : std::as_const(documentEdits)) {
995 const QQmlJS::SourceLocation fixLocation = edit.m_location;
996 qsizetype cutLocation = fixLocation.offset + offsetEdit;
997 const QString before = code.left(cutLocation);
998 const QString after = code.mid(cutLocation + fixLocation.length);
999
1000 const QString replacement = edit.m_replacement;
1001 code = before + replacement + after;
1002 offsetEdit += replacement.size() - fixLocation.length;
1003 }
1004
1005 QQmlJS::Engine engine;
1006 QQmlJS::Lexer lexer(&engine);
1007
1008 lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
1009 QQmlJS::Parser parser(&engine);
1010
1011 bool success = parser.parse();
1012
1013 if (!success) {
1014 const auto diagnosticMessages = parser.diagnosticMessages();
1015
1016 if (!silent) {
1017 qDebug() << "File became unparseable after suggestions were applied. Please file a bug "
1018 "report.";
1019 } else {
1020 return FixError;
1021 }
1022
1023 for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
1024 qWarning().noquote() << QString::fromLatin1("%1:%2:%3: %4")
1025 .arg(m_logger->filePath())
1026 .arg(m.loc.startLine)
1027 .arg(m.loc.startColumn)
1028 .arg(m.message);
1029 }
1030 return FixError;
1031 }
1032
1033 *fixedCode = code;
1034 return FixSuccess;
1035}
1036
1037QT_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={})