Class: RBS::Sorter

Inherits:
Object
  • Object
show all
Includes:
AST
Defined in:
lib/rbs/sorter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, stdout: $stdout) ⇒ Sorter

Returns a new instance of Sorter.



9
10
11
12
# File 'lib/rbs/sorter.rb', line 9

def initialize(path, stdout: $stdout)
  @path = path
  @stdout = stdout
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/rbs/sorter.rb', line 7

def path
  @path
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



7
8
9
# File 'lib/rbs/sorter.rb', line 7

def stdout
  @stdout
end

Instance Method Details

#group(member) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rbs/sorter.rb', line 31

def group(member)
  case member
  when Declarations::Alias
    -3
  when Declarations::Constant
    -2
  when Declarations::Class, Declarations::Module
    -1
  when Members::Include
    0.0
  when Members::Prepend
    0.2
  when Members::Extend
    0.4
  when Members::ClassVariable
    1
  when Members::ClassInstanceVariable
    2
  when Members::InstanceVariable
    3
  when Members::AttrAccessor, Members::AttrWriter, Members::AttrReader
    if member.kind == :singleton
      5.0
    else
      6.0
    end
  when Members::MethodDefinition
    case member.kind
    when :singleton_instance
      4
    when :singleton
      if member.name == :new
        5.2
      elsif member.visibility == :public
        5.4
      else
        5.6
      end
    else
      if member.name == :initialize
        6.2
      elsif member.visibility == :public
        6.4
      else
        6.6
      end
    end
  when Members::Alias
    if member.singleton?
      5.4
    else
      6.4
    end
  else
    raise
  end
end

#key(member) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rbs/sorter.rb', line 89

def key(member)
  case member
  when Members::Include, Members::Extend, Members::Prepend
    member.name.to_s
  when Members::ClassVariable, Members::ClassInstanceVariable, Members::InstanceVariable
    member.name.to_s
  when Members::AttrAccessor, Members::AttrWriter, Members::AttrReader
    member.name.to_s
  when Members::MethodDefinition
    member.name.to_s
  when Members::Alias
    member.new_name.to_s
  when Declarations::Constant
    member.name.to_s
  when Declarations::Alias
    member.name.to_s
  when Declarations::Class, Declarations::Module
    member.name.to_s
  else
    raise
  end
end

#runObject



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

def run
  stdout.puts "Opening #{path}..."

  buffer = Buffer.new(name: path, content: path.read)
  sigs = Parser.parse_signature(buffer)

  sigs.each do |m|
    sort_decl! m
  end

  stdout.puts "Writing #{path}..."
  path.open('w') do |out|
    writer = RBS::Writer.new(out: out)
    writer.write sigs
  end
end

#sort_decl!(decl) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
159
160
161
162
163
164
165
166
# File 'lib/rbs/sorter.rb', line 112

def sort_decl!(decl)
  case decl
  when Declarations::Class, Declarations::Module, Declarations::Interface
    decl.members.each { |m| sort_decl! m }

    current_visibility = :public
    decl.members.map! do |m|
      case m
      when Members::Public
        current_visibility = :public
        nil
      when Members::Private
        current_visibility = :private
        nil
      when Members::MethodDefinition, Members::AttrReader, Members::AttrWriter, Members::AttrAccessor
        m.update(visibility: m.visibility || current_visibility)
      else
        m
      end
    end
    decl.members.compact!

    decl.members.sort! do |m1, m2|
      group1 = group(m1)
      group2 = group(m2)

      if group1 == group2
        key(m1) <=> key(m2)
      else
        group1 <=> group2
      end
    end

    current_visibility = :public
    decl.members.map! do |m|
      case m
      when Members::MethodDefinition, Members::AttrReader, Members::AttrWriter, Members::AttrAccessor
        new = m.update(visibility: nil)
        cur = current_visibility
        current_visibility = m.visibility
        if cur != m.visibility
          [
            m.visibility == :public ? Members::Public.new(location: nil) : Members::Private.new(location: nil),
            new
          ]
        else
          new
        end
      else
        m
      end
    end
    decl.members.flatten!
  end
end