class RuboCop::Cop::Style::EmptyLiteral
would be a literal, like an empty array, hash or string.
This cop checks for the use of a method, the result of which
def autocorrect(node)
def autocorrect(node) name = case node when ARRAY_NODE '[]' when HASH_NODE # `some_method {}` is not same as `some_method Hash.new` # because the braces are interpreted as a block, so we avoid # the correction. Parentheses around the arguments would # solve the problem, but we let the user add those manually. return if first_arg_in_method_call_without_parentheses?(node) '{}' when STR_NODE if enforce_double_quotes? '""' else "''" end end ->(corrector) { corrector.replace(node.source_range, name) } end
def enforce_double_quotes?
def enforce_double_quotes? string_literals_config['EnforcedStyle'] == 'double_quotes' end
def first_arg_in_method_call_without_parentheses?(node)
def first_arg_in_method_call_without_parentheses?(node) return false unless node.parent && node.parent.send_type? _receiver, _method_name, *args = *node.parent node.object_id == args.first.object_id && !parentheses?(node.parent) end
def on_send(node)
def on_send(node) case node when ARRAY_NODE add_offense(node, :expression, ARR_MSG) when HASH_NODE # If Hash.new takes a block, it can't be changed to {}. return if node.parent && node.parent.block_type? add_offense(node, :expression, HASH_MSG) when STR_NODE add_offense(node, :expression, STR_MSG) end end
def string_literals_config
def string_literals_config config.for_cop('Style/StringLiterals') end