Class: RuboCop::Cop::Style::RedundantLineContinuation
- Extended by:
- AutoCorrector
- Includes:
- MatchRange, ReparsedEquivalence
- Defined in:
- lib/rubocop/cop/style/redundant_line_continuation.rb
Overview
Checks for redundant line continuation.
A line continuation is redundant when removing the backslash does not change how the program parses: the source is reparsed without the backslash and the resulting AST is compared to the original. Only backslashes that are pure noise are reported; backslashes that are significant — inside strings, for string concatenation, before an operator or argument that would otherwise start a new statement, and so on — are left alone, as are backslashes in comments.
Constant Summary collapse
- MSG =
'Redundant line continuation.'- LINE_CONTINUATION =
'\\'- LINE_CONTINUATION_PATTERN =
/(\\\n)/.freeze
- STRING_TOKEN_TYPES =
%i[tSTRING tSTRING_CONTENT].freeze
- STRING_LITERAL_ENDING_TOKEN_TYPES =
%i[tSTRING tSTRING_END].freeze
- STRING_LITERAL_BEGINNING_TOKEN_TYPES =
%i[tSTRING tSTRING_BEG].freeze
Constants included from ReparsedEquivalence
ReparsedEquivalence::MAX_VERIFICATION_FRAGMENT_SIZE
Constants included from RangeHelp
RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source, #project_index
Instance Method Summary collapse
Methods included from AutoCorrector
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_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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#on_new_investigation ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubocop/cop/style/redundant_line_continuation.rb', line 82 def on_new_investigation return unless processed_source.ast # A backslash is redundant if the source parses to the same AST # without it; verification is the offense logic itself, so oversized # scopes are reparsed regardless of size. verified_by_reparse(line_continuation_candidates, oversized: :verify).each do |range| add_offense(range) do |corrector| corrector.remove_leading(range, 1) end end inspect_end_of_ruby_code_line_continuation end |