Class: RBS::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out:) ⇒ Writer

Returns a new instance of Writer.



5
6
7
# File 'lib/rbs/writer.rb', line 5

def initialize(out:)
  @out = out
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



3
4
5
# File 'lib/rbs/writer.rb', line 3

def out
  @out
end

Instance Method Details

#attribute(kind, attr) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rbs/writer.rb', line 238

def attribute(kind, attr)
  var = case attr.ivar_name
        when nil
          ""
        when false
          "()"
        else
          "(#{attr.ivar_name})"
        end
  "attr_#{kind} #{attr.name}#{var}: #{attr.type}"
end

#method_name(name) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/rbs/writer.rb', line 203

def method_name(name)
  s = name.to_s

  if /\A#{Parser::KEYWORDS_RE}\z/.match?(s)
    "`#{s}`"
  else
    s
  end
end

#name_and_args(name, args) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/rbs/writer.rb', line 141

def name_and_args(name, args)
  if name && args
    if args.empty?
      "#{name}"
    else
      "#{name}[#{args.join(", ")}]"
    end
  end
end

#name_and_params(name, params) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbs/writer.rb', line 117

def name_and_params(name, params)
  if params.empty?
    "#{name}"
  else
    ps = params.each.map do |param|
      s = ""
      if param.skip_validation
        s << "unchecked "
      end
      case param.variance
      when :invariant
        # nop
      when :covariant
        s << "out "
      when :contravariant
        s << "in "
      end
      s + param.name.to_s
    end

    "#{name}[#{ps.join(", ")}]"
  end
end

#preserve_empty_line(prev, decl) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rbs/writer.rb', line 250

def preserve_empty_line(prev, decl)
  return unless prev

  decl = decl.comment if decl.respond_to?(:comment) && decl.comment

  # When the signature is not constructed by the parser,
  # it always inserts an empty line.
  if !prev.location || !decl.location
    out.puts
    return
  end

  prev_end_line = prev.location.end_line
  start_line = decl.location.start_line
  if start_line - prev_end_line > 1
    out.puts
  end
end

#write(decls) ⇒ Object



39
40
41
42
43
44
# File 'lib/rbs/writer.rb', line 39

def write(decls)
  [nil, *decls].each_cons(2) do |prev, decl|
    preserve_empty_line(prev, decl)
    write_decl decl
  end
end

#write_annotation(annotations, level:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rbs/writer.rb', line 9

def write_annotation(annotations, level:)
  prefix = " " * level

  annotations.each do |annotation|
    string = annotation.string
    case
    when string !~ /\}/
      out.puts "#{prefix}%a{#{string}}"
    when string !~ /\)/
      out.puts "#{prefix}%a(#{string})"
    when string !~ /\]/
      out.puts "#{prefix}%a[#{string}]"
    when string !~ /\>/
      out.puts "#{prefix}%a<#{string}>"
    when string !~ /\|/
      out.puts "#{prefix}%a|#{string}|"
    end
  end
end

#write_comment(comment, level:) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/rbs/writer.rb', line 29

def write_comment(comment, level:)
  if comment
    prefix = " " * level
    comment.string.lines.each do |line|
      line = " #{line}" unless line.chomp.empty?
      out.puts "#{prefix}##{line}"
    end
  end
end

#write_decl(decl) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbs/writer.rb', line 46

def write_decl(decl)
  case decl
  when AST::Declarations::Class
    super_class = if decl.super_class
                    " < #{name_and_args(decl.super_class.name, decl.super_class.args)}"
                  end
    write_comment decl.comment, level: 0
    write_annotation decl.annotations, level: 0
    out.puts "class #{name_and_params(decl.name, decl.type_params)}#{super_class}"

    [nil, *decl.members].each_cons(2) do |prev, member|
      preserve_empty_line prev, member
      write_member member
    end

    out.puts "end"

  when AST::Declarations::Module
    self_type = if decl.self_type
                  " : #{decl.self_type}"
                end

    write_comment decl.comment, level: 0
    write_annotation decl.annotations, level: 0
    out.puts "module #{name_and_params(decl.name, decl.type_params)}#{self_type}"
    decl.members.each.with_index do |member, index|
      if index > 0
        out.puts
      end
      write_member member
    end
    out.puts "end"
  when AST::Declarations::Constant
    write_comment decl.comment, level: 0
    out.puts "#{decl.name}: #{decl.type}"

  when AST::Declarations::Global
    write_comment decl.comment, level: 0
    out.puts "#{decl.name}: #{decl.type}"

  when AST::Declarations::Alias
    write_comment decl.comment, level: 0
    write_annotation decl.annotations, level: 0
    out.puts "type #{decl.name} = #{decl.type}"

  when AST::Declarations::Interface
    write_comment decl.comment, level: 0
    write_annotation decl.annotations, level: 0
    out.puts "interface #{name_and_params(decl.name, decl.type_params)}"
    decl.members.each.with_index do |member, index|
      if index > 0
        out.puts
      end
      write_member member
    end
    out.puts "end"

  when AST::Declarations::Extension
    write_comment decl.comment, level: 0
    write_annotation decl.annotations, level: 0
    out.puts "extension #{name_and_args(decl.name, decl.type_params)} (#{decl.extension_name})"
    decl.members.each.with_index do |member, index|
      if index > 0
        out.puts
      end
      write_member member
    end
    out.puts "end"
  end
end

#write_def(member) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rbs/writer.rb', line 213

def write_def(member)
  name = case member.kind
         when :instance
           "#{method_name(member.name)}"
         when :singleton_instance
           "self?.#{method_name(member.name)}"
         when :singleton
           "self.#{method_name(member.name)}"
         end

  attrs = member.attributes.empty? ? "" : member.attributes.join(" ") + " "
  prefix = "  #{attrs}def #{name}:"
  padding = " " * (prefix.size-1)

  out.print prefix

  member.types.each.with_index do |type, index|
    if index > 0
      out.print padding
      out.print "|"
    end
    out.puts " #{type}"
  end
end

#write_member(member) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbs/writer.rb', line 151

def write_member(member)
  case member
  when AST::Members::Include
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    out.puts "  include #{name_and_args(member.name, member.args)}"
  when AST::Members::Extend
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    out.puts "  extend #{name_and_args(member.name, member.args)}"
  when AST::Members::Prepend
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    out.puts "  prepend #{name_and_args(member.name, member.args)}"
  when AST::Members::AttrAccessor
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    out.puts "  #{attribute(:accessor, member)}"
  when AST::Members::AttrReader
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    out.puts "  #{attribute(:reader, member)}"
  when AST::Members::AttrWriter
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    out.puts "  #{attribute(:writer, member)}"
  when AST::Members::Public
    out.puts "  public"
  when AST::Members::Private
    out.puts "  private"
  when AST::Members::Alias
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    new_name = member.singleton? ? "self.#{member.new_name}" : member.new_name
    old_name = member.singleton? ? "self.#{member.old_name}" : member.old_name
    out.puts "  alias #{new_name} #{old_name}"
  when AST::Members::InstanceVariable
    write_comment member.comment, level: 2
    out.puts "  #{member.name}: #{member.type}"
  when AST::Members::ClassInstanceVariable
    write_comment member.comment, level: 2
    out.puts "  self.#{member.name}: #{member.type}"
  when AST::Members::ClassVariable
    write_comment member.comment, level: 2
    out.puts "  #{member.name}: #{member.type}"
  when AST::Members::MethodDefinition
    write_comment member.comment, level: 2
    write_annotation member.annotations, level: 2
    write_def member
  end
end