Class: RuboCop::Cop::Style::DisableCopsWithinSourceCodeDirective

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

Overview

Detects comments to enable/disable RuboCop. This is useful if you want to make sure that every RuboCop error gets fixed and not quickly disabled with a comment.

Specific cops can be allowed with the AllowedCops configuration. Note that if this configuration is set, rubocop:disable all is still disallowed.

AllowedCops and DisallowedCops accept cop names and department names alike. Listing a cop does not cover its whole department: a directive disabling Metrics is broader than one disabling Metrics/AbcSize.

Alternatively, specific cops can be disallowed with the DisallowedCops configuration. When DisallowedCops is set, only directives for the listed cops (and all) will be flagged. This is useful when you want to protect a small set of critical cops from being disabled rather than allowlisting all other cops. AllowedCops and DisallowedCops should not both be set at the same time; if DisallowedCops is set, it takes precedence.

AllowedDirectives names directive forms - disable, todo, disable-next, todo-next, push, next - and exempts them from the cop entirely. rubocop:todo is the usual candidate, since --disable-uncorrectable generates those and they record debt rather than a decision someone should be asked to justify.

With AllowWithReason set to true, a disable directive carrying a -- trailing justification comment is allowed, so a team can require every disable to be documented instead of banning them outright. Enable directives are not checked in this mode, since they end a suppression rather than start one.

This cop cannot be disabled via directive comments when it is explicitly enabled with Enabled: true. This prevents users from bypassing the cop with # rubocop:disable Style/DisableCopsWithinSourceCodeDirective.

Examples:

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

# good
def foo
end

AllowedCops: [Metrics/AbcSize]

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

DisallowedCops: [Lint/Void]

# bad
# rubocop:disable Lint/Void
foo
# rubocop:enable Lint/Void

# good
# rubocop:disable Metrics/AbcSize
foo
# rubocop:enable Metrics/AbcSize

AllowedDirectives: [todo]

# good - the cop does not look at `todo` directives at all
x = 0 # rubocop:todo Layout/SpaceAroundOperators

# bad
x = 0 # rubocop:disable Layout/SpaceAroundOperators

AllowedCops: [Metrics]

# good - every cop the directive disables is in an exempt department
# rubocop:disable Metrics/AbcSize
def foo
end
# rubocop:enable Metrics/AbcSize

AllowWithReason: true

# bad
x = 0 # rubocop:disable Layout/SpaceAroundOperators

# good
x = 0 # rubocop:disable Layout/SpaceAroundOperators -- would misalign the table

Constant Summary collapse

MSG =

rubocop:enable Lint/RedundantCopDisableDirective

'RuboCop disable/enable directives are not permitted.'
MSG_FOR_COPS =
'RuboCop disable/enable directives for %<cops>s are not permitted.'
MSG_MISSING_REASON =
'RuboCop disable directives without a `--` justification ' \
'comment are not permitted.'

Constants inherited from Base

Base::DEPARTMENT_SEVERITIES, 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, #preview?, #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



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb', line 101

def on_new_investigation
  processed_source.comments.each do |comment|
    directive = DirectiveComment.new(comment)
    next if ignored?(directive)

    directive_cops = directive_cops(directive)
    disallowed_cops = compute_disallowed_cops(directive_cops)

    next unless disallowed_cops.any?

    register_offense(comment, directive_cops, disallowed_cops)
  end
end