Skip to content

Commit 7fa3e33

Browse files
authored
linter: fix ban char to be case insensitive (#619)
1 parent 6077ff5 commit 7fa3e33

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

crates/squawk_linter/src/rules/ban_char_field.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
use std::collections::HashSet;
2+
13
use squawk_syntax::{
24
Parse, SourceFile, TokenText,
35
ast::{self, AstNode},
46
};
57

6-
use crate::visitors::check_not_allowed_types;
78
use crate::{Linter, Rule, Violation};
9+
use crate::{identifier::Identifier, visitors::check_not_allowed_types};
10+
11+
use lazy_static::lazy_static;
12+
13+
lazy_static! {
14+
static ref CHAR_TYPES: HashSet<Identifier> = HashSet::from([
15+
Identifier::new("char"),
16+
Identifier::new("character"),
17+
Identifier::new("bpchar"),
18+
]);
19+
}
820

921
fn is_char_type(x: TokenText<'_>) -> bool {
10-
if x == "char" || x == "character" || x == "bpchar" {
11-
return true;
12-
}
13-
false
22+
CHAR_TYPES.contains(&Identifier::new(&x.to_string()))
1423
}
1524

1625
fn check_path_type(ctx: &mut Linter, path_type: ast::PathType) {
@@ -126,6 +135,18 @@ create table t (
126135
assert_debug_snapshot!(errors);
127136
}
128137

138+
#[test]
139+
fn case_insensitive() {
140+
let sql = r#"
141+
create table t (
142+
a Char
143+
);
144+
"#;
145+
let errors = lint(sql, Rule::BanCharField);
146+
assert_ne!(errors.len(), 0);
147+
assert_debug_snapshot!(errors);
148+
}
149+
129150
#[test]
130151
fn array_char_type_err() {
131152
let sql = r#"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/squawk_linter/src/rules/ban_char_field.rs
3+
expression: errors
4+
---
5+
[
6+
Violation {
7+
code: BanCharField,
8+
message: "Using `character` is likey a mistake and should almost always be replaced by `text` or `varchar`.",
9+
text_range: 22..26,
10+
help: None,
11+
fix: None,
12+
},
13+
]

0 commit comments

Comments
 (0)