File: sets.rb

package info (click to toggle)
ruby-rubocop-ast 1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,256 kB
  • sloc: ruby: 15,071; yacc: 90; makefile: 9
file content (37 lines) | stat: -rw-r--r-- 929 bytes parent folder | download
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
32
33
34
35
36
37
# frozen_string_literal: true

module RuboCop
  module AST
    class NodePattern
      # Utility to assign a set of values to a constant
      module Sets
        REGISTRY = Hash.new do |h, set|
          name = Sets.name(set).freeze
          Sets.const_set(name, set)
          h[set] = "::RuboCop::AST::NodePattern::Sets::#{name}"
        end

        MAX = 4
        def self.name(set)
          elements = set
          elements = set.first(MAX - 1) << :etc if set.size > MAX
          name = elements.to_a.join('_').upcase.gsub(/[^A-Z0-9_]/, '')
          uniq("SET_#{name}")
        end

        def self.uniq(name)
          return name unless Sets.const_defined?(name)

          (2..Float::INFINITY).each do |i|
            uniq = "#{name}_#{i}"
            return uniq unless Sets.const_defined?(uniq)
          end
        end

        def self.[](set)
          REGISTRY[set]
        end
      end
    end
  end
end