You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/pg_analyser/CONTRIBUTING.md
+22-7Lines changed: 22 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,7 @@ We follow a naming convention according to what the rule does:
35
35
A rule should be informative to the user, and give as much explanation as possible.
36
36
37
37
When writing a rule, you must adhere to the following **pillars**:
38
+
38
39
1. Explain to the user the error. Generally, this is the message of the diagnostic.
39
40
1. Explain to the user **why** the error is triggered. Generally, this is implemented with an additional node.
40
41
1. Tell the user what they should do. Generally, this is implemented using a code action. If a code action is not applicable a note should tell the user what they should do to fix the error.
@@ -53,6 +54,7 @@ Let's say we want to create a new **lint** rule called `useMyRuleName`, follow t
53
54
```shell
54
55
just new-lintrule safety useMyRuleName
55
56
```
57
+
56
58
The script will generate a bunch of files inside the `pg_analyser` crate.
57
59
Among the other files, you'll find a file called `use_my_new_rule_name.rs` inside the `pg_analyser/lib/src/lint/safety` folder. You'll implement your rule in this file.
58
60
@@ -127,6 +129,7 @@ The compiler should warn you that `MyRuleOptions` does not implement some requir
127
129
We currently require implementing _serde_'s traits `Deserialize`/`Serialize`.
128
130
129
131
Also, we use other `serde` macros to adjust the JSON configuration:
132
+
130
133
-`rename_all = "snake_case"`: it renames all fields in camel-case, so they are in line with the naming style of the `pglsp.toml`.
131
134
-`deny_unknown_fields`: it raises an error if the configuration contains extraneous fields.
132
135
-`default`: it uses the `Default` value when the field is missing from `pglsp.toml`. This macro makes the field optional.
@@ -159,7 +162,6 @@ pub enum Behavior {
159
162
160
163
Below, there are many tips and guidelines on how to create a lint rule using our infrastructure.
161
164
162
-
163
165
#### `declare_lint_rule`
164
166
165
167
This macro is used to declare an analyzer rule type, and implement the [RuleMeta] trait for it.
@@ -235,6 +237,7 @@ impl Rule for BanDropColumn {
235
237
### Document the rule
236
238
237
239
The documentation needs to adhere to the following rules:
240
+
238
241
- The **first** paragraph of the documentation is used as brief description of the rule, and it **must** be written in one single line. Breaking the paragraph in multiple lines will break the table content of the rules page.
239
242
- The next paragraphs can be used to further document the rule with as many details as you see fit.
240
243
- The documentation must have a `## Examples` header, followed by two headers: `### Invalid` and `### Valid`. `### Invalid` must go first because we need to show when the rule is triggered.
@@ -246,7 +249,7 @@ The documentation needs to adhere to the following rules:
246
249
247
250
Here's an example of how the documentation could look like:
248
251
249
-
```rust
252
+
````rust
250
253
declare_lint_rule! {
251
254
/// Dropping a column may break existing clients.
252
255
///
@@ -269,12 +272,26 @@ declare_lint_rule! {
269
272
sources:&[RuleSource::Squawk("ban-drop-column")],
270
273
}
271
274
}
272
-
```
275
+
````
273
276
274
277
This will cause the documentation generator to ensure the rule does emit
275
278
exactly one diagnostic for this code, and to include a snapshot for the
276
279
diagnostic in the resulting documentation page.
277
280
281
+
### Testing the Rule
282
+
283
+
#### Quick Test
284
+
285
+
To quickly test your rule, head to the `pg_analyser/src/lib.rs` file and modify the `debug_test` function.
286
+
287
+
You should:
288
+
289
+
- remove the `#[ignore]` macro if present
290
+
- change the content of the `SQL` static `&str` to whatever you need
291
+
- pass your group and rule to the `RuleFilter::Rule(..)`
292
+
293
+
If you run the test, you'll see any diagnostics your rule created in your console.
294
+
278
295
### Code generation
279
296
280
297
For simplicity, use `just` to run all the commands with:
@@ -294,15 +311,14 @@ Stage and commit your changes:
294
311
> git commit -m 'feat(pg_analyser): myRuleName'
295
312
```
296
313
297
-
298
314
### Deprecate a rule
299
315
300
316
There are occasions when a rule must be deprecated, to avoid breaking changes. The reason
301
317
of deprecation can be multiple.
302
318
303
319
In order to do, the macro allows adding additional field to add the reason for deprecation
0 commit comments