Did you know ... | Search Documentation: |
Pack logtalk -- logtalk-3.85.0/tools/linter/NOTES.md |
This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2024 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
linter
Logtalk provides a built-in linter tool that runs automatically when compiling and loading source files. The lint warnings are controlled by a [set of flags](../userman/programming.html#programming-flags-lint). The default values for these flags are defined in the backend Prolog compiler adapter files and can be overriden from a settings file, from a source file (e.g. a loader file), or from an entity. These flags can be set globally using the [set_logtalk_flag/2](../refman/predicates/set_logtalk_flag_2.html) built-in predicate. For (source file or entity) local scope, use instead the [set_logtalk_flag/2](../refman/directives/set_logtalk_flag_2.html) directive.
The linter flags can be managed as a group using the [linter](../userman/programming.html#flag-linter) meta-flag. See the documentation for details.
Some lint checks are turned off by default, specially when computationally expensive. Still, it's a good idea to turn them on to check your code on a regular basis (e.g. in CI/CD pipelines).
Note that, in some cases, the linter may generate false warnings due to source code analysis limitations or special cases that, while valid when intended, usually result from programming issues. When a code rewrite is not a sensible solution to avoid the warning, the workaround is to turn off as locally as possible the flag that controls the warning.
Lint checks include:
phrase/2-3
built-in methods)lgtunit
, lgtdoc
, make
,
and dead_code_scanner
tools. For large projects, the data generated by
the code_metrics
tool may also be relevant in accessing code quality
and suggesting code refactoring candidates.
By loading the tutor
tool, most lint warnings are expanded with explanations
and suggestions on how to fix the reported issues. See also the
coding guidelines for
additional explanations.
Experimental support for extending the linter with user-defined warnings is
available using the [logtalk_linter_hook/7](../refman/predicates/logtalk_linter_hook_7.html)
multifile hook predicate. For example, the format
and list
library objects
define this hook predicate to lint calls to the format/2-3
and append/3
predicates for common errors and misuses.
This tool can also be applied to Prolog modules that Logtalk is able to compile
as objects. For example, if the Prolog module file is named module.pl
, try:
| ?- logtalk_load(module, [source_data(on)])
.
Due to the lack of standardization of module systems and the abundance of proprietary extensions, this solution is not expected to work for all cases.
This tool can also be applied to plain Prolog code. For example, if the Prolog
file is named code.pl
, simply define an object including its code:
:- object(code). :- include('code.pl'). :- end_object.
Save the object to an e.g. code.lgt
file in the same directory as the
Prolog file and then load it:
| ?- logtalk_load(code, [source_data(on)])
.
In alternative, use the object_wrapper_hook
provided by the hook_objects
library:
| ?- logtalk_load(hook_objects(loader))
.
...
| ?- logtalk_load(code, [hook(object_wrapper_hook), source_data(on)])
.
With either wrapping solution, pay special attention to any compilation warnings that may signal issues that could prevent the plain Prolog from being fully checked when wrapped by an object.