2016-08-19 01:00:06 +02:00
|
|
|
# Linters
|
|
|
|
|
2021-07-01 00:24:15 +02:00
|
|
|
A linter is a program that runs on the source code of software and
|
|
|
|
reports potentially problematic code to the user. Linters help enforce
|
|
|
|
coding guidelines, from minor details like whitespace formatting or
|
|
|
|
capitalization patterns for strings to preventing problematic coding
|
|
|
|
patterns that can lead to security bugs.
|
|
|
|
|
2016-08-19 01:00:06 +02:00
|
|
|
## Overview
|
|
|
|
|
|
|
|
Zulip does extensive linting of much of its source code, including
|
2022-06-27 22:35:01 +02:00
|
|
|
Python/JavaScript/TypeScript files, HTML templates (Django/Handlebars), CSS files,
|
2016-08-19 01:00:06 +02:00
|
|
|
JSON fixtures, Markdown documents, puppet manifests, and shell scripts.
|
|
|
|
|
|
|
|
For some files we simply check for small things like trailing whitespace,
|
|
|
|
but for other files, we are quite thorough about checking semantic
|
|
|
|
correctness.
|
|
|
|
|
|
|
|
Obviously, a large reason for linting code is to enforce the [Zulip
|
2021-08-20 21:53:28 +02:00
|
|
|
coding standards](../contributing/code-style.md). But we also use the linters to
|
2016-08-19 01:00:06 +02:00
|
|
|
prevent common coding errors.
|
|
|
|
|
|
|
|
We borrow some open source tools for much of our linting, and the links
|
|
|
|
below will direct you to the official documentation for these projects.
|
|
|
|
|
2021-02-13 00:21:10 +01:00
|
|
|
- [Black](https://github.com/psf/black)
|
2020-10-23 02:43:28 +02:00
|
|
|
- [ESLint](https://eslint.org)
|
2021-02-13 00:21:10 +01:00
|
|
|
- [isort](https://pycqa.github.io/isort/)
|
2016-08-19 01:00:06 +02:00
|
|
|
- [mypy](http://mypy-lang.org/)
|
2020-07-22 21:25:49 +02:00
|
|
|
- [Prettier](https://prettier.io/)
|
2020-10-23 02:43:28 +02:00
|
|
|
- [Puppet](https://puppet.com/) (puppet provides its own mechanism for
|
2016-12-03 01:46:10 +01:00
|
|
|
validating manifests)
|
2023-10-27 00:19:05 +02:00
|
|
|
- [ruff](https://github.com/astral-sh/ruff)
|
2018-07-25 14:07:24 +02:00
|
|
|
- [stylelint](https://github.com/stylelint/stylelint)
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
Zulip also uses some home-grown code to perform tasks like validating
|
|
|
|
indentation in template files, enforcing coding standards that are unique
|
|
|
|
to Zulip, allowing certain errors from third party linters to pass through,
|
|
|
|
and exempting legacy files from lint checks.
|
|
|
|
|
|
|
|
## Running the linters
|
|
|
|
|
2018-11-29 23:09:03 +01:00
|
|
|
If you run `./tools/test-all`, it will automatically run the linters.
|
|
|
|
You can also run them individually or pass specific files:
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
|
|
|
./tools/lint
|
2023-02-22 23:03:47 +01:00
|
|
|
./tools/lint web/src/compose.js
|
|
|
|
./tools/lint web/src/
|
2021-08-20 07:09:04 +02:00
|
|
|
```
|
2019-06-24 23:24:31 +02:00
|
|
|
|
|
|
|
`./tools/lint` has many useful options; you can read about them in its
|
2021-08-20 21:53:28 +02:00
|
|
|
internal documentation using `./tools/lint --help`. Of particular
|
2019-06-24 23:24:31 +02:00
|
|
|
note are:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- `--fix`: Several of our linters support automatically fixing basic
|
2019-06-24 23:24:31 +02:00
|
|
|
issues; this option will ask `tools/lint` to run those.
|
2021-08-20 21:45:39 +02:00
|
|
|
- `--verbose`: Provides detailed information on how to fix many common
|
2019-06-24 23:24:31 +02:00
|
|
|
linter errors not covered by `--fix`.
|
2021-08-20 21:45:39 +02:00
|
|
|
- `--skip` and `--only`: Only run certain linters.
|
|
|
|
- `-m`: Only check modified files.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2021-03-15 18:37:12 +01:00
|
|
|
Finally, you can rely on our continuous integration setup to run linters for you,
|
2020-04-28 20:26:58 +02:00
|
|
|
but it is good practice to run lint checks locally.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2021-08-10 07:55:12 +02:00
|
|
|
:::{important}
|
|
|
|
We provide a
|
2022-02-16 01:39:15 +01:00
|
|
|
[Git pre-commit hook](../git/zulip-tools.md#set-up-git-repo-script)
|
2021-08-10 07:55:12 +02:00
|
|
|
that can automatically run `tools/lint` on just the files that
|
2021-08-20 21:53:28 +02:00
|
|
|
changed (in a few 100ms) whenever you make a commit. This can save
|
2021-08-10 07:55:12 +02:00
|
|
|
you a lot of time, by automatically detecting linter errors as you
|
|
|
|
make them.
|
|
|
|
:::
|
2018-11-29 23:09:03 +01:00
|
|
|
|
2022-02-28 22:15:06 +01:00
|
|
|
**Note:** The linters only check files that Git tracks. Remember to `git add`
|
2017-01-25 03:12:05 +01:00
|
|
|
new files before running lint checks.
|
|
|
|
|
2016-08-19 01:00:06 +02:00
|
|
|
Our linting tools generally support the ability to lint files
|
|
|
|
individually--with some caveats--and those options will be described
|
|
|
|
later in this document.
|
|
|
|
|
|
|
|
## General considerations
|
|
|
|
|
2019-09-30 19:37:56 +02:00
|
|
|
Once you have read the [Zulip coding guidelines](../contributing/code-style.md), you can
|
2016-08-19 01:00:06 +02:00
|
|
|
be pretty confident that 99% of the code that you write will pass through
|
|
|
|
the linters fine, as long as you are thorough about keeping your code clean.
|
2017-04-21 23:07:06 +02:00
|
|
|
And, of course, for minor oversights, `lint` is your friend, not your foe.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
Occasionally, our linters will complain about things that are more of
|
|
|
|
an artifact of the linter limitations than any actual problem with your
|
2021-08-20 21:53:28 +02:00
|
|
|
code. There is usually a mechanism where you can bypass the linter in
|
2016-08-19 01:00:06 +02:00
|
|
|
extreme cases, but often it can be a simple matter of writing your code
|
2021-08-20 21:53:28 +02:00
|
|
|
in a slightly different style to appease the linter. If you have
|
2016-08-19 01:00:06 +02:00
|
|
|
problems getting something to lint, you can submit an unfinished PR
|
|
|
|
and ask the reviewer to help you work through the lint problem, or you
|
2021-12-09 20:15:18 +01:00
|
|
|
can find other people in the [Zulip Community](https://zulip.com/development-community/)
|
2016-08-19 01:00:06 +02:00
|
|
|
to help you.
|
|
|
|
|
|
|
|
Also, bear in mind that 100% of the lint code is open source, so if you
|
|
|
|
find limitations in either the Zulip home-grown stuff or our third party
|
|
|
|
tools, feedback will be highly appreciated.
|
|
|
|
|
|
|
|
Finally, one way to clean up your code is to thoroughly exercise it
|
2022-02-24 00:17:21 +01:00
|
|
|
with tests. The [Zulip test documentation](testing.md)
|
2016-08-19 01:00:06 +02:00
|
|
|
describes our test system in detail.
|
|
|
|
|
|
|
|
## Lint checks
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
Most of our lint checks get performed by `./tools/lint`. These include the
|
2016-08-19 01:00:06 +02:00
|
|
|
following checks:
|
|
|
|
|
2022-10-30 00:07:03 +02:00
|
|
|
- Check Python code with ruff.
|
2021-02-13 00:21:10 +01:00
|
|
|
- Check Python formatting with Black and isort.
|
2020-10-23 02:43:28 +02:00
|
|
|
- Check JavaScript and TypeScript code with ESLint.
|
2020-08-04 23:58:56 +02:00
|
|
|
- Check CSS, JavaScript, TypeScript, and YAML formatting with Prettier.
|
2016-08-19 01:00:06 +02:00
|
|
|
- Check Python code for custom Zulip rules.
|
|
|
|
- Check non-Python code for custom Zulip rules.
|
2020-10-23 02:43:28 +02:00
|
|
|
- Check Puppet manifests with the Puppet validator.
|
2016-08-19 01:00:06 +02:00
|
|
|
- Check HTML templates for matching tags and indentations.
|
2018-07-25 14:07:24 +02:00
|
|
|
- Check CSS for parsability and formatting.
|
2016-08-19 01:00:06 +02:00
|
|
|
- Check JavaScript code for addClass calls.
|
2021-08-20 21:53:28 +02:00
|
|
|
- Running `mypy` to check static types in Python code. Our
|
2022-02-24 00:17:21 +01:00
|
|
|
[documentation on using mypy](mypy.md) covers mypy in
|
2018-12-17 05:58:06 +01:00
|
|
|
more detail.
|
2021-08-20 21:53:28 +02:00
|
|
|
- Running `tsc` to compile TypeScript code. Our [documentation on
|
2019-09-30 19:37:56 +02:00
|
|
|
TypeScript](typescript.md) covers TypeScript in more detail.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2017-04-21 23:07:06 +02:00
|
|
|
The rest of this document pertains to the checks that occur in `./tools/lint`.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2017-04-21 23:07:06 +02:00
|
|
|
## lint
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2017-04-21 23:07:06 +02:00
|
|
|
Zulip has a script called `lint` that lives in our "tools" directory.
|
2016-08-19 01:00:06 +02:00
|
|
|
It is the workhorse of our linting system, although in some cases it
|
2022-10-30 00:07:03 +02:00
|
|
|
dispatches the heavy lifting to other components such as ruff,
|
2016-12-03 01:46:10 +01:00
|
|
|
eslint, and other home grown tools.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2021-09-01 00:15:31 +02:00
|
|
|
You can find the source code [here](https://github.com/zulip/zulip/blob/main/tools/lint).
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2017-04-21 23:07:06 +02:00
|
|
|
In order for our entire lint suite to run in a timely fashion, the `lint`
|
2019-07-28 01:23:13 +02:00
|
|
|
script performs several lint checks in parallel by forking out subprocesses.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2022-10-30 00:07:03 +02:00
|
|
|
Note that our project does custom regex-based checks on the code. The code for these
|
2021-09-01 00:15:31 +02:00
|
|
|
types of checks mostly lives [here](https://github.com/zulip/zulip/tree/main/tools/linter_lib).
|
2017-06-05 17:02:58 +02:00
|
|
|
|
2016-08-19 01:00:06 +02:00
|
|
|
### Special options
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
You can use the `-h` option for `lint` to see its usage. One particular
|
2016-08-19 01:00:06 +02:00
|
|
|
flag to take note of is the `--modified` flag, which enables you to only run
|
2021-08-20 21:53:28 +02:00
|
|
|
lint checks against files that are modified in your Git repo. Most of the
|
2016-08-19 01:00:06 +02:00
|
|
|
"sub-linters" respect this flag, but some will continue to process all the files.
|
|
|
|
Generally, a good workflow is to run with `--modified` when you are iterating on
|
2017-11-09 16:26:38 +01:00
|
|
|
the code, and then run without that option right before committing new code.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2019-06-18 15:27:10 +02:00
|
|
|
If you need to troubleshoot the linters, there is a `--verbose-timing`
|
|
|
|
option that can give you clues about which linters may be running
|
|
|
|
slow, for example.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
### Lint checks
|
|
|
|
|
|
|
|
The next part of this document describes the lint checks that we apply to
|
|
|
|
various file types.
|
|
|
|
|
|
|
|
#### Generic source code checks
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
We check almost our entire codebase for trailing whitespace. Also, we
|
2016-08-19 01:00:06 +02:00
|
|
|
disallow tab (\t) characters in all but two files.
|
|
|
|
|
|
|
|
We also have custom regex-based checks that apply to specific file types.
|
|
|
|
For relatively minor files like Markdown files and JSON fixtures, this
|
|
|
|
is the extent of our checking.
|
|
|
|
|
2016-08-19 21:08:22 +02:00
|
|
|
Finally, we're checking line length in Python code (and hope to extend
|
2021-08-20 21:53:28 +02:00
|
|
|
this to other parts of the codebase soon). You can use
|
2016-08-19 21:08:22 +02:00
|
|
|
`#ignorelinelength` for special cases where a very long line makes
|
|
|
|
sense (e.g. a link in a comment to an extremely long URL).
|
|
|
|
|
2016-08-19 01:00:06 +02:00
|
|
|
#### Python code
|
|
|
|
|
2021-02-13 00:21:10 +01:00
|
|
|
Our Python code is formatted using Black (using the options in the
|
|
|
|
`[tool.black]` section of `pyproject.toml`) and isort (using the
|
2021-08-20 21:53:28 +02:00
|
|
|
options in `.isort.cfg`). The `lint` script enforces this by running
|
2021-02-13 00:21:10 +01:00
|
|
|
Black and isort in check mode, or in write mode with `--fix`.
|
|
|
|
|
2022-10-30 00:07:03 +02:00
|
|
|
The bulk of our Python linting gets outsourced to the "ruff" tool,
|
|
|
|
which is configured in the `tool.ruff` section of `pyproject.toml`.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
Zulip also has custom regex-based rules that it applies to Python code.
|
2021-08-20 21:53:28 +02:00
|
|
|
Look for `python_rules` in the source code for `lint`. Note that we
|
2017-01-15 05:13:22 +01:00
|
|
|
provide a mechanism to exclude certain lines of codes from these checks.
|
2016-08-19 01:00:06 +02:00
|
|
|
Often, it is simply the case that our regex approach is too crude to
|
2021-08-20 21:53:28 +02:00
|
|
|
correctly exonerate certain valid constructs. In other cases, the code
|
2016-08-19 01:00:06 +02:00
|
|
|
that we exempt may be deemed not worthwhile to fix.
|
|
|
|
|
|
|
|
#### JavaScript code
|
|
|
|
|
|
|
|
We check our JavaScript code in a few different ways:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2016-12-03 01:46:10 +01:00
|
|
|
- We run eslint.
|
2020-07-22 21:25:49 +02:00
|
|
|
- We check code formatting with Prettier.
|
2016-08-19 01:00:06 +02:00
|
|
|
- We perform custom Zulip regex checks on the code.
|
|
|
|
|
|
|
|
#### Puppet manifests
|
|
|
|
|
|
|
|
We use Puppet as our tool to manage configuration files, using
|
2021-08-20 21:53:28 +02:00
|
|
|
Puppet "manifests." To lint Puppet manifests, we use the "parser validate"
|
2020-10-23 02:43:28 +02:00
|
|
|
option of Puppet.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
#### HTML templates
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
Zulip uses two HTML templating systems:
|
|
|
|
|
2024-05-24 16:57:31 +02:00
|
|
|
- [Django templates](https://docs.djangoproject.com/en/5.0/topics/templates/)
|
2020-03-21 18:17:19 +01:00
|
|
|
- [handlebars](https://handlebarsjs.com/)
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2017-01-15 05:13:22 +01:00
|
|
|
Zulip has an internal tool that validates both types of templates for
|
2021-08-20 21:53:28 +02:00
|
|
|
correct indentation and matching tags. You can find the code here:
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2021-09-01 00:15:31 +02:00
|
|
|
- driver: [check-templates](https://github.com/zulip/zulip/blob/main/tools/check-templates)
|
|
|
|
- engine: [lib/template_parser.py](https://github.com/zulip/zulip/blob/main/tools/lib/template_parser.py)
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
We exempt some legacy files from indentation checks, but we are hoping to
|
|
|
|
clean those files up eventually.
|
|
|
|
|
|
|
|
#### CSS
|
|
|
|
|
2018-07-25 14:07:24 +02:00
|
|
|
Zulip uses [stylelint](https://github.com/stylelint/stylelint) to lint
|
|
|
|
its CSS; see our
|
2021-09-01 00:15:31 +02:00
|
|
|
[configuration](https://github.com/zulip/zulip/blob/main/stylelint.config.js)
|
2018-07-25 14:07:24 +02:00
|
|
|
for the rules we currently enforce.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
2018-08-03 02:09:42 +02:00
|
|
|
#### Shell scripts
|
|
|
|
|
|
|
|
Zulip uses [shellcheck](https://github.com/koalaman/shellcheck) to
|
2021-08-20 21:53:28 +02:00
|
|
|
lint our shell scripts. We recommend the
|
2018-08-03 02:09:42 +02:00
|
|
|
[shellcheck gallery of bad code][shellcheck-bad-code] as a resource on
|
|
|
|
how to not write bad shell.
|
|
|
|
|
|
|
|
[shellcheck-bad-code]: https://github.com/koalaman/shellcheck/blob/master/README.md#user-content-gallery-of-bad-code
|
|
|
|
|
2016-08-19 01:00:06 +02:00
|
|
|
#### Markdown, shell scripts, JSON fixtures
|
|
|
|
|
2018-08-03 02:09:42 +02:00
|
|
|
We mostly validate miscellaneous source files like `.json`, and `.md`
|
|
|
|
files for whitespace issues.
|
2016-08-19 01:00:06 +02:00
|
|
|
|
|
|
|
## Philosophy
|
|
|
|
|
|
|
|
If you want to help improve Zulip's system for linting, here are some
|
|
|
|
considerations.
|
|
|
|
|
|
|
|
#### Speed
|
|
|
|
|
|
|
|
We want our linters to be fast enough that most developers
|
|
|
|
will feel comfortable running them in a pre-commit hook, so we run
|
|
|
|
our linters in parallel and support incremental checks.
|
|
|
|
|
|
|
|
#### Accuracy
|
|
|
|
|
|
|
|
We try to catch as many common mistakes as possible, either via a
|
|
|
|
linter or an automated test.
|
|
|
|
|
|
|
|
#### Completeness
|
|
|
|
|
|
|
|
Our goal is to have most common style issues by caught by the linters, so new
|
|
|
|
contributors to the codebase can efficiently fix produce code with correct
|
|
|
|
style without needing to go back-and-forth with a reviewer.
|