Skip to content

Commit 9b1493e

Browse files
committed
finish codegen
1 parent d33599d commit 9b1493e

File tree

17 files changed

+82
-239
lines changed

17 files changed

+82
-239
lines changed

crates/pg_analyse/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod categories;
2-
mod context;
2+
pub mod context;
33
mod filter;
44
pub mod macros;
55
mod options;
@@ -15,7 +15,6 @@ pub use crate::categories::{
1515
ActionCategory, RefactorKind, RuleCategories, RuleCategoriesBuilder, RuleCategory,
1616
SourceActionKind, SUPPRESSION_ACTION_CATEGORY,
1717
};
18-
pub use crate::context::RuleContext;
1918
pub use crate::filter::{AnalysisFilter, GroupKey, RuleKey};
2019
pub use crate::options::{AnalyzerConfiguration, AnalyzerOptions, AnalyzerRules};
2120
pub use crate::registry::{MetadataRegistry, RegistryVisitor, RuleRegistry, RuleRegistryBuilder};

crates/pg_analyse/src/rule.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use pg_console::fmt::Display;
22
use pg_console::{markup, MarkupBuf};
33
use pg_diagnostics::advice::CodeSuggestionAdvice;
4-
use pg_diagnostics::location::AsSpan;
54
use pg_diagnostics::{
65
Advices, Category, Diagnostic, DiagnosticTags, Location, LogCategory, MessageAndDescription,
76
Visit,
@@ -162,11 +161,11 @@ pub struct Detail {
162161
impl RuleDiagnostic {
163162
/// Creates a new [`RuleDiagnostic`] with a severity and title that will be
164163
/// used in a builder-like way to modify labels.
165-
pub fn new(category: &'static Category, span: impl AsSpan, title: impl Display) -> Self {
164+
pub fn new(category: &'static Category, span: Option<TextRange>, title: impl Display) -> Self {
166165
let message = markup!({ title }).to_owned();
167166
Self {
168167
category,
169-
span: span.as_span(),
168+
span,
170169
message: MessageAndDescription::from(message),
171170
tags: DiagnosticTags::empty(),
172171
rule_advice: RuleAdvice::default(),
@@ -200,17 +199,17 @@ impl RuleDiagnostic {
200199
/// Attaches a label to this [`RuleDiagnostic`].
201200
///
202201
/// The given span has to be in the file that was provided while creating this [`RuleDiagnostic`].
203-
pub fn label(mut self, span: impl AsSpan, msg: impl Display) -> Self {
202+
pub fn label(mut self, span: Option<TextRange>, msg: impl Display) -> Self {
204203
self.rule_advice.details.push(Detail {
205204
log_category: LogCategory::Info,
206205
message: markup!({ msg }).to_owned(),
207-
range: span.as_span(),
206+
range: span,
208207
});
209208
self
210209
}
211210

212211
/// Attaches a detailed message to this [`RuleDiagnostic`].
213-
pub fn detail(self, span: impl AsSpan, msg: impl Display) -> Self {
212+
pub fn detail(self, span: Option<TextRange>, msg: impl Display) -> Self {
214213
self.label(span, msg)
215214
}
216215

crates/pg_diagnostics_categories/src/categories.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
// must be between `define_categories! {\n` and `\n ;\n`.
1414

1515
define_categories! {
16-
"lint/nursery/banDropColumn": "https://pglsp.dev/linter/rules/ban-drop-column",
16+
"lint/safety/banDropColumn": "https://pglsp.dev/linter/rules/ban-drop-column",
1717
// end lint rules
1818
;
1919
// General categories
2020
"stdin",
21-
"lint",
2221
"check",
2322
"configuration",
2423
"database/connection",
@@ -31,8 +30,9 @@ define_categories! {
3130
"syntax",
3231
"dummy",
3332

34-
// Lint groups
33+
// Lint groups start
3534
"lint",
3635
"lint/performance",
37-
"lint/suspicious",
36+
"lint/safety",
37+
// Lint groups end
3838
}

crates/pg_linter/src/lint.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Generated file, do not edit by hand, see `xtask/codegen`
22
3-
pub mod nursery;
4-
pub mod performance;
5-
::pg_analyse::declare_category! { pub Lint { kind : Lint , groups : [self :: nursery :: Nursery , self :: performance :: Performance ,] } }
3+
pub mod safety;
4+
::pg_analyse::declare_category! { pub Lint { kind : Lint , groups : [self :: safety :: Safety ,] } }

crates/pg_linter/src/lint/nursery/ban_drop_column.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

crates/pg_linter/src/lint/performance.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

crates/pg_linter/src/lint/performance/prefer_text_field.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
33
use pg_analyse::declare_lint_group;
44
pub mod ban_drop_column;
5-
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: ban_drop_column :: BanDropColumn ,] } }
5+
declare_lint_group! { pub Safety { name : "safety" , rules : [self :: ban_drop_column :: BanDropColumn ,] } }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use pg_analyse::{context::RuleContext, declare_lint_rule, Rule, RuleDiagnostic};
2+
use pg_console::markup;
3+
4+
declare_lint_rule! {
5+
/// Dropping a column may break existing clients.
6+
///
7+
/// Update your application code to no longer read or write the column.
8+
///
9+
/// You can leave the column as nullable or delete the column once queries no longer select or modify the column.
10+
///
11+
/// ## Examples
12+
///
13+
/// ### Invalid
14+
///
15+
/// ```sql,expect_diagnostic
16+
/// alter table test drop column id;
17+
/// ```
18+
///
19+
pub BanDropColumn {
20+
version: "next",
21+
name: "banDropColumn",
22+
recommended: true,
23+
}
24+
}
25+
26+
impl Rule for BanDropColumn {
27+
type Options = ();
28+
29+
fn run(ctx: &RuleContext<Self>) -> Vec<RuleDiagnostic> {
30+
let mut diagnostics = Vec::new();
31+
32+
if let pg_query_ext::NodeEnum::AlterTableStmt(stmt) = &ctx.stmt() {
33+
for cmd in &stmt.cmds {
34+
if let Some(pg_query_ext::NodeEnum::AlterTableCmd(cmd)) = &cmd.node {
35+
if cmd.subtype() == pg_query_ext::protobuf::AlterTableType::AtDropColumn {
36+
diagnostics.push(RuleDiagnostic::new(
37+
rule_category!(),
38+
None,
39+
markup! {
40+
"Dropping a column may break existing clients."
41+
},
42+
).detail(None, "You can leave the column as nullable or delete the column once queries no longer select or modify the column."));
43+
}
44+
}
45+
}
46+
}
47+
48+
diagnostics
49+
}
50+
}

crates/pg_linter/src/options.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
33
use crate::lint;
44
pub type BanDropColumn =
5-
<lint::nursery::ban_drop_column::BanDropColumn as pg_analyse::Rule>::Options;
6-
pub type PreferTextField =
7-
<lint::performance::prefer_text_field::PreferTextField as pg_analyse::Rule>::Options;
5+
<lint::safety::ban_drop_column::BanDropColumn as pg_analyse::Rule>::Options;

0 commit comments

Comments
 (0)