Skip to content

Commit 2317e29

Browse files
authored
refactor: pgls suppressions (#592)
i believe we now have renamed everything
1 parent 9952744 commit 2317e29

File tree

3 files changed

+185
-29
lines changed

3 files changed

+185
-29
lines changed

crates/pgls_suppressions/src/parser.rs

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'a> SuppressionsParser<'a> {
5656
}
5757

5858
/// Will parse the suppressions at the start of the file.
59-
/// As soon as anything is encountered that's not a `pgt-ignore-all`
59+
/// As soon as anything is encountered that's not a `pgt-ignore-all` or `pgls-ignore-all`
6060
/// suppression or an empty line, this will stop.
6161
fn parse_file_suppressions(&mut self) {
6262
while let Some((_, preview)) = self.lines.peek() {
@@ -65,7 +65,10 @@ impl<'a> SuppressionsParser<'a> {
6565
continue;
6666
}
6767

68-
if !preview.trim().starts_with("-- pgt-ignore-all") {
68+
let trimmed = preview.trim();
69+
if !trimmed.starts_with("-- pgt-ignore-all")
70+
&& !trimmed.starts_with("-- pgls-ignore-all")
71+
{
6972
return;
7073
}
7174

@@ -82,7 +85,8 @@ impl<'a> SuppressionsParser<'a> {
8285

8386
fn parse_suppressions(&mut self) {
8487
for (idx, line) in self.lines.by_ref() {
85-
if !line.trim().starts_with("-- pgt-ignore") {
88+
let trimmed = line.trim();
89+
if !trimmed.starts_with("-- pgt-ignore") && !trimmed.starts_with("-- pgls-ignore") {
8690
continue;
8791
}
8892

@@ -350,4 +354,102 @@ drop table posts;
350354
String::from("This start suppression does not have a matching end.")
351355
);
352356
}
357+
358+
#[test]
359+
fn test_parse_pgls_prefix_line_suppressions() {
360+
let doc = r#"
361+
SELECT 1;
362+
-- pgls-ignore lint/safety/banDropColumn
363+
SELECT 2;
364+
"#;
365+
let suppressions = SuppressionsParser::parse(doc);
366+
367+
// Should have a line suppression on line 2 (0-based index)
368+
let suppression = suppressions
369+
.line_suppressions
370+
.get(&2)
371+
.expect("no suppression found");
372+
373+
assert_eq!(suppression.kind, SuppressionKind::Line);
374+
assert_eq!(
375+
suppression.rule_specifier,
376+
RuleSpecifier::Rule(
377+
"lint".to_string(),
378+
"safety".to_string(),
379+
"banDropColumn".to_string()
380+
)
381+
);
382+
}
383+
384+
#[test]
385+
fn test_parse_pgls_prefix_file_suppressions() {
386+
let doc = r#"
387+
-- pgls-ignore-all lint
388+
-- pgls-ignore-all typecheck
389+
390+
SELECT 1;
391+
-- pgls-ignore-all lint/safety
392+
"#;
393+
394+
let suppressions = SuppressionsParser::parse(doc);
395+
396+
assert_eq!(suppressions.diagnostics.len(), 1);
397+
assert_eq!(suppressions.file_suppressions.len(), 2);
398+
399+
assert_eq!(
400+
suppressions.file_suppressions[0].rule_specifier,
401+
RuleSpecifier::Category("lint".to_string())
402+
);
403+
assert_eq!(
404+
suppressions.file_suppressions[1].rule_specifier,
405+
RuleSpecifier::Category("typecheck".to_string())
406+
);
407+
}
408+
409+
#[test]
410+
fn test_parse_pgls_prefix_range_suppressions() {
411+
let doc = r#"
412+
-- pgls-ignore-start lint/safety/banDropTable
413+
drop table users;
414+
drop table auth;
415+
drop table posts;
416+
-- pgls-ignore-end lint/safety/banDropTable
417+
"#;
418+
419+
let suppressions = SuppressionsParser::parse(doc);
420+
421+
assert_eq!(suppressions.range_suppressions.len(), 1);
422+
assert_eq!(
423+
suppressions.range_suppressions[0]
424+
.start_suppression
425+
.rule_specifier,
426+
RuleSpecifier::Rule(
427+
"lint".to_string(),
428+
"safety".to_string(),
429+
"banDropTable".to_string()
430+
)
431+
);
432+
}
433+
434+
#[test]
435+
fn test_parse_mixed_prefix_suppressions() {
436+
let doc = r#"
437+
-- pgt-ignore-all lint
438+
439+
SELECT 1;
440+
-- pgls-ignore lint/safety/banDropColumn
441+
SELECT 2;
442+
-- pgt-ignore typecheck
443+
"#;
444+
445+
let suppressions = SuppressionsParser::parse(doc);
446+
447+
assert_eq!(suppressions.file_suppressions.len(), 1);
448+
assert_eq!(suppressions.line_suppressions.len(), 2);
449+
450+
assert_eq!(
451+
suppressions.file_suppressions[0].rule_specifier,
452+
RuleSpecifier::Category("lint".to_string())
453+
);
454+
}
353455
}

crates/pgls_suppressions/src/suppression.rs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,17 @@ pub(crate) struct Suppression {
114114

115115
impl Suppression {
116116
/// Creates a suppression from a suppression comment line.
117-
/// The line start must match `-- pgt-ignore`, otherwise, this will panic.
117+
/// The line start must match `-- pgt-ignore` or `-- pgls-ignore`, otherwise, this will panic.
118118
/// Leading whitespace is ignored.
119119
pub(crate) fn from_line(line: &str, offset: &TextSize) -> Result<Self, SuppressionDiagnostic> {
120120
let start_trimmed = line.trim_ascii_start();
121121
let leading_whitespace_offset = line.len() - start_trimmed.len();
122122
let trimmed = start_trimmed.trim_ascii_end();
123123

124124
assert!(
125-
start_trimmed.starts_with("-- pgt-ignore"),
126-
"Only try parsing suppressions from lines starting with `-- pgt-ignore`."
125+
start_trimmed.starts_with("-- pgt-ignore")
126+
|| start_trimmed.starts_with("-- pgls-ignore"),
127+
"Only try parsing suppressions from lines starting with `-- pgt-ignore` or `-- pgls-ignore`."
127128
);
128129

129130
let full_offset = *offset + TextSize::new(leading_whitespace_offset.try_into().unwrap());
@@ -141,10 +142,10 @@ impl Suppression {
141142

142143
let _ = parts.next();
143144
let kind = match parts.next().unwrap() {
144-
"pgt-ignore-all" => SuppressionKind::File,
145-
"pgt-ignore-start" => SuppressionKind::Start,
146-
"pgt-ignore-end" => SuppressionKind::End,
147-
"pgt-ignore" => SuppressionKind::Line,
145+
"pgt-ignore-all" | "pgls-ignore-all" => SuppressionKind::File,
146+
"pgt-ignore-start" | "pgls-ignore-start" => SuppressionKind::Start,
147+
"pgt-ignore-end" | "pgls-ignore-end" => SuppressionKind::End,
148+
"pgt-ignore" | "pgls-ignore" => SuppressionKind::Line,
148149
k => {
149150
return Err(SuppressionDiagnostic {
150151
span,
@@ -452,4 +453,57 @@ mod tests {
452453
];
453454
assert!(spec.is_disabled(&disabled5));
454455
}
456+
457+
#[test]
458+
fn test_pgls_prefix_line_suppressions() {
459+
let line = "-- pgls-ignore lint/safety/banDropColumn: explanation";
460+
let offset = &TextSize::new(0);
461+
let suppression = Suppression::from_line(line, offset).unwrap();
462+
463+
assert_eq!(suppression.kind, SuppressionKind::Line);
464+
assert_eq!(
465+
suppression.rule_specifier,
466+
RuleSpecifier::Rule(
467+
"lint".to_string(),
468+
"safety".to_string(),
469+
"banDropColumn".to_string()
470+
)
471+
);
472+
assert_eq!(suppression.explanation.as_deref(), Some("explanation"));
473+
}
474+
475+
#[test]
476+
fn test_pgls_prefix_file_kind() {
477+
let line = "-- pgls-ignore-all lint/safety: explanation";
478+
let offset = &TextSize::new(0);
479+
let suppression = Suppression::from_line(line, offset).unwrap();
480+
481+
assert_eq!(suppression.kind, SuppressionKind::File);
482+
assert_eq!(
483+
suppression.rule_specifier,
484+
RuleSpecifier::Group("lint".to_string(), "safety".to_string())
485+
);
486+
assert_eq!(suppression.explanation.as_deref(), Some("explanation"));
487+
}
488+
489+
#[test]
490+
fn test_pgls_prefix_start_and_end_kind() {
491+
let start_line = "-- pgls-ignore-start typecheck";
492+
let end_line = "-- pgls-ignore-end typecheck";
493+
let offset = &TextSize::new(0);
494+
495+
let start_suppression = Suppression::from_line(start_line, offset).unwrap();
496+
assert_eq!(start_suppression.kind, SuppressionKind::Start);
497+
assert_eq!(
498+
start_suppression.rule_specifier,
499+
RuleSpecifier::Category("typecheck".to_string())
500+
);
501+
502+
let end_suppression = Suppression::from_line(end_line, offset).unwrap();
503+
assert_eq!(end_suppression.kind, SuppressionKind::End);
504+
assert_eq!(
505+
end_suppression.rule_specifier,
506+
RuleSpecifier::Category("typecheck".to_string())
507+
);
508+
}
455509
}

docs/guides/suppressions.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ You can suppress specific diagnostics or rules in your code using suppression co
77
To suppress a rule, add a comment above the line causing the diagnostic with the following format:
88

99
```sql
10-
-- pgt-ignore lint/safety/banDropTable
10+
-- pgls-ignore lint/safety/banDropTable
1111
drop table users;
1212
```
1313

@@ -20,15 +20,15 @@ Where group and specific rule are optional.
2020
So, to suppress the `lint/safety/banDropTable` diagnostic, all of these would work:
2121

2222
```sql
23-
-- pgt-ignore lint
24-
-- pgt-ignore lint/safety
25-
-- pgt-ignore lint/safety/banDropTable
23+
-- pgls-ignore lint
24+
-- pgls-ignore lint/safety
25+
-- pgls-ignore lint/safety/banDropTable
2626
```
2727

2828
You can also add an explanation to the suppression by adding a `:` and the explanation text:
2929

3030
```sql
31-
-- pgt-ignore lint/safety/banDropTable: My startup never had any users.
31+
-- pgls-ignore lint/safety/banDropTable: My startup never had any users.
3232
drop table users;
3333
```
3434

@@ -41,37 +41,37 @@ create table users (
4141
-- ...
4242
);
4343

44-
-- pgt-ignore-start typecheck: The `users` table will be created with this migration.
44+
-- pgls-ignore-start typecheck: The `users` table will be created with this migration.
4545
alter table users drop constraint users_pkey;
4646

4747
alter table users add primary key (user_id);
48-
-- pgt-ignore-end typecheck
48+
-- pgls-ignore-end typecheck
4949
```
5050

51-
Every `pgt-ignore-start` needs a `pgt-ignore-end` suppression comment, and the suppressed rules must match exactly.
51+
Every `pgls-ignore-start` needs a `pgls-ignore-end` suppression comment, and the suppressed rules must match exactly.
5252

5353
This _won't_ work, because the start tag suppresses a different diagnostic:
5454

5555
```sql
56-
-- pgt-ignore-start lint/safety/banDropColumn
57-
-- pgt-ignore-end lint/safety
56+
-- pgls-ignore-start lint/safety/banDropColumn
57+
-- pgls-ignore-end lint/safety
5858
```
5959

6060
Nesting is allowed, so this works fine:
6161

6262
```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
63+
-- pgls-ignore-start typecheck: outer
64+
-- pgls-ignore-start lint/safety: inner
65+
-- pgls-ignore-end lint/safety: inner
66+
-- pgls-ignore-end typecheck: outer
6767
```
6868

6969
### Suppressing Rules for Entire Files
7070

7171
Instead of repeating the same suppression on multiple lines, you can suppress for an entire file.
7272

7373
```sql
74-
-- pgt-ignore-all lint/safety/banDropTable
74+
-- pgls-ignore-all lint/safety/banDropTable
7575

7676
drop table tasks;
7777
drop table projects;
@@ -83,12 +83,12 @@ drop table users;
8383
You can suppress multiple rules by adding multiple suppression comments above a statement:
8484

8585
```sql
86-
-- pgt-ignore lint/safety/banDropColumn
87-
-- pgt-ignore typecheck
86+
-- pgls-ignore lint/safety/banDropColumn
87+
-- pgls-ignore typecheck
8888
alter table tasks drop column created_at;
8989
```
9090

9191
## Notes
9292

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.
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

Comments
 (0)