Skip to content

Commit

Permalink
Add information for VariableDuplicationError
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Mar 8, 2025
1 parent 1209f49 commit faea4c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/rbs/definition_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ def validate_variable(var)
case l.source
when AST::Members::InstanceVariable
if r.source.instance_of?(AST::Members::InstanceVariable) && l.declared_in == r.declared_in
raise InstanceVariableDuplicationError.new(member: l.source)
raise InstanceVariableDuplicationError.new(type_name: l.declared_in, variable_name: l.source.name, location: l.source.location)
end
when AST::Members::ClassInstanceVariable
if r.source.instance_of?(AST::Members::ClassInstanceVariable) && l.declared_in == r.declared_in
raise ClassInstanceVariableDuplicationError.new(member: l.source)
raise ClassInstanceVariableDuplicationError.new(type_name: l.declared_in, variable_name: l.source.name, location: l.source.location)
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions lib/rbs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,24 +325,25 @@ def type_name
class VariableDuplicationError < DefinitionError
include DetailedMessageable

attr_reader :member

def initialize(member:)
@member = member
attr_reader :type_name
attr_reader :variable_name
attr_reader :location

super "#{Location.to_string location}: Duplicated variable name #{member.name}"
end
def initialize(type_name:, variable_name:, location:)
@type_name = type_name
@variable_name = variable_name
@location = location

def location
loc = @member.location or raise
loc[:name]
super "#{Location.to_string location}: Duplicated #{kind} variable name `#{variable_name}` in `#{type_name}`"
end
end

class InstanceVariableDuplicationError < VariableDuplicationError
def kind = 'instance'
end

class ClassInstanceVariableDuplicationError < VariableDuplicationError
def kind = 'class instance'
end

class UnknownMethodAliasError < DefinitionError
Expand Down
11 changes: 7 additions & 4 deletions sig/errors.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,20 @@ module RBS
class VariableDuplicationError < DefinitionError
include DetailedMessageable

attr_reader member: AST::Members::Var

def initialize: (member: AST::Members::Var) -> void
attr_reader type_name: TypeName
attr_reader variable_name: Symbol
attr_reader location: Location[untyped, untyped]

def location: () -> Location[bot, bot]
def initialize: (type_name: TypeName, variable_name: Symbol, location: Location[untyped, untyped]) -> void
def kind: () -> String
end

class InstanceVariableDuplicationError < VariableDuplicationError
def kind: () -> String
end

class ClassInstanceVariableDuplicationError < VariableDuplicationError
def kind: () -> String
end

# The `alias` member declares an alias from unknown method
Expand Down

0 comments on commit faea4c3

Please sign in to comment.