Class: RuboCop::CommentConfig
- Inherits:
-
Object
- Object
- RuboCop::CommentConfig
- Extended by:
- SimpleForwardable
- Includes:
- DisableNext, PushPop
- Defined in:
- lib/rubocop/comment_config.rb,
lib/rubocop/comment_config/push_pop.rb,
lib/rubocop/comment_config/disable_next.rb,
lib/rubocop/comment_config/directive_range.rb
Overview
This class parses the special rubocop:disable comments in a source
and provides a way to check if each cop is enabled at arbitrary line.
Defined Under Namespace
Modules: DisableNext, PushPop Classes: ConfigDisabledCopDirectiveComment, CopAnalysis, DirectiveRange
Constant Summary collapse
- CONFIG_DISABLED_LINE_RANGE_MIN =
-Float::INFINITY
Instance Attribute Summary collapse
-
#processed_source ⇒ Object
readonly
Returns the value of attribute processed_source.
Instance Method Summary collapse
- #comment_only_line?(line_number) ⇒ Boolean
- #cop_disabled_line_ranges ⇒ Object
- #cop_enabled_at_line?(cop, line_number) ⇒ Boolean
-
#cop_enabled_at_lines?(cop, first_line, last_line) ⇒ Boolean
Whether the cop is enabled for all of the given line span.
- #cop_opted_in?(cop) ⇒ Boolean
- #extra_enabled_comments ⇒ Object
-
#initialize(processed_source) ⇒ CommentConfig
constructor
A new instance of CommentConfig.
-
#opt_in_cops ⇒ Set<String>
private
The names of the cops that are opted in by an
enablecomment directive or a+argument of apush/nextdirective, used to mobilize cops disabled in the config on demand.
Methods included from DisableNext
#detached_next_directives, #statement_scope_after
Constructor Details
#initialize(processed_source) ⇒ CommentConfig
Returns a new instance of CommentConfig.
34 35 36 37 38 39 |
# File 'lib/rubocop/comment_config.rb', line 34 def initialize(processed_source) @processed_source = processed_source @no_directives = !processed_source.raw_source.include?('rubocop') @stack = [] @detached_next_directives = [] end |
Instance Attribute Details
#processed_source ⇒ Object (readonly)
Returns the value of attribute processed_source.
30 31 32 |
# File 'lib/rubocop/comment_config.rb', line 30 def processed_source @processed_source end |
Instance Method Details
#comment_only_line?(line_number) ⇒ Boolean
75 76 77 |
# File 'lib/rubocop/comment_config.rb', line 75 def comment_only_line?(line_number) non_comment_token_line_numbers.none?(line_number) end |
#cop_disabled_line_ranges ⇒ Object
63 64 65 |
# File 'lib/rubocop/comment_config.rb', line 63 def cop_disabled_line_ranges @cop_disabled_line_ranges ||= analyze end |
#cop_enabled_at_line?(cop, line_number) ⇒ Boolean
41 42 43 |
# File 'lib/rubocop/comment_config.rb', line 41 def cop_enabled_at_line?(cop, line_number) cop_enabled_at_lines?(cop, line_number, line_number) end |
#cop_enabled_at_lines?(cop, first_line, last_line) ⇒ Boolean
Whether the cop is enabled for all of the given line span. The cop counts as disabled for the span when any of its disabled ranges overlaps it, so that a directive on any line of a multi-line offense suppresses the offense.
49 50 51 52 53 54 55 56 57 |
# File 'lib/rubocop/comment_config.rb', line 49 def cop_enabled_at_lines?(cop, first_line, last_line) cop = cop.cop_name if cop.respond_to?(:cop_name) disabled_line_ranges = cop_disabled_line_ranges[cop] return true unless disabled_line_ranges disabled_line_ranges.none? do |range| range.end >= first_line && range.begin <= last_line end end |
#cop_opted_in?(cop) ⇒ Boolean
59 60 61 |
# File 'lib/rubocop/comment_config.rb', line 59 def cop_opted_in?(cop) opt_in_cops.include?(cop.cop_name) end |
#extra_enabled_comments ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/rubocop/comment_config.rb', line 67 def extra_enabled_comments disable_count = Hash.new(0) registry.disabled_names(config).each do |cop_name| disable_count[cop_name] += 1 end extra_enabled_comments_with_names(extras: Hash.new { |h, k| h[k] = [] }, names: disable_count) end |
#opt_in_cops ⇒ Set<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The names of the cops that are opted in by an enable comment directive
or a + argument of a push/next directive, used to mobilize cops
disabled in the config on demand.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rubocop/comment_config.rb', line 85 def opt_in_cops @opt_in_cops ||= begin cops = Set.new each_directive do |directive| if directive.enabled? cops.merge(directive.raw_cop_names) unless directive.all_cops? elsif directive.push? || directive.next? cops.merge(directive.signed_args.fetch('+', [])) end end cops end end |