|
| 1 | +# Rule Suppressions |
| 2 | + |
| 3 | +You can suppress specific diagnostics or rules in your code using suppression comments. This is useful when you want to ignore a particular rule for an entire file, a line or a block of code. |
| 4 | + |
| 5 | +## How to Suppress a Rule |
| 6 | + |
| 7 | +To suppress a rule, add a comment above the line causing the diagnostic with the following format: |
| 8 | + |
| 9 | +```sql |
| 10 | +-- pgt-ignore lint/safety/banDropTable |
| 11 | +drop table users; |
| 12 | +``` |
| 13 | + |
| 14 | +You can suppress single rules, groups of rules, or entire categories. The format of the rule to suppress is: |
| 15 | + |
| 16 | +`category(/group(/specific-rule))` |
| 17 | + |
| 18 | +Where group and specific rule are optional. |
| 19 | + |
| 20 | +So, to suppress the `lint/safety/banDropTable` diagnostic, all of these would work: |
| 21 | + |
| 22 | +```sql |
| 23 | +-- pgt-ignore lint |
| 24 | +-- pgt-ignore lint/safety |
| 25 | +-- pgt-ignore lint/safety/banDropTable |
| 26 | +``` |
| 27 | + |
| 28 | +You can also add an explanation to the suppression by adding a `:` and the explanation text: |
| 29 | + |
| 30 | +```sql |
| 31 | +-- pgt-ignore lint/safety/banDropTable: My startup never had any users. |
| 32 | +drop table users; |
| 33 | +``` |
| 34 | + |
| 35 | +### Suppressing Rules for Block of Code |
| 36 | + |
| 37 | +You can suppress rules for blocks of code. |
| 38 | + |
| 39 | +```sql |
| 40 | +create table users ( |
| 41 | + -- ... |
| 42 | +); |
| 43 | + |
| 44 | +-- pgt-ignore-start typecheck: The `users` table will be created with this migration. |
| 45 | +alter table users drop constraint users_pkey; |
| 46 | + |
| 47 | +alter table users add primary key (user_id); |
| 48 | +-- pgt-ignore-end typecheck |
| 49 | +``` |
| 50 | + |
| 51 | +Every `pgt-ignore-start` needs a `pgt-ignore-end` suppression comment, and the suppressed rules must match exactly. |
| 52 | + |
| 53 | +This _won't_ work, because the start tag suppresses a different diagnostic: |
| 54 | + |
| 55 | +```sql |
| 56 | +-- pgt-ignore-start lint/safety/banDropColumn |
| 57 | +-- pgt-ignore-end lint/safety |
| 58 | +``` |
| 59 | + |
| 60 | +Nesting is allowed, so this works fine: |
| 61 | + |
| 62 | +```sql |
| 63 | +-- pgt-ignore-start typecheck: outer |
| 64 | +-- pgt-ignore-start lint/safety: inner |
| 65 | +-- pgt-ignore-end lint/safety: inner |
| 66 | +-- pgt-ignore-end typecheck: outer |
| 67 | +``` |
| 68 | + |
| 69 | +### Suppressing Rules for Entire Files |
| 70 | + |
| 71 | +Instead of repeating the same suppression on multiple lines, you can suppress for an entire file. |
| 72 | + |
| 73 | +```sql |
| 74 | +-- pgt-ignore-all lint/safety/banDropTable |
| 75 | + |
| 76 | +drop table tasks; |
| 77 | +drop table projects; |
| 78 | +drop table users; |
| 79 | +``` |
| 80 | + |
| 81 | +## Suppressing Multiple Rules |
| 82 | + |
| 83 | +You can suppress multiple rules by adding multiple suppression comments above a statement: |
| 84 | + |
| 85 | +```sql |
| 86 | +-- pgt-ignore lint/safety/banDropColumn |
| 87 | +-- pgt-ignore typecheck |
| 88 | +alter table tasks drop column created_at; |
| 89 | +``` |
| 90 | + |
| 91 | +## Notes |
| 92 | + |
| 93 | +- Trying to suppress diagnostics that have already been disabled in your [configuration file](/#configuration) will show a warning. |
| 94 | +- Trying to suppress diagnostics that don't haven't been raised will also show a warning. |
0 commit comments