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
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pgls_lexer = { path = "./crates/pgls_lexer", version = "0.0.0"
pgls_lexer_codegen = { path = "./crates/pgls_lexer_codegen", version = "0.0.0" }
pgls_lsp = { path = "./crates/pgls_lsp", version = "0.0.0" }
pgls_markup = { path = "./crates/pgls_markup", version = "0.0.0" }
pgls_matcher = { path = "./crates/pgls_matcher", version = "0.0.0" }
pgls_plpgsql_check = { path = "./crates/pgls_plpgsql_check", version = "0.0.0" }
pgls_query = { path = "./crates/pgls_query", version = "0.0.0" }
pgls_query_ext = { path = "./crates/pgls_query_ext", version = "0.0.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pgls_analyser = { workspace = true }
pgls_console = { workspace = true }
pgls_diagnostics = { workspace = true }
pgls_env = { workspace = true }
pgls_matcher = { workspace = true }
pgls_text_size = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, features = ["indexmap1"], optional = true }
Expand Down
9 changes: 9 additions & 0 deletions crates/pgls_configuration/src/rules/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ impl<T: Clone + Default + 'static> RuleConfiguration<T> {
}
}
}
impl<T: Default> RuleConfiguration<T> {
/// Get a reference to the typed options if present
pub fn get_options_ref(&self) -> Option<&T> {
match self {
Self::Plain(_) => None,
Self::WithOptions(options) => Some(&options.options),
}
}
}
impl<T: Default> Default for RuleConfiguration<T> {
fn default() -> Self {
Self::Plain(RulePlainConfiguration::Error)
Expand Down
2 changes: 2 additions & 0 deletions crates/pgls_configuration/src/splinter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Generated file, do not edit by hand, see `xtask/codegen`

#![doc = r" Generated file, do not edit by hand, see `xtask/codegen`"]
mod options;
pub use options::SplinterRuleOptions;
mod rules;
use biome_deserialize::StringSet;
use biome_deserialize_macros::{Merge, Partial};
Expand Down
25 changes: 25 additions & 0 deletions crates/pgls_configuration/src/splinter/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use biome_deserialize_macros::Deserializable;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Shared options for all splinter rules.
///
/// These options allow configuring per-rule filtering of database objects.
#[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SplinterRuleOptions {
/// A list of glob patterns for database objects to ignore.
///
/// Patterns use Unix-style globs where:
/// - `*` matches any sequence of characters
/// - `?` matches any single character
///
/// Each pattern should be in the format `schema.object_name`, for example:
/// - `"public.my_table"` - ignores a specific table
/// - `"audit.*"` - ignores all objects in the audit schema
/// - `"*.audit_*"` - ignores objects with audit_ prefix in any schema
#[serde(default)]
pub ignore: Vec<String>,
}
306 changes: 285 additions & 21 deletions crates/pgls_configuration/src/splinter/rules.rs

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions crates/pgls_matcher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors.workspace = true
categories.workspace = true
description = "Unix shell style pattern matching for Postgres Language Server"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "pgls_matcher"
repository.workspace = true
version = "0.0.0"

[dependencies]
pgls_console = { workspace = true }
pgls_diagnostics = { workspace = true }
rustc-hash = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl Diagnostic for PatternError {

#[cfg(test)]
mod test {
use crate::matcher::Matcher;
use crate::matcher::pattern::MatchOptions;
use crate::Matcher;
use crate::pattern::MatchOptions;
use std::env;

#[test]
Expand All @@ -145,7 +145,7 @@ mod test {
let dir = format!("{}/**/*.rs", current.display());
let mut ignore = Matcher::new(MatchOptions::default());
ignore.add_pattern(&dir).unwrap();
let path = env::current_dir().unwrap().join("src/workspace.rs");
let path = env::current_dir().unwrap().join("src/lib.rs");
let result = ignore.matches(path.to_str().unwrap());

assert!(result);
Expand All @@ -157,7 +157,7 @@ mod test {
let dir = format!("{}/**/*.rs", current.display());
let mut ignore = Matcher::new(MatchOptions::default());
ignore.add_pattern(&dir).unwrap();
let path = env::current_dir().unwrap().join("src/workspace.rs");
let path = env::current_dir().unwrap().join("src/lib.rs");
let result = ignore.matches_path(path.as_path());

assert!(result);
Expand All @@ -184,14 +184,14 @@ mod test {

#[test]
fn matches_single_path() {
let dir = "workspace.rs";
let dir = "lib.rs";
let mut ignore = Matcher::new(MatchOptions {
require_literal_separator: true,
case_sensitive: true,
require_literal_leading_dot: true,
});
ignore.add_pattern(dir).unwrap();
let path = env::current_dir().unwrap().join("src/workspace.rs");
let path = env::current_dir().unwrap().join("src/lib.rs");
let result = ignore.matches(path.to_str().unwrap());

assert!(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::matcher::pattern::CharSpecifier::{CharRange, SingleChar};
use crate::matcher::pattern::MatchResult::{
EntirePatternDoesntMatch, Match, SubPatternDoesntMatch,
};
use crate::matcher::pattern::PatternToken::{
use crate::pattern::CharSpecifier::{CharRange, SingleChar};
use crate::pattern::MatchResult::{EntirePatternDoesntMatch, Match, SubPatternDoesntMatch};
use crate::pattern::PatternToken::{
AnyChar, AnyExcept, AnyPattern, AnyRecursiveSequence, AnySequence, AnyWithin, Char,
};
use std::error::Error;
Expand Down
14 changes: 8 additions & 6 deletions crates/pgls_splinter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ repository.workspace = true
version = "0.0.0"

[dependencies]
pgls_analyse.workspace = true
pgls_diagnostics.workspace = true
pgls_schema_cache.workspace = true
serde.workspace = true
serde_json.workspace = true
sqlx.workspace = true
pgls_analyse.workspace = true
pgls_configuration.workspace = true
pgls_diagnostics.workspace = true
pgls_matcher.workspace = true
pgls_schema_cache.workspace = true
serde.workspace = true
serde_json.workspace = true
sqlx.workspace = true

[dev-dependencies]
insta.workspace = true
Expand Down
29 changes: 27 additions & 2 deletions crates/pgls_splinter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod rule;
pub mod rules;

use pgls_analyse::{AnalysisFilter, RegistryVisitor, RuleMeta};
use pgls_configuration::splinter::Rules;
use pgls_schema_cache::SchemaCache;
use sqlx::PgPool;

Expand All @@ -17,6 +18,8 @@ pub use rule::SplinterRule;
pub struct SplinterParams<'a> {
pub conn: &'a PgPool,
pub schema_cache: Option<&'a SchemaCache>,
/// Optional rules configuration for per-rule database object filtering
pub rules_config: Option<&'a Rules>,
}

/// Visitor that collects enabled splinter rules based on filter
Expand Down Expand Up @@ -133,8 +136,30 @@ pub async fn run_splinter(

tx.commit().await?;

// Convert results to diagnostics
let diagnostics: Vec<SplinterDiagnostic> = results.into_iter().map(Into::into).collect();
let mut diagnostics: Vec<SplinterDiagnostic> = results.into_iter().map(Into::into).collect();

if let Some(rules_config) = params.rules_config {
let rule_matchers = rules_config.get_ignore_matchers();

if !rule_matchers.is_empty() {
diagnostics.retain(|diag| {
let rule_name = diag.category.name().split('/').next_back().unwrap_or("");

if let Some(matcher) = rule_matchers.get(rule_name) {
if let (Some(schema), Some(name)) =
(&diag.advices.schema, &diag.advices.object_name)
{
let object_identifier = format!("{schema}.{name}");

if matcher.matches(&object_identifier) {
return false;
}
}
}
true
});
}
}

Ok(diagnostics)
}
Loading