lib/steep/errors.rb in steep-0.38.0 vs lib/steep/errors.rb in steep-0.39.0
- old
+ new
@@ -6,13 +6,24 @@
def initialize(node:)
@node = node
end
def location_to_str
- Rainbow("#{node.loc.expression.source_buffer.name}:#{node.loc.first_line}:#{node.loc.column}").red
+ file = Rainbow(node.loc.expression.source_buffer.name).cyan
+ line = Rainbow(node.loc.first_line).bright
+ column = Rainbow(node.loc.column).bright
+ "#{file}:#{line}:#{column}"
end
+ def format_message(message, class_name: self.class.name.split("::").last)
+ if message.empty?
+ "#{location_to_str}: #{Rainbow(class_name).red}"
+ else
+ "#{location_to_str}: #{Rainbow(class_name).red}: #{message}"
+ end
+ end
+
def print_to(io)
source = node.loc.expression.source
io.puts "#{to_s} (#{Rainbow(source.split(/\n/).first).blue})"
end
end
@@ -43,11 +54,11 @@
@rhs_type = rhs_type
@result = result
end
def to_s
- "#{location_to_str}: IncompatibleAssignment: lhs_type=#{lhs_type}, rhs_type=#{rhs_type}"
+ format_message "lhs_type=#{lhs_type}, rhs_type=#{rhs_type}"
end
end
class IncompatibleArguments < Base
attr_reader :node
@@ -59,11 +70,11 @@
@receiver_type = receiver_type
@method_type = method_type
end
def to_s
- "#{location_to_str}: IncompatibleArguments: receiver=#{receiver_type}, method_type=#{method_type}"
+ format_message "receiver=#{receiver_type}, method_type=#{method_type}"
end
end
class UnresolvedOverloading < Base
attr_reader :node
@@ -77,11 +88,11 @@
@method_name = method_name
@method_types = method_types
end
def to_s
- "#{location_to_str}: UnresolvedOverloading: receiver=#{receiver_type}, method_name=#{method_name}, method_types=#{method_types.join(" | ")}"
+ format_message "receiver=#{receiver_type}, method_name=#{method_name}, method_types=#{method_types.join(" | ")}"
end
end
class ArgumentTypeMismatch < Base
attr_reader :node
@@ -95,11 +106,11 @@
@expected = expected
@actual = actual
end
def to_s
- "#{location_to_str}: ArgumentTypeMismatch: receiver=#{receiver_type}, expected=#{expected}, actual=#{actual}"
+ format_message "receiver=#{receiver_type}, expected=#{expected}, actual=#{actual}"
end
end
class BlockParameterTypeMismatch < Base
attr_reader :expected
@@ -110,11 +121,11 @@
@expected = expected
@actual = actual
end
def to_s
- "#{location_to_str}: BlockParameterTypeMismatch: expected=#{expected}, actual=#{actual}"
+ format_message "expected=#{expected}, actual=#{actual}"
end
end
class NoMethod < Base
attr_reader :type
@@ -125,11 +136,11 @@
@type = type
@method = method
end
def to_s
- "#{location_to_str}: NoMethodError: type=#{type}, method=#{method}"
+ format_message "type=#{type}, method=#{method}", class_name: "NoMethodError"
end
end
class ReturnTypeMismatch < Base
attr_reader :expected
@@ -144,11 +155,11 @@
@actual = actual
@result = result
end
def to_s
- "#{location_to_str}: ReturnTypeMismatch: expected=#{expected}, actual=#{actual}"
+ format_message "expected=#{expected}, actual=#{actual}"
end
end
class UnexpectedBlockGiven < Base
attr_reader :method_type
@@ -157,11 +168,11 @@
super(node: node)
@method_type = method_type
end
def to_s
- "#{location_to_str}: UnexpectedBlockGiven: method_type=#{method_type}"
+ format_message "method_type=#{method_type}"
end
end
class RequiredBlockMissing < Base
attr_reader :method_type
@@ -170,11 +181,11 @@
super(node: node)
@method_type = method_type
end
def to_s
- "#{location_to_str}: RequiredBlockMissing: method_type=#{method_type.to_s}"
+ format_message "method_type=#{method_type}"
end
end
class BlockTypeMismatch < Base
attr_reader :expected
@@ -189,11 +200,11 @@
@actual = actual
@result = result
end
def to_s
- "#{location_to_str}: BlockTypeMismatch: expected=#{expected}, actual=#{actual}"
+ format_message "expected=#{expected}, actual=#{actual}"
end
end
class BlockBodyTypeMismatch < Base
attr_reader :expected
@@ -208,11 +219,11 @@
@actual = actual
@result = result
end
def to_s
- "#{location_to_str}: BlockBodyTypeMismatch: expected=#{expected}, actual=#{actual}"
+ format_message "expected=#{expected}, actual=#{actual}"
end
end
class BreakTypeMismatch < Base
attr_reader :expected
@@ -227,29 +238,29 @@
@actual = actual
@result = result
end
def to_s
- "#{location_to_str}: BreakTypeMismatch: expected=#{expected}, actual=#{actual}"
+ format_message "expected=#{expected}, actual=#{actual}"
end
end
class UnexpectedJump < Base
def to_s
- "#{location_to_str}: UnexpectedJump"
+ format_message ""
end
end
class UnexpectedJumpValue < Base
def to_s
- "#{location_to_str}: UnexpectedJumpValue"
+ format_message ""
end
end
class MethodArityMismatch < Base
def to_s
- "#{location_to_str}: MethodArityMismatch: method=#{node.children[0]}"
+ format_message "method=#{node.children[0]}"
end
end
class IncompatibleMethodTypeAnnotation < Base
attr_reader :interface_method
@@ -264,11 +275,11 @@
@annotation_method = annotation_method
@result = result
end
def to_s
- "#{location_to_str}: IncompatibleMethodTypeAnnotation: interface_method=#{interface_method.type_name}.#{interface_method.name}, annotation_method=#{annotation_method.name}"
+ format_message "interface_method=#{interface_method.type_name}.#{interface_method.name}, annotation_method=#{annotation_method.name}"
end
end
class MethodDefinitionWithOverloading < Base
attr_reader :method
@@ -277,11 +288,11 @@
super(node: node)
@method = method
end
def to_s
- "#{location_to_str}: MethodDefinitionWithOverloading: method=#{method.name}, types=#{method.types.join(" | ")}"
+ format_message "method=#{method.name}, types=#{method.types.join(" | ")}"
end
end
class MethodReturnTypeAnnotationMismatch < Base
attr_reader :method_type
@@ -296,11 +307,11 @@
@annotation_type = annotation_type
@result = result
end
def to_s
- "#{location_to_str}: MethodReturnTypeAnnotationMismatch: method_type=#{method_type.return_type}, annotation_type=#{annotation_type}"
+ format_message "method_type=#{method_type.return_type}, annotation_type=#{annotation_type}"
end
end
class MethodBodyTypeMismatch < Base
attr_reader :expected
@@ -322,17 +333,17 @@
node.children[0]
when :defs
prefix = node.children[0].type == :self ? "self" : "*"
"#{prefix}.#{node.children[1]}"
end
- "#{location_to_str}: MethodBodyTypeMismatch: method=#{method}, expected=#{expected}, actual=#{actual}"
+ format_message "method=#{method}, expected=#{expected}, actual=#{actual}"
end
end
class UnexpectedYield < Base
def to_s
- "#{location_to_str}: UnexpectedYield"
+ format_message ""
end
end
class UnexpectedSuper < Base
attr_reader :method
@@ -341,11 +352,11 @@
super(node: node)
@method = method
end
def to_s
- "#{location_to_str}: UnexpectedSuper: method=#{method}"
+ format_message "method=#{method}"
end
end
class IncompatibleZuper < Base
attr_reader :method
@@ -354,11 +365,11 @@
super(node: node)
@method = method
end
def to_s
- "#{location_to_str}: IncompatibleZuper: method=#{method}"
+ format_message "method=#{method}"
end
end
class MethodDefinitionMissing < Base
attr_reader :module_name
@@ -377,11 +388,11 @@
when :instance
"#{missing_method}"
when :module
"self.#{missing_method}"
end
- "#{location_to_str}: MethodDefinitionMissing: module=#{module_name}, method=#{method}"
+ format_message "module=#{module_name}, method=#{method}"
end
end
class UnexpectedDynamicMethod < Base
attr_reader :module_name
@@ -392,11 +403,11 @@
@module_name = module_name
@method_name = method_name
end
def to_s
- "#{location_to_str}: UnexpectedDynamicMethod: module=#{module_name}, method=#{method_name}"
+ format_message "module=#{module_name}, method=#{method_name}"
end
end
class UnknownConstantAssigned < Base
attr_reader :type
@@ -405,21 +416,21 @@
super(node: node)
@type = type
end
def to_s
- "#{location_to_str}: UnknownConstantAssigned: type=#{type}"
+ format_message "type=#{type}"
end
end
class FallbackAny < Base
def initialize(node:)
@node = node
end
def to_s
- "#{location_to_str}: FallbackAny"
+ format_message ""
end
end
class UnsatisfiableConstraint < Base
attr_reader :method_type
@@ -438,11 +449,11 @@
end
include ResultPrinter
def to_s
- "#{location_to_str}: UnsatisfiableConstraint: method_type=#{method_type}, constraint=#{sub_type} <: '#{var} <: #{super_type}"
+ format_message "method_type=#{method_type}, constraint=#{sub_type} <: '#{var} <: #{super_type}"
end
end
class IncompatibleAnnotation < Base
attr_reader :var_name
@@ -457,11 +468,11 @@
end
include ResultPrinter
def to_s
- "#{location_to_str}: IncompatibleAnnotation: var_name=#{var_name}, #{relation}"
+ format_message "var_name=#{var_name}, #{relation}"
end
end
class IncompatibleTypeCase < Base
attr_reader :var_name
@@ -476,11 +487,11 @@
end
include ResultPrinter
def to_s
- "#{location_to_str}: IncompatibleTypeCase: var_name=#{var_name}, #{relation}"
+ format_message "var_name=#{var_name}, #{relation}"
end
end
class ElseOnExhaustiveCase < Base
attr_reader :type
@@ -489,11 +500,11 @@
super(node: node)
@type = type
end
def to_s
- "#{location_to_str}: ElseOnExhaustiveCase: type=#{type}"
+ format_message "type=#{type}"
end
end
class UnexpectedSplat < Base
attr_reader :type
@@ -502,11 +513,11 @@
super(node: node)
@type = type
end
def to_s
- "#{location_to_str}: UnexpectedSplat: type=#{type}"
+ format_message "type=#{type}"
end
end
class IncompatibleTuple < Base
attr_reader :expected_tuple
@@ -517,11 +528,11 @@
@result = result
@expected_tuple = expected_tuple
end
def to_s
- "#{location_to_str}: IncompatibleTuple: expected_tuple=#{expected_tuple}"
+ format_message "expected_tuple=#{expected_tuple}"
end
end
class UnexpectedKeyword < Base
attr_reader :unexpected_keywords
@@ -530,11 +541,11 @@
super(node: node)
@unexpected_keywords = unexpected_keywords
end
def to_s
- "#{location_to_str}: UnexpectedKeyword: #{unexpected_keywords.to_a.join(", ")}"
+ format_message unexpected_keywords.to_a.join(", ")
end
end
class MissingKeyword < Base
attr_reader :missing_keywords
@@ -543,11 +554,11 @@
super(node: node)
@missing_keywords = missing_keywords
end
def to_s
- "#{location_to_str}: MissingKeyword: #{missing_keywords.to_a.join(", ")}"
+ format_message missing_keywords.to_a.join(", ")
end
end
class UnsupportedSyntax < Base
attr_reader :message
@@ -556,12 +567,11 @@
super(node: node)
@message = message
end
def to_s
- msg = message || "#{node.type} is not supported"
- "#{location_to_str}: UnsupportedSyntax: #{msg}"
+ format_message(message || "#{node.type} is not supported")
end
end
class UnexpectedError < Base
attr_reader :message
@@ -572,11 +582,11 @@
@error = error
@message = error.message
end
def to_s
- <<-MESSAGE
-#{location_to_str}: UnexpectedError: #{error.class}
+ format_message <<-MESSAGE
+#{error.class}
>> #{message}
MESSAGE
end
end
end