class I18nLinter::Linter

Public Class Methods

new(options, config) click to toggle source
# File lib/i18n_linter/linter.rb, line 8
def initialize(options, config)
  @options = options
  @config = config
  @strings = []
end

Public Instance Methods

lint(filename:, file:) click to toggle source
# File lib/i18n_linter/linter.rb, line 14
def lint(filename:, file:)
  parsed_file = tokenize_file(filename, file)
  find_strings(filename, parsed_file)
  compile(filename)
end
show_errors(results) click to toggle source
# File lib/i18n_linter/linter.rb, line 20
def show_errors(results)
  puts
  results.each do |result|
    file = File.readlines(result.filename)
    line = result.line
    print_block(result, file, line)
  end
  puts
end

Private Instance Methods

array?(elem) click to toggle source
# File lib/i18n_linter/linter.rb, line 69
def array?(elem)
  elem.class == Array
end
check_rest_of_tokens(filename, tokens) click to toggle source
# File lib/i18n_linter/linter.rb, line 92
def check_rest_of_tokens(filename, tokens)
  tokens[1..-1].each { |child| find_strings(filename, child) }
end
check_rules(filename, tokens) click to toggle source
# File lib/i18n_linter/linter.rb, line 60
def check_rules(filename, tokens)
  if string_element?(tokens)
    string = tokens[1]
    @strings << StringLine.new(tokens[2], string) if Rules.check_string_rules(@config, string)
  else
    test_rules(filename, tokens)
  end
end
compile(filename) click to toggle source
# File lib/i18n_linter/linter.rb, line 77
def compile(filename)
  result_set = ResultSet.new
  @strings.each do |string_line|
    result_set.add_result(Result.new(filename, string_line, string_line.string))
  end
  @strings = []
  result_set
end
find_strings(filename, tokens) click to toggle source
# File lib/i18n_linter/linter.rb, line 50
def find_strings(filename, tokens)
  return unless array?(tokens)

  if array?(tokens[0])
    tokens.each { |child| find_strings(filename, child) }
  else
    check_rules(filename, tokens)
  end
end
get_string_array(file, current_index) click to toggle source
# File lib/i18n_linter/linter.rb, line 40
def get_string_array(file, current_index)
  rest_of_file(file, current_index).take_while { |token|
    !%i[on_tstring_end on_label_end].include?(token.type)
  }.map(&:content)
end
get_token(file, index) click to toggle source
# File lib/i18n_linter/linter.rb, line 36
def get_token(file, index)
  file[index]
end
print_block(result, file, line) click to toggle source
rest_of_file(file, current_index) click to toggle source
# File lib/i18n_linter/linter.rb, line 46
def rest_of_file(file, current_index)
  file.last(file.length - current_index)
end
string_element?(elem) click to toggle source
# File lib/i18n_linter/linter.rb, line 73
def string_element?(elem)
  elem[0] == :@tstring_content
end
test_rules(filename, tokens) click to toggle source
# File lib/i18n_linter/linter.rb, line 86
def test_rules(filename, tokens)
  return if tokens.empty? || Rules.check_negative_rules(@config, tokens)

  check_rest_of_tokens(filename, tokens)
end
tokenize_file(filename, file) click to toggle source
# File lib/i18n_linter/linter.rb, line 32
def tokenize_file(filename, file)
  Ripper.sexp(file, filename)
end