Class: StoryboardLint::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/storyboardlint.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sb_scanner, source_scanner, matcher) ⇒ Linter

Returns a new instance of Linter.



196
197
198
199
200
# File 'lib/storyboardlint.rb', line 196

def initialize(sb_scanner, source_scanner, matcher)
  @matcher = matcher
  @sb_scanner = sb_scanner
  @source_scanner = source_scanner
end

Class Method Details

.run!(*args) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/storyboardlint.rb', line 240

def self.run!(*args)
  options = OpenStruct.new
  
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: storyboardlint <target directory> [options]"
    opts.separator  ""
    opts.separator  "Options"

    opts.on("--storyboard-prefix [PREFIX]", "Storyboard IDs have to begin with PREFIX.") do |prefix|
      options.storyboard_prefix = prefix
    end
    
    opts.on("--storyboard-suffix [SUFFIX]", "Storyboard IDs have to end with SUFFIX") do |suffix|
      options.storyboard_suffix = suffix
    end
    
    opts.on("--segue-prefix [PREFIX]", "Segue IDs have to begin with PREFIX") do |prefix|
      options.segue_prefix = prefix
    end
    
    opts.on("--segue-suffix [SUFFIX]", "Segue IDs have to end with SUFFIX") do |suffix|
      options.segue_suffix = suffix
    end
    
    opts.on("--reuse-prefix [PREFIX]", "Reuse IDs have to begin with PREFIX") do |prefix|
      options.reuse_prefix = prefix
    end
    
    opts.on("--reuse-suffix [SUFFIX]", "Reuse IDs have to end with SUFFIX") do |suffix|
      options.reuse_suffix = suffix
    end
    
    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail("--version", "Show version") do
      puts "StoryboardLint v0.1.1"
      exit
    end
  end
  
  if ARGV.length < 1
    puts opt_parser
    exit 0
  end
  
  opt_parser.parse(args)

  matcher = StoryboardLint::Matcher.new(options)
  sb_scanner = StoryboardLint::StoryboardScanner.new(ARGV[0])
  source_scanner = StoryboardLint::SourceScanner.new(ARGV[0], matcher)

  linter = StoryboardLint::Linter.new(sb_scanner, source_scanner, matcher)
  linter.lint
  
  return 0
end

Instance Method Details

#check_custom_classesObject



220
221
222
223
224
225
226
# File 'lib/storyboardlint.rb', line 220

def check_custom_classes
  @sb_scanner.custom_class_names.each do |custom_class|
    if !@source_scanner.class_names.map {|cn| cn[:class_name]}.include?(custom_class[:class_name])
      puts "error: Custom class '#{custom_class[:class_name]}' used in #{File.basename(custom_class[:file])} could not be found in source code."    
    end
  end
end

#check_idsObject



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/storyboardlint.rb', line 228

def check_ids
  [{:method_name => :segue_ids, :name => 'Segue ID'},
   {:method_name => :storyboard_ids, :name => 'Storyboard ID'},
   {:method_name => :reuse_ids, :name => 'Reuse ID'}].each do |data|
    @source_scanner.send(data[:method_name]).each do |source_item|
      if !@sb_scanner.send(data[:method_name]).map {|sb_item| sb_item[:id]}.include?(source_item[:id])
        puts "#{source_item[:file]}:#{source_item[:line]}: warning: #{data[:name]} '#{source_item[:id]}' could not be found in any Storyboard."    
      end
    end
  end
end

#check_namingObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/storyboardlint.rb', line 208

def check_naming
  [{:items => @sb_scanner.segue_ids, :regex => @matcher.segue_id_regex_sb, :name => 'Segue ID'},
   {:items => @sb_scanner.storyboard_ids, :regex => @matcher.storyboard_id_regex_sb, :name => 'Storyboard ID'},
   {:items => @sb_scanner.reuse_ids, :regex => @matcher.reuse_id_regex_sb, :name => 'Reuse ID'}].each do |data|
    data[:items].each do |item|
      if item[:id] !~ data[:regex]
        puts "warning: #{data[:name]} '#{item[:id]}' used in #{File.basename(item[:file])} does not match '#{data[:regex]}."
      end
    end
  end
end

#lintObject



202
203
204
205
206
# File 'lib/storyboardlint.rb', line 202

def lint
 check_naming
 check_custom_classes
 check_ids
end