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, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Constructor Details

#initializeKeyDuplicatesRewriter

Returns a new instance of KeyDuplicatesRewriter.


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

def initialize
  @keys = UniqKeysSet.new
end

Instance Method Details

#on_hash(node) ⇒ Object


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

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

#on_kwsplat(node) ⇒ Object


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

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


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

def on_pair(node)
  key, _value = *node

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

  super(node)
end