Skip to content

Commit

Permalink
Merge pull request soutaro#1287 from tk0miya/refactor/Array.fetch
Browse files Browse the repository at this point in the history
refactor: Use Array#fetch instead of Array#[] to resolve type errors
  • Loading branch information
soutaro authored Nov 25, 2024
2 parents d239222 + 7b5a677 commit 0e24e0e
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 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
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
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 0e24e0e

Please sign in to comment.