Skip to content

Commit

Permalink
Add value_omission method to AST::PairNode
Browse files Browse the repository at this point in the history
Follow up to whitequark/parser#818

This PR adds `value_omission` method to `AST::PairNode` for Ruby 3.1's
hash value omission.
It can replace string match with that named API.
  • Loading branch information
koic committed Dec 12, 2021
1 parent 85e01c2 commit 2ae1403
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_add_value_omission_to_pair_node.md
@@ -0,0 +1 @@
* [#10219](https://github.com/rubocop/rubocop/pull/10219): Add `value_omission` method to `AST::PairNode` for Ruby 3.1's hash value omission. ([@koic][])
7 changes: 7 additions & 0 deletions lib/rubocop/ast/node/pair_node.rb
Expand Up @@ -62,6 +62,13 @@ def inverse_delimiter(*deprecated, with_spacing: deprecated.first)
def value_on_new_line?
key.loc.line != value.loc.line
end

# Checks whether the `pair` uses hash value omission.
#
# @return [Boolean] whether this `pair` uses hash value omission
def value_omission?
source.end_with?(':')
end
end
end
end
14 changes: 14 additions & 0 deletions spec/rubocop/ast/pair_node_spec.rb
Expand Up @@ -711,4 +711,18 @@
end
end
end

describe '#value_omission?' do
context 'when using hash value omission', :ruby31 do
let(:source) { '{ x: }' }

it { expect(pair_node).to be_value_omission }
end

context 'when not using hash value omission' do
let(:source) { '{ x: x }' }

it { expect(pair_node).not_to be_value_omission }
end
end
end

0 comments on commit 2ae1403

Please sign in to comment.