Skip to content

Commit

Permalink
[#499] Improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Apr 9, 2024
1 parent 8b5fce5 commit 54e41c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
35 changes: 21 additions & 14 deletions rubocop/custom_cops/class_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,24 @@ def validate_expressions_order(expressions)
expressions.each_cons(2) do |first, second|
next unless EXPRESSION_TYPE_ORDER[first[:category]] > EXPRESSION_TYPE_ORDER[second[:category]]

categories = expressions.map { |expression| expression[:category] }
add_offense(
second[:expression],
message: error_message(second[:category], categories)
message: error_message(second[:category], expressions)
)
end
end

# Find the correct spot for the out of order expression
# rubocop:disable Metrics/MethodLength
def error_message(out_of_order_expression, expression_types)
expression_types.filter { _1 != out_of_order_expression }
.each_with_index do |expression, index|
def error_message(out_of_order_expression, expressions)
expressions.filter { _1[:category] != out_of_order_expression }
.each_with_index do |expression, index|
error = if index.zero?
before_first_expression(expression, out_of_order_expression)
elsif index == expression_types.size - 1
elsif index == expressions.size - 1
after_last_expression(expression, out_of_order_expression)
else
previous_expression = expression_types[index - 1]
previous_expression = expressions[index - 1]
in_between_expressions(previous_expression, expression, out_of_order_expression)
end

Expand All @@ -89,22 +88,30 @@ def error_message(out_of_order_expression, expression_types)
# rubocop:enable Metrics/MethodLength

def before_first_expression(current_expression, out_of_order_expression)
return unless EXPRESSION_TYPE_ORDER[out_of_order_expression] < EXPRESSION_TYPE_ORDER[current_expression]
unless EXPRESSION_TYPE_ORDER[out_of_order_expression] < EXPRESSION_TYPE_ORDER[current_expression[:category]]
return
end

"#{out_of_order_expression} should come before #{current_expression}."
"#{out_of_order_expression} should come before `#{current_expression[:expression].source}`."
end

def after_last_expression(current_expression, out_of_order_expression)
return unless EXPRESSION_TYPE_ORDER[out_of_order_expression] > EXPRESSION_TYPE_ORDER[current_expression]
unless EXPRESSION_TYPE_ORDER[out_of_order_expression] > EXPRESSION_TYPE_ORDER[current_expression[:category]]
return
end

"#{out_of_order_expression} should come after #{current_expression}."
"#{out_of_order_expression} should come after `#{current_expression[:expression].source}`."
end

def in_between_expressions(previous_expression, current_expression, out_of_order_expression)
return unless EXPRESSION_TYPE_ORDER[out_of_order_expression] < EXPRESSION_TYPE_ORDER[current_expression] &&
EXPRESSION_TYPE_ORDER[out_of_order_expression] > EXPRESSION_TYPE_ORDER[previous_expression]
unless EXPRESSION_TYPE_ORDER[out_of_order_expression] < EXPRESSION_TYPE_ORDER[current_expression[:category]] &&
EXPRESSION_TYPE_ORDER[out_of_order_expression] > EXPRESSION_TYPE_ORDER[previous_expression[:category]]
return
end

"#{out_of_order_expression} should come after #{previous_expression} and before #{current_expression}."
"#{out_of_order_expression} should come after "\
"`#{previous_expression[:expression].source}` "\
"and before `#{current_expression[:expression].source}`."
end
end
end
6 changes: 3 additions & 3 deletions rubocop/spec/class_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Test
include B
extend A
^^^^^^^^ extend should come before include.
^^^^^^^^ extend should come before `include B`.
end
RUBY

Expand All @@ -29,7 +29,7 @@ def initialize
end
CONSTANT = 1
^^^^^^^^^^^^ constant_assignment should come after include and before initialization.
^^^^^^^^^^^^ constant_assignment should come after `include B` and before `def initialize[...]
end
RUBY

Expand All @@ -51,7 +51,7 @@ def foo
end
def self.bar
^^^^^^^^^^^^ public_class_method should come after constant_assignment and before initialization.
^^^^^^^^^^^^ public_class_method should come after `CONSTANT = 1` and before `def initialize[...]
"bar"
end
end
Expand Down

0 comments on commit 54e41c8

Please sign in to comment.