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
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.
Constant Summary
-
MSG_PAIR =
# File 'lib/rubocop/cop/style/directive_scope.rb', line 53'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 55'Use `disable-next` 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
- #check_pair(directive) private
- #check_push_pop(directive) private
- #comment_config private
- #convert_pair(corrector, directive, enable) private
- #disable_next_replacement(directive) private
- #disable_only_push?(directive) ⇒ Boolean private
- #plain_disable?(directive) ⇒ Boolean private
- #ranges_opened_by(directive) private
- #remove_line(corrector, comment) 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, | |
| #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
#check_pair(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 84
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 102
def check_push_pop(directive) pop = single_statement_closing_directive(directive) return unless pop&.pop? add_offense(directive.comment, message: MSG_PUSH_POP) do |corrector| corrector.replace(directive.comment, disable_next_replacement(directive)) remove_line(corrector, pop.comment) end end
#comment_config (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 72
def comment_config processed_source.comment_config end
#convert_pair(corrector, directive, enable) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 95
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
#disable_next_replacement(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 158
def disable_next_replacement(directive) cops = directive.push_args['-'].join(', ') reason = directive.reason text = "# rubocop:disable-next #{cops}" reason ? "#{text} -- #{reason}" : text end
#disable_only_push?(directive) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 80
def disable_only_push?(directive) directive.push? && !directive.push_args.empty? && directive.push_args.keys == ['-'] end
#on_new_investigation
[ GitHub ]# 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
#plain_disable?(directive) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/directive_scope.rb', line 76
def plain_disable?(directive) directive.disabled? && !directive.disable_next? end
#ranges_opened_by(directive) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 139
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
#remove_line(corrector, comment) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/directive_scope.rb', line 165
def remove_line(corrector, comment) corrector.remove(range_by_whole_lines(comment.source_range, include_final_newline: true)) 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 129
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? directive.push? ? ends.first + 1 : 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 114
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 152
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