Skip to content

Commit

Permalink
refactor: Use Array#fetch instead of Array#[] to resolve type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Oct 18, 2024
1 parent 61a9792 commit c9327a1
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/steep/ast/ignore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def ignored_diagnostics
return :all
end

if raw_diagnostics.size == 1 && raw_diagnostics[0].source == "all"
if raw_diagnostics.size == 1 && raw_diagnostics.fetch(0).source == "all"
return :all
end

Expand Down
6 changes: 3 additions & 3 deletions lib/steep/diagnostic/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def self.from_rbs_error(error, factory:)
when RBS::DuplicatedDeclarationError
Diagnostic::Signature::DuplicatedDeclaration.new(
type_name: error.name,
location: error.decls[0].location
location: error.decls.fetch(0).location
)
when RBS::GenericParameterMismatchError
Diagnostic::Signature::GenericParameterMismatch.new(
Expand All @@ -494,7 +494,7 @@ def self.from_rbs_error(error, factory:)
Diagnostic::Signature::InvalidMethodOverload.new(
class_name: error.type_name,
method_name: error.method_name,
location: error.members[0].location
location: error.members.fetch(0).location
)
when RBS::DuplicatedMethodDefinitionError
Diagnostic::Signature::DuplicatedMethodDefinition.new(
Expand All @@ -518,7 +518,7 @@ def self.from_rbs_error(error, factory:)
Diagnostic::Signature::RecursiveAlias.new(
class_name: error.type.name,
names: error.defs.map(&:name),
location: error.defs[0].original&.location
location: error.defs.fetch(0).original&.location
)
when RBS::RecursiveAncestorError
Diagnostic::Signature::RecursiveAncestor.new(
Expand Down
2 changes: 1 addition & 1 deletion lib/steep/drivers/diagnostic_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def print_source_line(diagnostic, prefix: "")
start_pos = diagnostic[:range][:start]
end_pos = diagnostic[:range][:end]

line = buffer.lines[start_pos[:line]]
line = buffer.lines.fetch(start_pos[:line])

leading = line[0...start_pos[:character]] || ""
if start_pos[:line] == end_pos[:line]
Expand Down
4 changes: 2 additions & 2 deletions lib/steep/services/hover_provider/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def content_for(target:, path:, line:, column:)
result_node =
case parents[0]&.type
when :block, :numblock
if node == parents[0].children[0]
parents[0]
if node == parents.fetch(0).children[0]
parents.fetch(0)
else
node
end
Expand Down
2 changes: 1 addition & 1 deletion lib/steep/services/hover_provider/singleton_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def content_for(service:, path:, line:, column:)
when Project::Target
Ruby.new(service: service).content_for(target: target, path: path, line: line, column: column)
when Array
RBS.new(service: service).content_for(target: target[0], path: path, line: line, column: column)
RBS.new(service: service).content_for(target: target.fetch(0), path: path, line: line, column: column)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/steep/services/signature_help_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ def active_parameter_for(method_type, argument_nodes, last_argument_nodes, node)
# Cursor is not on the argument (maybe on comma after argument)
return 0 if last_argument_nodes.nil? # No arguments

case last_argument_nodes[-2].type
case last_argument_nodes.fetch(-2).type
when :splat
method_type.type.required_positionals.size + method_type.type.optional_positionals.size + 1 if method_type.type.rest_positionals
when :kwargs
case last_argument_nodes[-3].type
case last_argument_nodes.fetch(-3).type
when :pair
argname = last_argument_nodes[-3].children.first.children.first
argname = last_argument_nodes.fetch(-3).children.first.children.first
if method_type.type.required_keywords.key?(argname)
positionals + method_type.type.required_keywords.keys.index(argname).to_i + 1
elsif method_type.type.optional_keywords.key?(argname)
Expand All @@ -163,7 +163,7 @@ def active_parameter_for(method_type, argument_nodes, last_argument_nodes, node)
positionals + method_type.type.required_keywords.size + method_type.type.optional_keywords.size if method_type.type.rest_keywords
end
else
pos = (node.children[2...] || raise).index { |c| c.location == last_argument_nodes[-2].location }.to_i
pos = (node.children[2...] || raise).index { |c| c.location == last_argument_nodes.fetch(-2).location }.to_i
if method_type.type.rest_positionals
[pos + 1, positionals - 1].min
else
Expand All @@ -172,14 +172,14 @@ def active_parameter_for(method_type, argument_nodes, last_argument_nodes, node)
end
else
# Cursor is on the argument
case argument_nodes[-2].type
case argument_nodes.fetch(-2).type
when :splat
method_type.type.required_positionals.size + method_type.type.optional_positionals.size if method_type.type.rest_positionals
when :kwargs
if argument_nodes[-3]
case argument_nodes[-3].type
case argument_nodes.fetch(-3).type
when :pair
argname = argument_nodes[-3].children.first.children.first
argname = argument_nodes.fetch(-3).children.first.children.first
if method_type.type.required_keywords.key?(argname)
positionals + method_type.type.required_keywords.keys.index(argname).to_i
elsif method_type.type.optional_keywords.key?(argname)
Expand All @@ -192,7 +192,7 @@ def active_parameter_for(method_type, argument_nodes, last_argument_nodes, node)
end
end
else
pos = (node.children[2...] || raise).index { |c| c.location == argument_nodes[-2].location }.to_i
pos = (node.children[2...] || raise).index { |c| c.location == argument_nodes.fetch(-2).location }.to_i
[pos, positionals - 1].min
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/steep/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def find_nodes(line:, column:)
position = buffer.loc_to_pos([line, column])

if heredoc_nodes = find_heredoc_nodes(line, column, position)
Source.each_child_node(heredoc_nodes[0]) do |child|
Source.each_child_node(heredoc_nodes.fetch(0)) do |child|
if nodes = find_nodes_loc(child, position, heredoc_nodes)
return nodes
end
Expand Down
2 changes: 1 addition & 1 deletion lib/steep/subtyping/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def check_type0(relation)
types: relation.sub_type.types
)

check_type(Relation.new(sub_type: tuple_element_type, super_type: super_type.args[0]))
check_type(Relation.new(sub_type: tuple_element_type, super_type: super_type.args.fetch(0)))
end

when relation.sub_type.is_a?(AST::Types::Tuple)
Expand Down
20 changes: 10 additions & 10 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ def synthesize(node, hint: nil, condition: false)
if hint.one_arg?
if hint.type.params
# Assumes Symbol#to_proc implementation
param_type = hint.type.params.required[0]
param_type = hint.type.params.required.fetch(0)
case param_type
when AST::Types::Any
type = AST::Types::Any.instance
Expand All @@ -2423,7 +2423,7 @@ def synthesize(node, hint: nil, condition: false)
params: Interface::Function::Params.empty.with_first_param(
Interface::Function::Params::PositionalParams::Required.new(param_type)
),
return_type: return_types[0],
return_type: return_types.fetch(0),
location: nil
),
block: nil,
Expand Down Expand Up @@ -3485,7 +3485,7 @@ def try_special_method(node, receiver_type:, method_name:, method_overload:, arg
return_type = method_type.type.return_type
if AST::Builtin::Array.instance_type?(return_type)
# @type var return_type: AST::Types::Name::Instance
elem = return_type.args[0]
elem = return_type.args.fetch(0)
type = AST::Builtin::Array.instance_type(unwrap(elem))

_, constr = add_typing(node, type: type)
Expand All @@ -3508,8 +3508,8 @@ def try_special_method(node, receiver_type:, method_name:, method_overload:, arg
return_type = method_type.type.return_type
if AST::Builtin::Hash.instance_type?(return_type)
# @type var return_type: AST::Types::Name::Instance
key = return_type.args[0]
value = return_type.args[1]
key = return_type.args.fetch(0)
value = return_type.args.fetch(1)
type = AST::Builtin::Hash.instance_type(key, unwrap(value))

_, constr = add_typing(node, type: type)
Expand Down Expand Up @@ -3609,7 +3609,7 @@ def type_method_call(node, method_name:, receiver_type:, method:, arguments:, bl
end

if fails.one?
call, constr = fails[0]
call, constr = fails.fetch(0)

constr.typing.save!

Expand Down Expand Up @@ -3824,7 +3824,7 @@ def try_method_type(node, receiver_type:, method_name:, method_overload:, argume
args_ = []

type_args.each_with_index do |type, index|
param = method_type.type_params[index]
param = method_type.type_params.fetch(index)

if upper_bound = param.upper_bound
if result = no_subtyping?(sub_type: type.value, super_type: upper_bound)
Expand Down Expand Up @@ -4903,7 +4903,7 @@ def try_array_type(node, hint)
case
when AST::Builtin::Array.instance_type?(type)
type.is_a?(AST::Types::Name::Instance) or raise
element_types << type.args[0]
element_types << type.args.fetch(0)
when type.is_a?(AST::Types::Tuple)
element_types.push(*type.types)
else
Expand Down Expand Up @@ -5024,8 +5024,8 @@ def type_hash(hash_node, hint:)
constr.synthesize(elem_, hint: hint_hash).tap do |(type, _)|
if AST::Builtin::Hash.instance_type?(type)
# @type var type: AST::Types::Name::Instance
key_types << type.args[0]
value_types << type.args[1]
key_types << type.args.fetch(0)
value_types << type.args.fetch(1)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/steep/type_inference/block_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def zip(params_type, block, factory:)
when AST::Builtin::Array.instance_type?(type)
type.is_a?(AST::Types::Name::Instance) or raise

type_arg = type.args[0]
type_arg = type.args.fetch(0)
params.each do |param|
unless param == rest_param
zip << [param, AST::Types::Union.build(types: [type_arg, AST::Builtin.nil_type])]
Expand Down Expand Up @@ -359,7 +359,7 @@ def expandable?
true
when (leading_params.any? || trailing_params.any?) && rest_param
true
when params.size == 1 && params[0].node.type == :arg
when params.size == 1 && params.fetch(0).node.type == :arg
true
else
false
Expand Down Expand Up @@ -409,7 +409,7 @@ def self.from_multiple(node, annotations)

def untyped_args?(params)
flat = params.flat_unnamed_params
flat.size == 1 && flat[0][1].is_a?(AST::Types::Any)
flat.size == 1 && flat.dig(0, 1)&.is_a?(AST::Types::Any)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/steep/type_inference/type_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def join(*envs)
refined_type = AST::Types::Union.build(types: pairs.map {|call, type| type || call.return_type })

# Any *pure_method_call* can be used because it's *pure*
(call, _ = envs[0].pure_method_calls[node]) or raise
(call, _ = envs.fetch(0).pure_method_calls[node]) or raise

hash[node] = [call, refined_type]
end
Expand Down

0 comments on commit c9327a1

Please sign in to comment.