Class: RuboCop::Cop::Style::EmptyStringInsideInterpolation

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, Interpolation
Defined in:
lib/rubocop/cop/style/empty_string_inside_interpolation.rb

Overview

Checks for empty strings being assigned inside string interpolation.

Empty strings are a meaningless outcome inside of string interpolation, so we remove them. Alternatively, when configured to do so, we prioritise using empty strings.

While this cop would also apply to variables that are only going to be used as strings, RuboCop can't detect that, so we only check inside of string interpolation.

Examples:

EnforcedStyle: trailing_conditional (default)

# bad
"#{condition ? 'foo' : ''}"

# good
"#{'foo' if condition}"

# bad
"#{condition ? '' : 'foo'}"

# good
"#{'foo' unless condition}"

EnforcedStyle: ternary

# bad
"#{'foo' if condition}"

# good
"#{condition ? 'foo' : ''}"

# bad
"#{'foo' unless condition}"

# good
"#{condition ? '' : 'foo'}"

Constant Summary collapse

MSG_TRAILING_CONDITIONAL =
'Do not use trailing conditionals in string interpolation.'
MSG_TERNARY =
'Do not return empty strings in string interpolation.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from Interpolation

#on_dstr, #on_node_with_interpolations

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_interpolation(node) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/style/empty_string_inside_interpolation.rb', line 48

def on_interpolation(node)
  node.each_child_node(:if) do |child_node|
    if style == :trailing_conditional
      trailing_conditional_correction(child_node)
    elsif style == :ternary
      ternary_correction(node, child_node)
    end
  end
end