Class: Opal::Rewriters::Hashes::KeyDuplicatesRewriter

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/rewriters/hashes/key_duplicates_rewriter.rb

Defined Under Namespace

Classes: UniqKeysSet

Constant Summary

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Base

#current_node

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #prepend_to_body, #process, #s, s

Constructor Details

#initializeKeyDuplicatesRewriter

Returns a new instance of KeyDuplicatesRewriter



9
10
11
# File 'opal/lib/opal/rewriters/hashes/key_duplicates_rewriter.rb', line 9

def initialize
  @keys = UniqKeysSet.new
end

Instance Method Details

#on_hash(node) ⇒ Object



13
14
15
16
17
18
# File 'opal/lib/opal/rewriters/hashes/key_duplicates_rewriter.rb', line 13

def on_hash(node)
  previous_keys, @keys = @keys, UniqKeysSet.new
  super(node)
ensure
  @keys = previous_keys
end

#on_kwsplat(node) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'opal/lib/opal/rewriters/hashes/key_duplicates_rewriter.rb', line 30

def on_kwsplat(node)
  hash, _ = *node

  if hash.type == :hash
    hash = process_regular_node(hash)
  end

  node.updated(nil, [hash])
end

#on_pair(node) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'opal/lib/opal/rewriters/hashes/key_duplicates_rewriter.rb', line 20

def on_pair(node)
  key, _value = *node

  if %i(str sym).include?(key.type)
    @keys << key
  end

  super(node)
end