From da0b25ea93f1ca5a738c6099f8acfeec4a78ab20 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 2 May 2025 08:58:27 -0400 Subject: [PATCH 1/6] add regression test --- cli/src/reporter.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cli/src/reporter.rs b/cli/src/reporter.rs index f20cb229..a62f737d 100644 --- a/cli/src/reporter.rs +++ b/cli/src/reporter.rs @@ -666,6 +666,7 @@ mod test_reporter { check_sql_with_rule, violations::{RuleViolation, RuleViolationKind}, }; + use squawk_parser::ast::Span; fn lint_sql(sql: &str) -> Vec { check_sql_with_rule(sql, &RuleViolationKind::AddingRequiredField, None, false).unwrap() @@ -825,4 +826,18 @@ SELECT 1; } "#); } + + #[test] + fn regression_slicing_issue_425() { + let sql = "ALTER TABLE test ADD COLUMN IF NOT EXISTS test INTEGER;"; + let violation = RuleViolation::new( + RuleViolationKind::PreferBigInt, + Span { + start: 42, + len: None, + }, + None, + ); + pretty_violations(vec![violation], sql, "main.sql"); + } } From 2e54eda2853636a653d5d91ea15f84fe688ca011 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 2 May 2025 09:22:53 -0400 Subject: [PATCH 2/6] add another test --- cli/src/reporter.rs | 23 +++++++++++++++++++ ...reporter__highlight_column_for_issues.snap | 8 +++++++ 2 files changed, 31 insertions(+) create mode 100644 cli/src/snapshots/squawk__reporter__test_reporter__highlight_column_for_issues.snap diff --git a/cli/src/reporter.rs b/cli/src/reporter.rs index a62f737d..beca911f 100644 --- a/cli/src/reporter.rs +++ b/cli/src/reporter.rs @@ -829,6 +829,7 @@ SELECT 1; #[test] fn regression_slicing_issue_425() { + // Squawk was crashing with an slicing issue. let sql = "ALTER TABLE test ADD COLUMN IF NOT EXISTS test INTEGER;"; let violation = RuleViolation::new( RuleViolationKind::PreferBigInt, @@ -840,4 +841,26 @@ SELECT 1; ); pretty_violations(vec![violation], sql, "main.sql"); } + #[test] + fn highlight_column_for_issues() { + // Display only the columns with issues for large DDLs. + fn lint_sql(sql: &str) -> Vec { + check_sql_with_rule(sql, &RuleViolationKind::PreferTextField, None, false).unwrap() + } + // Squawk was crashing with an slicing issue. + let sql = "create table test_table ( + col1 varchar(255), + col2 varchar(255), + col3 varchar(255) + --- other columns +);"; + let violations = lint_sql(sql); + let res = pretty_violations(violations, sql, "main.sql"); + let columns = res + .violations + .iter() + .map(|v| v.sql.clone()) + .collect::(); + assert_display_snapshot!(columns); + } } diff --git a/cli/src/snapshots/squawk__reporter__test_reporter__highlight_column_for_issues.snap b/cli/src/snapshots/squawk__reporter__test_reporter__highlight_column_for_issues.snap new file mode 100644 index 00000000..e5158419 --- /dev/null +++ b/cli/src/snapshots/squawk__reporter__test_reporter__highlight_column_for_issues.snap @@ -0,0 +1,8 @@ +--- +source: cli/src/reporter.rs +expression: columns +--- +col1 varchar(255), +col2 varchar(255), +col3 varchar(255) + From b4c455dc0261f4e33733ba62b9ed41f5a073c517 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 2 May 2025 09:23:00 -0400 Subject: [PATCH 3/6] fix bug --- cli/src/reporter.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/src/reporter.rs b/cli/src/reporter.rs index beca911f..17cd26cf 100644 --- a/cli/src/reporter.rs +++ b/cli/src/reporter.rs @@ -396,7 +396,8 @@ pub fn pretty_violations( } else { // Use current line let tail = sql[start..].find('\n').unwrap_or(sql.len() - start); - &sql[start..=start + tail] + + &sql.chars().skip(start).take(tail + 1).collect::() }; // TODO(sbdchd): could remove the leading whitespace and comments to From fdc077e59d49fc748b6e1cea8a3bcce66cfb84ef Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 2 May 2025 09:25:40 -0400 Subject: [PATCH 4/6] bump version --- CHANGELOG.md | 8 ++++++++ Cargo.lock | 2 +- cli/Cargo.toml | 2 +- flake.nix | 2 +- package.json | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 596350d5..2a262423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## v1.6.1 - 2025-05-02 + +### Fixed + +- Fixed panic when formatting violations (#426) + ## v1.6.0 - 2025-04-02 +### Added + - Added `ban-alter-domain-with-add-constraint` and `ban-create-domain-with-constraint` (#418). Thanks @johnmastro! ## v1.5.5 - 2025-03-20 diff --git a/Cargo.lock b/Cargo.lock index feff7189..ae1bacae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1586,7 +1586,7 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] name = "squawk" -version = "1.6.0" +version = "1.6.1" dependencies = [ "atty", "base64 0.12.3", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 2dd9249b..b201c94b 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "squawk" -version = "1.6.0" +version = "1.6.1" authors = ["Steve Dignam "] edition = "2018" license = "GPL-3.0" diff --git a/flake.nix b/flake.nix index b50038c3..4c22eacc 100644 --- a/flake.nix +++ b/flake.nix @@ -18,7 +18,7 @@ { squawk = final.rustPlatform.buildRustPackage { pname = "squawk"; - version = "1.6.0"; + version = "1.6.1"; cargoLock = { lockFile = ./Cargo.lock; diff --git a/package.json b/package.json index 700f4efe..46667b74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "squawk-cli", - "version": "1.6.0", + "version": "1.6.1", "description": "linter for PostgreSQL, focused on migrations", "repository": "git@github.com:sbdchd/squawk.git", "author": "Steve Dignam ", From 4a0f0c4ad291e6661d26458aac4c68675e8f25a1 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 2 May 2025 09:25:59 -0400 Subject: [PATCH 5/6] . --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a262423..74937fe5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed panic when formatting violations (#426) +- Fixed panic when formatting violations (#426). ## v1.6.0 - 2025-04-02 From 1f416c199f75cbcbefa8a4a028aea2b94394b3ed Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 2 May 2025 20:55:29 -0400 Subject: [PATCH 6/6] fix line numbers --- cli/src/reporter.rs | 16 +++++++++++++--- ...k__reporter__test_reporter__span_offsets.snap | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cli/src/reporter.rs b/cli/src/reporter.rs index 17cd26cf..cfe882d3 100644 --- a/cli/src/reporter.rs +++ b/cli/src/reporter.rs @@ -386,9 +386,19 @@ pub fn pretty_violations( #[allow(clippy::cast_sign_loss)] let start = start as usize; - // 1-indexed - // remove the leading whitespace on last line - let lineno = sql[..start].trim_end().lines().count() + 1; + let mut lineno = 0; + + for (idx, char) in sql.chars().enumerate() { + if char == '\n' { + lineno += 1; + } + + if idx == start { + break; + } + } + + lineno += 1; let content = if let Some(len) = len { #[allow(clippy::cast_sign_loss)] diff --git a/cli/src/snapshots/squawk__reporter__test_reporter__span_offsets.snap b/cli/src/snapshots/squawk__reporter__test_reporter__span_offsets.snap index 2a181364..e821d116 100644 --- a/cli/src/snapshots/squawk__reporter__test_reporter__span_offsets.snap +++ b/cli/src/snapshots/squawk__reporter__test_reporter__span_offsets.snap @@ -8,7 +8,7 @@ ViolationContent { violations: [ ReportViolation { file: "main.sql", - line: 1, + line: 2, column: 2, level: Warning, messages: [