Class: RuboCop::Cop::Style::DirectiveScope

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/style/directive_scope.rb

Overview

Checks for directive scopes that can be expressed with the tighter disable-next form: a disable/enable pair wrapping exactly one statement, or a push/pop that only disables cops around exactly one statement. A statement-scoped directive cannot drift as the surrounding code changes and needs no closing boundary.

Examples:

# bad
# rubocop:disable Metrics/AbcSize
def foo
end
# rubocop:enable Metrics/AbcSize

# good
# rubocop:disable-next Metrics/AbcSize
def foo
end

# bad
# rubocop:push -Metrics/AbcSize
def foo
end
# rubocop:pop

# good
# rubocop:disable-next Metrics/AbcSize
def foo
end

# good - the region spans more than one statement
# rubocop:disable Metrics/AbcSize
def foo
end

def bar
end
# rubocop:enable Metrics/AbcSize

Constant Summary collapse

MSG_PAIR =
'Use `%<mode>s-next` instead of a `%<mode>s`/`enable` pair ' \
'around a single statement.'
MSG_PUSH_POP =
'Use `disable-next` instead of `push`/`pop` around a single statement.'

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

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 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?, #skipped_unsafe_correction_with_disable_uncorrectable?

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_new_investigationObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/style/directive_scope.rb', line 57

def on_new_investigation
  processed_source.comments.each do |comment|
    directive = DirectiveComment.new(comment)
    next unless comment_config.comment_only_line?(directive.line_number)

    if plain_disable?(directive)
      check_pair(directive)
    elsif disable_only_push?(directive)
      check_push_pop(directive)
    end
  end
end