lib/rubocop/cop/layout/class_structure.rb in rubocop-1.3.0 vs lib/rubocop/cop/layout/class_structure.rb in rubocop-1.3.1
- old
+ new
@@ -145,10 +145,14 @@
}.freeze
MSG = '`%<category>s` is supposed to appear before ' \
'`%<previous>s`.'
+ def_node_matcher :dynamic_constant?, <<~PATTERN
+ (casgn nil? _ (send ...))
+ PATTERN
+
# Validates code style on class declaration.
# Add offense when find a node out of expected order.
def on_class(class_node)
previous = -1
walk_over_nested_class_definition(class_node) do |node, category|
@@ -166,15 +170,14 @@
private
# Autocorrect by swapping between two nodes autocorrecting them
def autocorrect(corrector, node)
- node_classification = classify(node)
previous = node.left_siblings.find do |sibling|
- classification = classify(sibling)
- !ignore?(classification) && node_classification != classification
+ !ignore_for_autocorrect?(node, sibling)
end
+ return unless previous
current_range = source_range_with_comment(node)
previous_range = source_range_with_comment(previous)
corrector.insert_before(previous_range, current_range.source)
@@ -237,9 +240,18 @@
def ignore?(classification)
classification.nil? ||
classification.to_s.end_with?('=') ||
expected_order.index(classification).nil?
+ end
+
+ def ignore_for_autocorrect?(node, sibling)
+ classification = classify(node)
+ sibling_class = classify(sibling)
+
+ ignore?(sibling_class) ||
+ classification == sibling_class ||
+ dynamic_constant?(node)
end
def humanize_node(node)
if node.def_type?
return :initializer if node.method?(:initialize)