~cytrogen/masto-fe

ref: 57f47e50f09008e03e845358a3a26658c8b3c22d masto-fe/lib/linter/rubocop_middle_dot.rb -rw-r--r-- 930 bytes
57f47e50 — Claire Merge pull request #2416 from ClearlyClaire/glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Bans the usage of “•” (bullet) in HTML/HAML in favor of “·” (middle dot) in string literals
      class MiddleDot < Base
        extend AutoCorrector
        extend Util

        # rubocop:disable Style/MiddleDot
        BULLET = '•'
        # rubocop:enable Style/MiddleDot
        MIDDLE_DOT = '·'
        MESSAGE = "Use '#{MIDDLE_DOT}' (middle dot) instead of '#{BULLET}' (bullet)".freeze

        def on_str(node)
          # Constants like __FILE__ are handled as strings,
          # but don't respond to begin.
          return unless node.loc.respond_to?(:begin) && node.loc.begin

          return unless node.value.include?(BULLET)

          add_offense(node, message: MESSAGE) do |corrector|
            corrector.replace(node, node.source.gsub(BULLET, MIDDLE_DOT))
          end
        end
      end
    end
  end
end