class Appium::Lint

Constants

DATE
VERSION

Attributes

input[R]

OpenStruct.new data: ”, lines: ”, file: ”

Public Class Methods

init_data(opts={}) click to toggle source
# File lib/appium_doc_lint/lint.rb, line 20
def self.init_data opts={}, input
  raise 'Input must exist' unless input
  data = opts[:data]
  if data
    input.data  = data.freeze
    input.lines = data.split(/\r?\n/).freeze
    input.file  = nil
  else
    file = opts[:file]
    raise 'File path must be provided' unless file
    raise "File must exist and be readable #{file}" unless File.exist?(file) && File.readable?(file)
    raise 'File must not be a dir' if File.directory?(file)
    file = File.expand_path(file)

    input.data  = File.read(file).freeze
    input.lines = input.data.split(/\r?\n/).freeze
    input.file  = file.freeze
  end

  input
end
new() click to toggle source
# File lib/appium_doc_lint/lint.rb, line 16
def initialize
  @rules = [ExtMissing, H2Missing, H2Multiple, H2Invalid, H156Invalid, LineBreakInvalid]
end
new_input(opts) click to toggle source
# File lib/appium_doc_lint/lint.rb, line 42
def self.new_input opts
  input = OpenStruct.new(data: '', lines: '', file: '')
  self.init_data opts, input
end

Public Instance Methods

call(opts={}) click to toggle source
# File lib/appium_doc_lint/lint.rb, line 47
def call opts={}
  @input = self.class.new_input opts

  all_warnings = {}
  @rules.each do |rule|
    warnings = rule.new(@input).call
    unless warnings.empty?
      all_warnings.merge!(warnings) do |key, old_val, new_val|
        # flatten to prevent { :a => [[1, 2], 2]}
        [old_val, new_val].flatten
      end
    end
  end

  return {} if all_warnings.empty?

  # sort by line number
  all_warnings = all_warnings.sort.to_h

  # wrap with file path if available
  input.file ? { input.file => all_warnings } : all_warnings
end
glob(dir_glob) click to toggle source
# File lib/appium_doc_lint/lint.rb, line 70
def glob dir_glob
  results = {}
  Dir.glob dir_glob do |markdown|
    next if File.directory?(markdown)
    markdown = File.expand_path markdown
    results.merge!(self.call(file: markdown))
  end
  # glob order is system dependent so sort the results.
  results.sort.to_h
end
report(data) click to toggle source

Format data into a report

# File lib/appium_doc_lint/lint.rb, line 82
def report data
  return nil if data.nil? || data.empty?
  result = ''
  data.each do |file_name, analysis|
    rel_path = File.join('.', File.expand_path(file_name).sub(Dir.pwd, ''))
    result += "\n#{rel_path}\n"
    analysis.each do |line_number, warning|
      result += "  #{line_number}: #{warning.join(',')}\n"
    end
  end
  result.strip!

  result.empty? ? nil : result
end