2019-09-28 18:06:15 +03:00
# elm-review
2017-01-18 19:09:15 +03:00
2019-09-28 18:06:15 +03:00
![](https://travis-ci.com/jfmengels/elm-review.svg?branch=master)
2019-08-01 10:30:40 +03:00
2020-03-29 22:02:42 +03:00
`elm-review` analyzes [Elm ](https://elm-lang.org/ ) projects, to help find mistakes before your users find them.
2017-01-18 19:09:15 +03:00
2020-06-20 18:52:17 +03:00
[![elm-review reporter output ](https://github.com/jfmengels/elm-review/blob/2.2.0/documentation/images/elm-review-report.png?raw=true )](https://github.com/jfmengels/elm-review/blob/2.0.1/documentation/images/elm-review-report-hq.png?raw=true)
2019-08-30 02:33:57 +03:00
2019-09-28 18:06:15 +03:00
## What does `elm-review` do?
2017-01-18 19:09:15 +03:00
2020-04-01 21:13:34 +03:00
`elm-review` analyzes your source code, trying to recognize code that is known to cause problems.
2022-04-26 12:14:07 +03:00
All the rules describing problematic code are written in Elm, and `elm-review` does not come with built-in rules;
2020-04-01 21:13:34 +03:00
instead users are encouraged to write rules themselves and publish them as Elm packages, for everyone to benefit.
[Search the package registry ](https://klaftertief.github.io/elm-search/?q=Review.Rule.Rule ) to find what's out there!
2017-01-18 19:09:15 +03:00
2020-04-01 21:13:34 +03:00
Encouraging users to write rules also makes it easy to add custom rules that only apply to your project.
Such as rules that:
2019-06-24 15:29:36 +03:00
2020-04-01 21:13:34 +03:00
- enforce that e.g. image paths only live in an `Images` module, which other modules can reference.
- make everyone use a common `Button` component, instead of creating their own.
- help users of a library you made, to avoid making mistakes that your API could not prevent them from doing.
2019-06-24 15:29:36 +03:00
2020-04-01 21:13:34 +03:00
Beware how and why you introduce rules in your project though.
Often a good API, that guides users to correct solutions, is the best way to go, so instead of writing a rule, maybe there is an API that can be improved?
But if a rule seems like the best solution, remember to discuss it with your team.
It's easy to mix up patterns that are objectively bad, with patterns that you personally find problematic, and forbidding patterns that other people find useful can be very disruptive.
2019-06-24 15:29:36 +03:00
2020-07-25 23:40:28 +03:00
## Try it out
2017-01-18 19:09:15 +03:00
2020-04-01 21:13:34 +03:00
The easiest way to run `elm-review` , if you have `Node.js` and `npm` installed, is to use the [`elm-review` CLI tool ](https://github.com/jfmengels/node-elm-review ).
2017-01-18 19:09:15 +03:00
2019-09-28 18:06:15 +03:00
```bash
# Save it to your package.json, if you use npm in your project.
# This is the recommended way.
npm install elm-review --save-dev
# Install globally. This is not recommended.
npm install -g elm-review
```
2020-07-25 23:40:28 +03:00
You can also **try it out without installing it or configuring it** if you have Node.js installed.
2020-08-09 20:01:13 +03:00
All you need is to find a configuration on GitHub, and note that `elm-review` packages are encouraged to provide an example one. Once you found the configuration, run `elm-review` by specifying the name of the GitHub repository and the path to the configuration:
2020-07-25 23:40:28 +03:00
```bash
2020-10-26 17:28:54 +03:00
npx elm-review --template jfmengels/elm-review-unused/example
2020-07-25 23:40:28 +03:00
```
Do you want to find and remove all the dead code in your project? Then run the following command (this might take a while if your project has a lot of dead code though!), and think about whether you want this goodness in your project!
```bash
2020-10-26 17:28:54 +03:00
npx elm-review --template jfmengels/elm-review-unused/example --fix-all
2020-07-25 23:40:28 +03:00
```
More information on that in [the CLI documentation ](https://github.com/jfmengels/node-elm-review#try-it-out ).
2017-01-18 19:09:15 +03:00
2019-08-22 11:43:53 +03:00
## Configuration
2022-04-26 12:14:07 +03:00
`elm-review` is configured through a `review/` folder in your project. It is a self-contained Elm project where you can
specify your dependencies, and write, import, and configure review rules.
Rules are configured in the `review/ReviewConfig.elm` file:
2019-08-22 11:43:53 +03:00
```elm
2019-09-28 18:06:15 +03:00
module ReviewConfig exposing (config)
2019-08-22 11:43:53 +03:00
2019-09-28 18:06:15 +03:00
import Review.Rule exposing (Rule)
2019-08-22 11:43:53 +03:00
import Third.Party.Rule
import My.Own.Custom.rule
import Another.Rule
config : List Rule
config =
[ Third.Party.Rule.rule
, My.Own.Custom.rule
, Another.Rule.rule { ruleOptions = [] }
]
```
2022-04-26 12:14:07 +03:00
## Get started
You can get started with a fresh configuration by running the `elm-review init` command with the command line tool installed,
which will add a `review` folder to your project.
You can also use an existing configuration using `elm-review init --template <some configuration>` .
I created [some configurations ](https://github.com/jfmengels/elm-review-config ) that I believe can be good **starting** points.
```bash
# Start with an empty configuration
elm-review init
# Starter configuration for an Elm application
elm-review init --template jfmengels/elm-review-config/application
# Starter configuration for an Elm package
elm-review init --template jfmengels/elm-review-config/package
```
Once you have set up an initial configuration, you can add new rules. As `elm-review` does not
[come with built-in rules ](https://github.com/jfmengels/elm-review/blob/master/documentation/design/no-built-in-rules.md ),
you can look for packages with rules on the [Elm package registry ](https://package.elm-lang.org/ ) by searching for packages named `elm-review-` .
Once you've found a package that you like, you can install it with the `elm install` command, just like any other Elm project dependency.
2017-06-19 01:30:56 +03:00
2019-09-20 14:39:11 +03:00
```bash
2019-09-28 18:06:15 +03:00
cd review/ # Go inside your review configuration directory
2019-09-20 14:39:11 +03:00
elm install authorName/packageName
2022-04-26 12:14:07 +03:00
# then update your `review/ReviewConfiguration` to add the rule
# as explained in the package's documentation
2019-09-18 12:49:33 +03:00
```
2019-08-22 12:42:43 +03:00
2022-04-26 12:14:07 +03:00
Before you start adding rules or an unfamiliar existing configuration, I suggest reading the rest of this document, especially the section on [when to enable a rule ](#when-to-write-or-enable-a-rule ).
2017-06-19 01:30:56 +03:00
2019-06-24 15:29:36 +03:00
## Write your own rule
2021-03-07 12:58:59 +03:00
You can write your own rule using this package's API and [`elm-syntax` ](https://package.elm-lang.org/packages/stil4m/elm-syntax/7.2.1/ ).
2022-04-26 12:21:45 +03:00
Check out the [`Review.Rule` ](https://package.elm-lang.org/packages/jfmengels/elm-review/2.7.2/Review-Rule/ ) documentation for how to get started.
2019-06-24 15:29:36 +03:00
2021-03-07 12:57:19 +03:00
**NOTE**: If you want to **create a package** containing `elm-review` rules, I highly recommend using the
[CLI's ](https://github.com/jfmengels/node-elm-review/ ) `elm-review new-package` subcommand. This will create a new package that will help you use the best practices and give you helpful tools like easy auto-publishing. More information is available in the maintenance file generated along with it.
If you want to **add/create a rule** for the package or for your local configuration, then I recommend using `elm-review new-rule` , which will create a source and test file which you can use as a starting point. For packages, it will add the rule everywhere it should be present (`exposed-modules`, README, ...).
2020-08-09 20:01:13 +03:00
2020-04-01 21:13:34 +03:00
Here's an example of a rule that prevents a typo in a string that was made too often at your company.
2019-06-24 15:29:36 +03:00
```elm
2019-07-27 21:36:28 +03:00
module NoStringWithMisspelledCompanyName exposing (rule)
2019-06-24 15:29:36 +03:00
2020-06-08 15:03:24 +03:00
import Elm.Syntax.Expression as Expression exposing (Expression)
2019-06-24 15:29:36 +03:00
import Elm.Syntax.Node as Node exposing (Node)
2019-09-28 18:06:15 +03:00
import Review.Rule as Rule exposing (Error, Rule)
2019-06-24 15:29:36 +03:00
2019-08-22 12:04:59 +03:00
-- Create a new rule
2019-06-24 15:29:36 +03:00
rule : Rule
rule =
2019-08-22 12:04:59 +03:00
-- Define the rule with the same name as the module it is defined in
2020-01-19 22:37:19 +03:00
Rule.newModuleRuleSchema "NoStringWithMisspelledCompanyName" ()
2019-08-22 12:04:59 +03:00
-- Make it look at expressions
2019-07-27 21:36:28 +03:00
|> Rule.withSimpleExpressionVisitor expressionVisitor
2020-01-19 22:37:19 +03:00
|> Rule.fromModuleRuleSchema
2019-06-24 15:29:36 +03:00
2019-08-22 12:04:59 +03:00
-- This function will visit all the expressions (like `1` , `"string"` , `foo bar` , `a + b` , ...)
-- and report problems that it finds
2020-04-05 14:14:18 +03:00
expressionVisitor : Node Expression -> List (Error {})
2019-07-27 21:36:28 +03:00
expressionVisitor node =
case Node.value node of
2019-08-22 12:04:59 +03:00
-- It will look at string literals (like "a", """a""")
2020-04-05 14:14:18 +03:00
Expression.Literal str ->
2019-07-27 21:36:28 +03:00
if String.contains "frits.com" str then
2019-08-22 12:04:59 +03:00
-- Return a single error, describing the problem
2019-07-27 21:36:28 +03:00
[ Rule.error
{ message = "Replace `frits.com` by `fruits.com` "
, details = [ "This typo has been made and noticed by users too many times. Our company is `fruits.com` , not `frits.com` ." ]
}
2019-08-22 12:04:59 +03:00
-- This is the location of the problem in the source code
2019-07-27 21:36:28 +03:00
(Node.range node)
]
else
[]
2019-06-24 15:29:36 +03:00
_ ->
[]
```
Then add the rule in your configuration:
```elm
2019-09-28 18:06:15 +03:00
module ReviewConfig exposing (config)
2019-06-24 15:29:36 +03:00
2019-07-27 21:36:28 +03:00
import NoStringWithMisspelledCompanyName
2020-06-20 18:52:17 +03:00
import Review.Rule exposing (Rule)
2019-06-24 15:29:36 +03:00
2019-07-25 15:35:58 +03:00
config : List Rule
2019-06-24 15:29:36 +03:00
config =
2019-07-27 21:36:28 +03:00
[ NoStringWithMisspelledCompanyName.rule
2019-06-24 15:29:36 +03:00
-- other rules...
]
```
2020-08-14 18:08:46 +03:00
If you want to write a rule but might not have an idea of where to start,
have a look on the [elm-review-rule-ideas ](https://github.com/jfmengels/elm-review-rule-ideas )
repository. You may also want to look for rules in the [Elm packages registry ](https://package.elm-lang.org/ ) and in the [GitHub `elm-review` topic ](https://github.com/topics/elm-review ).
2019-06-24 15:29:36 +03:00
2019-07-28 15:52:11 +03:00
## When to write or enable a rule
2020-04-01 21:13:34 +03:00
The bar to write or enable a rule should be pretty high.
A new rule can often turn out to be a nuisance to someone, sometimes in ways you didn't predict, so making sure the rule solves a real problem, and that your team is on board with it, is important.
If a developer disagrees with a rule, they may try to circumvent it, resulting in code that is even more error prone than the pattern that was originally forbidden.
So the value provided by the rule should be much greater than the trouble it causes, and if you find that a rule doesn't live up to this, consider disabling it.
Review rules are most useful when some pattern must never appear in the code.
2021-11-11 12:54:09 +03:00
It gets less useful when a pattern is allowed to appear in certain cases, as there is [no good solution for handling exceptions to rules ](#is-there-a-way-to-ignore-errors- ).
2020-04-01 21:13:34 +03:00
If you really need to make exceptions, they must be written in the rule itself, or the rule should be configurable.
For rules that enforce a certain **coding style** , or suggest simplifications to your code, I would ask you to raise the bar for inclusion even higher.
A few examples:
2019-07-28 15:52:11 +03:00
- I much prefer using `|>` over `<|` , and I think using the latter to pipe
2019-08-22 11:43:53 +03:00
functions over several lines is harder to read. Even if using `|>` was indeed
better for most situations and even if my teammates agree, this would prevent
me from writing tests [the suggested way ](https://github.com/elm-explorations/test#quick-start )
for instance.
2019-07-28 15:52:11 +03:00
- If a record contains only one field, then I could suggest not using a record
2020-04-01 21:13:34 +03:00
and use the field directly, which would make things simpler. But using a
2019-07-28 15:52:11 +03:00
record can have the advantage of being more explicit: `findFiles [] folder` is
harder to understand than `findFiles { exceptions = [] } folder` .
2020-04-01 21:13:34 +03:00
Some rules might suggest using advanced techniques to avoid pitfalls, which can make it harder for newcomers to get something done.
When enabling this kind of rule, make sure that the message it gives is helpful enough to unblock users.
2019-07-28 15:52:11 +03:00
2020-04-01 21:13:34 +03:00
When wondering whether to enable a rule, I suggest using this checklist:
2020-04-01 21:25:27 +03:00
- [ ] I have had problems with the pattern I want to forbid.
- [ ] I could not find a way to solve the problem by changing the API of the problematic code or introducing a new API.
- [ ] If the rule exists, I have read its documentation and the section about when not to enable the rule, and it doesn't apply to my situation.
- [ ] I have thought very hard about what the corner cases could be and what kind of patterns this would forbid that are actually okay, and they are acceptable.
- [ ] I think the rule explains well enough how to solve the issue, to make sure beginners are not blocked by it.
- [ ] I have communicated with my teammates and they all agree to enforce the rule.
- [ ] I am ready to disable the rule if it turns out to be more disturbing than helpful.
2019-07-28 15:52:11 +03:00
2021-11-11 12:54:09 +03:00
## Is there a way to ignore errors?
2017-06-19 01:30:56 +03:00
2021-11-11 12:54:09 +03:00
`elm-review` does not provide a way to disable errors on a case-by-case basis — by line or sections of code —
like a lot of static analysis tools do. I've written about this in
_[How disable comments make static analysis tools worse](https://jfmengels.net/disable-comments/)_.
2020-02-24 21:32:07 +03:00
2021-11-11 12:54:09 +03:00
Because you can't ignore errors easily, `elm-review` puts more burden on the rules, requiring them to be of higher quality
— less false positives — and better designed — avoiding rules that will inherently have lots of exceptions or false positives.
2020-02-24 21:32:07 +03:00
2021-11-11 12:54:09 +03:00
It does provide 2 systems that I think are better alternatives for the health of your project.
### Configuring exceptions
2022-04-26 12:21:45 +03:00
You can [configure exceptions ](https://package.elm-lang.org/packages/jfmengels/elm-review/2.7.2/Review-Rule/#configuring-exceptions ),
2021-11-11 12:54:09 +03:00
which consists of marking specific directories or files as not relevant to a rule or set of rules, preventing errors to be reported for those.
It is a good fit if you wish for `elm-review` to not report errors in vendored or generated code,
or in files and directories that by the nature of the rule should be exempted.
### Temporarily suppressing errors
**NOTE:** This is only available starting from v2.7.0 of the `elm-review` CLI onwards, which at the time of writing is in a beta version.
Run `npm install elm-review@beta` to start using this. Please provide feedback on any of aspect of the feature, so that we can get this out of beta.
2021-11-12 21:28:01 +03:00
`elm-review` has a system to temporarily suppressed errors which aims to help you gradually adopt rules that
2021-11-11 12:54:09 +03:00
report many errors in your project without having you fix all the issues beforehand.
Running `elm-review suppress` will generate one JSON file in `review/suppressed/` (in your review configuration) for
every rule that currently reports errors, and records the number of suppressed errors per file in your project.
2021-11-13 02:20:58 +03:00
These files should be included in your versioning system.
2021-11-11 12:54:09 +03:00
As long as suppression files exist for your project, running `elm-review` will behave as usual but with these additional behaviors:
- Suppressed errors won't be reported.
- If there are outstanding errors for the ignored rules and files, the related
suppressed errors will be reported until you reduce the number of errors
back to the number in the JSON file. This is a good opportunity to fix more!
- If no errors are being reported and there are less suppressed errors than
before, suppression files will be updated automatically, in order to make
sure no new errors get re-introduced unknowingly.
While you can run the `suppress` command to ignore newly reported errors, please do so with moderation. The aim is to
allow enabling rules while there are errors remaining and to have these fixed incrementally, not to make it easier to
ignore errors.
2021-11-12 21:28:01 +03:00
Use `elm-review suppress --help` to start using this, and read more about the
[design choices ](https://github.com/jfmengels/elm-review/blob/master/documentation/design/suppress.md ) that went into
the feature.
2021-11-11 12:54:09 +03:00
Note that to avoid uncommitted suppression files in your project's main branch, it is recommended to use
`elm-review suppress --check-after-tests` at the end of your test suite.