class YamlLint::Linter

Runs the actual linting

Attributes

disable_extension_check[R]
errors[R]
extensions[R]
valid_extensions[R]

Public Class Methods

new(opts = {}) click to toggle source

Initilize the linter Params:

disable_ext_check

Disables file extension check (optional, false)

# File lib/yamllint/linter.rb, line 19
def initialize(opts = {})
  @errors = {}
  @valid_extensions = %w(yaml yml)

  @disable_extension_check = opts[:disable_ext_check] || false
  @extensions = opts[:extensions]

  @valid_extensions += @extensions unless @extensions.nil?
end

Public Instance Methods

check(path) click to toggle source

Check a single file

# File lib/yamllint/linter.rb, line 35
def check(path)
  raise FileNotFoundError, "#{path}: no such file" unless File.exist?(path)

  valid = false
  unless disable_extension_check
    unless check_filename(path)
      errors[path] = ['File extension must be .yaml or .yml']
      return valid
    end
  end

  File.open(path, 'r') do |f|
    error_array = []
    valid = check_data(f.read, error_array)
    errors[path] = error_array unless error_array.empty?
  end

  valid
end
check_all(*files_to_check) click to toggle source

Check a list of files

# File lib/yamllint/linter.rb, line 30
def check_all(*files_to_check)
  files_to_check.flatten.each { |f| check(f) }
end
check_stream(io_stream) click to toggle source

Check an IO stream

# File lib/yamllint/linter.rb, line 56
def check_stream(io_stream)
  yaml_data = io_stream.read
  error_array = []

  valid = check_data(yaml_data, error_array)
  errors[''] = error_array unless error_array.empty?

  valid
end
display_errors() click to toggle source

Output the lint errors

# File lib/yamllint/linter.rb, line 77
def display_errors
  errors.each do |path, errors|
    puts path
    errors.each do |err|
      puts "  #{err}"
    end
  end
end
errors?() click to toggle source

Are there any lint errors found?

# File lib/yamllint/linter.rb, line 67
def errors?
  !errors.empty?
end
errors_count() click to toggle source

Return the number of lint errors found

# File lib/yamllint/linter.rb, line 72
def errors_count
  errors.length
end

Private Instance Methods

check_data(yaml_data, errors_array) click to toggle source

Check the data in the file or stream

# File lib/yamllint/linter.rb, line 96
def check_data(yaml_data, errors_array)
  valid = check_not_empty?(yaml_data, errors_array)
  valid &&= check_syntax_valid?(yaml_data, errors_array)
  valid &&= check_overlapping_keys?(yaml_data, errors_array)

  valid
end
check_filename(filename) click to toggle source

Check file extension

# File lib/yamllint/linter.rb, line 89
def check_filename(filename)
  extension = filename.split('.').last
  return true if valid_extensions.include?(extension)
  false
end
check_not_empty?(yaml_data, errors_array) click to toggle source

Check that the data is not empty

# File lib/yamllint/linter.rb, line 105
def check_not_empty?(yaml_data, errors_array)
  if yaml_data.empty?
    errors_array << 'The YAML should not be an empty string'
    false
  elsif yaml_data.strip.empty?
    errors_array << 'The YAML should not just be spaces'
    false
  else
    true
  end
end
check_overlapping_keys?(yaml_data, errors_array) click to toggle source

Check if there is overlapping key

# File lib/yamllint/linter.rb, line 258
def check_overlapping_keys?(yaml_data, errors_array)
  overlap_detector = KeyOverlapDetector.new
  data = Psych.parser.parse(yaml_data)

  overlap_detector.parse(data)

  overlap_detector.overlapping_keys.each do |key|
    err_meg = "The same key is defined more than once: #{key.join('.')}"
    errors_array << err_meg
  end

  overlap_detector.overlapping_keys.empty?
end
check_syntax_valid?(yaml_data, errors_array) click to toggle source

Check that the data is valid YAML

# File lib/yamllint/linter.rb, line 118
def check_syntax_valid?(yaml_data, errors_array)
  YAML.load(yaml_data)
  true
rescue YAML::SyntaxError => e
  errors_array << e.message
  false
end