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
11
anti-patterns.
12
13
\e qmllint is a tool shipped with Qt, that verifies the syntatic validity of
14
QML files. You can \l{Using qmllint with CMake}{integrate qmllint into your build system}
15
for ease of use. It also warns about some QML anti-patterns. See
16
\{Configuring qmllint warnings} for how to disable a specific warning type.
17
18
\note When using IDE, such as Qt Creator, you don't need to run \c qmllint
19
manually. The IDE uses \l {QML Language Server#Linting}{QML Language Server},
20
that provides real-time linting output and diagnostics as you type.
21
22
By default, some issues will result in warnings that will be printed. If there
23
are more warnings than a limit which can be configured with \c{--max-warnings},
24
the exit code will be non-zero.
25
Minor issues however (such as unused imports) are just informational messages
26
by default and will never affect the exit code.
27
qmllint is very configurable and allows for
28
\l{Configuring qmllint warnings}{disabling warnings or changing how they are treated}.
29
30
qmllint warns about:
31
\list
32
\li Unqualified accesses of properties
33
\li Usage of signal handlers without a matching signal
34
\li Usage of with statements in QML
35
\li Issues related to compiling QML code
36
\li Unused imports
37
\li Deprecated components and properties
38
\li And many other things
39
\endlist
40
41
See \l{QML Lint Warning and Errors} on how to fix qmllint warnings and errors.
42
43
\note In order for qmllint to work properly, it requires type information.
44
That information is provided by QML modules in the import paths.
45
The current directory, as well as the import paths for Qt's built-in types,
46
are used as import paths by default.
47
To add more import paths not included in the default,
48
add them via the \c{-I} flag.
49
50
To get an overview and explanation of all available command line options, run \c{qmllint --help}.
51
52
\section2 Compiler warnings
53
54
qmllint can warn you about code that cannot be compiled by \l{qmlsc}.
55
56
These warnings are not enabled by default. In order to enable them,
57
\l{Configuring qmllint warnings}{configure qmllint} to use the compiler warning
58
category.
59
60
\section2 Using qmllint with CMake
61
For projects using the \l{qt6_add_qml_module}{qt_add_qml_module()} CMake API for creating QML
62
modules, \l{Linting QML sources}{convenience targets} such as \c all_qmllint are created
63
automatically. These run qmllint on all the QML files of a specific module or of the project.
64
65
66
\section2 Automatically applying fix suggestions
67
68
Addressing warnings emitted by qmllint can involve quite some manual editing
69
and validation work. Some of the warnings qmllint emits come with an associated
70
fix suggestion that can be triggered to address the warning automatically. To
71
trigger these fixits, you can:
72
\list
73
\li Pass \c{--fix} to qmllint.
74
\li Activate the fixits from your IDE.
75
\endlist
76
77
Applying all the fixits accross your project at once may be too large of a
78
change. Therefore, it is recommended to divide the work into more manageable
79
parts. You can split the work by linting only a subset of the files, or by
80
running only certain categories.
81
82
\badcode
83
qmllint --only-explicit-categories --ignore-settings --fix --unqualified=warning <files>
84
\endcode
85
86
This will apply the available fixits for unqualified accesses only, by disabling
87
all other warning categories with \c{--only-explicit-categories} and
88
\c{--ignore-settings}.
89
90
If you are unsure about a change, use \c{--dry-run} to safely see the result of
91
a run without actually changing the files.
92
93
94
\section2 Marking components and properties as deprecated
95
96
qmllint allows you to mark both properties and components as deprecated:
97
98
\code
99
@Deprecated { reason: "Use NewCustomText instead" }
100
Text {
101
@Deprecated { reason: "Use newProperty instead" }
102
property int oldProperty
103
property int newProperty
104
Component.onCompleted: console.log(oldProperty); // Warning: XY.qml:8:40: Property "oldProperty" is deprecated (Reason: Use newProperty instead)
105
}
106
\endcode
107
108
Deprecation warnings for components will be shown every time the component is created.
109
110
\section2 Disabling warnings inline
111
112
You may at any point disable warnings temporarily in a file using \c{// qmllint
113
disable}.
114
115
You can do this at the end of a line when a single line produces warnings:
116
117
\code
118
Item {
119
property string foo
120
Item {
121
property string bar: foo // qmllint disable unqualified
122
}
123
}
124
\endcode
125
126
Alternatively you can disable comments for a block of lines by putting the
127
comment in a line only containing \c{// qmllint disable}, ending the block with
128
\c{// qmllint enable}:
129
130
\code
131
Item {
132
property string foo
133
Item {
134
// qmllint disable unqualified
135
property string bar: foo
136
property string bar2: foo
137
// qmllint enable unqualified
138
}
139
}
140
\endcode
141
142
qmllint interprets all single line comments starting with \c {qmllint} as
143
directives. Thus you may not start a comment that way unless you wish to enable
144
or disable warnings.
145
146
\note As done in the examples above it is preferable to explicitly specify the
147
warning or a list of warnings you want to disable instead of disabling all
148
warnings. This can be done by simply listing warning categories after \c{qmllint disable} (the names are
149
the same as the options listed in \c{--help}).
150
151
152
\section2 Configuring qmllint warnings
153
154
You can configure the warnings that qmllint emits and their severity level.
155
These levels can be \c{info}, \{warning}, \c{error}, or \c{disable}. You can
156
customize the warnings using the \c{.qmllint.ini} settings file or by passing
157
command line options to qmllint. Command line options override the default and
158
the settings file.
159
160
To customize the level of a warning category via the command line, set the
161
corresponding command line option to the desired level.
162
163
For example, to disable warnings about deprecations, call qmllint with the
164
\c{--deprecated=disable} option. And to turn unused imports into an error, pass
165
\c{--unused-imports=error}.
166
167
Customizing warnings in a more permanent way can be done by modifying the
168
settings file. See the upcoming section on \l{Settings} files for more details.
169
170
171
\section2 Settings
172
173
In addition to passing command-line options, you can also
174
configure qmllint via a settings file.
175
The command line \c{--write-defaults} will generate one for you.
176
177
Setting files are named \c{.qmllint.ini} and look like this:
178
179
\quotefile qmllint/config.ini
180
181
Warning levels may be set to \c{info}, \c{warning}, \c{error}, or \c{disable}
182
just as with command line options.
183
184
qmllint will automatically look for a settings file at the location of the qml
185
file that is being linted.
186
It also looks through all parent directories to find this file and
187
automatically applies the settings therein. You can disable this behavior by
188
using \c{--ignore-settings}.
189
You may always override these defaults by specifying
190
\l{Configuring qmllint warnings}{command line parameters} that take precedence
191
over the warning levels in settings.
192
193
\section3 Context property settings
194
195
Context properties can be defined or ignored by name in their own settings file.
196
Context property settings allow a more fine-grained approach to disabling unqualified
197
access warnings for context property usages, while \c{.qmllint.ini} only allows to
198
disable all unqualified access warnings, potentially including non-context property
199
related ones.
200
201
Context property settings are named \c{.contextProperties.ini} and should be located
202
inside the source folder of your project. They look like this:
203
204
\quotefile qmllint/contextProperties.ini
205
206
To disable qmllint warnings about unqualified access to a context property name,
207
add the context property name to \c{disableUnqualifiedAccess}. Separate multiple
208
context property names with a comma.
209
210
To warn about context property usages, add the context property name to \c{warnOnUsage}.
211
Separate multiple context property names with a comma.
212
213
To control the qmllint heuristic, set \c{disableHeuristic} to \c{true} or \c{false}.
214
215
\section2 Scripting
216
217
qmllint can write or output JSON via the \c{--json <file>} option which will return valid JSON
218
with warning messages, file and line location of warnings, and their severity
219
level. Use the special filename '-' to write to stdout instead of a file.
220
This can be used to more easily integrate qmllint in your pre-commit hooks or
221
CI testing.
222
223
\sa {Type Description Files}
224
\sa {Qt Quick Tools and Utilities}
225
*/
qtdeclarative
src
qml
doc
src
tools
qtqml-tooling-qmllint.qdoc
Generated on
for Qt by
1.16.1