Class: Hone::Patterns::SliceWithLength
- Defined in:
- lib/hone/patterns/slice_with_length.rb
Overview
Pattern: str[n, str.length] -> str
Endless range syntax avoids the length calculation.
From sqids-ruby commit 9413b68
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#add_finding, inherited, #initialize, scan_file
Constructor Details
This class inherits a constructor from Hone::Patterns::Base
Instance Method Details
#visit_call_node(node) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hone/patterns/slice_with_length.rb', line 14 def visit_call_node(node) super # Look for: receiver[offset, receiver.length] pattern return unless node.name == :[] && node.arguments args = node.arguments.arguments return unless args&.length == 2 first_arg = args[0] second_arg = args[1] # Check if second arg is receiver.length or receiver.size return unless second_arg.is_a?(Prism::CallNode) return unless %i[length size].include?(second_arg.name) return unless nodes_match?(node.receiver, second_arg.receiver) add_finding( node, message: "Use endless range `[#{first_arg.location.slice}..]` instead of `[#{first_arg.location.slice}, #{second_arg.location.slice}]`", speedup: "Minor, avoids length calculation" ) end |