lib/rbs/errors.rb in rbs-3.0.4 vs lib/rbs/errors.rb in rbs-3.1.0
- old
+ new
@@ -20,15 +20,20 @@
class LoadingError < BaseError; end
class DefinitionError < BaseError; end
module DetailedMessageable
def detailed_message(highlight: false, **)
+ msg = super
+
+ # Support only one line
+ return msg unless location.start_line == location.end_line
+
indent = " " * location.start_column
marker = "^" * (location.end_column - location.start_column)
io = StringIO.new
- io.puts super
+ io.puts msg
io.puts
io.print "\e[1m" if highlight
io.puts " #{location.buffer.lines[location.end_line - 1]}"
io.puts " #{indent}#{marker}"
io.print "\e[m" if highlight
@@ -123,11 +128,13 @@
end
end
end
end
- class NoTypeFoundError < BaseError
+ class NoTypeFoundError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :location
def initialize(type_name:, location:)
@type_name = type_name
@@ -161,26 +168,34 @@
raise new(type_name: type_name, location: location)
end
end
class InheritModuleError < DefinitionError
+ include DetailedMessageable
+
attr_reader :super_decl
def initialize(super_decl)
@super_decl = super_decl
super "#{Location.to_string(super_decl.location)}: Cannot inherit a module: #{super_decl.name}"
end
+ def location
+ @super_decl.location
+ end
+
def self.check!(super_decl, env:)
return if env.class_decl?(super_decl.name) || env.class_alias?(super_decl.name)
raise new(super_decl)
end
end
class NoSelfTypeFoundError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :location
def initialize(type_name:, location:)
@type_name = type_name
@@ -195,10 +210,12 @@
(env.module_name?(type_name) || env.interface_name?(type_name)) or raise new(type_name: type_name, location: self_type.location)
end
end
class NoMixinFoundError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :member
def initialize(type_name:, member:)
@type_name = type_name
@@ -215,10 +232,12 @@
(env.module_name?(type_name) || env.interface_name?(type_name)) or raise new(type_name: type_name, member: member)
end
end
class DuplicatedMethodDefinitionError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type
attr_reader :method_name
attr_reader :members
def initialize(type:, method_name:, members:)
@@ -254,10 +273,12 @@
members.drop(1).map(&:location)
end
end
class DuplicatedInterfaceMethodDefinitionError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type
attr_reader :method_name
attr_reader :member
def initialize(type:, method_name:, member:)
@@ -266,10 +287,14 @@
@member = member
super "#{member.location}: Duplicated method definition: #{qualified_method_name}"
end
+ def location
+ member.location
+ end
+
def qualified_method_name
case type
when Types::ClassSingleton
"#{type.name}.#{method_name}"
else
@@ -281,10 +306,12 @@
type.name
end
end
class UnknownMethodAliasError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :original_name
attr_reader :aliased_name
attr_reader :location
@@ -308,10 +335,12 @@
super "#{Location.to_string entry.primary.decl.location}: Superclass mismatch: #{name}"
end
end
class InvalidOverloadMethodError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :method_name
attr_reader :kind
attr_reader :members
@@ -328,10 +357,14 @@
"."
end
super "#{Location.to_string members[0].location}: Invalid method overloading: #{type_name}#{delimiter}#{method_name}"
end
+
+ def location
+ members[0].location
+ end
end
class GenericParameterMismatchError < LoadingError
attr_reader :name
attr_reader :decl
@@ -355,10 +388,12 @@
super "#{Location.to_string last_decl.location}: Duplicated declaration: #{name}"
end
end
class InvalidVarianceAnnotationError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :param
attr_reader :location
def initialize(type_name:, param:, location:)
@@ -369,10 +404,12 @@
super "#{Location.to_string location}: Type parameter variance error: #{param.name} is #{param.variance} but used as incompatible variance"
end
end
class RecursiveAliasDefinitionError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type
attr_reader :defs
def initialize(type:, defs:)
@type = type
@@ -387,10 +424,12 @@
original.location
end
end
class MixinClassError < DefinitionError
+ include DetailedMessageable
+
attr_reader :type_name
attr_reader :member
def initialize(type_name:, member:)
@type_name = type_name
@@ -424,10 +463,12 @@
end
end
end
class RecursiveTypeAliasError < BaseError
+ include DetailedMessageable
+
attr_reader :alias_names
attr_reader :location
def initialize(alias_names:, location:)
@alias_names = alias_names
@@ -440,10 +481,12 @@
@alias_names.map(&:name).join(', ')
end
end
class NonregularTypeAliasError < BaseError
+ include DetailedMessageable
+
attr_reader :diagnostic
attr_reader :location
def initialize(diagnostic:, location:)
@diagnostic = diagnostic
@@ -452,10 +495,12 @@
super "#{Location.to_string location}: Nonregular generic type alias is prohibited: #{diagnostic.type_name}, #{diagnostic.nonregular_type}"
end
end
class CyclicTypeParameterBound < BaseError
+ include DetailedMessageable
+
attr_reader :params, :type_name, :method_name, :location
def initialize(type_name:, method_name:, params:, location:)
@type_name = type_name
@method_name = method_name
@@ -465,10 +510,12 @@
super "#{Location.to_string(location)}: Cyclic type parameter bound is prohibited"
end
end
class InconsistentClassModuleAliasError < BaseError
+ include DetailedMessageable
+
attr_reader :alias_entry
def initialize(entry)
@alias_entry = entry
@@ -480,17 +527,27 @@
["class", "module"]
end
super "#{Location.to_string(entry.decl.location&.[](:old_name))}: A #{expected_kind} `#{entry.decl.new_name}` cannot be an alias of a #{actual_kind} `#{entry.decl.old_name}`"
end
+
+ def location
+ @alias_entry.decl.location
+ end
end
class CyclicClassAliasDefinitionError < BaseError
+ include DetailedMessageable
+
attr_reader :alias_entry
def initialize(entry)
@alias_entry = entry
super "#{Location.to_string(entry.decl.location&.[](:old_name))}: A #{alias_entry.decl.new_name} is a cyclic definition"
+ end
+
+ def location
+ @alias_entry.decl.location
end
end
end