class Terrestrial::Cli::Parser::ObjC

Constants

DOT_TRANSLATED_REGEX
LANGUAGE
NSLOCALIZEDSTRING_REGEX
STRING_REGEX
TRANSLATED_WITH_CONTEXT_REGEX

Public Class Methods

analyse_line_for_dot_translated(line, index, file_path) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 65
def self.analyse_line_for_dot_translated(line, index, file_path)
  results = []
  line.scan(DOT_TRANSLATED_REGEX).each do |match|
    results.push(Hash.new.tap do |h|
      h["file"] = file_path
      h["line_number"] = index + 1
      h["string"] = match[0]
      h["type"] = ".translated"
      h["context"] = ""
    end)
  end
  results
end
analyse_line_for_strings(line, index, file_path) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 24
def self.analyse_line_for_strings(line, index, file_path)
  results = []
  line.scan(STRING_REGEX).each do |match|
    unless looks_suspicious(line)
      results.push(Hash.new.tap do |entry|
        entry["language"] = LANGUAGE
        entry["file"] = file_path
        entry["line_number"] = index + 1
        entry["string"] = match[0]
        entry["type"] = guess_type(line)
       # entry.variables = get_variable_names(line) if entry.type == "stringWithFormat"

      end)
    end
  end
  results
end
analyse_line_for_translatedWithContext(line, index, file_path) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 51
def self.analyse_line_for_translatedWithContext(line, index, file_path)
  results = []
  line.scan(TRANSLATED_WITH_CONTEXT_REGEX).each do |match|
    results.push(Hash.new.tap do |h|
      h["file"] = file_path
      h["line_number"] = index + 1
      h["string"] = match[0]
      h["type"] = "translatedWithContext"
      h["context"] = get_context(line, h["string"])
    end)
  end
  results
end
find_api_calls(file) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 42
def self.find_api_calls(file)
  results = []
  File.readlines(file).each_with_index do |line, index|
    results.concat(analyse_line_for_dot_translated(line, index, file))
    results.concat(analyse_line_for_translatedWithContext(line, index, file))
  end
  results
end
find_strings(file) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 12
def self.find_strings(file)
  results = []
  if is_view_controller?(file)
    File.readlines(file, :encoding => "UTF-8").each_with_index do |line, index|
      line.encode!('UTF-16', :undef => :replace, :invalid => :replace, :replace => "")
      line.encode!('UTF-8')
      results.concat(analyse_line_for_strings(line,index, file))
    end
  end
  results
end
get_context(line, match) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 79
def self.get_context(line, match)
  line.match(/"#{match}" translatedWithContext:\s?@"([^"]*)"/)[1]
end
get_variable_names(line) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 91
def self.get_variable_names(line)
  line
    .scan(/stringWithFormat:\s?@"[^"]+",\s?(.*?)\][^\s*,]/)
    .first.first # Array of arrays Yo.
    .split(",")
    .map {|var| var.gsub(/\s+/, "")}
end
guess_type(line) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 83
def self.guess_type(line)
  if line.include? "stringWithFormat"
    "stringWithFormat"
  else
    "unknown"
  end
end
is_view_controller?(file) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 123
def self.is_view_controller?(file)
  !file.match(/ViewController/).nil?
end
looks_suspicious(line) click to toggle source
# File lib/terrestrial/cli/parser/objc.rb, line 99
def self.looks_suspicious(line)
  without_strings = line.gsub(STRING_REGEX, "")
  without_strings.include?("_LOG") || 
  without_strings.include?("DLog") || 
  without_strings.include?("NSLog") || 
  without_strings.include?("NSAssert") ||
  without_strings.downcase.include?(".translated") || 
  without_strings.downcase.include?("nslocalizedstring") || 
  without_strings.downcase.include?("uistoryboard") ||
  without_strings.downcase.include?("instantiateviewcontrollerwithidentifier") ||
  without_strings.downcase.include?("uiimage") ||
  without_strings.downcase.include?("nsentitydescription") ||
  without_strings.downcase.include?("nspredicate") ||
  without_strings.downcase.include?("dateformat") ||
  without_strings.downcase.include?("datefromstring") ||
  without_strings.downcase.include?("==") ||
  without_strings.downcase.include?("isequaltostring") ||
  without_strings.downcase.include?("valueforkey") ||
  without_strings.downcase.include?("cellidentifier") ||
  without_strings.downcase.include?("uifont") ||
  without_strings.downcase.include?("static ") ||
  without_strings.downcase.include?("print(")
end