Class: Uniword::Lint::BuiltinRules::BannedWords

Inherits:
Rule
  • Object
show all
Defined in:
lib/uniword/lint/builtin_rules/banned_words.rb

Overview

Rule: paragraphs containing banned words trigger a finding.

Constant Summary

Constants inherited from Rule

Rule::TYPES

Instance Attribute Summary

Attributes inherited from Rule

#name, #options, #severity

Instance Method Summary collapse

Methods inherited from Rule

#finding, register, type, types

Constructor Details

#initialize(words:, **rest) ⇒ BannedWords

Returns a new instance of BannedWords.

Parameters:

  • words (Array<String>)

    words to flag



13
14
15
16
# File 'lib/uniword/lint/builtin_rules/banned_words.rb', line 13

def initialize(words:, **rest)
  super(**rest)
  @banned = Set.new(Array(words).map(&:downcase))
end

Instance Method Details

#check(document) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/uniword/lint/builtin_rules/banned_words.rb', line 18

def check(document)
  document.paragraphs.each_with_index do |paragraph, idx|
    text = paragraph.text.to_s.downcase
    @banned.each do |word|
      next unless text.match?(/\b#{Regexp.escape(word)}\b/)

      yield finding(
        message: "Paragraph #{idx + 1} contains banned word " \
                 "'#{word}'",
        path: "paragraph[#{idx}]",
      )
    end
  end
end