|
1 |
| -WIP |
| 1 | +Dart_frog_lint is a developer tool for users of dart_frog, to help spot commong mistakes. |
| 2 | +It helps verify that file conventions are properly respected. |
| 3 | + |
| 4 | +## Table of content |
| 5 | + |
| 6 | +- [Table of content](#table-of-content) |
| 7 | +- [Installing dart\_frog\_lint](#installing-dart_frog_lint) |
| 8 | +- [Enabling/disabling lints.](#enablingdisabling-lints) |
| 9 | + - [Disable one specific rule](#disable-one-specific-rule) |
| 10 | + - [Disable all lints by default](#disable-all-lints-by-default) |
| 11 | +- [Running dart\_frog\_lint in the terminal/CI](#running-dart_frog_lint-in-the-terminalci) |
| 12 | +- [All the lints](#all-the-lints) |
| 13 | + - [dart\_frog\_entrypoint](#dart_frog_entrypoint) |
| 14 | + - [dart\_frog\_middleware](#dart_frog_middleware) |
| 15 | + - [dart\_frog\_route](#dart_frog_route) |
| 16 | + |
| 17 | +## Installing dart_frog_lint |
| 18 | + |
| 19 | +Dart_frog_lint is implemented using [custom_lint]. As such, it uses custom_lint's installation logic. |
| 20 | +Long story short: |
| 21 | + |
| 22 | +- Add both dart_frog_lint and custom_lint to your `pubspec.yaml`: |
| 23 | + ```yaml |
| 24 | + dev_dependencies: |
| 25 | + custom_lint: |
| 26 | + dart_frog_lint: |
| 27 | + ``` |
| 28 | +- Enable `custom_lint`'s plugin in your `analysis_options.yaml`: |
| 29 | + |
| 30 | + ```yaml |
| 31 | + analyzer: |
| 32 | + plugins: |
| 33 | + - custom_lint |
| 34 | + ``` |
| 35 | + |
| 36 | +## Enabling/disabling lints. |
| 37 | + |
| 38 | +By default when installing dart_frog_lint, most of the lints will be enabled. |
| 39 | +To change this, you have a few options. |
| 40 | + |
| 41 | +### Disable one specific rule |
| 42 | + |
| 43 | +You may dislike one of the various lint rules offered by dart_frog_lint. |
| 44 | +In that event, you can explicitly disable this lint rule for your project |
| 45 | +by modifying the `analysis_options.yaml` |
| 46 | + |
| 47 | +```yaml |
| 48 | +analyzer: |
| 49 | + plugins: |
| 50 | + - custom_lint |
| 51 | +
|
| 52 | +custom_lint: |
| 53 | + rules: |
| 54 | + # Explicitly disable one lint rule |
| 55 | + - dart_frog_request: false |
| 56 | +``` |
| 57 | + |
| 58 | +Note that you can both enable and disable lint rules at once. |
| 59 | +This can be useful if your `analysis_options.yaml` includes another one: |
| 60 | + |
| 61 | +```yaml |
| 62 | +include: path/to/another/analys_options.yaml |
| 63 | +analyzer: |
| 64 | + plugins: |
| 65 | + - custom_lint |
| 66 | +
|
| 67 | +custom_lint: |
| 68 | + rules: |
| 69 | + # Enable one rule |
| 70 | + - dart_frog_middleware |
| 71 | + # Disable another |
| 72 | + - dart_frog_request: false |
| 73 | +``` |
| 74 | + |
| 75 | +### Disable all lints by default |
| 76 | + |
| 77 | +Instead of having all lints on by default and manually disabling lints of your choice, |
| 78 | +you can switch to the opposite logic: |
| 79 | +Have lints off by default, and manually enable lints. |
| 80 | + |
| 81 | +This can be done in your `analysis_options.yaml` with the following: |
| 82 | + |
| 83 | +```yaml |
| 84 | +analyzer: |
| 85 | + plugins: |
| 86 | + - custom_lint |
| 87 | +
|
| 88 | +custom_lint: |
| 89 | + # Forcibly disable lint rules by default |
| 90 | + enable_all_lint_rules: false |
| 91 | + rules: |
| 92 | + # You can now enable one specific rule in the "rules" list |
| 93 | + - missing_provider_scope |
| 94 | +``` |
| 95 | + |
| 96 | +## Running dart_frog_lint in the terminal/CI |
| 97 | + |
| 98 | +Custom lint rules created by dart_frog_lint may not show-up in `dart analyze`. |
| 99 | +To fix this, you can run a custom command line: `custom_lint`. |
| 100 | + |
| 101 | +Since your project should already have custom_lint installed |
| 102 | +(cf [installing dart_frog_lint](#installing-dart_frog_lint)), then you should be |
| 103 | +able to run: |
| 104 | + |
| 105 | +```sh |
| 106 | +dart run custom_lint |
| 107 | +``` |
| 108 | + |
| 109 | +Alternatively, you can globally install `custom_lint`: |
| 110 | + |
| 111 | +```sh |
| 112 | +# Install custom_lint for all projects |
| 113 | +dart pub global activate custom_lint |
| 114 | +# run custom_lint's command line in a project |
| 115 | +custom_lint |
| 116 | +``` |
| 117 | + |
| 118 | +## All the lints |
| 119 | + |
| 120 | +### dart_frog_entrypoint |
| 121 | + |
| 122 | +The `dart_frog_entrypoint` lint checks that `main.dart` files contain a |
| 123 | +valid `run` function. See also [Creating a custom entrypoint](https://dartfrog.vgv.dev/docs/advanced/custom_entrypoint). |
| 124 | + |
| 125 | +**Good**: |
| 126 | + |
| 127 | +```dart |
| 128 | +// main.dart |
| 129 | +Future<HttpServer> run(Handler handler, InternetAddress ip, int port) { |
| 130 | + // TODO |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +**Bad**: |
| 135 | + |
| 136 | +```dart |
| 137 | +// An empty main.dart file |
| 138 | +// The file must contain a top-level function named "run" |
| 139 | +``` |
| 140 | + |
| 141 | +```dart |
| 142 | +// main.dart |
| 143 | +
|
| 144 | +// A "run" function is present, but the return value or parameters are incorrrect |
| 145 | +void run() {} |
| 146 | +``` |
| 147 | + |
| 148 | +### dart_frog_middleware |
| 149 | + |
| 150 | +The `dart_frog_middleware` lint checks that `routes/*_middleware.dart` files contain a |
| 151 | +valid `middleware` function. See also [Middlewares](https://dartfrog.vgv.dev/docs/basics/middleware). |
| 152 | + |
| 153 | +**Good**: |
| 154 | + |
| 155 | +```dart |
| 156 | +// routes/my_middleware.dart |
| 157 | +Handler middleware(Handler handler) { |
| 158 | + return (context) async { |
| 159 | + // TODO |
| 160 | + }; |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +**Bad**: |
| 165 | + |
| 166 | +```dart |
| 167 | +// routes/my_middleware.dart |
| 168 | +// The file must contain a valid top-level "middleware" function |
| 169 | +``` |
| 170 | + |
| 171 | +```dart |
| 172 | +// routes/my_middleware.dart |
| 173 | +// The file must contain a valid top-level "middleware" function |
| 174 | +
|
| 175 | +// A "middleware" function is present, but the return value or parameters are incorrrect |
| 176 | +void middleware() {} |
| 177 | +``` |
| 178 | + |
| 179 | +### dart_frog_route |
| 180 | + |
| 181 | +The `dart_frog_route` lint checks that `routes/*.dart` files contain a |
| 182 | +valid `onRequest` function. See also [Routes](https://dartfrog.vgv.dev/docs/basics/routes). |
| 183 | + |
| 184 | +**Good**: |
| 185 | + |
| 186 | +```dart |
| 187 | +// routes/hello.dart |
| 188 | +Response onRequest(RequestContext context) { |
| 189 | + // TODO |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +```dart |
| 194 | +// routes/posts/[id].dart |
| 195 | +// Dynamic routes are supported too |
| 196 | +Response onRequest(RequestContext context, String id) { |
| 197 | + // TODO |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +```dart |
| 202 | +// routes/example.dart |
| 203 | +// Routes can return a Future<T> |
| 204 | +Future<Response> onRequest(RequestContext context) async { |
| 205 | + // TODO |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +**Bad**: |
| 210 | + |
| 211 | +```dart |
| 212 | +// routes/hello.dart |
| 213 | +// The file must contain a valid top-level "onRequest" function |
| 214 | +``` |
| 215 | + |
| 216 | +```dart |
| 217 | +// routes/hello.dart |
| 218 | +// An "onRequest" function is present, but the return value or parameters are incorrrect |
| 219 | +void onRequest() {} |
| 220 | +``` |
| 221 | + |
| 222 | +```dart |
| 223 | +// routes/posts/[id].dart |
| 224 | +// The route is a dynamic route, but onRequest doesn't receive the correct number of parameters. |
| 225 | +Response onRequest(RequestContext context) { |
| 226 | + // TODO |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +[custom_lint]: https://pub.dev/packages/custom_lint |
0 commit comments