Skip to content

Commit c6e6395

Browse files
committed
progress
1 parent ddbefc3 commit c6e6395

File tree

7 files changed

+39
-20
lines changed

7 files changed

+39
-20
lines changed

agentic/port_squawk_rules.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@ pub struct RuleContext<'a, R: Rule> {
1818
file_context: &'a AnalysedFileContext,
1919
}
2020

21+
2122
pub struct AnalysedFileContext<'a> {
22-
// all other statements in this file
23-
pub all_stmts: &'a Vec<pgt_query::NodeEnum>,
24-
// total count of statements in this file
25-
pub stmt_count: usize,
23+
// all statements in this file
24+
pub stmts: &'a Vec<pgt_query::NodeEnum>,
25+
26+
pos: usize,
27+
}
28+
29+
impl<'a> AnalysedFileContext<'a> {
30+
pub fn new(stmts: &'a Vec<pgt_query::NodeEnum>) -> Self {
31+
Self { stmts, pos: 0 }
32+
}
33+
2634
// all statements before the currently analysed one
27-
pub previous_stmts: Vec<&'a pgt_query::NodeEnum>,
35+
pub fn previous_stmts(&self) -> &[pgt_query::NodeEnum] {
36+
&self.stmts[0..self.pos]
37+
}
38+
39+
// total count of statements in this file
40+
pub fn stmt_count(&self) -> usize {
41+
self.stmts.len()
42+
}
2843
}
2944
```
3045

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
pub struct AnalysedFileContext<'a> {
2-
pub all_stmts: &'a Vec<pgt_query::NodeEnum>,
3-
pub stmt_count: usize,
4-
pub previous_stmts: Vec<&'a pgt_query::NodeEnum>,
2+
pub stmts: &'a Vec<pgt_query::NodeEnum>,
3+
4+
pos: usize,
55
}
66

77
impl<'a> AnalysedFileContext<'a> {
88
pub fn new(stmts: &'a Vec<pgt_query::NodeEnum>) -> Self {
9-
Self {
10-
all_stmts: stmts,
11-
stmt_count: stmts.len(),
12-
previous_stmts: Vec::new(),
13-
}
9+
Self { stmts, pos: 0 }
10+
}
11+
12+
pub fn previous_stmts(&self) -> &[pgt_query::NodeEnum] {
13+
&self.stmts[0..self.pos]
14+
}
15+
16+
pub fn stmt_count(&self) -> usize {
17+
self.stmts.len()
1418
}
1519

16-
pub fn update_from(&mut self, stmt_root: &'a pgt_query::NodeEnum) {
17-
self.previous_stmts.push(stmt_root);
20+
pub fn next(&mut self) {
21+
self.pos += 1;
1822
}
1923
}

crates/pgt_analyser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a> Analyser<'a> {
8484

8585
diagnostics.extend(stmt_diagnostics);
8686

87-
file_context.update_from(&roots[i]);
87+
file_context.next();
8888
}
8989

9090
diagnostics

crates/pgt_analyser/src/lint/safety/ban_concurrent_index_creation_in_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Rule for BanConcurrentIndexCreationInTransaction {
3737
//
3838
// since our analyser assumes we're always in a transaction context, we always flag concurrent indexes
3939
if let pgt_query::NodeEnum::IndexStmt(stmt) = ctx.stmt() {
40-
if stmt.concurrent && ctx.file_context().stmt_count > 1 {
40+
if stmt.concurrent && ctx.file_context().stmt_count() > 1 {
4141
diagnostics.push(RuleDiagnostic::new(
4242
rule_category!(),
4343
None,

crates/pgt_analyser/src/lint/safety/disallow_unique_constraint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Rule for DisallowUniqueConstraint {
5050

5151
// Look for tables created in previous statements of this file
5252
let table_created_in_transaction = if let Some(table_name) = table_name {
53-
ctx.file_context().previous_stmts.iter().any(|prev_stmt| {
53+
ctx.file_context().previous_stmts().iter().any(|prev_stmt| {
5454
if let pgt_query::NodeEnum::CreateStmt(create) = prev_stmt {
5555
create
5656
.relation

crates/pgt_analyser/src/lint/safety/require_concurrent_index_creation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn is_table_created_in_file(
7979
table_name: &str,
8080
) -> bool {
8181
// Check all statements in the file to see if this table was created
82-
for stmt in file_context.all_stmts {
82+
for stmt in file_context.stmts {
8383
if let pgt_query::NodeEnum::CreateStmt(create_stmt) = stmt {
8484
if let Some(relation) = &create_stmt.relation {
8585
if relation.relname == table_name {

crates/pgt_analyser/src/lint/safety/transaction_nesting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Rule for TransactionNesting {
8686
}
8787

8888
fn has_transaction_start_before(file_context: &AnalysedFileContext) -> bool {
89-
for stmt in &file_context.previous_stmts {
89+
for stmt in file_context.previous_stmts() {
9090
if let pgt_query::NodeEnum::TransactionStmt(tx_stmt) = stmt {
9191
match tx_stmt.kind() {
9292
pgt_query::protobuf::TransactionStmtKind::TransStmtBegin

0 commit comments

Comments
 (0)