Skip to content

Commit

Permalink
feat: add is fixable to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Dec 31, 2024
1 parent 6eafafd commit cb1370d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
30 changes: 7 additions & 23 deletions crates/lib-core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait SqlError {
fn check_tuple(&self) -> CheckTuple;
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
pub struct SQLBaseError {
pub fatal: bool,
pub ignore: bool,
Expand All @@ -26,6 +26,7 @@ pub struct SQLBaseError {
pub description: String,
pub rule: Option<ErrorStructRule>,
pub source_slice: Range<usize>,
pub fixable: bool,
}

#[derive(Debug, PartialEq, Clone, Default)]
Expand All @@ -34,26 +35,7 @@ pub struct ErrorStructRule {
pub code: &'static str,
}

impl Default for SQLBaseError {
fn default() -> Self {
Self::new()
}
}

impl SQLBaseError {
pub fn new() -> Self {
Self {
description: String::new(),
fatal: false,
ignore: false,
warning: false,
line_no: 0,
line_pos: 0,
rule: None,
source_slice: 0..0,
}
}

pub fn rule_code(&self) -> &'static str {
self.rule.as_ref().map_or("????", |rule| rule.code)
}
Expand Down Expand Up @@ -96,11 +78,12 @@ pub struct SQLLintError {
}

impl SQLLintError {
pub fn new(description: &str, segment: ErasedSegment) -> Self {
pub fn new(description: &str, segment: ErasedSegment, fixable: bool) -> Self {
Self {
base: SQLBaseError::new().config(|this| {
base: SQLBaseError::default().config(|this| {
this.description = description.into();
this.set_position_marker(segment.get_position_marker().unwrap().clone());
this.fixable = fixable;
}),
}
}
Expand Down Expand Up @@ -220,11 +203,12 @@ impl From<SQLParseError> for SQLBaseError {
(line_no, line_pos) = pos_marker.source_position();
}

Self::new().config(|this| {
Self::default().config(|this| {
this.fatal = true;
this.line_no = line_no;
this.line_pos = line_pos;
this.description = value.description;
this.fixable = false;
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/cli/formatters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ mod tests {
))
.finish();

let mut v = SQLLintError::new("DESC", s);
let mut v = SQLLintError::new("DESC", s, false);

v.rule = Some(ErrorStructRule {
name: "some-name",
Expand Down
11 changes: 9 additions & 2 deletions crates/lib/src/core/linter/linted_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ pub struct LintedFile {
}

impl LintedFile {
#[allow(unused_variables)]
pub fn get_violations(&self, fixable: Option<bool>) -> Vec<SQLBaseError> {
self.violations.clone().into_iter().map_into().collect_vec()
if let Some(fixable) = fixable {
self.violations
.iter()
.filter(|v| v.fixable == fixable)
.cloned()
.collect_vec()
} else {
self.violations.clone().into_iter().map_into().collect_vec()
}
}

/// Use patches and raw file to fix the source file.
Expand Down
6 changes: 4 additions & 2 deletions crates/lib/src/core/rules/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ impl LintResult {
.clone()
.unwrap_or_else(|| rule.description().to_string());

SQLLintError::new(description.as_str(), anchor)
let is_fixable = rule.is_fix_compatible();

SQLLintError::new(description.as_str(), anchor, is_fixable)
.config(|this| {
this.rule = Some(ErrorStructRule {
name: rule.name(),
Expand Down Expand Up @@ -209,7 +211,7 @@ pub trait Rule: CloneRule + dyn_clone::DynClone + Debug + 'static + Send + Sync
let resp = match resp {
Ok(t) => t,
Err(_) => {
vs.push(SQLLintError::new("Unexpected exception. Could you open an issue at https://github.com/quarylabs/sqruff", tree.clone()));
vs.push(SQLLintError::new("Unexpected exception. Could you open an issue at https://github.com/quarylabs/sqruff", tree.clone(), false));
return (vs, fixes);
}
};
Expand Down
8 changes: 8 additions & 0 deletions crates/lib/src/core/rules/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl NoQADirective {
description: format!("Rule {} not found in rule set", rule),
rule: None,
source_slice: Default::default(),
fixable: false,
});
}
}
Expand Down Expand Up @@ -134,6 +135,7 @@ impl NoQADirective {
.into(),
rule: None,
source_slice: Default::default(),
fixable: false,
})
} else {
Ok(Some(NoQADirective::RangeIgnoreRules(RangeIgnoreRules {
Expand Down Expand Up @@ -172,6 +174,7 @@ impl NoQADirective {
.to_string(),
rule: None,
source_slice: Default::default(),
fixable: false,
})
} else {
Ok(Some(NoQADirective::RangeIgnoreRules(RangeIgnoreRules {
Expand All @@ -197,6 +200,7 @@ impl NoQADirective {
.into(),
rule: None,
source_slice: Default::default(),
fixable: false,
})
} else {
return Ok(Some(NoQADirective::LineIgnoreRules(LineIgnoreRules {
Expand All @@ -218,6 +222,7 @@ impl NoQADirective {
.into(),
rule: None,
source_slice: Default::default(),
fixable: false,
})
}
} else {
Expand All @@ -232,6 +237,7 @@ impl NoQADirective {
.to_string(),
rule: None,
source_slice: Default::default(),
fixable: false,
})
}
} else {
Expand Down Expand Up @@ -317,6 +323,7 @@ impl IgnoreMask {
description: "Could not get position marker".to_string(),
rule: None,
source_slice: Default::default(),
fixable: false,
})?
.source_position();
NoQADirective::parse_from_comment(comment_content, line_no, line_pos)
Expand Down Expand Up @@ -484,6 +491,7 @@ mod tests {
code: "AL02",
}),
source_slice: Default::default(),
fixable: true,
};
let mask = IgnoreMask {
ignore_list: vec![NoQADirective::LineIgnoreRules(LineIgnoreRules {
Expand Down

0 comments on commit cb1370d

Please sign in to comment.