Skip to content

Commit

Permalink
[Fix rubocop#9328] Move module to mutable_constant
Browse files Browse the repository at this point in the history
  • Loading branch information
thearjunmdas committed Jul 21, 2021
1 parent a68088a commit 7f321a6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
1 change: 0 additions & 1 deletion lib/rubocop.rb
Expand Up @@ -113,7 +113,6 @@
require_relative 'rubocop/cop/mixin/rational_literal'
require_relative 'rubocop/cop/mixin/rescue_node'
require_relative 'rubocop/cop/mixin/safe_assignment'
require_relative 'rubocop/cop/mixin/shareable_constant_value'
require_relative 'rubocop/cop/mixin/space_after_punctuation'
require_relative 'rubocop/cop/mixin/space_before_punctuation'
require_relative 'rubocop/cop/mixin/surrounding_space'
Expand Down
37 changes: 0 additions & 37 deletions lib/rubocop/cop/mixin/shareable_constant_value.rb

This file was deleted.

36 changes: 36 additions & 0 deletions lib/rubocop/cop/style/mutable_constant.rb
Expand Up @@ -53,6 +53,42 @@ module Style
# end
# end.freeze
class MutableConstant < Base
# Handles magic comment shareable_constant_value with O(n ^ 2) complexity
# n - number of lines in the source
# Iterates over all lines before a CONSTANT
# until it reaches shareable_constant_value
module ShareableConstantValue
module_function

def recent_shareable_value?(node)
shareable_constant_comment = magic_comment_in_scope node
return false if shareable_constant_comment.nil?

shareable_constant_value = MagicComment.parse(shareable_constant_comment)
.shareable_constant_value
shareable_constant_value_enabled? shareable_constant_value
end

# Identifies the most recent magic comment with valid shareable constant values
# thats in scope for this node
def magic_comment_in_scope(node)
processed_source_till_node(node).reverse_each.find do |line|
MagicComment.parse(line).valid_shareable_constant_value?
end
end

private

def processed_source_till_node(node)
processed_source.lines[0..(node.last_line - 1)]
end

def shareable_constant_value_enabled?(value)
%w[literal experimental_everything experimental_copy].include? value
end
end
private_constant :ShareableConstantValue

include ShareableConstantValue
include FrozenStringLiteral
include ConfigurableEnforcedStyle
Expand Down

0 comments on commit 7f321a6

Please sign in to comment.