Skip to content
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

Bugfix: Change textrules API to pass line: Option<&str>. #261

Merged
merged 2 commits into from
Aug 9, 2023
Merged
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
42 changes: 26 additions & 16 deletions src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ macro_rules! pluginrules {
};
}

#[derive(Clone, Copy)]
pub enum TextRuleEvent<'a> {
StartOfFile,
Line(&'a str),
}

#[derive(Clone, Copy)]
pub enum TextRuleResult {
Pass,
Expand All @@ -44,7 +50,7 @@ pub enum TextRuleResult {
pub trait TextRule: Sync + Send {
fn check(
&mut self,
text: &str,
event: TextRuleEvent,
config: &ConfigOption,
) -> TextRuleResult;
fn name(&self) -> String;
Expand Down Expand Up @@ -157,27 +163,31 @@ impl Linter {
Ok(())
}

pub fn textrules_check(&mut self, line: &str, path: &Path, beg: &usize) -> Vec<LintFailed> {
pub fn textrules_check(&mut self, event: TextRuleEvent, path: &Path, beg: &usize) -> Vec<LintFailed> {

let mut ret = Vec::new();
'outer: for rule in &mut self.textrules {

match rule.check(line, &self.option) {
match rule.check(event, &self.option) {
TextRuleResult::Fail {offset, len} => {
for exclude in &self.option.exclude_paths {
if exclude.is_match(&path.to_string_lossy()) {
continue 'outer;
match event {
TextRuleEvent::StartOfFile => {}
TextRuleEvent::Line(_) => {
for exclude in &self.option.exclude_paths {
if exclude.is_match(&path.to_string_lossy()) {
continue 'outer;
}
}
let result = LintFailed {
path: path.to_path_buf(),
beg: beg + offset,
len,
name: rule.name(),
hint: rule.hint(&self.option),
reason: rule.reason(),
};
ret.push(result);
}
}
let result = LintFailed {
path: path.to_path_buf(),
beg: beg + offset,
len,
name: rule.name(),
hint: rule.hint(&self.option),
reason: rule.reason(),
};
ret.push(result);
}
_ => (),
}
Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sv_filelist_parser;
use sv_parser::Error as SvParserError;
use sv_parser::{parse_sv_str, preprocess, Define, DefineText};
use svlint::config::Config;
use svlint::linter::Linter;
use svlint::linter::{Linter, TextRuleEvent};
use svlint::printer::Printer;

// -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -265,16 +265,20 @@ pub fn run_opt_config(printer: &mut Printer, opt: &Opt, config: Config) -> Resul
}
}
} else {
// Iterate over lines in the file, applying each textrule to each
// line in turn.

let text: String = read_to_string(&path)?;
// Signal beginning of file to all TextRules, which *may* be used
// by textrules to reset their internal state.
let _ = linter.textrules_check(TextRuleEvent::StartOfFile, &path, &0);

let text: String = read_to_string(&path)?;
let mut beg: usize = 0;

// Iterate over lines in the file, applying each textrule to each
// line in turn.
for line in text.split_inclusive('\n') {
let line_stripped = line.trim_end_matches(&['\n', '\r']);

for failed in linter.textrules_check(&line_stripped, &path, &beg) {
for failed in linter.textrules_check(TextRuleEvent::Line(&line_stripped), &path, &beg) {
pass = false;
if !opt.silent {
printer.print_failed(&failed, opt.single, opt.github_actions)?;
Expand All @@ -285,9 +289,9 @@ pub fn run_opt_config(printer: &mut Printer, opt: &Opt, config: Config) -> Resul

match parse_sv_str(text.as_str(), &path, &defines, &includes, opt.ignore_include, false) {
Ok((syntax_tree, new_defines)) => {

// Iterate over nodes in the concrete syntax tree, applying
// each syntaxrule to each node in turn.

for node in syntax_tree.into_iter().event() {
for failed in linter.syntaxrules_check(&syntax_tree, &node) {
pass = false;
Expand Down
24 changes: 13 additions & 11 deletions src/textrules/header_copyright.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
use crate::config::ConfigOption;
use crate::linter::{TextRule, TextRuleResult};
use crate::linter::{TextRule, TextRuleEvent, TextRuleResult};
use regex::Regex;

#[derive(Default)]
pub struct HeaderCopyright {
re: Option<Regex>,
linenum: Option<usize>,
linenum: usize,
}

impl TextRule for HeaderCopyright {
fn check(
&mut self,
line: &str,
event: TextRuleEvent,
option: &ConfigOption,
) -> TextRuleResult {
let line: &str = match event {
TextRuleEvent::StartOfFile => {
self.linenum = 0;
return TextRuleResult::Pass;
}
TextRuleEvent::Line(x) => x,
};
self.linenum += 1;

if self.re.is_none() {
let year = &option.copyright_year;
let holder = &option.copyright_holder;
Expand All @@ -22,14 +31,7 @@ impl TextRule for HeaderCopyright {
}
let re = self.re.as_ref().unwrap();

if self.linenum.is_none() {
self.linenum = Some(0);
}
if let Some(x) = self.linenum {
self.linenum = Some(x+1)
}

if self.linenum.unwrap() == option.copyright_linenum {
if self.linenum == option.copyright_linenum {
let is_match: bool = re.is_match(line);
if is_match {
TextRuleResult::Pass
Expand Down
11 changes: 9 additions & 2 deletions src/textrules/style_directives.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::ConfigOption;
use crate::linter::{TextRule, TextRuleResult};
use crate::linter::{TextRule, TextRuleEvent, TextRuleResult};
use regex::Regex;

#[derive(Default)]
Expand All @@ -10,9 +10,16 @@ pub struct StyleDirectives {
impl TextRule for StyleDirectives {
fn check(
&mut self,
line: &str,
event: TextRuleEvent,
_option: &ConfigOption,
) -> TextRuleResult {
let line: &str = match event {
TextRuleEvent::StartOfFile => {
return TextRuleResult::Pass;
}
TextRuleEvent::Line(x) => x,
};

if self.re.is_none() {
let keywords =
[ "begin_keywords" // {{{
Expand Down
11 changes: 9 additions & 2 deletions src/textrules/style_semicolon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::ConfigOption;
use crate::linter::{TextRule, TextRuleResult};
use crate::linter::{TextRule, TextRuleEvent, TextRuleResult};
use regex::Regex;

#[derive(Default)]
Expand All @@ -10,9 +10,16 @@ pub struct StyleSemicolon {
impl TextRule for StyleSemicolon {
fn check(
&mut self,
line: &str,
event: TextRuleEvent,
_option: &ConfigOption,
) -> TextRuleResult {
let line: &str = match event {
TextRuleEvent::StartOfFile => {
return TextRuleResult::Pass;
}
TextRuleEvent::Line(x) => x,
};

if self.re.is_none() {
self.re = Some(Regex::new("([ ]+);").unwrap());
}
Expand Down
11 changes: 9 additions & 2 deletions src/textrules/style_textwidth.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use crate::config::ConfigOption;
use crate::linter::{TextRule, TextRuleResult};
use crate::linter::{TextRule, TextRuleEvent, TextRuleResult};

#[derive(Default)]
pub struct StyleTextwidth;

impl TextRule for StyleTextwidth {
fn check(
&mut self,
line: &str,
event: TextRuleEvent,
option: &ConfigOption,
) -> TextRuleResult {
let line: &str = match event {
TextRuleEvent::StartOfFile => {
return TextRuleResult::Pass;
}
TextRuleEvent::Line(x) => x,
};

let char_indices: Vec<_> = line.char_indices().collect();
let n_chars = char_indices.len();
if n_chars > option.textwidth {
Expand Down