Skip to content

Commit 5b16863

Browse files
committed
Simplify mapping logic
1 parent 9264cad commit 5b16863

File tree

1 file changed

+13
-43
lines changed

1 file changed

+13
-43
lines changed

lib/parser.rb

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ def to_alias
9191
annotation = []
9292
annotation << LuaLS.multiline_comment(@comment)
9393
annotation << "---@alias #{@class_name}\n"
94-
@items.each do |item|
95-
annotation << item.to_alias
96-
end
94+
annotation << @items.map(&:to_alias).join
9795
annotation.join
9896
end
9997

@@ -114,9 +112,7 @@ def to_attrs
114112
annotation << attribute_fields
115113

116114
annotation << "---@class #{@class_name}_attrs\n"
117-
@items.each do |item|
118-
annotation << item.to_attrs
119-
end
115+
annotation << @items.map(&:to_attrs).join
120116
annotation << "df.#{@class_name}.attrs = {}\n\n"
121117
end
122118

@@ -126,10 +122,7 @@ def render
126122
annotation << "\n"
127123
annotation << LuaLS.multiline_comment(@comment)
128124
annotation << "---@class _#{@class_name}: DFEnumType\n"
129-
130-
@items.each do |item|
131-
annotation << item.to_field
132-
end
125+
annotation << @items.map(&:to_field).join
133126
annotation << "df.#{@class_name} = {}\n\n"
134127

135128
annotation << to_attrs unless @attributes.empty?
@@ -233,11 +226,7 @@ def to_type
233226
annotation << LuaLS.multiline_comment(@comment)
234227
annotation << "---@class #{@class_name}: DFBitfield\n"
235228
annotation << "---@field _enum _#{@class_name}\n"
236-
fields = []
237-
@items.each do |item|
238-
fields.append(item.to_field_bitfield) if item.render?
239-
end
240-
annotation << fields.join('')
229+
annotation << @items.map(&:to_field_bitfield).join
241230
annotation << "\n"
242231
annotation.join
243232
end
@@ -246,9 +235,7 @@ def render
246235
annotation = ''
247236
annotation << to_type
248237
annotation << "---@class _#{@class_name}: DFBitfieldType\n"
249-
@items.each do |item|
250-
annotation << item.to_field
251-
end
238+
annotation << @items.map(&:to_field).join
252239
annotation << "df.#{@class_name} = {}\n\n"
253240
end
254241
end
@@ -262,10 +249,6 @@ def initialize(node:, index:)
262249
@comment = node['comment']
263250
end
264251

265-
def render?
266-
true
267-
end
268-
269252
def to_alias_string
270253
return '' unless @name
271254

@@ -343,15 +326,11 @@ def to_object
343326
end
344327

345328
annotation << "---@field _type _#{@class_name}\n"
346-
@fields.each do |field|
347-
annotation << field.to_field if field
348-
end
329+
annotation << @fields.map(&:to_field).join
349330

350331
unless @methods.empty?
351332
annotation << "local #{@class_name}\n\n"
352-
@methods.each do |method|
353-
annotation << method.render if method.render?
354-
end
333+
annotation << @methods.select(&:render?).map(&:render).join
355334
end
356335

357336
annotation << "\n"
@@ -388,14 +367,11 @@ def to_type
388367
end
389368

390369
def render
391-
annotation = to_object
370+
annotation = []
371+
annotation << to_object
392372
annotation << to_type
393-
394-
@fields.each do |subtype|
395-
annotation << subtype.render if subtype.render?
396-
end
397-
398-
annotation
373+
annotation << @fields.select(&:render?).map(&:render).join
374+
annotation.join
399375
end
400376
end
401377

@@ -416,11 +392,7 @@ def render
416392
annotation << "---@class df.global: DFGlobal\n"
417393
annotation << @fields.map(&:to_field).join
418394
annotation << "df.global = {}\n\n"
419-
420-
@fields.each do |field|
421-
annotation << field.render if field.render?
422-
end
423-
395+
annotation << @fields.select(&:render?).map(&:render).join
424396
annotation.join
425397
end
426398
end
@@ -582,9 +554,7 @@ def methods
582554

583555
def render
584556
annotation = []
585-
@methods.each do |method|
586-
annotation << method.render if method.render?
587-
end
557+
annotation << @methods.select(&:render?).map(&:render).join
588558
annotation.join
589559
end
590560
end

0 commit comments

Comments
 (0)