Module: RuboCop::Cop::HashShorthandSyntax

Included in:
Style::HashSyntax
Defined in:
lib/rubocop/cop/mixin/hash_shorthand_syntax.rb

Overview

This module checks for Ruby 3.1’s hash value omission syntax. rubocop:disable Metrics/ModuleLength

Defined Under Namespace

Classes: DefNode

Constant Summary collapse

OMIT_HASH_VALUE_MSG =
'Omit the hash value.'
EXPLICIT_HASH_VALUE_MSG =
'Include the hash value.'
DO_NOT_MIX_MSG_PREFIX =
'Do not mix explicit and implicit hash values.'
DO_NOT_MIX_OMIT_VALUE_MSG =
"#{DO_NOT_MIX_MSG_PREFIX} #{OMIT_HASH_VALUE_MSG}"
DO_NOT_MIX_EXPLICIT_VALUE_MSG =
"#{DO_NOT_MIX_MSG_PREFIX} #{EXPLICIT_HASH_VALUE_MSG}"

Instance Method Summary collapse

Instance Method Details

#on_hash_for_mixed_shorthand(hash_node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/cop/mixin/hash_shorthand_syntax.rb', line 14

def on_hash_for_mixed_shorthand(hash_node)
  return if ignore_mixed_hash_shorthand_syntax?(hash_node)

  hash_value_type_breakdown = breakdown_value_types_of_hash(hash_node)

  if hash_with_mixed_shorthand_syntax?(hash_value_type_breakdown)
    mixed_shorthand_syntax_check(hash_value_type_breakdown)
  else
    no_mixed_shorthand_syntax_check(hash_value_type_breakdown)
  end
end

#on_pair(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/mixin/hash_shorthand_syntax.rb', line 26

def on_pair(node)
  return if ignore_hash_shorthand_syntax?(node)

  hash_key_source = node.key.source

  if enforced_shorthand_syntax == 'always'
    return if node.value_omission? || require_hash_value?(hash_key_source, node)

    message = OMIT_HASH_VALUE_MSG
    replacement = "#{hash_key_source}:"
    self.config_to_allow_offenses = { 'Enabled' => false }
  else
    return unless node.value_omission?

    message = EXPLICIT_HASH_VALUE_MSG
    replacement = "#{hash_key_source}: #{hash_key_source}"
  end

  register_offense(node, message, replacement)
end