Class: Yamlint::Rules::Anchors::AnchorHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnchorHandler

Returns a new instance of AnchorHandler.



68
69
70
71
72
73
# File 'lib/yamlint/rules/anchors.rb', line 68

def initialize
  super
  @anchors = {}
  @aliases = []
  @parser = nil
end

Instance Attribute Details

#parserObject

Returns the value of attribute parser.



66
67
68
# File 'lib/yamlint/rules/anchors.rb', line 66

def parser
  @parser
end

Instance Method Details

#alias(anchor) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/yamlint/rules/anchors.rb', line 87

def alias(anchor)
  mark = @parser&.mark
  @aliases << {
    name: anchor,
    line: mark ? mark.line + 1 : 1,
    column: mark ? mark.column + 1 : 1
  }
end

#duplicated_anchorsObject



100
101
102
# File 'lib/yamlint/rules/anchors.rb', line 100

def duplicated_anchors
  @anchors.values.select { |v| v.is_a?(Array) && v.length > 1 }.flatten
end

#scalar(_value, anchor, _tag, _plain, _quoted, _style) ⇒ Object



75
76
77
# File 'lib/yamlint/rules/anchors.rb', line 75

def scalar(_value, anchor, _tag, _plain, _quoted, _style)
  record_anchor(anchor) if anchor
end

#start_mapping(anchor, _tag, _implicit, _style) ⇒ Object



79
80
81
# File 'lib/yamlint/rules/anchors.rb', line 79

def start_mapping(anchor, _tag, _implicit, _style)
  record_anchor(anchor) if anchor
end

#start_sequence(anchor, _tag, _implicit, _style) ⇒ Object



83
84
85
# File 'lib/yamlint/rules/anchors.rb', line 83

def start_sequence(anchor, _tag, _implicit, _style)
  record_anchor(anchor) if anchor
end

#undeclared_aliasesObject



96
97
98
# File 'lib/yamlint/rules/anchors.rb', line 96

def undeclared_aliases
  @aliases.reject { |a| @anchors.key?(a[:name]) }
end

#unused_anchorsObject



104
105
106
107
108
109
# File 'lib/yamlint/rules/anchors.rb', line 104

def unused_anchors
  used_names = @aliases.map { |a| a[:name] }
  @anchors.except(*used_names).map do |_, info|
    info.is_a?(Array) ? info.first : info
  end
end