class RuboCop::CommentConfig
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.
Constants
- COMMENT_DIRECTIVE_REGEXP
- COPS_PATTERN
- COP_NAMES_PATTERN
- COP_NAME_PATTERN
- CopAnalysis
- UNNEEDED_DISABLE
Attributes
processed_source[R]
Public Class Methods
new(processed_source)
click to toggle source
# File lib/rubocop/comment_config.rb, line 21 def initialize(processed_source) @processed_source = processed_source end
Public Instance Methods
cop_disabled_line_ranges()
click to toggle source
# File lib/rubocop/comment_config.rb, line 33 def cop_disabled_line_ranges @cop_disabled_line_ranges ||= analyze end
cop_enabled_at_line?(cop, line_number)
click to toggle source
# File lib/rubocop/comment_config.rb, line 25 def cop_enabled_at_line?(cop, line_number) 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? { |range| range.include?(line_number) } end
extra_enabled_comments()
click to toggle source
# File lib/rubocop/comment_config.rb, line 37 def extra_enabled_comments extra_enabled_comments_with_names([], {}) end
Private Instance Methods
all_cop_names()
click to toggle source
# File lib/rubocop/comment_config.rb, line 153 def all_cop_names @all_cop_names ||= Cop::Cop.registry.names - [UNNEEDED_DISABLE] end
analyze()
click to toggle source
# File lib/rubocop/comment_config.rb, line 57 def analyze analyses = Hash.new { |hash, key| hash[key] = CopAnalysis.new([], nil) } each_mentioned_cop do |cop_name, disabled, line, single_line| analyses[cop_name] = analyze_cop(analyses[cop_name], disabled, line, single_line) end analyses.each_with_object({}) do |element, hash| cop_name, analysis = *element hash[cop_name] = cop_line_ranges(analysis) end end
analyze_cop(analysis, disabled, line, single_line)
click to toggle source
# File lib/rubocop/comment_config.rb, line 71 def analyze_cop(analysis, disabled, line, single_line) if single_line analyze_single_line(analysis, line, disabled) elsif disabled analyze_disabled(analysis, line) else analyze_rest(analysis, line) end end
analyze_disabled(analysis, line)
click to toggle source
# File lib/rubocop/comment_config.rb, line 88 def analyze_disabled(analysis, line) if (start_line = analysis.start_line_number) # Cop already disabled on this line, so we end the current disabled # range before we start a new range. return CopAnalysis.new(analysis.line_ranges + [start_line..line], line) end CopAnalysis.new(analysis.line_ranges, line) end
analyze_rest(analysis, line)
click to toggle source
# File lib/rubocop/comment_config.rb, line 98 def analyze_rest(analysis, line) if (start_line = analysis.start_line_number) return CopAnalysis.new(analysis.line_ranges + [start_line..line], nil) end CopAnalysis.new(analysis.line_ranges, nil) end
analyze_single_line(analysis, line, disabled)
click to toggle source
# File lib/rubocop/comment_config.rb, line 81 def analyze_single_line(analysis, line, disabled) return analysis unless disabled CopAnalysis.new(analysis.line_ranges + [(line..line)], analysis.start_line_number) end
comment_only_line?(line_number)
click to toggle source
# File lib/rubocop/comment_config.rb, line 157 def comment_only_line?(line_number) non_comment_token_line_numbers.none? do |non_comment_line_number| non_comment_line_number == line_number end end
cop_line_ranges(analysis)
click to toggle source
# File lib/rubocop/comment_config.rb, line 106 def cop_line_ranges(analysis) return analysis.line_ranges unless analysis.start_line_number analysis.line_ranges + [(analysis.start_line_number..Float::INFINITY)] end
directive_parts(comment)
click to toggle source
# File lib/rubocop/comment_config.rb, line 135 def directive_parts(comment) match = comment.text.match(COMMENT_DIRECTIVE_REGEXP) return unless match switch, cops_string = match.captures cop_names = cops_string == 'all' ? all_cop_names : cops_string.split(/,\s*/) disabled = (switch == 'disable') [cop_names, disabled] end
each_directive() { |comment, *directive| ... }
click to toggle source
# File lib/rubocop/comment_config.rb, line 124 def each_directive return if processed_source.comments.nil? processed_source.each_comment do |comment| directive = directive_parts(comment) next unless directive yield comment, *directive end end
each_mentioned_cop() { |qualified_cop_name(cop_name), disabled, comment_line_number, single_line| ... }
click to toggle source
# File lib/rubocop/comment_config.rb, line 112 def each_mentioned_cop each_directive do |comment, cop_names, disabled| comment_line_number = comment.loc.expression.line single_line = !comment_only_line?(comment_line_number) cop_names.each do |cop_name| yield qualified_cop_name(cop_name), disabled, comment_line_number, single_line end end end
enable_all?(comment)
click to toggle source
# File lib/rubocop/comment_config.rb, line 170 def enable_all?(comment) _, cops = comment.text.match(COMMENT_DIRECTIVE_REGEXP).captures cops == 'all' end
extra_enabled_comments_with_names(extras, names)
click to toggle source
# File lib/rubocop/comment_config.rb, line 43 def extra_enabled_comments_with_names(extras, names) each_directive do |comment, cop_names, disabled| next unless comment_only_line?(comment.loc.expression.line) if !disabled && enable_all?(comment) handle_enable_all(names, extras, comment) else handle_switch(cop_names, names, disabled, extras, comment) end end extras end
handle_enable_all(names, extras, comment)
click to toggle source
# File lib/rubocop/comment_config.rb, line 175 def handle_enable_all(names, extras, comment) enabled_cops = 0 names.each do |name, counter| next unless counter.positive? names[name] -= 1 enabled_cops += 1 end extras << [comment, 'all'] if enabled_cops.zero? end
handle_switch(cop_names, names, disabled, extras, comment)
click to toggle source
# File lib/rubocop/comment_config.rb, line 187 def handle_switch(cop_names, names, disabled, extras, comment) cop_names.each do |name| names[name] ||= 0 if disabled names[name] += 1 elsif (names[name]).positive? names[name] -= 1 else extras << [comment, name] end end end
non_comment_token_line_numbers()
click to toggle source
# File lib/rubocop/comment_config.rb, line 163 def non_comment_token_line_numbers @non_comment_token_line_numbers ||= begin non_comment_tokens = processed_source.tokens.reject(&:comment?) non_comment_tokens.map(&:line).uniq end end
qualified_cop_name(cop_name)
click to toggle source
# File lib/rubocop/comment_config.rb, line 149 def qualified_cop_name(cop_name) Cop::Cop.qualified_cop_name(cop_name.strip, processed_source.file_path) end