Class: RuboCop::Cop::Style::DirectiveScope
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::AutoCorrector,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::RangeHelp,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| Defined in: | lib/rubocop/cop/style/directive_scope.rb |
Overview
Checks for directive scopes that can be expressed with the tighter
next-statement forms: a disable/enable pair, an
enable/disable pair, or a push/pop with signed arguments
wrapping exactly one statement. A statement-scoped directive cannot
drift as the surrounding code changes and needs no closing boundary.
Constant Summary
-
MSG_ENABLE_PAIR =
# File 'lib/rubocop/cop/style/directive_scope.rb', line 70'Use `enable-next` instead of an `enable`/`disable` pair ' \ 'around a single statement.'
-
MSG_PAIR =
# File 'lib/rubocop/cop/style/directive_scope.rb', line 68'Use `%<mode>s-next` instead of a `%<mode>s`/`enable` pair ' \ 'around a single statement.'
-
MSG_PUSH_POP =
# File 'lib/rubocop/cop/style/directive_scope.rb', line 72'Use `%<replacement>s` instead of `push`/`pop` around a single statement.'
::RuboCop::Cop::Base - Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp - Included
Class Attribute Summary
::RuboCop::Cop::AutoCorrector - Extended
::RuboCop::Cop::Base - Inherited
| .gem_requirements, .lint?, | |
| .support_autocorrect? | Returns if class supports autocorrect. |
| .support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to #on_new_investigation and |
Class Method Summary
::RuboCop::Cop::Base - Inherited
| .autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
| .badge | Naming. |
| .callbacks_needed, .cop_name, .department, | |
| .documentation_url | Returns a url to view this cops documentation online. |
| .exclude_from_registry | Call for abstract Cop classes. |
| .inherited, | |
| .joining_forces | Override and return the Force class(es) you need to join. |
| .match? | Returns true if the cop name or the cop namespace matches any of the given names. |
| .new, | |
| .requires_gem | Register a version requirement for the given gem name. |
| .restrict_on_send | Reserved for Commissioner. |
::RuboCop::ExcludeLimit - Extended
| exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
| transform | |
Instance Attribute Summary
::RuboCop::Cop::Base - Inherited
::RuboCop::Cop::AutocorrectLogic - Included
| #autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?, | |
| #skipped_unsafe_correction_with_disable_uncorrectable? | Whether a correction exists but is skipped because it is unsafe in a safe autocorrect run, while |
Instance Method Summary
- #on_new_investigation
-
#balancing_pop_line(push_directive)
private
The line of the
popbalancing thispush, taking nesting into account, ornilwhen the push is never popped. -
#check_enable_pair(directive)
private
An
enable/disablepair around one statement inside a disabled region re-enables the cops for just that statement -enable-nextsays the same without the closing boundary. - #check_pair(directive) private
- #check_push_pop(directive) private
- #closed_at?(ranges, line) ⇒ Boolean private
-
#closed_open_disables?(directive, closing) ⇒ Boolean
private
Every cop of the pair must have a range the
enableclosed and a range the trailingdisablereopened - otherwise theenablewas not inside a disabled region and the conversion would change what is covered. - #comment_config private
- #convert_pair(corrector, directive, enable) private
- #each_directive_after(reference) private
- #enable_pair_closing(directive) private
- #plain_disable?(directive) ⇒ Boolean private
- #plain_enable?(directive) ⇒ Boolean private
- #push_replacement(directive) private
- #qualified_name(cop_name) private
- #ranges_opened_by(directive) private
- #re_disable_below(directive, line) private
- #remove_line(corrector, comment) private
- #reopened_by?(ranges, closing) ⇒ Boolean private
- #replacement_mode(directive) private
- #signed_push?(directive) ⇒ Boolean private
-
#single_closing_line(directive)
private
The single line on which every disabled range opened by this directive ends, or
nilwhen the ranges disagree or never close. -
#single_statement_closing_directive(directive)
private
The directive closing this one’s scope, when that scope wraps exactly one statement -
nilotherwise. -
#wraps_single_statement?(directive, closing_line) ⇒ Boolean
private
The region wraps a single statement only when the directive sits immediately above it and the closing directive immediately below - any other line inside the region (a comment, a blank line) may carry offenses of the suppressed cops that the tighter scope would no longer cover.
::RuboCop::Cop::RangeHelp - Included
| #add_range, | |
| #arguments_range | A range containing the first to the last argument of a method call or method definition. |
| #column_offset_between, | |
| #contents_range | A range containing only the contents of a literal with delimiters (e.g. |
| #directions, | |
| #effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
| #final_pos | rubocop:disable-next Metrics/ParameterLists. |
| #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range | |
::RuboCop::Cop::Base - Inherited
| #add_global_offense | Adds an offense that has no particular location. |
| #add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
| #begin_investigation | Called before any investigation. |
| #callbacks_needed | — grouped under the Commissioner heading above. |
| #cop_config | Configuration Helpers. |
| #cop_name, #excluded_file?, | |
| #external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
| #inspect, | |
| #message | Gets called if no message is specified when calling |
| #name | Alias for Base#cop_name. |
| #offenses, | |
| #on_investigation_end | Called after all on_… |
| #on_new_investigation | Called before all on_… |
| #on_other_file | Called instead of all on_… |
| #parse | There should be very limited reasons for a Cop to do it’s own parsing. |
| #parser_engine, | |
| #ready | Called between investigations. |
| #relevant_file?, | |
| #target_gem_version | Returns a gems locked versions (i.e. |
| #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
| #callback_argument | Reserved for Cop::Cop. |
| #complete_investigation | Called to complete an investigation. |
| #correct, #covering_disabled_range, #current_corrector, | |
| #current_offense_locations | Reserved for Commissioner: |
| #current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, | |
| #enabled_lines? | A multi-line offense is suppressed by a directive on any line of its range, not only its first line, matching the intuition that the directive is attached to the offending code. |
| #file_name_matches_any?, #find_message, #find_severity, #matches_absolute_include_pattern?, #range_for_original, #range_from_node_or_range, | |
| #reset_investigation | Actually private methods. |
| #suppression_reason | The |
| #use_corrector | |
::RuboCop::Cop::AutocorrectLogic - Included
::RuboCop::Cop::IgnoredNode - Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#balancing_pop_line(push_directive) (private)
The line of the pop balancing this push, taking nesting into
account, or nil when the push is never popped.
# File 'lib/rubocop/cop/style/directive_scope.rb', line 141
def balancing_pop_line(push_directive) depth = 0 each_directive_after(push_directive) do |directive| if directive.push? depth += 1 elsif directive.pop? return directive.line_number if depth.zero? depth -= 1 end end nil end
#check_enable_pair(directive) (private)
An enable/disable pair around one statement inside a disabled
region re-enables the cops for just that statement - enable-next
says the same without the closing boundary. The pair only counts
when the enable really closed open disables (otherwise the
trailing disable opens a new region and the conversion would
change what is covered).
# File 'lib/rubocop/cop/style/directive_scope.rb', line 194
def check_enable_pair(directive) closing = enable_pair_closing(directive) return unless closing add_offense(directive.comment, message: MSG_ENABLE_PAIR) do |corrector| corrector.replace(directive.comment, directive.comment.text.sub(/\benable\b/, 'enable-next')) remove_line(corrector, closing.comment) end end
#check_pair(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 107
def check_pair(directive) enable = single_statement_closing_directive(directive) return unless enable&.enabled? return unless enable.raw_cop_names.sort == directive.raw_cop_names.sort = format(MSG_PAIR, mode: directive.mode) add_offense(directive.comment, message: ) do |corrector| convert_pair(corrector, directive, enable) end end
#check_push_pop(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 125
def check_push_pop(directive) pop_line = balancing_pop_line(directive) return unless pop_line && comment_config.comment_only_line?(pop_line) return unless wraps_single_statement?(directive, pop_line) pop_comment = processed_source.comment_at_line(pop_line) replacement = push_replacement(directive) = format(MSG_PUSH_POP, replacement: replacement_mode(directive)) add_offense(directive.comment, message: ) do |corrector| corrector.replace(directive.comment, replacement) remove_line(corrector, pop_comment) end end
#closed_at?(ranges, line) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 237
def closed_at?(ranges, line) ranges.any? { |range| range.end == line } end
#closed_open_disables?(directive, closing) ⇒ Boolean (private)
Every cop of the pair must have a range the enable closed and a
range the trailing disable reopened - otherwise the enable was
not inside a disabled region and the conversion would change what
is covered.
# File 'lib/rubocop/cop/style/directive_scope.rb', line 230
def closed_open_disables?(directive, closing) directive.cop_names.all? do |cop| ranges = comment_config.cop_disabled_line_ranges[qualified_name(cop)] ranges && closed_at?(ranges, directive.line_number) && reopened_by?(ranges, closing) end end
#comment_config (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 91
def comment_config processed_source.comment_config end
#convert_pair(corrector, directive, enable) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 118
def convert_pair(corrector, directive, enable) replacement = directive.comment.text.sub(/\b#{directive.mode}\b/, "#{directive.mode}-next") corrector.replace(directive.comment, replacement) remove_line(corrector, enable.comment) end
#each_directive_after(reference) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 155
def each_directive_after(reference) processed_source.comments.each do |comment| directive = DirectiveComment.new(comment) next unless directive.cop_names || directive.push? || directive.pop? yield directive if directive.line_number > reference.line_number end end
#enable_pair_closing(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 205
def enable_pair_closing(directive) scope = comment_config.statement_scope_after(directive.line_number) return nil unless scope && scope.begin == directive.line_number + 1 closing = re_disable_below(directive, scope.end + 1) closing if closing && closed_open_disables?(directive, closing) end
#on_new_investigation
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 74
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 signed_push?(directive) check_push_pop(directive) elsif plain_enable?(directive) check_enable_pair(directive) end end end
#plain_disable?(directive) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 95
def plain_disable?(directive) directive.disabled? && !directive.disable_next? end
#plain_enable?(directive) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 99
def plain_enable?(directive) directive.enabled? && !directive.enable_next? && !directive.all_cops? end
#push_replacement(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 175
def push_replacement(directive) text = case replacement_mode(directive) when 'disable-next' "# rubocop:disable-next #{directive.signed_args['-'].join(', ')}" when 'enable-next' "# rubocop:enable-next #{directive.signed_args['+'].join(', ')}" else "# rubocop:next #{directive.cops}" end reason = directive.reason reason ? "#{text} -- #{reason}" : text end
#qualified_name(cop_name) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 247
def qualified_name(cop_name) Registry.qualified_cop_name(cop_name.strip, processed_source.file_path, correct_namespace: false) end
#ranges_opened_by(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 279
def ranges_opened_by(directive) comment_config.cop_disabled_line_ranges.each_value.flat_map do |ranges| ranges.select do |range| range.respond_to?(:directive) && range.directive.comment.equal?(directive.comment) end end end
#re_disable_below(directive, line) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 213
def re_disable_below(directive, line) return nil unless comment_config.comment_only_line?(line) comment = processed_source.comment_at_line(line) return nil unless comment closing = DirectiveComment.new(comment) return nil unless closing.disabled? && !closing.disable_next? return nil unless closing.raw_cop_names.sort == directive.raw_cop_names.sort closing end
#remove_line(corrector, comment) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 298
def remove_line(corrector, comment) corrector.remove(range_by_whole_lines(comment.source_range, include_final_newline: true)) end
#reopened_by?(ranges, closing) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 241
def reopened_by?(ranges, closing) ranges.any? do |range| range.respond_to?(:directive) && range.directive.comment.equal?(closing.comment) end end
#replacement_mode(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 164
def replacement_mode(directive) ops = directive.signed_args.keys.sort if ops == ['-'] 'disable-next' elsif ops == ['+'] 'enable-next' else 'next' end end
#signed_push?(directive) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 103
def signed_push?(directive) directive.push? && !directive.signed_args.empty? end
#single_closing_line(directive) (private)
The single line on which every disabled range opened by this
directive ends, or nil when the ranges disagree or never close.
For a disable that is the enable line; for a push it is the
line before the pop.
# File 'lib/rubocop/cop/style/directive_scope.rb', line 269
def single_closing_line(directive) ranges = ranges_opened_by(directive) return nil if ranges.empty? ends = ranges.map(&:end).uniq return nil unless ends.size == 1 && ends.first.to_f.finite? ends.first end
#single_statement_closing_directive(directive) (private)
The directive closing this one’s scope, when that scope wraps
exactly one statement - nil otherwise.
# File 'lib/rubocop/cop/style/directive_scope.rb', line 254
def single_statement_closing_directive(directive) closing_line = single_closing_line(directive) return nil unless closing_line closing_comment = processed_source.comment_at_line(closing_line) return nil unless closing_comment return nil unless wraps_single_statement?(directive, closing_line) DirectiveComment.new(closing_comment) end
#wraps_single_statement?(directive, closing_line) ⇒ Boolean (private)
The region wraps a single statement only when the directive sits immediately above it and the closing directive immediately below - any other line inside the region (a comment, a blank line) may carry offenses of the suppressed cops that the tighter scope would no longer cover.
# File 'lib/rubocop/cop/style/directive_scope.rb', line 292
def wraps_single_statement?(directive, closing_line) scope = comment_config.statement_scope_after(directive.line_number) !scope.nil? && scope.begin == directive.line_number + 1 && scope.end + 1 == closing_line end