Module: Typelizer::UnionTypeSorter

Defined in:
lib/typelizer/union_type_sorter.rb

Overview

Sorts union type members within TypeScript type strings. Handles types like “Type3 | Type1 | Type2” -> “Type1 | Type2 | Type3” Also handles complex nested types like “Array<Type3 | Type1>” -> “Array<Type1 | Type3>”

Class Method Summary collapse

Class Method Details

.balanced_brackets?(str) ⇒ Boolean

Checks if brackets are balanced in the string

Parameters:

  • str (String)

    The string to check

Returns:

  • (Boolean)

    True if brackets are balanced



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/typelizer/union_type_sorter.rb', line 126

def self.balanced_brackets?(str)
  angle_depth = 0
  paren_depth = 0
  brace_depth = 0
  bracket_depth = 0

  str.each_char do |char|
    case char
    when "<"
      angle_depth += 1
    when ">"
      angle_depth -= 1
      return false if angle_depth < 0
    when "("
      paren_depth += 1
    when ")"
      paren_depth -= 1
      return false if paren_depth < 0
    when "{"
      brace_depth += 1
    when "}"
      brace_depth -= 1
      return false if brace_depth < 0
    when "["
      bracket_depth += 1
    when "]"
      bracket_depth -= 1
      return false if bracket_depth < 0
    end
  end

  angle_depth == 0 && paren_depth == 0 && brace_depth == 0 && bracket_depth == 0
end

.sort(type_str, sort_order) ⇒ String

Sorts union type members in a type string

Parameters:

  • type_str (String)

    The type string potentially containing unions

  • sort_order (Symbol, Proc, nil)

    The sort order (:none, :alphabetical, or Proc)

Returns:

  • (String)

    The type string with sorted union members



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/typelizer/union_type_sorter.rb', line 12

def self.sort(type_str, sort_order)
  return type_str if type_str.nil? || type_str.empty?

  case sort_order
  when :none, nil
    type_str
  when :alphabetical
    sort_unions_alphabetically(type_str)
  when Proc
    result = sort_order.call(type_str)
    result.is_a?(String) ? result : type_str
  else
    type_str
  end
rescue => e
  Typelizer.logger.warn("UnionTypeSorter error: #{e.message}, preserving original order")
  type_str
end

.sort_simple_union(union_str) ⇒ String

Sorts a simple union string (no nested generics)

Parameters:

  • union_str (String)

    String like “Type3 | Type1 | Type2”

Returns:

  • (String)

    Sorted string like “Type1 | Type2 | Type3”



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/typelizer/union_type_sorter.rb', line 60

def self.sort_simple_union(union_str)
  return union_str unless union_str.include?("|")

  parts = split_union_members(union_str)
  return union_str if parts.size <= 1

  # Sort while preserving special cases:
  # - 'null' should typically stay at the end
  # - Keep the relative order of complex nested types
  regular_parts, null_parts = parts.partition { |p| p.strip.downcase != "null" }

  sorted_regular = regular_parts.sort_by { |p| p.strip.downcase }
  (sorted_regular + null_parts).join(" | ")
end

.sort_top_level_union(type_str) ⇒ String

Sorts top-level union (handles cases where unions aren’t inside generics)

Parameters:

  • type_str (String)

    The type string

Returns:

  • (String)

    The sorted type string



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/typelizer/union_type_sorter.rb', line 78

def self.sort_top_level_union(type_str)
  return type_str unless type_str.include?("|")

  parts = split_union_members(type_str)
  return type_str if parts.size <= 1

  # Separate null from other types
  regular_parts, null_parts = parts.partition { |p| p.strip.downcase != "null" }

  sorted_regular = regular_parts.sort_by { |p| p.strip.downcase }
  (sorted_regular + null_parts).join(" | ")
end

.sort_unions_alphabetically(type_str) ⇒ String

Sorts union members alphabetically while preserving structure

Parameters:

  • type_str (String)

    The type string to sort

Returns:

  • (String)

    The sorted type string



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/typelizer/union_type_sorter.rb', line 34

def self.sort_unions_alphabetically(type_str)
  # Handle the string by sorting unions at each level
  # We need to be careful with nested generics like Array<A | B | C>

  result = type_str.dup

  # First, handle unions inside angle brackets (generics)
  # Match content inside < > and sort unions within
  result = result.gsub(/<([^<>]+)>/) do |match|
    inner = Regexp.last_match(1)
    sorted_inner = sort_simple_union(inner)
    "<#{sorted_inner}>"
  end

  # Then handle any remaining top-level unions
  # But avoid sorting if the string has unbalanced brackets
  if balanced_brackets?(result)
    result = sort_top_level_union(result)
  end

  result
end

.split_union_members(str) ⇒ Array<String>

Splits union members while respecting nested brackets

Parameters:

  • str (String)

    The string to split

Returns:

  • (Array<String>)

    Array of union members



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/typelizer/union_type_sorter.rb', line 94

def self.split_union_members(str)
  members = []
  current = +""
  depth = 0

  str.each_char do |char|
    case char
    when "<", "(", "{", "["
      depth += 1
      current << char
    when ">", ")", "}", "]"
      depth -= 1
      current << char
    when "|"
      if depth == 0
        members << current.strip unless current.strip.empty?
        current = +""
      else
        current << char
      end
    else
      current << char
    end
  end

  members << current.strip unless current.strip.empty?
  members
end