Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions crates/squawk_ide/src/code_actions/quote_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use rowan::TextSize;
use salsa::Database as Db;
use squawk_syntax::ast::{self, AstNode};
use squawk_syntax::{
ast::{self, AstNode},
quote::normalize_identifier,
};

use crate::{db::File, offsets::token_from_offset};

Expand Down Expand Up @@ -29,7 +32,7 @@ pub(super) fn quote_identifier(
return None;
}

let quoted = format!(r#""{}""#, text.to_lowercase());
let quoted = format!(r#""{}""#, normalize_identifier(&text));

actions.push(CodeAction {
title: "Quote identifier".to_owned(),
Expand Down Expand Up @@ -57,6 +60,14 @@ mod test {
);
}

#[test]
fn quote_doesnt_show_up_for_already_quoted_ident() {
assert!(code_action_not_applicable(
quote_identifier,
r#"create table T("X"$0 int);"#
));
}

#[test]
fn quote_identifier_on_name() {
assert_snapshot!(apply_code_action(
Expand Down
3 changes: 3 additions & 0 deletions crates/squawk_ide/src/code_actions/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::test_utils::Fixture;

use super::{ActionKind, CodeAction};

#[must_use]
pub(super) fn apply_code_action(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
Expand Down Expand Up @@ -108,13 +109,15 @@ fn code_action_not_applicable_(
actions.is_empty()
}

#[must_use]
pub(super) fn code_action_not_applicable(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
) -> bool {
code_action_not_applicable_(f, sql, false)
}

#[must_use]
pub(super) fn code_action_not_applicable_with_errors(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ mod tests {
use tabled::builder::Builder;
use tabled::settings::Style;

#[must_use]
fn completions(sql: &str) -> String {
let fixture = Fixture::new(sql);
let offset = fixture.marker().offset();
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ mod tests {
}
}

#[must_use]
fn symbols(sql: &str) -> String {
let db = Database::default();
let file = File::new(&db, sql.to_string().into());
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/expand_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ mod tests {
use insta::assert_debug_snapshot;
use squawk_syntax::{SourceFile, ast::AstNode};

#[must_use]
fn expand(sql: &str) -> Vec<String> {
let fixture = Fixture::new(sql);
let offset = fixture.marker().offset();
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/find_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod test {
use rowan::TextRange;
use rustc_hash::FxHashMap;

#[must_use]
#[track_caller]
fn find_refs(sql: &str) -> String {
let fixture = Fixture::new(sql);
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/folding_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ mod tests {
}
}

#[must_use]
fn check(sql: &str) -> String {
let db = Database::default();
let file = File::new(&db, sql.to_string().into());
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ mod test {
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
use insta::assert_snapshot;

#[must_use]
#[track_caller]
fn check_hover(sql: &str) -> String {
check_hover_(sql).expect("should find hover information")
Expand Down Expand Up @@ -1641,6 +1642,7 @@ mod test {
None
}

#[must_use]
#[track_caller]
fn check_hover_info(sql: &str) -> super::Hover {
let db = Database::default();
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mod tests {
use super::*;
use insta::assert_snapshot;

#[must_use]
fn infer(sql: &str) -> String {
let parse = ast::SourceFile::parse(sql);
for stmt in parse.tree().stmts() {
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ mod test {
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
use insta::assert_snapshot;

#[must_use]
#[track_caller]
fn check_inlay_hints(sql: &str) -> String {
let db = Database::default();
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ mod test {
use insta::assert_snapshot;
use std::fmt::Write;

#[must_use]
fn semantic_tokens(sql: &str) -> String {
let db = Database::default();
let file = File::new(&db, sql.to_string().into());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ mod test {
use crate::test_utils::{lint_errors, lint_ok};
use crate::{LinterSettings, Rule};

#[must_use]
fn lint_errors_with(sql: &str, settings: LinterSettings) -> String {
crate::test_utils::lint_errors_with(sql, settings, Rule::AddingFieldWithDefault)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_linter/src/rules/ban_char_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::BanCharField)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod test {
);
}

#[must_use]
fn lint_errors_with(sql: &str, settings: LinterSettings) -> String {
crate::test_utils::lint_errors_with(
sql,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ mod test {
crate::test_utils::lint_ok_with(sql, settings, Rule::ConstraintMissingNotValid);
}

#[must_use]
fn lint_errors_with(sql: &str, settings: LinterSettings) -> String {
crate::test_utils::lint_errors_with(sql, settings, Rule::ConstraintMissingNotValid)
}
Expand Down
4 changes: 3 additions & 1 deletion crates/squawk_linter/src/rules/prefer_bigint_over_int.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_hash::FxHashSet;

use squawk_syntax::ast::AstNode;
use squawk_syntax::quote::normalize_identifier;
use squawk_syntax::{Parse, SourceFile, ast, identifier::Identifier};

use crate::{Edit, Fix, Linter, Rule, Violation};
Expand All @@ -24,7 +25,7 @@ fn int_types() -> &'static FxHashSet<Identifier> {
}

fn int_to_bigint_replacement(int_type: &str) -> &'static str {
match int_type.to_lowercase().as_str() {
match normalize_identifier(int_type).as_str() {
"int" | "integer" => "bigint",
"int4" => "int8",
"serial" => "bigserial",
Expand Down Expand Up @@ -76,6 +77,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::PreferBigintOverInt)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_hash::FxHashSet;

use squawk_syntax::ast::AstNode;
use squawk_syntax::quote::normalize_identifier;
use squawk_syntax::{Parse, SourceFile, ast, identifier::Identifier};

use crate::{Edit, Fix, Linter, Rule, Violation};
Expand All @@ -23,7 +24,7 @@ fn small_int_types() -> &'static FxHashSet<Identifier> {
}

fn smallint_to_bigint(smallint_type: &str) -> &'static str {
match smallint_type.to_lowercase().as_str() {
match normalize_identifier(smallint_type).as_str() {
"smallint" => "bigint",
"int2" => "int8",
"smallserial" => "bigserial",
Expand Down Expand Up @@ -74,6 +75,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::PreferBigintOverSmallint)
}
Expand Down
4 changes: 3 additions & 1 deletion crates/squawk_linter/src/rules/prefer_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use squawk_syntax::{
Parse, SourceFile,
ast::{self, AstNode},
identifier::Identifier,
quote::normalize_identifier,
};

use crate::{Edit, Fix, Linter, Rule, Violation};
Expand All @@ -27,7 +28,7 @@ fn serial_types() -> &'static FxHashSet<Identifier> {
}

fn replace_serial(serial_type: &str) -> &'static str {
match serial_type.to_lowercase().as_str() {
match normalize_identifier(serial_type).as_str() {
"serial" | "serial4" => "integer generated by default as identity",
"serial2" | "smallserial" => "smallint generated by default as identity",
"serial8" | "bigserial" => "bigint generated by default as identity",
Expand Down Expand Up @@ -75,6 +76,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::PreferIdentity)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_linter/src/rules/prefer_repack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ mod test {
lint_ok_with(sql, pg19(), Rule::PreferRepack);
}

#[must_use]
fn fix(sql: &str) -> String {
fix_sql_with(sql, pg19(), Rule::PreferRepack)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_linter/src/rules/prefer_robust_stmts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ mod test {

use crate::{LinterSettings, Rule};

#[must_use]
fn fix(sql: &str) -> String {
crate::test_utils::fix_sql(sql, Rule::PreferRobustStmts)
}

#[must_use]
fn lint_errors(sql: &str) -> String {
crate::test_utils::lint_errors(sql, Rule::PreferRobustStmts)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_linter/src/rules/prefer_text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::PreferTextField)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_linter/src/rules/prefer_timestamptz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod test {
use crate::Rule;
use crate::test_utils::{fix_sql, lint_errors, lint_ok};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::PreferTimestampTz)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod test {
crate::test_utils::lint_ok_with(sql, settings, Rule::RequireConcurrentIndexCreation);
}

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::RequireConcurrentIndexCreation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::RequireConcurrentIndexDeletion)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::RequireConcurrentPartitionDetach)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::RequireConcurrentReindex)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_linter/src/rules/require_timeout_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ mod test {
test_utils::{fix_sql, lint_errors, lint_ok},
};

#[must_use]
fn fix(sql: &str) -> String {
fix_sql(sql, Rule::RequireTimeoutSettings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ warning[prefer-identity]: Serial types make schema, dependency, and permission m
├ help: Use an `IDENTITY` column instead.
╭╴
6 - id "bigserial"
6 + id integer generated by default as identity
6 + id bigint generated by default as identity
╰╴
1 change: 1 addition & 0 deletions crates/squawk_linter/src/rules/transaction_nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod test {
use crate::test_utils::{lint_errors, lint_ok};
use crate::{LinterSettings, Rule};

#[must_use]
fn lint_errors_with(sql: &str, settings: LinterSettings) -> String {
crate::test_utils::lint_errors_with(sql, settings, Rule::TransactionNesting)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_linter/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub(crate) fn lint_errors_with(sql: &str, settings: LinterSettings, rule: Rule)
format_violations(sql, &errors)
}

#[must_use]
pub(crate) fn fix_sql_with(sql: &str, settings: LinterSettings, rule: Rule) -> String {
let errors = lint_settings(sql, settings.clone(), rule);
assert!(!errors.is_empty(), "Should start with linter errors");
Expand Down Expand Up @@ -91,6 +92,7 @@ pub(crate) fn fix_sql_with(sql: &str, settings: LinterSettings, rule: Rule) -> S
result
}

#[must_use]
pub(crate) fn fix_sql(sql: &str, rule: Rule) -> String {
fix_sql_with(sql, LinterSettings::default(), rule)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_parser/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ fn regression_suite(fixture: Fixture<&str>) {
}
}

#[must_use]
fn parse_text(text: &str) -> (String, Option<String>) {
let lexed = LexedStr::new(text);
let input = lexed.to_input();
Expand Down
8 changes: 3 additions & 5 deletions crates/squawk_syntax/src/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::quote::normalize_identifier;

/// Postgres Identifiers are case insensitive unless they're quoted.
///
/// This type handles the casing rules for us to make comparisions easier.
Expand All @@ -8,11 +10,7 @@ impl Identifier {
// TODO: we need to handle more advanced identifiers like:
// U&"d!0061t!+000061" UESCAPE '!'
pub fn new(s: &str) -> Self {
let normalized = if s.starts_with('"') && s.ends_with('"') {
s[1..s.len() - 1].to_string()
} else {
s.to_lowercase()
};
let normalized = normalize_identifier(s);
Identifier(normalized)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_syntax/src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn needs_quoting(text: &str) -> bool {

pub fn is_reserved_word(text: &str) -> bool {
RESERVED_KEYWORDS
.binary_search(&text.to_lowercase().as_str())
.binary_search(&text.to_ascii_lowercase().as_str())
.is_ok()
}

Expand Down
Loading
Loading