Skip to content

Commit 5033d08

Browse files
committed
Add support for IN2 rule
1 parent 2c0367e commit 5033d08

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

src/analysis/parsing/parser.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,14 @@ impl <'a> FileParser<'a> {
369369
self.advance();
370370
},
371371
TokenKind::Whitespace => {
372-
// TODO: this might not work correctly with tabs
373-
self.current_column += self.lexer.slice().len() as u32;
372+
let slice = self.lexer.slice();
373+
for ch in slice.chars() {
374+
if ch == '\t' {
375+
self.current_column += 4;
376+
} else {
377+
self.current_column += 1;
378+
}
379+
}
374380
self.advance();
375381
},
376382
TokenKind::MultilineComment => {

src/lint/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
use rules::{instantiate_rules, CurrentRules};
77
use rules::{spacing::{SpBraceOptions, SpPunctOptions, NspFunparOptions,
88
NspInparenOptions, NspUnaryOptions, NspTrailingOptions},
9-
indentation::{LongLineOptions, IN3Options,
9+
indentation::{LongLineOptions, IN2Options, IN3Options,
1010
IN9Options, ContinuationLineOptions},
1111
};
1212
use crate::analysis::{DMLError, IsolatedAnalysis, LocalDMLError};
@@ -55,6 +55,8 @@ pub struct LintCfg {
5555
#[serde(default)]
5656
pub long_lines: Option<LongLineOptions>,
5757
#[serde(default)]
58+
pub in2: Option<IN2Options>,
59+
#[serde(default)]
5860
pub in3: Option<IN3Options>,
5961
#[serde(default)]
6062
pub continuation_line: Option<ContinuationLineOptions>,
@@ -74,6 +76,7 @@ impl Default for LintCfg {
7476
long_lines: Some(LongLineOptions {
7577
max_length: MAX_LENGTH_DEFAULT,
7678
}),
79+
in2: Some(IN2Options{}),
7780
in3: Some(IN3Options{indentation_spaces: 4}),
7881
continuation_line: Some(ContinuationLineOptions {
7982
indentation_spaces: INDENTATION_LEVEL_DEFAULT,
@@ -126,6 +129,7 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
126129
// Per line checks
127130
let lines: Vec<&str> = file.lines().collect();
128131
for (row, line) in lines.iter().enumerate() {
132+
rules.in2.check(&mut linting_errors, row, line);
129133
rules.long_lines.check(&mut linting_errors, row, line);
130134
rules.nsp_trailing.check(&mut linting_errors, row, line);
131135
}

src/lint/rules/indentation.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,38 @@ impl Rule for LongLinesRule {
5959
}
6060
}
6161

62+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
63+
pub struct IN2Options {}
64+
65+
pub struct IN2Rule {
66+
pub enabled: bool,
67+
}
68+
69+
impl IN2Rule {
70+
pub fn check(&self, acc: &mut Vec<LocalDMLError>, row: usize, line: &str) {
71+
if !self.enabled { return; }
72+
let rowu32 = row.try_into().unwrap();
73+
74+
for (col, _) in line.match_indices('\t') {
75+
let colu32 = col.try_into().unwrap();
76+
let msg = IN2Rule::description().to_owned();
77+
let dmlerror = LocalDMLError {
78+
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, colu32, colu32 + 1),
79+
description: msg,
80+
};
81+
acc.push(dmlerror);
82+
}
83+
}
84+
}
85+
impl Rule for IN2Rule {
86+
fn name() -> &'static str {
87+
"IN2"
88+
}
89+
fn description() -> &'static str {
90+
"Tab characters (ASCII 9) should never be used to indent lines."
91+
}
92+
}
93+
6294
pub struct IN3Rule {
6395
pub enabled: bool,
6496
indentation_spaces: u32

src/lint/rules/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod tests;
77
use spacing::{SpBracesRule,
88
SpPunctRule, NspFunparRule, NspInparenRule,
99
NspUnaryRule, NspTrailingRule};
10-
use indentation::{LongLinesRule, IN3Rule, IN9Rule, ContinuationLineRule};
10+
use indentation::{LongLinesRule, IN2Rule, IN3Rule, IN9Rule, ContinuationLineRule};
1111
use crate::lint::LintCfg;
1212

1313
pub struct CurrentRules {
@@ -18,6 +18,7 @@ pub struct CurrentRules {
1818
pub nsp_unary: NspUnaryRule,
1919
pub nsp_trailing: NspTrailingRule,
2020
pub long_lines: LongLinesRule,
21+
pub in2: IN2Rule,
2122
pub in3: IN3Rule,
2223
pub continuation_line: ContinuationLineRule,
2324
pub in9: IN9Rule
@@ -32,6 +33,7 @@ pub fn instantiate_rules(cfg: &LintCfg) -> CurrentRules {
3233
nsp_unary: NspUnaryRule { enabled: cfg.nsp_unary.is_some() },
3334
nsp_trailing: NspTrailingRule { enabled: cfg.nsp_trailing.is_some() },
3435
long_lines: LongLinesRule::from_options(&cfg.long_lines),
36+
in2: IN2Rule { enabled: cfg.in2.is_some() },
3537
in3: IN3Rule::from_options(&cfg.in3),
3638
continuation_line: ContinuationLineRule::from_options(&cfg.continuation_line),
3739
in9: IN9Rule::from_options(&cfg.in9),

src/lint/rules/tests/indentation/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod in2;
12
pub mod in3;
23
pub mod in6;
34
pub mod in9;

0 commit comments

Comments
 (0)