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