Skip to content

Commit

Permalink
Merge pull request #11240 from fatkodima/optimize
Browse files Browse the repository at this point in the history
Optimize rubocop
  • Loading branch information
koic committed Dec 7, 2022
2 parents be7c482 + f609ddb commit 3b4db02
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 40 deletions.
5 changes: 5 additions & 0 deletions lib/rubocop/comment_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initialize(cop_name)

def initialize(processed_source)
@processed_source = processed_source
@no_directives = !processed_source.raw_source.include?('rubocop')
end

def cop_enabled_at_line?(cop, line_number)
Expand Down Expand Up @@ -74,6 +75,8 @@ def extra_enabled_comments_with_names(extras:, names:)
end

def analyze # rubocop:todo Metrics/AbcSize
return {} if @no_directives

analyses = Hash.new { |hash, key| hash[key] = CopAnalysis.new([], nil) }
inject_disabled_cops_directives(analyses)

Expand Down Expand Up @@ -146,6 +149,8 @@ def cop_line_ranges(analysis)
end

def each_directive
return if @no_directives

processed_source.comments.each do |comment|
directive = DirectiveComment.new(comment)
yield directive if directive.cop_names
Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class Config
def initialize(hash = {}, loaded_path = nil)
@loaded_path = loaded_path
@for_cop = Hash.new do |h, cop|
qualified_cop_name = Cop::Registry.qualified_cop_name(cop, loaded_path)
cop_name = cop.respond_to?(:cop_name) ? cop.cop_name : cop
qualified_cop_name = Cop::Registry.qualified_cop_name(cop_name, loaded_path)
cop_options = self[qualified_cop_name].dup || {}
cop_options['Enabled'] = enable_cop?(qualified_cop_name, cop_options)
h[cop] = cop_options
h[cop] = h[cop_name] = cop_options
end
@hash = hash
@validator = ConfigValidator.new(self)
Expand Down Expand Up @@ -116,7 +117,7 @@ def deprecation_check
# Note: the 'Enabled' attribute is calculated according to the department's
# and 'AllCops' configuration; other attributes are not inherited.
def for_cop(cop)
@for_cop[cop.respond_to?(:cop_name) ? cop.cop_name : cop]
@for_cop[cop]
end

# @return [Config] for the given cop merged with that of its department (if any)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def disabled(config)
end

def enabled?(cop, config)
return true if options.fetch(:only, []).include?(cop.cop_name)
return true if options[:only]&.include?(cop.cop_name)

cfg = config.for_cop(cop)

Expand Down
6 changes: 1 addition & 5 deletions lib/rubocop/cop/style/string_literals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ def message(_node)
end

def offense?(node)
# If it's a string within an interpolation, then it's not an offense
# for this cop.
return false if inside_interpolation?(node)

wrong_quotes?(node)
wrong_quotes?(node) && !inside_interpolation?(node)
end

def consistent_multiline?
Expand Down
49 changes: 20 additions & 29 deletions lib/rubocop/cop/variable_force.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,26 @@ def skip_children!
:skip_children
end

# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
# rubocop:disable Layout/ClassStructure
NODE_HANDLER_METHOD_NAMES = [
[VARIABLE_ASSIGNMENT_TYPE, :process_variable_assignment],
[REGEXP_NAMED_CAPTURE_TYPE, :process_regexp_named_captures],
[MULTIPLE_ASSIGNMENT_TYPE, :process_variable_multiple_assignment],
[VARIABLE_REFERENCE_TYPE, :process_variable_referencing],
[RESCUE_TYPE, :process_rescue],
[ZERO_ARITY_SUPER_TYPE, :process_zero_arity_super],
[SEND_TYPE, :process_send],
*ARGUMENT_DECLARATION_TYPES.product([:process_variable_declaration]),
*OPERATOR_ASSIGNMENT_TYPES.product([:process_variable_operator_assignment]),
*LOOP_TYPES.product([:process_loop]),
*SCOPE_TYPES.product([:process_scope])
].to_h.freeze
private_constant :NODE_HANDLER_METHOD_NAMES
# rubocop:enable Layout/ClassStructure

def node_handler_method_name(node)
case node.type
when VARIABLE_ASSIGNMENT_TYPE
:process_variable_assignment
when REGEXP_NAMED_CAPTURE_TYPE
:process_regexp_named_captures
when MULTIPLE_ASSIGNMENT_TYPE
:process_variable_multiple_assignment
when VARIABLE_REFERENCE_TYPE
:process_variable_referencing
when RESCUE_TYPE
:process_rescue
when ZERO_ARITY_SUPER_TYPE
:process_zero_arity_super
when SEND_TYPE
:process_send
when *ARGUMENT_DECLARATION_TYPES
:process_variable_declaration
when *OPERATOR_ASSIGNMENT_TYPES
:process_variable_operator_assignment
when *LOOP_TYPES
:process_loop
when *SCOPE_TYPES
:process_scope
end
NODE_HANDLER_METHOD_NAMES[node.type]
end
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity

def process_variable_declaration(node)
variable_name = node.children.first
Expand Down Expand Up @@ -358,13 +350,12 @@ def descendant_reference(node)
end
end

# Use Node#equal? for accurate check.
def scanned_node?(node)
scanned_nodes.any? { |scanned_node| scanned_node.equal?(node) }
scanned_nodes.include?(node)
end

def scanned_nodes
@scanned_nodes ||= []
@scanned_nodes ||= Set.new.compare_by_identity
end

# Hooks invoked by VariableTable.
Expand Down
16 changes: 14 additions & 2 deletions lib/rubocop/path_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ def smart_path(path)
end
end

# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
def match_path?(pattern, path)
case pattern
when String
File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) ||
hidden_file_in_not_hidden_dir?(pattern, path)
matches =
if pattern == path
true
elsif pattern.match?(/[*{\[?]/)
File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
end

matches || hidden_file_in_not_hidden_dir?(pattern, path)
when Regexp
begin
pattern.match?(path)
Expand All @@ -48,6 +55,7 @@ def match_path?(pattern, path)
end
end
end
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity

# Returns true for an absolute Unix or Windows path.
def absolute?(path)
Expand All @@ -67,8 +75,12 @@ def hidden_file?(path)
maybe_hidden_file?(path) && File.basename(path).start_with?('.')
end

HIDDEN_FILE_PATTERN = "#{File::SEPARATOR}."

# Loose check to reduce memory allocations
def maybe_hidden_file?(path)
return false unless path.include?(HIDDEN_FILE_PATTERN)

separator_index = path.rindex(File::SEPARATOR)
return false unless separator_index

Expand Down

0 comments on commit 3b4db02

Please sign in to comment.