Skip to content

Commit

Permalink
feat(linter): add no-empty-comments rule
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Dec 18, 2024
1 parent 5c6c999 commit dd8ce40
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/linter/src/plugin/comment/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::plugin::comment::rules::docblock_syntax::DocblockSyntaxRule;
use crate::plugin::comment::rules::no_empty_comments::NoEmptyCommentsRule;
use crate::plugin::comment::rules::no_shell_style::NoShellStyleRule;
use crate::plugin::comment::rules::no_trailing_whitespace::NoTrailingWhitespaceRule;
use crate::plugin::comment::rules::no_untagged_fixme::NoUntaggedFixmeRule;
Expand All @@ -24,6 +25,7 @@ impl Plugin for CommentPlugin {
fn get_rules(&self) -> Vec<Box<dyn Rule>> {
vec![
Box::new(NoUntaggedTodoRule),
Box::new(NoEmptyCommentsRule),
Box::new(NoUntaggedFixmeRule),
Box::new(NoShellStyleRule),
Box::new(NoTrailingWhitespaceRule),
Expand Down
1 change: 1 addition & 0 deletions crates/linter/src/plugin/comment/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod docblock_syntax;
pub mod no_empty_comments;
pub mod no_shell_style;
pub mod no_trailing_whitespace;
pub mod no_untagged_fixme;
Expand Down
44 changes: 44 additions & 0 deletions crates/linter/src/plugin/comment/rules/no_empty_comments.rs
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);
});
}
}
}
}

0 comments on commit dd8ce40

Please sign in to comment.