class RuboCop::Cop::Chef::Deprecations::SearchUsesPositionalParameters
correct
query(:node, ‘:’)
search(:node, '*:*', start: 0, rows: 1000, filter_result: { :ip_address => ["ipaddress"] }) search(:node, '*:*', start: 0, rows: 1000) search(:node, '*:*', start: 0)
Constants
- MSG
- NAMED_PARAM_LOOKUP_TABLE
- RESTRICT_ON_SEND
- VALID_TYPES
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb, line 50 def on_send(node) search_method?(node) do add_offense(node, severity: :warning) do |corrector| corrector.replace(node, corrected_string(node)) end if positional_arguments?(node) end end
Private Instance Methods
corrected_string(node)
click to toggle source
Return the corrected search string
@param [RuboCop::AST::Node] node
@return [String]
# File lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb, line 85 def corrected_string(node) args = node.arguments.dup # If the 2nd argument is a String and not an Integer as a String # then it's the old sort field and we need to delete it. Same thing # goes for nil values here. args.delete_at(2) if (args[2].str_type? && !integer_like_val?(args[2])) || args[2].nil_type? "search(#{args.collect.with_index { |arg, i| hashify_argument(arg, i) }.join(', ')})" end
hashify_argument(arg, position)
click to toggle source
lookup the position in NAMED_PARAM_LOOKUP_TABLE
to create a new hashified version of the query. Also convert Integer like Strings into Integers
@param [RuboCop::AST::Node] arg @param [Integer] position
@return [String]
# File lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb, line 105 def hashify_argument(arg, position) hash_key = NAMED_PARAM_LOOKUP_TABLE[position] if hash_key # convert Integers stored as Strings into plain Integers if integer_like_val?(arg) "#{hash_key}: #{Integer(arg.value)}" else "#{hash_key}: #{arg.source}" end else arg.source end end
integer_like_val?(val)
click to toggle source
Does this value look like an Integer (it’s an integer or a string)
@param [RuboCop::AST::Node] val
@return [Boolean]
# File lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb, line 126 def integer_like_val?(val) Integer(val.value) true rescue false end
positional_arguments?(node)
click to toggle source
Are the arguments in the passed node object positional
@param [RuboCop::AST::Node] node
@return [Boolean]
# File lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb, line 69 def positional_arguments?(node) return false if node.arguments.count < 3 node.arguments[2..-1].each do |arg| # hashes, blocks, or variable/methods are valid. Anything else is not return true unless VALID_TYPES.include?(arg.type) end false end