Class: Yamlint::Rules::KeyDuplicates::DuplicateKeyHandler

Inherits:
Psych::Handler
  • Object
show all
Defined in:
lib/yamlint/rules/key_duplicates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDuplicateKeyHandler

Returns a new instance of DuplicateKeyHandler.



36
37
38
39
40
41
# File 'lib/yamlint/rules/key_duplicates.rb', line 36

def initialize
  super
  @stack = []
  @problems = []
  @parser = nil
end

Instance Attribute Details

#parser=(value) ⇒ Object (writeonly)

Sets the attribute parser

Parameters:

  • value

    the value to set the attribute parser to.



43
44
45
# File 'lib/yamlint/rules/key_duplicates.rb', line 43

def parser=(value)
  @parser = value
end

#problemsObject (readonly)

Returns the value of attribute problems.



34
35
36
# File 'lib/yamlint/rules/key_duplicates.rb', line 34

def problems
  @problems
end

Instance Method Details

#end_mappingObject



55
56
57
58
# File 'lib/yamlint/rules/key_duplicates.rb', line 55

def end_mapping
  frame = @stack.pop
  close_parent_value(frame)
end

#end_sequenceObject



68
69
70
71
# File 'lib/yamlint/rules/key_duplicates.rb', line 68

def end_sequence
  frame = @stack.pop
  close_parent_value(frame)
end

#scalar(value, _anchor, _tag, _plain, _quoted, _style) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yamlint/rules/key_duplicates.rb', line 73

def scalar(value, _anchor, _tag, _plain, _quoted, _style)
  frame = @stack.last
  return unless frame && frame[:type] == :mapping

  if frame[:expecting_key]
    current_keys = frame[:keys]
    mark = @parser&.mark

    if current_keys.key?(value)
      @problems << {
        key: value,
        line: mark ? mark.line + 1 : 1,
        column: mark ? mark.column + 1 : 1
      }
    else
      current_keys[value] = true
    end

    frame[:expecting_key] = false
  else
    frame[:expecting_key] = true
  end
end

#start_mappingObject



45
46
47
48
49
50
51
52
53
# File 'lib/yamlint/rules/key_duplicates.rb', line 45

def start_mapping(*)
  parent = current_mapping_frame
  @stack << {
    type: :mapping,
    keys: {},
    expecting_key: true,
    close_parent: capture_close_parent(parent)
  }
end

#start_sequenceObject



60
61
62
63
64
65
66
# File 'lib/yamlint/rules/key_duplicates.rb', line 60

def start_sequence(*)
  parent = current_mapping_frame
  @stack << {
    type: :sequence,
    close_parent: capture_close_parent(parent)
  }
end