-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): add no-empty-comments rule
Signed-off-by: azjezz <azjezz@protonmail.com>
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
crates/linter/src/plugin/comment/rules/no_empty_comments.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use mago_ast::Program; | ||
use mago_fixer::SafetyClassification; | ||
use mago_reporting::*; | ||
use mago_walker::Walker; | ||
|
||
use crate::context::LintContext; | ||
use crate::plugin::comment::rules::utils::comment_content; | ||
use crate::rule::Rule; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct NoEmptyCommentsRule; | ||
|
||
impl Rule for NoEmptyCommentsRule { | ||
#[inline] | ||
fn get_name(&self) -> &'static str { | ||
"no-empty-comments" | ||
} | ||
|
||
#[inline] | ||
fn get_default_level(&self) -> Option<Level> { | ||
Some(Level::Note) | ||
} | ||
} | ||
|
||
impl<'a> Walker<LintContext<'a>> for NoEmptyCommentsRule { | ||
fn walk_program<'ast>(&self, program: &'ast Program, context: &mut LintContext<'a>) { | ||
for trivia in program.trivia.iter() { | ||
if let Some(content) = comment_content(trivia, context) { | ||
let content = content.trim(); | ||
if !content.is_empty() { | ||
continue; | ||
} | ||
|
||
let issue = Issue::new(context.level(), "Empty comments are not allowed.") | ||
.with_annotation(Annotation::primary(trivia.span).with_message("This is an empty comment.")) | ||
.with_help("Consider removing this comment."); | ||
|
||
context.report_with_fix(issue, |plan| { | ||
plan.delete(trivia.span.to_range(), SafetyClassification::Safe); | ||
}); | ||
} | ||
} | ||
} | ||
} |