Skip to content

Commit f20dda3

Browse files
committed
feat: introduce jinja format rule
1 parent 2b22330 commit f20dda3

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

crates/lib/src/core/rules/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub enum RuleGroups {
3434
Ambiguous,
3535
Capitalisation,
3636
Convention,
37+
Jinja,
3738
Layout,
3839
References,
3940
Structure,

crates/lib/src/rules.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod convention;
1010
pub mod layout;
1111
pub mod references;
1212
pub mod structure;
13+
pub mod jinja;
1314

1415
pub fn rules() -> Vec<ErasedRule> {
1516
chain!(
@@ -19,7 +20,8 @@ pub fn rules() -> Vec<ErasedRule> {
1920
convention::rules(),
2021
layout::rules(),
2122
references::rules(),
22-
structure::rules()
23+
structure::rules(),
24+
jinja::rules(),
2325
)
2426
.collect_vec()
2527
}

crates/lib/src/rules/jinja.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub mod jj01;
2+
3+
pub fn rules() -> Vec<ErasedRule> {
4+
use crate::core::rules::base::Erased as _;
5+
6+
vec![jj01::RuleJJ01::default().erased()]
7+
}

crates/lib/src/rules/jinja/jj01.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use ahash::AHashMap;
2+
use sqruff_lib_core::dialects::syntax::{SyntaxKind, SyntaxSet};
3+
use sqruff_lib_core::parser::segments::base::SegmentBuilder;
4+
5+
use crate::core::config::Value;
6+
use crate::core::rules::base::{Erased, ErasedRule, LintResult, Rule, RuleGroups};
7+
use crate::core::rules::context::RuleContext;
8+
use crate::core::rules::crawlers::{Crawler, SegmentSeekerCrawler};
9+
use crate::utils::reflow::sequence::{Filter, ReflowInsertPosition, ReflowSequence, TargetSide};
10+
11+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
12+
pub enum Aliasing {
13+
Explicit,
14+
Implicit,
15+
}
16+
17+
#[derive(Debug, Clone)]
18+
pub struct RuleJJ01 {
19+
}
20+
21+
impl Rule for RuleJJ01 {
22+
fn load_from_config(&self, _config: &AHashMap<String, Value>) -> Result<ErasedRule, String> {
23+
Ok()
24+
}
25+
26+
fn name(&self) -> &'static str {
27+
"jinja.padding"
28+
}
29+
30+
fn description(&self) -> &'static str {
31+
"Jinja tags should have a single whitespace on either side."
32+
}
33+
34+
fn long_description(&self) -> &'static str {
35+
r#"
36+
**Anti-pattern**
37+
38+
Jinja tags with either no whitespace or very long whitespace are hard to read.
39+
40+
```sql
41+
SELECT {{ a }} from {{ref('foo')}}
42+
```
43+
44+
**Best practice**
45+
46+
A single whitespace surrounding Jinja tags, alternatively longer gaps containing newlines are acceptable.
47+
48+
```sql
49+
SELECT {{ a }} from {{ ref('foo') }};
50+
SELECT {{ a }} from {{
51+
ref('foo')
52+
}};
53+
```
54+
"#
55+
}
56+
57+
fn groups(&self) -> &'static [RuleGroups] {
58+
&[RuleGroups::All, RuleGroups::Jinja]
59+
}
60+
61+
fn eval(&self, rule_cx: &RuleContext) -> Vec<LintResult> {
62+
unimplemented!()
63+
}
64+
65+
fn is_fix_compatible(&self) -> bool {
66+
true
67+
}
68+
69+
fn crawl_behaviour(&self) -> Crawler {
70+
SegmentSeekerCrawler::new(const { SyntaxSet::new(&[SyntaxKind::AliasExpression]) }).into()
71+
}
72+
}

0 commit comments

Comments
 (0)