class Danger::PluginLinter

Attributes

errors[RW]
json[RW]
warnings[RW]

Public Class Methods

new(json) click to toggle source
# File lib/danger/plugin_support/plugin_linter.rb, line 22
def initialize(json)
  @json = json
  @warnings = []
  @errors = []
end

Public Instance Methods

failed?() click to toggle source

Did the linter pass/fail?

# File lib/danger/plugin_support/plugin_linter.rb, line 53
def failed?
  errors.count > 0
end
lint() click to toggle source

Lints the current JSON, looking at:

  • Class rules

  • Method rules

  • Attribute rules

# File lib/danger/plugin_support/plugin_linter.rb, line 33
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
print_summary(ui) click to toggle source

Prints a summary of the errors and warnings.

Private Instance Methods

apply_rules(json, type, rules) click to toggle source

Runs the rule, if it fails then additional metadata is added to the rule (for printing later) and it’s added to either ‘warnings` or `errors`.

# File lib/danger/plugin_support/plugin_linter.rb, line 146
def apply_rules(json, type, rules)
  rules.each do |rule|
    next unless rule.function.call(json)

    rule.metadata = json
    rule.type = type

    case rule.modifier
    when :warning
      warnings << rule
    when :error
      errors << rule
    end
  end
end
class_rules() click to toggle source

Rules that apply to a class

# File lib/danger/plugin_support/plugin_linter.rb, line 93
def class_rules
  [
    Rule.new(:error, 4..6, "Description Markdown", "Above your class you need documentation that covers the scope, and the usage of your plugin.", proc do |json|
      json[:body_md] && json[:body_md].empty?
    end),
    Rule.new(:warning, 30, "Tags", "This plugin does not include `@tags tag1, tag2` and thus will be harder to find in search.", proc do |json|
      json[:tags] && json[:tags].empty?
    end),
    Rule.new(:warning, 29, "References", "Ideally, you have a reference implementation of your plugin that you can show to people, add `@see org/repo` to have the site auto link it.", proc do |json|
      json[:see] && json[:see].empty?
    end),
    Rule.new(:error, 8..27, "Examples", "You should include some examples of common use-cases for your plugin.", proc do |json|
      json[:example_code] && json[:example_code].empty?
    end)
  ]
end
method_rules() click to toggle source

Rules that apply to individual methods, and attributes

# File lib/danger/plugin_support/plugin_linter.rb, line 112
def method_rules
  [
    Rule.new(:error, 40..41, "Description", "You should include a description for your method.", proc do |json|
      json[:body_md] && json[:body_md].empty?
    end),
    Rule.new(:warning, 43..45, "Params", "You should give a 'type' for the param, yes, ruby is duck-typey but it's useful for newbies to the language, use `@param [Type] name`.", proc do |json|
      json[:param_couplets] && json[:param_couplets].flat_map(&:values).include?(nil)
    end),
    Rule.new(:warning, 43..45, "Unknown Param", "You should give a 'type' for the param, yes, ruby is duck-typey but it's useful for newbies to the language, use `@param [Type] name`.", proc do |json|
      json[:param_couplets] && json[:param_couplets].flat_map(&:values).include?("Unknown")
    end),
    Rule.new(:warning, 46, "Return Type", "If the function has no useful return value, use ` @return  [void]` - this will be ignored by documentation generators.", proc do |json|
      return_hash = json[:tags].find { |tag| tag[:name] == "return" }
      !(return_hash && return_hash[:types] && !return_hash[:types].first.empty?)
    end)
  ]
end