Skip to content

Commit 400c8b8

Browse files
gvozdvmozgubenfdking
authored andcommitted
chore: configuring rules through config
1 parent 893ee66 commit 400c8b8

File tree

33 files changed

+463
-68
lines changed

33 files changed

+463
-68
lines changed

crates/lib/src/cli/formatters.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,18 @@ impl Status {
291291
mod tests {
292292
use std::fs::File;
293293

294+
use ahash::AHashMap;
294295
use anstyle::AnsiColor;
295296
use fancy_regex::Regex;
296297
use tempdir::TempDir;
297298

298299
use super::OutputStreamFormatter;
299300
use crate::cli::formatters::split_string_on_spaces;
301+
use crate::core::config::Value;
300302
use crate::core::errors::SQLLintError;
301303
use crate::core::parser::markers::PositionMarker;
302304
use crate::core::parser::segments::raw::RawSegment;
303-
use crate::core::rules::base::{Erased, LintResult, Rule};
305+
use crate::core::rules::base::{Erased, ErasedRule, LintResult, Rule};
304306
use crate::core::rules::context::RuleContext;
305307
use crate::core::rules::crawlers::Crawler;
306308
use crate::core::templaters::base::TemplatedFile;
@@ -368,6 +370,10 @@ mod tests {
368370
fn crawl_behaviour(&self) -> Crawler {
369371
todo!()
370372
}
373+
374+
fn from_config(&self, _config: &AHashMap<String, Value>) -> ErasedRule {
375+
unimplemented!()
376+
}
371377
}
372378

373379
let s = RawSegment::new(

crates/lib/src/core/config.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,16 @@ pub fn removed_configs() -> [RemovedConfig<'static>; 12] {
9898

9999
/// split_comma_separated_string takes a string and splits it on commas and
100100
/// trims and filters out empty strings.
101-
pub fn split_comma_separated_string(raw_str: &str) -> Vec<String> {
102-
raw_str.split(',').map(|x| x.trim().to_string()).filter(|x| !x.is_empty()).collect()
101+
pub fn split_comma_separated_string(raw_str: &str) -> Value {
102+
let values = raw_str
103+
.split(',')
104+
.flat_map(|x| {
105+
let trimmed = x.trim();
106+
(!trimmed.is_empty()).then(|| Value::String(trimmed.into()))
107+
})
108+
.collect();
109+
110+
Value::Array(values)
103111
}
104112

105113
/// The class that actually gets passed around as a config object.
@@ -125,8 +133,11 @@ impl FluffConfig {
125133
&self.raw[section][key]
126134
}
127135

136+
pub fn get_section(&self, section: &str) -> &AHashMap<String, Value> {
137+
&self.raw[section].as_map().unwrap()
138+
}
139+
128140
// TODO This is not a translation that is particularly accurate.
129-
#[track_caller]
130141
pub fn new(
131142
mut configs: AHashMap<String, Value>,
132143
extra_config_path: Option<String>,
@@ -147,6 +158,30 @@ impl FluffConfig {
147158

148159
ConfigLoader.incorporate_vals(&mut configs, values);
149160

161+
for (in_key, out_key) in [
162+
// Deal with potential ignore & warning parameters
163+
("ignore", "ignore"),
164+
("warnings", "warnings"),
165+
("rules", "rule_allowlist"),
166+
// Allowlists and denylistsignore_words
167+
("exclude_rules", "rule_denylist"),
168+
] {
169+
match configs["core"].as_map().unwrap().get(in_key) {
170+
Some(value) if !value.is_none() => {
171+
let string = value.as_string().unwrap();
172+
let values = split_comma_separated_string(string);
173+
174+
configs
175+
.get_mut("core")
176+
.unwrap()
177+
.as_map_mut()
178+
.unwrap()
179+
.insert(out_key.into(), values);
180+
}
181+
_ => {}
182+
}
183+
}
184+
150185
Self {
151186
raw: configs,
152187
dialect,
@@ -442,9 +477,20 @@ pub enum Value {
442477
Float(f64),
443478
String(Box<str>),
444479
Map(AHashMap<String, Value>),
480+
Array(Vec<Value>),
445481
None,
446482
}
447483

484+
impl Value {
485+
pub fn is_none(&self) -> bool {
486+
matches!(self, Value::None)
487+
}
488+
489+
pub fn as_array(&self) -> Option<&[Value]> {
490+
if let Self::Array(v) = self { Some(v) } else { None }
491+
}
492+
}
493+
448494
impl Index<&str> for Value {
449495
type Output = Value;
450496

@@ -465,6 +511,7 @@ impl Value {
465511
Value::String(ref v) => !v.is_empty(),
466512
Value::Map(ref v) => !v.is_empty(),
467513
Value::None => false,
514+
Value::Array(ref v) => !v.is_empty(),
468515
}
469516
}
470517

crates/lib/src/core/linter/linter.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use crate::core::parser::lexer::{Lexer, StringOrTemplate};
2222
use crate::core::parser::parser::Parser;
2323
use crate::core::parser::segments::base::ErasedSegment;
2424
use crate::core::parser::segments::fix::AnchorEditInfo;
25-
use crate::core::rules::base::{ErasedRule, LintFix};
25+
use crate::core::rules::base::{ErasedRule, LintFix, RulePack};
2626
use crate::core::templaters::base::{RawTemplater, TemplatedFile, Templater};
27+
use crate::rules::get_ruleset;
2728

2829
pub struct Linter {
2930
config: FluffConfig,
@@ -38,7 +39,7 @@ impl Linter {
3839
formatter: Option<OutputStreamFormatter>,
3940
templater: Option<Box<dyn Templater>>,
4041
) -> Linter {
41-
let rules = crate::rules::layout::get_rules(&config);
42+
let rules = crate::rules::layout::get_rules();
4243
match templater {
4344
Some(templater) => Linter { config, formatter, templater, rules },
4445
None => Linter { config, formatter, templater: Box::<RawTemplater>::default(), rules },
@@ -180,12 +181,22 @@ impl Linter {
180181
result
181182
}
182183

184+
pub fn get_rulepack(&self) -> RulePack {
185+
let rs = get_ruleset();
186+
rs.get_rulepack(&self.config)
187+
}
188+
183189
pub fn render_file(&mut self, fname: String) -> RenderedFile {
184190
let in_str = std::fs::read_to_string(&fname).unwrap();
185191
self.render_string(in_str, fname, self.config.clone(), None).unwrap()
186192
}
187193

188-
pub fn lint_rendered(&mut self, rendered: RenderedFile, fix: bool) -> LintedFile {
194+
pub fn lint_rendered(
195+
&mut self,
196+
rendered: RenderedFile,
197+
rule_pack: &RulePack,
198+
fix: bool,
199+
) -> LintedFile {
189200
let parsed = Self::parse_rendered(rendered, false);
190201
self.lint_parsed(parsed, self.rules.clone(), fix)
191202
}

crates/lib/src/core/linter/runner.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ pub struct SequentialRunner;
2727
impl Runner for SequentialRunner {
2828
fn run(&mut self, paths: Vec<String>, fix: bool, linter: &mut Linter) -> Vec<LintedFile> {
2929
let mut acc = Vec::with_capacity(paths.len());
30+
let rule_pack = linter.get_rulepack();
3031

3132
for path in paths {
3233
let rendered = linter.render_file(path);
33-
let linted_file = linter.lint_rendered(rendered, fix);
34+
let linted_file = linter.lint_rendered(rendered, &rule_pack, fix);
3435

3536
acc.push(linted_file);
3637
}

crates/lib/src/core/parser/grammar/conditional.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ impl Matchable for Conditional {
8686
return Ok(MatchResult::from_unmatched(segments.to_vec()));
8787
}
8888

89-
dbg!(self);
90-
9189
Ok(MatchResult {
9290
matched_segments: vec![self.meta.clone().to_erased_segment()],
9391
unmatched_segments: segments.to_vec(),

0 commit comments

Comments
 (0)