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
qtqml-tooling-qmllint.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-tooling-qmllint.html
6\title qmllint
7\keyword qmllint Reference
8\ingroup qtqml-tooling
9\ingroup qtqml-tooling-devtools
10\brief A tool for verifying the syntax of QML files and warning about
11anti-patterns.
12
13\e qmllint is a tool shipped with Qt, that verifies the syntatic validity of
14QML files. You can \l{Using qmllint with CMake}{integrate qmllint into your build system}
15for ease of use. It also warns about some QML anti-patterns. If you want to
16disable a specific warning type, you can find the appropriate flag for doing so
17by passing \c{--help} on the command line.
18
19By default, some issues will result in warnings that will be printed. If there
20are more warnings than a limit which can be configured with \c{--max-warnings},
21the exit code will be non-zero.
22Minor issues however (such as unused imports) are just informational messages
23by default and will never affect the exit code.
24qmllint is very configurable and allows for disabling warnings or changing how
25they are treated.
26Users may freely turn any issue into a warning, informational message, or
27disable them outright.
28
29qmllint warns about:
30\list
31 \li Unqualified accesses of properties
32 \li Usage of signal handlers without a matching signal
33 \li Usage of with statements in QML
34 \li Issues related to compiling QML code
35 \li Unused imports
36 \li Deprecated components and properties
37 \li And many other things
38\endlist
39
40See \l{QML Lint Warning and Errors} on how to fix qmllint warnings and errors.
41
42\note In order for qmllint to work properly, it requires type information.
43That information is provided by QML modules in the import paths.
44The current directory, as well as the import paths for Qt's built-in types,
45are used as import paths by default.
46To add more import paths not included in the default,
47add them via the \c{-I} flag.
48
49To get an overview and explanation of all available command line options, run \c{qmllint --help}.
50
51\section2 Compiler warnings
52
53qmllint can warn you about code that cannot be compiled by \l{qmlsc}.
54
55These warnings are not enabled by default. In order to enable them specify
56\c{--compiler warning} or adjust your settings file accordingly.
57
58\section2 Using qmllint with CMake
59For projects using the \l{qt6_add_qml_module}{qt_add_qml_module()} CMake API for creating QML
60modules, \l{Linting QML sources}{convenience targets} such as \c all_qmllint are created
61automatically. These run qmllint on all the QML files of a specific module or of the project.
62
63\section2 Marking components and properties as deprecated
64
65qmllint allows you to mark both properties and components as deprecated:
66
67\code
68@Deprecated { reason: "Use NewCustomText instead" }
69Text {
70 @Deprecated { reason: "Use newProperty instead" }
71 property int oldProperty
72 property int newProperty
73 Component.onCompleted: console.log(oldProperty); // Warning: XY.qml:8:40: Property "oldProperty" is deprecated (Reason: Use newProperty instead)
74}
75\endcode
76
77Deprecation warnings for components will be shown every time the component is created.
78
79\section2 Disabling warnings inline
80
81You may at any point disable warnings temporarily in a file using \c{// qmllint
82disable}.
83
84You can do this at the end of a line when a single line produces warnings:
85
86\code
87Item {
88 property string foo
89 Item {
90 property string bar: foo // qmllint disable unqualified
91 }
92}
93\endcode
94
95Alternatively you can disable comments for a block of lines by putting the
96comment in a line only containing \c{// qmllint disable}, ending the block with
97\c{// qmllint enable}:
98
99\code
100Item {
101 property string foo
102 Item {
103 // qmllint disable unqualified
104 property string bar: foo
105 property string bar2: foo
106 // qmllint enable unqualified
107 }
108}
109\endcode
110
111qmllint interprets all single line comments starting with \c {qmllint} as
112directives. Thus you may not start a comment that way unless you wish to enable
113or disable warnings.
114
115\note As done in the examples above it is preferable to explicitly specify the
116warning or a list of warnings you want to disable instead of disabling all
117warnings. This can be done by simply listing warning categories after \c{qmllint disable} (the names are
118the same as the options listed in \c{--help}).
119
120\section2 Settings
121
122In addition to passing command-line options, you can also
123configure qmllint via a settings file.
124The command line \c{--write-defaults} will generate one for you.
125
126Setting files are named \c{.qmllint.ini} and look like this:
127
128\quotefile qmllint/config.ini
129
130Warning levels may be set to \c{info}, \c{warning} or \c{disable} just as with
131command line options.
132
133qmllint will automatically look for a settings file at the location of the qml
134file that is being linted.
135It also looks through all parent directories to find this file and
136automatically applies the settings therein. You can disable this behavior by
137using \c{--ignore-settings}.
138You may always override these defaults by specifying command line parameters
139that take precedence over the warning levels in settings.
140
141\section3 Context property settings
142
143Context properties can be defined or ignored by name in their own settings file.
144Context property settings allow a more fine-grained approach to disabling unqualified
145access warnings for context property usages, while \c{.qmllint.ini} only allows to
146disable all unqualified access warnings, potentially including non-context property
147related ones.
148
149Context property settings are named \c{.contextProperties.ini} and look like this:
150
151\quotefile qmllint/contextProperties.ini
152
153To disable qmllint warnings about unqualified access to a context property name,
154add the context property name to \c{disableUnqualifiedAccess}.
155
156To warn about context property usages, add the context property name to \c{warnOnUsage}.
157
158To control the qmllint heuristic, set \c{disableHeuristic} to \c{true} or \c{false}.
159
160\section2 Scripting
161
162qmllint can write or output JSON via the \c{--json <file>} option which will return valid JSON
163with warning messages, file and line location of warnings, and their severity
164level. Use the special filename '-' to write to stdout instead of a file.
165This can be used to more easily integrate qmllint in your pre-commit hooks or
166CI testing.
167
168\sa {Type Description Files}
169\sa {Qt Quick Tools and Utilities}
170*/