Skip to content

Commit

Permalink
Suppress an offenses when using rubocop --enable-pending-cops
Browse files Browse the repository at this point in the history
This PR suppresses a RuboCop offenses when using `rubocop --enable-pending-cops`.

```console
% path/to/rubocop
% ./exe/rubocop --enable-pending-cops
(snip)

Offenses:

lib/rubocop/config_loader.rb:58:14: C: Style/HashEachMethods: Use
each_key instead of keys.each.
        hash.keys.each do |key|
             ^^^^^^^^^
lib/rubocop/cop/lint/percent_string_array.rb:45:18: C:
Style/HashEachMethods: Use each_value instead of values.each.
            node.values.each do |value|
                 ^^^^^^^^^^^

1142 files inspected, 2 offenses detected
```

`Style/HashEachMethods` cop will be enabled by default in RuboCop 1.0.

And this PR adds `each_value` method to `ArrayNode` for
prevents the following `NoMethodError`.

```console
RuboCop::ErrorWithAnalyzedFileLocation:
 cause: #<NoMethodError: undefined method `each_value' for
  #<RuboCop::AST::ArrayNode:0x00007fb46e9d5628>
  Did you mean?  each_node>
```
  • Loading branch information
koic authored and bbatsov committed Apr 12, 2020
1 parent 0ae2989 commit fa9a0c4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -13,7 +13,7 @@ InternalAffairs/NodeDestructuring:
# Offense count: 50
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 186
Max: 187

# Offense count: 209
# Configuration parameters: CountComments, ExcludedMethods.
Expand Down
13 changes: 13 additions & 0 deletions lib/rubocop/ast/node/array_node.rb
Expand Up @@ -18,6 +18,19 @@ def values
each_child_node.to_a
end

# Calls the given block for all values in the `array` literal.
#
# @yieldparam [Node] node each node
# @return [self] if a block is given
# @return [Enumerator] if no block is given
def each_value(&block)
return to_enum(__method__) unless block_given?

values.each(&block)

self
end

# Checks whether the `array` literal is delimited by square brackets.
#
# @return [Boolean] whether the array is enclosed in square brackets
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/config_loader.rb
Expand Up @@ -55,7 +55,10 @@ def load_file(file)
end

def add_missing_namespaces(path, hash)
hash.keys.each do |key|
# Using `hash.each_key` will cause the
# `can't add a new key into hash during iteration` error
hash_keys = hash.keys
hash_keys.each do |key|
q = Cop::Cop.qualified_cop_name(key, path)
next if q == key

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/percent_string_array.rb
Expand Up @@ -42,7 +42,7 @@ def on_percent_literal(node)

def autocorrect(node)
lambda do |corrector|
node.values.each do |value|
node.each_value do |value|
range = value.loc.expression

match = range.source.match(TRAILING_QUOTE)
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/ast/array_node_spec.rb
Expand Up @@ -31,6 +31,24 @@
end
end

describe '#each_value' do
let(:source) { '[1, 2, 3]' }

context 'with block' do
it { expect(array_node.each_value {}.is_a?(described_class)).to be(true) }
it do
ret = []
array_node.each_value { |i| ret << i.to_s }

expect(ret).to eq(['(int 1)', '(int 2)', '(int 3)'])
end
end

context 'without block' do
it { expect(array_node.each_value.is_a?(Enumerator)).to be(true) }
end
end

describe '#square_brackets?' do
context 'with square brackets' do
let(:source) { '[1, 2, 3]' }
Expand Down

0 comments on commit fa9a0c4

Please sign in to comment.