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

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

#exclude_limit

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

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity

 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
# File 'lib/rubocop/cop/style/empty_string_inside_interpolation.rb', line 49 def on_interpolation(node) node.each_child_node(:if) do |child_node| if style == :trailing_conditional if empty_if_outcome?(child_node) ternary_style_autocorrect(child_node, child_node.else_branch.source, 'unless') end if empty_else_outcome?(child_node) ternary_style_autocorrect(child_node, child_node.if_branch.source, 'if') end elsif style == :ternary next unless child_node.modifier_form? ternary_component = if child_node.unless? "'' : #{child_node.if_branch.source}" else "#{child_node.if_branch.source} : ''" end add_offense(node, message: MSG_TRAILING_CONDITIONAL) do |corrector| corrector.replace(node, "\#{#{child_node.condition.source} ? #{ternary_component}}") end end end end