class RuboCop::Cop::Style::EmptyLiteral
This cop checks for the use of a method, the result of which would be a literal, like an empty array, hash, or string.
@example
# bad a = Array.new h = Hash.new s = String.new # good a = [] h = {} s = ''
Constants
- ARR_MSG
- HASH_MSG
- STR_MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 49 def autocorrect(node) lambda do |corrector| corrector.replace(replacement_range(node), correction(node)) end end
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 36 def on_send(node) add_offense(node, message: ARR_MSG) if offense_array_node?(node) add_offense(node, message: HASH_MSG) if offense_hash_node?(node) str_node(node) do return if frozen_string_literals_enabled? add_offense(node, message: format(STR_MSG, prefer: preferred_string_literal)) end end
Private Instance Methods
correction(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 103 def correction(node) if offense_array_node?(node) '[]' elsif str_node(node) preferred_string_literal elsif offense_hash_node?(node) if first_argument_unparenthesized?(node) # `some_method {}` is not same as `some_method Hash.new` # because the braces are interpreted as a block. We will have # to rewrite the arguments to wrap them in parenthesis. args = node.parent.arguments "(#{args[1..-1].map(&:source).unshift('{}').join(', ')})" else '{}' end end end
enforce_double_quotes?()
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 61 def enforce_double_quotes? string_literals_config['EnforcedStyle'] == 'double_quotes' end
first_argument_unparenthesized?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 69 def first_argument_unparenthesized?(node) parent = node.parent unless parent && %i[send super zsuper].include?(parent.type) return false end node.object_id == parent.arguments.first.object_id && !parentheses?(node.parent) end
offense_array_node?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 94 def offense_array_node?(node) array_node(node) && !array_with_block(node.parent) end
offense_hash_node?(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 98 def offense_hash_node?(node) # If Hash.new takes a block, it can't be changed to {}. hash_node(node) && !hash_with_block(node.parent) end
preferred_string_literal()
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 57 def preferred_string_literal enforce_double_quotes? ? '""' : "''" end
replacement_range(node)
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 79 def replacement_range(node) if hash_node(node) && first_argument_unparenthesized?(node) # `some_method {}` is not same as `some_method Hash.new` # because the braces are interpreted as a block. We will have # to rewrite the arguments to wrap them in parenthesis. args = node.parent.arguments range_between(args[0].loc.expression.begin_pos - 1, args[-1].loc.expression.end_pos) else node.source_range end end
string_literals_config()
click to toggle source
# File lib/rubocop/cop/style/empty_literal.rb, line 65 def string_literals_config config.for_cop('Style/StringLiterals') end