rubocop_middle_dot.rb 930 B

12345678910111213141516171819202122232425262728293031
  1. # frozen_string_literal: true
  2. module RuboCop
  3. module Cop
  4. module Style
  5. # Bans the usage of “•” (bullet) in HTML/HAML in favor of “·” (middle dot) in string literals
  6. class MiddleDot < Base
  7. extend AutoCorrector
  8. extend Util
  9. # rubocop:disable Style/MiddleDot
  10. BULLET = '•'
  11. # rubocop:enable Style/MiddleDot
  12. MIDDLE_DOT = '·'
  13. MESSAGE = "Use '#{MIDDLE_DOT}' (middle dot) instead of '#{BULLET}' (bullet)".freeze
  14. def on_str(node)
  15. # Constants like __FILE__ are handled as strings,
  16. # but don't respond to begin.
  17. return unless node.loc.respond_to?(:begin) && node.loc.begin
  18. return unless node.value.include?(BULLET)
  19. add_offense(node, message: MESSAGE) do |corrector|
  20. corrector.replace(node, node.source.gsub(BULLET, MIDDLE_DOT))
  21. end
  22. end
  23. end
  24. end
  25. end
  26. end