Class: RuboCop::Cop::AI::AdverbSpam

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/ai/adverb_spam.rb

Overview

This cop checks for comments that contain an excessive sequence of adverbs or seemingly random words, which are often generated by malfunctioning AI tools or LLM doc generators.

Examples:

# bad
# @param c_m [String] securely accurately dynamically visually intuitively conditionally ...

# good
# @param c_m [String] the properly formatted parameter

Constant Summary collapse

MSG =
'Avoid AI-generated spam inside comments (excessive adverbs/meaningless words).'

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/ai/adverb_spam.rb', line 25

def on_new_investigation
  # Pattern to match sequences of 2 or more words ending in 'ly'
  # including optional spaces and punctuation in between (but NOT newlines).
  pattern = /(?:[ \t]*\b[a-zA-Z]+ly\b[ \t,.]*){2,}/i

  processed_source.comments.each do |comment|
    text = comment.text
    next unless text.match?(pattern)

    add_offense(comment) do |corrector|
      new_text = text.gsub(pattern, '').rstrip
      # Ensure the comment still starts with '#' if it got stripped entirely
      new_text = '#' if new_text.empty?
      corrector.replace(comment, new_text)
    end
  end
end