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.

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.

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

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.'

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?

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb', line 62

def on_new_investigation
  processed_source.comments.each do |comment|
    directive_cops = directive_cops(comment)
    disallowed_cops = compute_disallowed_cops(directive_cops)

    next unless disallowed_cops.any?

    register_offense(comment, directive_cops, disallowed_cops)
  end
end