Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value_omission method to AST::PairNode #219

Merged
merged 1 commit into from Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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