Class: Yamlint::Rules::Truthy

Inherits:
LineRule show all
Defined in:
lib/yamlint/rules/truthy.rb

Constant Summary collapse

TRUTHY_VALUES =
%w[
  YES Yes yes
  NO No no
  TRUE True
  FALSE False
  ON On on
  OFF Off off
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #level

Instance Method Summary collapse

Methods inherited from LineRule

#check

Methods inherited from Base

#check, defaults, desc, #fixable?, inherited, #initialize, rule_id

Constructor Details

This class inherits a constructor from Yamlint::Rules::Base

Instance Method Details

#check_line(line, line_number, _context) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yamlint/rules/truthy.rb', line 22

def check_line(line, line_number, _context)
  return if line.strip.empty?
  return if line.strip.start_with?('#')

  problems = []
  allowed = @config[:'allowed-values']

  TRUTHY_VALUES.each do |truthy|
    next if allowed.include?(truthy)

    next unless line.match?(/:\s*#{Regexp.escape(truthy)}\s*(?:#.*)?$/)

    problems << problem(
      line: line_number,
      column: line.index(truthy) + 1,
      message: "truthy value should be one of [#{allowed.join(', ')}]",
      fixable: false
    )
  end

  problems
end