Skip to content

Commit b69604f

Browse files
committed
Refactor flattening of dstrs to be more clear
* And kill alive mutations
1 parent 39d0661 commit b69604f

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

config/flay.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
---
22
threshold: 13
3-
total_score: 666
3+
total_score: 670

lib/unparser/preprocessor.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,36 @@ class FlattenImplicitDSTR < self
161161
# No need to register :dsym, because Ruby doesn't do implicit concatenation of symbols
162162
register :dstr
163163

164+
FLATTEN_CHILDREN = IceNine.deep_freeze([:dstr, :str])
165+
164166
# Return preprocessor result
165167
#
166168
# @return [Parser::AST::Node]
167169
#
168170
# @api private
169171
#
170172
def result
171-
# This should only ever be true for DSTR nodes that are a result of implicit concatenation
172-
# (see the comments on this class). Any other DSTR node would have a :begin node as a child.
173-
if children.any? && children.all? { |child| [:dstr, :str].include?(child.type) }
174-
flat_children = children.flat_map do |child|
175-
if child.type.equal?(:str)
176-
child
177-
else # child.type is dstr
178-
child.children
179-
end
180-
end
181-
node.updated(nil, flat_children)
182-
else
183-
node
173+
return node unless implicit_dstr?
174+
175+
flat_children = children.flat_map do |child|
176+
child.type.equal?(:dstr) ? child.children : child
184177
end
178+
node.updated(nil, flat_children)
179+
end
180+
181+
private
182+
183+
# Test for implicit dstr
184+
#
185+
# This should only ever be true for dstr nodes that are a result of implicit concatenation
186+
# (see the comments on this class). Any other dstr node would have a :begin node as a child.
187+
#
188+
# @return [Boolean]
189+
#
190+
# @api private
191+
#
192+
def implicit_dstr?
193+
children.map(&:type).all?(&FLATTEN_CHILDREN.method(:include?))
185194
end
186195

187196
end # FlattenImplicitDSTR

0 commit comments

Comments
 (0)