Class: Danger::PluginLinter

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/plugin_support/plugin_linter.rb

Defined Under Namespace

Classes: Rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ PluginLinter

Returns a new instance of PluginLinter.



24
25
26
27
28
# File 'lib/danger/plugin_support/plugin_linter.rb', line 24

def initialize(json)
  @json = json
  @warnings = []
  @errors = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



22
23
24
# File 'lib/danger/plugin_support/plugin_linter.rb', line 22

def errors
  @errors
end

#jsonObject

Returns the value of attribute json.



22
23
24
# File 'lib/danger/plugin_support/plugin_linter.rb', line 22

def json
  @json
end

#warningsObject

Returns the value of attribute warnings.



22
23
24
# File 'lib/danger/plugin_support/plugin_linter.rb', line 22

def warnings
  @warnings
end

Instance Method Details

#failed?Boolean

Did the linter pass/fail?

Returns:

  • (Boolean)


55
56
57
# File 'lib/danger/plugin_support/plugin_linter.rb', line 55

def failed?
  errors.count > 0
end

#lintObject

Lints the current JSON, looking at:

  • Class rules

  • Method rules

  • Attribute rules



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/danger/plugin_support/plugin_linter.rb', line 35

def lint
  json.each do |plugin|
    apply_rules(plugin, "class", class_rules)

    plugin[:methods].each do |method|
      apply_rules(method, "method", method_rules)
    end

    plugin[:attributes].each do |method_hash|
      method_name = method_hash.keys.first
      method = method_hash[method_name]

      value = method[:write] || method[:read]
      apply_rules(value, "attribute", method_rules)
    end
  end
end

Prints a summary of the errors and warnings.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/danger/plugin_support/plugin_linter.rb', line 61

def print_summary(ui)
  # Print whether it passed/failed at the top
  if failed?
    ui.puts "\n[!] Failed\n".red
  else
    ui.notice "Passed"
  end

  # A generic proc to handle the similarities between
  # errors and warnings.
  do_rules = proc do |name, rules|
    unless rules.empty?
      ui.puts ""
      ui.section(name.bold) do
        rules.each do |rule|
          title = rule.title.bold + " - #{rule.object_applied_to}"
          subtitles = [rule.description, link(rule.ref)]
          subtitles += [rule.[:files].join(":")] if rule.[:files]
          ui.labeled(title, subtitles)
          ui.puts ""
        end
      end
    end
  end

  # Run the rules
  do_rules.call("Errors".red, errors)
  do_rules.call("Warnings".yellow, warnings)
end