Class: Terrestrial::Cli::Parser::ObjC

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/terrestrial/cli/parser/objc.rb

Constant Summary collapse

LANGUAGE =
:objc
STRING_REGEX =
/@"(.*?)"/
NSLOCALIZEDSTRING_REGEX =
/NSLocalizedString\(.*@"(.*)".*\)/
DOT_TRANSLATED_REGEX =
/@"([^"]*)".translated\W/
TRANSLATED_WITH_CONTEXT_REGEX =
/@"([^"]*)"\stranslatedWithContext:/

Class Method Summary collapse

Methods inherited from BaseParser

find_nslocalizedstrings, find_string, scan_lines

Class Method Details

.analyse_line_for_dot_translated(line, index, file_path) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



42
43
44
45
46
47
48
49
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object



79
80
81
# 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) ⇒ Object



91
92
93
94
95
96
97
# 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) ⇒ Object



83
84
85
86
87
88
89
# 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) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/terrestrial/cli/parser/objc.rb', line 123

def self.is_view_controller?(file)
  !file.match(/ViewController/).nil?
end

.looks_suspicious(line) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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