Class: Danger::DangerDartLinter

Inherits:
Plugin
  • Object
show all
Defined in:
lib/dart_linter/plugin.rb

Constant Summary collapse

TABLE_OUTPUT_FORMAT_TITLES =
{
  "S" => "Level",
  "F" => "File",
  "L" => "Line",
  "R" => "Rule",
  "D" => "Description",
  "P" => "Path"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#only_modified_filesObject

Lint only modified files (useful for legacy projects)

If you're dealing with a legacy project, with tons of warnings, you may want to lint only new/modified files.

Default: false



11
12
13
# File 'lib/dart_linter/plugin.rb', line 11

def only_modified_files
  @only_modified_files
end

#report_pathObject

Path to the Dart analysis output file You should set output from dart analyze here



15
16
17
# File 'lib/dart_linter/plugin.rb', line 15

def report_path
  @report_path
end

#table_output_formatObject

Table output format

Select the table output format:

  • [S]everity: info, warning, error
  • [F]ile: file path
  • [P]ath: violation file path with line
  • [L]ine: line number
  • [R]ule: lint rule
  • [D]escription: lint description

Default: "SFLR"



36
37
38
# File 'lib/dart_linter/plugin.rb', line 36

def table_output_format
  @table_output_format
end

#table_titleObject

Custom table title

Change title for the markdown table. Use {{violations.legth}} to dynamically insert the total issue count

Default: "Dart Analyze found Danger::DangerDartLinter.{violations{violations.length} issues"



23
24
25
# File 'lib/dart_linter/plugin.rb', line 23

def table_title
  @table_title
end

Instance Method Details

#filtered_violations(violations) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/dart_linter/plugin.rb', line 135

def filtered_violations(violations)
  return violations unless only_modified_files

  target_files = (git.modified_files - git.deleted_files) + git.added_files
  filtered_violations = violations.select { |violation| target_files.include? violation.file }
  return filtered_violations
end

#get_table_title(violations) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/dart_linter/plugin.rb', line 74

def get_table_title(violations)
  if table_title.to_s.strip.empty?
    "Dart Analyze found #{violations.length} issues"
  else
    table_title.gsub("{{violations.length}}", violations.length.to_s)
  end
end

#level_icon(level) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/dart_linter/plugin.rb', line 110

def level_icon(level)
  case level
  when "info" then "ℹ️"
  when "warning" then "⚠️"
  when "error" then ""
  else ""
  end
end

#lint(inline_mode: false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/dart_linter/plugin.rb', line 47

def lint(inline_mode: false)
  @table_output_format = table_output_format.to_s.strip.empty? ? "SFLR" : table_output_format
  if !report_path.nil? && File.exist?(report_path)
    report = File.open(report_path)
    violations = DartAnalyzeParser.violations(report)
    lint_mode(inline_mode: inline_mode, violations: violations)
  else
    fail("Could not run lint without setting report path or report file doesn't exists")
  end
end

#lint_mode(inline_mode:, violations:) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/dart_linter/plugin.rb', line 58

def lint_mode(inline_mode:, violations:)
  if inline_mode
    send_inline_comments(violations)
  else
    markdown(summary_table(violations))
  end
end

#markdown_table(violations) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dart_linter/plugin.rb', line 98

def markdown_table(violations)
  output_format = parse_output_format(table_output_format)
  headers = output_format.map { |char| TABLE_OUTPUT_FORMAT_TITLES[char] }.join(" | ")
  separators = output_format.map { |char| "-" * TABLE_OUTPUT_FORMAT_TITLES[char].length }.join(" | ")

  table = "### #{get_table_title(violations)} ❌\n\n"
  table << "| #{headers} |\n"
  table << "| #{separators} |\n"

  return violations.reduce(table) { |acc, violation| acc << table_row(violation, output_format) }
end

#parse_output_format(str_format) ⇒ Object



92
93
94
95
96
# File 'lib/dart_linter/plugin.rb', line 92

def parse_output_format(str_format)
  allowed_options = ["S", "F", "L", "R", "D", "P"]
  str_format = str_format.to_s.strip.upcase
  str_format.chars.uniq.select { |char| allowed_options.include?(char) }
end

#send_inline_comments(violations) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/dart_linter/plugin.rb', line 66

def send_inline_comments(violations)
  filtered_violations = filtered_violations(violations)

  filtered_violations.each do |violation|
    send("warn", violation.description, file: violation.file, line: violation.line)
  end
end

#summary_table(violations) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/dart_linter/plugin.rb', line 82

def summary_table(violations)
  filtered_violations = filtered_violations(violations)

  if filtered_violations.empty?
    return "### #{get_table_title(violations)}"
  else
    return markdown_table(filtered_violations)
  end
end

#table_row(violation, table_output_format) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dart_linter/plugin.rb', line 119

def table_row(violation, table_output_format)
  cells = table_output_format.map do |char|
    case char
    when "S" then level_icon(violation.level)
    when "F" then "`#{violation.file}`"
    when "P" then "`#{violation.file}:#{violation.line}`"
    when "L" then violation.line
    when "R" then violation.rule
    when "D" then violation.description
    end
  end
  Struct.new(:level, :rule, :description, :file, :line)

  "| #{cells.join(' | ')} |\n"
end