Skip to content

Commit

Permalink
Merge dafbc30 into d418418
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking authored Oct 8, 2024
2 parents d418418 + dafbc30 commit 473954f
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 32 deletions.
1 change: 1 addition & 0 deletions crates/cli/tests/ui/LT01_noqa.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1; --noqa: disable=LT01
2 changes: 2 additions & 0 deletions crates/cli/tests/ui/LT01_noqa.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The linter processed 1 file(s).
All Finished
8 changes: 7 additions & 1 deletion crates/lib-core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct SQLBaseError {
pub source_slice: Range<usize>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
pub struct ErrorStructRule {
pub name: &'static str,
pub code: &'static str,
Expand Down Expand Up @@ -126,6 +126,12 @@ impl From<SQLLintError> for SQLBaseError {
}
}

impl From<SQLBaseError> for SQLLintError {
fn from(value: SQLBaseError) -> Self {
Self { base: value }
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct SQLTemplaterError {}

Expand Down
61 changes: 45 additions & 16 deletions crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rayon::iter::{IntoParallelRefIterator as _, ParallelIterator as _};
use smol_str::{SmolStr, ToSmolStr};
use sqruff_lib_core::dialects::base::Dialect;
use sqruff_lib_core::errors::{
SQLFluffUserError, SQLLexError, SQLLintError, SQLParseError, SqlError,
SQLBaseError, SQLFluffUserError, SQLLexError, SQLLintError, SQLParseError, SqlError,
};
use sqruff_lib_core::helpers;
use sqruff_lib_core::linter::compute_anchor_edit_info;
Expand All @@ -28,6 +28,7 @@ use crate::core::linter::common::{ParsedString, RenderedFile};
use crate::core::linter::linted_file::LintedFile;
use crate::core::linter::linting_result::LintingResult;
use crate::core::rules::base::{ErasedRule, LintPhase, RulePack};
use crate::core::rules::noqa::IgnoreMask;
use crate::rules::get_ruleset;
use crate::templaters::placeholder::PlaceholderTemplater;
use crate::templaters::raw::RawTemplater;
Expand Down Expand Up @@ -187,25 +188,38 @@ impl Linter {
) -> LintedFile {
let mut violations = parsed_string.violations;

let (tree, initial_linting_errors) = parsed_string
.tree
.map(|tree| self.lint_fix_parsed(tables, tree, &parsed_string.templated_file, fix))
.unzip();
let (patches, ignore_mask, initial_linting_errors) =
parsed_string
.tree
.map_or((Vec::new(), None, Vec::new()), |erased_segment| {
let (tree, ignore_mask, initial_linting_errors) = self.lint_fix_parsed(
tables,
erased_segment,
&parsed_string.templated_file,
fix,
);
let patches = tree.iter_patches(&parsed_string.templated_file);
(patches, ignore_mask, initial_linting_errors)
});
violations.extend(initial_linting_errors.into_iter().map_into());

violations.extend(
initial_linting_errors
.unwrap_or_default()
.into_iter()
.map(Into::into),
);
// Filter violations with ignore mask
let violations = violations
.into_iter()
.filter(|violation| {
ignore_mask
.as_ref()
.map_or(true, |ignore_mask| !ignore_mask.is_masked(violation))
})
.collect();

// TODO Need to error out unused noqas
let linted_file = LintedFile {
path: parsed_string.filename,
patches: tree.map_or(Vec::new(), |tree| {
tree.iter_patches(&parsed_string.templated_file)
}),
patches,
templated_file: parsed_string.templated_file,
violations,
ignore_mask,
};

if let Some(formatter) = &self.formatter {
Expand All @@ -221,7 +235,7 @@ impl Linter {
mut tree: ErasedSegment,
templated_file: &TemplatedFile,
fix: bool,
) -> (ErasedSegment, Vec<SQLLintError>) {
) -> (ErasedSegment, Option<IgnoreMask>, Vec<SQLLintError>) {
let mut tmp;
let mut initial_linting_errors = Vec::new();
let phases: &[_] = if fix {
Expand All @@ -235,6 +249,21 @@ impl Linter {
// If we are fixing then we want to loop up to the runaway_limit, otherwise just
// once for linting.
let loop_limit = if fix { 10 } else { 1 };
// Look for comment segments which might indicate lines to ignore.
let (ignore_mask, violations): (Option<IgnoreMask>, Vec<SQLBaseError>) = {
let disable_noqa = self
.config
.get("disable_noqa", "core")
.as_bool()
.unwrap_or(false);
if disable_noqa {
(None, Vec::new())
} else {
let (ignore_mask, errors) = IgnoreMask::from_tree(&tree);
(Some(ignore_mask), errors)
}
};
initial_linting_errors.extend(violations.into_iter().map_into());

for phase in phases {
let mut rules_this_phase = if phases.len() > 1 {
Expand Down Expand Up @@ -327,7 +356,7 @@ impl Linter {
}
}

(tree, initial_linting_errors)
(tree, ignore_mask, initial_linting_errors)
}

/// Template the file.
Expand Down
8 changes: 3 additions & 5 deletions crates/lib/src/core/linter/linted_file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ops::Range;

use crate::core::rules::noqa::IgnoreMask;
use itertools::Itertools;
use rustc_hash::FxHashSet;
use sqruff_lib_core::errors::SQLBaseError;
Expand All @@ -12,16 +13,13 @@ pub struct LintedFile {
pub patches: Vec<FixPatch>,
pub templated_file: TemplatedFile,
pub violations: Vec<SQLBaseError>,
pub ignore_mask: Option<IgnoreMask>,
}

impl LintedFile {
#[allow(unused_variables)]
pub fn get_violations(&self, fixable: Option<bool>) -> Vec<SQLBaseError> {
self.violations
.clone()
.into_iter()
.map(Into::into)
.collect_vec()
self.violations.clone().into_iter().map_into().collect_vec()
}

/// Use patches and raw file to fix the source file.
Expand Down
1 change: 1 addition & 0 deletions crates/lib/src/core/rules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod base;
pub mod context;
pub mod crawlers;
pub mod noqa;
pub mod reference;
Loading

0 comments on commit 473954f

Please sign in to comment.