Skip to content

Commit

Permalink
Allow top-level const in MutableConst
Browse files Browse the repository at this point in the history
Allow top-level `::ENV`, `::Struct` in Style/MutableConstant cop.
  • Loading branch information
biinari authored and bbatsov committed Jul 6, 2020
1 parent 91820ea commit 5e03e9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/rubocop/cop/style/mutable_constant.rb
Expand Up @@ -150,14 +150,14 @@ def correct_splat_expansion(corrector, expr, splat_value)
def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
{
(const _ _)
(send (const nil? :Struct) :new ...)
(block (send (const nil? :Struct) :new ...) ...)
(send (const {nil? cbase} :Struct) :new ...)
(block (send (const {nil? cbase} :Struct) :new ...) ...)
(send _ :freeze)
(send {float int} {:+ :- :* :** :/ :% :<<} _)
(send _ {:+ :- :* :** :/ :%} {float int})
(send _ {:== :=== :!= :<= :>= :< :>} _)
(send (const nil? :ENV) :[] _)
(or (send (const nil? :ENV) :[] _) _)
(send (const {nil? cbase} :ENV) :[] _)
(or (send (const {nil? cbase} :ENV) :[] _) _)
(send _ {:count :length :size} ...)
(block (send _ {:count :length :size} ...) ...)
}
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/mutable_constant_spec.rb
Expand Up @@ -61,6 +61,7 @@
it_behaves_like 'immutable objects', 'FOO - BAR'
it_behaves_like 'immutable objects', "'foo' + 'bar'"
it_behaves_like 'immutable objects', "ENV['foo']"
it_behaves_like 'immutable objects', "::ENV['foo']"

it 'allows method call assignments' do
expect_no_offenses('TOP_TEST = Something.new')
Expand Down Expand Up @@ -192,9 +193,13 @@
it_behaves_like 'immutable objects', '1.5'
it_behaves_like 'immutable objects', ':sym'
it_behaves_like 'immutable objects', "ENV['foo']"
it_behaves_like 'immutable objects', "::ENV['foo']"
it_behaves_like 'immutable objects', 'OTHER_CONST'
it_behaves_like 'immutable objects', '::OTHER_CONST'
it_behaves_like 'immutable objects', 'Namespace::OTHER_CONST'
it_behaves_like 'immutable objects', '::Namespace::OTHER_CONST'
it_behaves_like 'immutable objects', 'Struct.new'
it_behaves_like 'immutable objects', '::Struct.new'
it_behaves_like 'immutable objects', 'Struct.new(:a, :b)'
it_behaves_like 'immutable objects', <<~RUBY
Struct.new(:node) do
Expand Down Expand Up @@ -303,6 +308,9 @@ def assignment?
expect_no_offenses(<<~RUBY)
CONST = ENV['foo'] || 'foo'
RUBY
expect_no_offenses(<<~RUBY)
CONST = ::ENV['foo'] || 'foo'
RUBY
end

it 'accepts operating on a constant and an interger' do
Expand Down

0 comments on commit 5e03e9b

Please sign in to comment.