Skip to content

feat: introduce jinja format rule #1298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions crates/lib-core/src/parser/segments/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl SegmentBuilder {
SegmentBuilder::token(id, raw, SyntaxKind::Symbol).finish()
}

pub fn raw(id: u32, raw: &str) -> ErasedSegment {
SegmentBuilder::token(id, raw, SyntaxKind::Raw).finish()
}

pub fn node(
id: u32,
syntax_kind: SyntaxKind,
Expand Down Expand Up @@ -139,6 +143,11 @@ impl ErasedSegment {
}
}

/// Return true if this segment has no children
pub fn is_raw(&self) -> bool {
self.segments().is_empty()
}

pub fn segments(&self) -> &[ErasedSegment] {
match &self.value.kind {
NodeOrTokenKind::Node(node) => &node.segments,
Expand Down
8 changes: 6 additions & 2 deletions crates/lib-core/src/templaters/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ impl TemplatedFileInner {
self.templated_str.is_some()
}

pub fn raw_sliced_iter(&self) -> impl Iterator<Item = &RawFileSlice> {
self.raw_sliced.iter()
}

/// Get the line number and position of a point in the source file.
/// Args:
/// - char_pos: The character position in the relevant file.
Expand Down Expand Up @@ -540,8 +544,8 @@ pub enum RawFileSliceType {
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct RawFileSlice {
/// Source string
raw: String,
pub(crate) slice_type: String,
pub raw: String,
pub slice_type: String,
/// Offset from beginning of source string
pub source_idx: usize,
slice_subtype: Option<RawFileSliceType>,
Expand Down
1 change: 1 addition & 0 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ name = "depth_map"
harness = false

[features]
default = ["python"]
python = ["pyo3", "sqruff-lib-core/serde"]

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/core/linter/linted_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sqruff_lib_core::errors::{SQLBaseError, SqlError};
use sqruff_lib_core::parser::segments::fix::FixPatch;
use sqruff_lib_core::templaters::base::{RawFileSlice, TemplatedFile};

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct LintedFile {
pub path: String,
pub patches: Vec<FixPatch>,
Expand Down
4 changes: 3 additions & 1 deletion crates/lib/src/core/rules/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum RuleGroups {
Ambiguous,
Capitalisation,
Convention,
Jinja,
Layout,
References,
Structure,
Expand Down Expand Up @@ -180,7 +181,8 @@ pub trait Rule: CloneRule + dyn_clone::DynClone + Debug + 'static + Send + Sync
tree: ErasedSegment,
config: &FluffConfig,
) -> Vec<SQLLintError> {
let mut root_context = RuleContext::new(tables, dialect, config, tree.clone());
let mut root_context =
RuleContext::new(tables, dialect, config, tree.clone(), &templated_file);
let mut vs = Vec::new();

// TODO Will to return a note that rules were skipped
Expand Down
5 changes: 3 additions & 2 deletions crates/lib/src/core/rules/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::config::FluffConfig;
pub struct RuleContext<'a> {
pub tables: &'a Tables,
pub dialect: &'a Dialect,
pub templated_file: Option<TemplatedFile>,
pub templated_file: &'a TemplatedFile,
pub path: Option<String>,
pub config: &'a FluffConfig,

Expand Down Expand Up @@ -41,13 +41,14 @@ impl<'a> RuleContext<'a> {
dialect: &'a Dialect,
config: &'a FluffConfig,
segment: ErasedSegment,
templated_file: &'a TemplatedFile,
) -> Self {
Self {
tables,
dialect,
config,
segment,
templated_file: <_>::default(),
templated_file,
path: <_>::default(),
parent_stack: <_>::default(),
raw_stack: <_>::default(),
Expand Down
4 changes: 3 additions & 1 deletion crates/lib/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod aliasing;
pub mod ambiguous;
pub mod capitalisation;
pub mod convention;
pub mod jinja;
pub mod layout;
pub mod references;
pub mod structure;
Expand All @@ -19,7 +20,8 @@ pub fn rules() -> Vec<ErasedRule> {
convention::rules(),
layout::rules(),
references::rules(),
structure::rules()
structure::rules(),
jinja::rules(),
)
.collect_vec()
}
Expand Down
9 changes: 9 additions & 0 deletions crates/lib/src/rules/jinja.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::core::rules::base::ErasedRule;

pub mod jj01;

pub fn rules() -> Vec<ErasedRule> {
use crate::core::rules::base::Erased as _;

vec![jj01::RuleJJ01.erased()]
}
Loading