Class: RuboCop::Formatter::ExtensionReviewFormatter::ERBContext

Inherits:
Object
  • Object
show all
Includes:
PathUtil, TextUtil
Defined in:
lib/rubocop/sketchup/formatter/extension_review.rb

Overview

This class provides helper methods used in the ERB template.

Constant Summary collapse

SEVERITY_COLORS =
{
  refactor:   Color.new(0x29, 0x6B, 0xF0, 1.0),
  convention: Color.new(0x29, 0x6B, 0xF0, 1.0),
  warning:    Color.new(0xED, 0x9C, 0x28, 1.0),
  error:      Color.new(0xD2, 0x32, 0x2D, 1.0),
  fatal:      Color.new(0xD2, 0x32, 0x2D, 1.0),
}.freeze
LOGO_IMAGE_PATH =
File.expand_path('../../../../assets/logo.png', __dir__)
SORT_ORDER =
%w[
  SketchupRequirements
  SketchupDeprecations
  SketchupPerformance
  SketchupSuggestions
  SketchupBugs
].freeze
DEPARTMENT_DESCRIPTIONS =
{
  'SketchupRequirements' => <<-DESCRIPTION,
  'SketchupDeprecations' => <<-DESCRIPTION,
  'SketchupPerformance' => <<-DESCRIPTION,
  'SketchupSuggestions' => <<-DESCRIPTION,
  'SketchupBugs' => <<-DESCRIPTION,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(categories, files, summary) ⇒ ERBContext

Returns a new instance of ERBContext.



145
146
147
148
149
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 145

def initialize(categories, files, summary)
  @categories = sort_categories(categories)
  @files = files.sort
  @summary = summary
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



143
144
145
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 143

def categories
  @categories
end

#filesObject (readonly)

Returns the value of attribute files.



143
144
145
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 143

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary.



143
144
145
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 143

def summary
  @summary
end

Instance Method Details

#base64_encoded_logo_imageObject



262
263
264
265
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 262

def base64_encoded_logo_image
  image = File.read(LOGO_IMAGE_PATH, binmode: true)
  Base64.encode64(image)
end

#bindingObject

Make Kernel#binding public.



210
211
212
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 210

def binding # rubocop:disable Lint/UselessMethodDefinition
  super
end

#cop_anchor(cop_name) ⇒ Object



252
253
254
255
256
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 252

def cop_anchor(cop_name)
  title = cop_name.downcase
  title.tr!('/', '_')
  "offense_#{title}"
end

#decorated_message(offense) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 214

def decorated_message(offense)
  offense.message
         .gsub(/`(.+?)`/) do
           "<code>#{Regexp.last_match(1)}</code>"
         end
         .gsub(/\((http[^ ]+)\)/) do
           url = Regexp.last_match(1)
           "<br><a href=\"#{url}\">#{url}</a>"
         end
end

#department(cop_name) ⇒ Object



151
152
153
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 151

def department(cop_name)
  cop_name.split('/').first
end

#department_description(cop_name) ⇒ Object



155
156
157
158
159
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 155

def department_description(cop_name)
  dep = department(cop_name)
  text = DEPARTMENT_DESCRIPTIONS[dep] || 'MISSING DESCRIPTION'
  format_plain_text(text)
end

#department_offense_count(cop_name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 161

def department_offense_count(cop_name)
  dep = department(cop_name)
  count = 0
  categories.each { |category, offenses|
    next unless department(category) == dep

    count += offenses.size
  }
  count
end

#escape(string) ⇒ Object



258
259
260
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 258

def escape(string)
  CGI.escapeHTML(string)
end

#format_plain_text(text) ⇒ Object



182
183
184
185
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 182

def format_plain_text(text)
  paragraphs = text.split(/(\n\r|\r\n|\r|\n){2,}/m)
  "<p>#{paragraphs.join('</p><p>')}</p>"
end

#highlighted_source_line(offense) ⇒ Object



225
226
227
228
229
230
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 225

def highlighted_source_line(offense)
  source_before_highlight(offense) +
    hightlight_source_tag(offense) +
    source_after_highlight(offense) +
    possible_ellipses(offense.location)
end

#hightlight_source_tag(offense) ⇒ Object



232
233
234
235
236
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 232

def hightlight_source_tag(offense)
  "<span class=\"highlight #{offense.severity}\">" \
    "#{escape(offense.highlighted_area.source)}" \
    '</span>'
end

#new_department?(cop_name) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 172

def new_department?(cop_name)
  @processed_departments ||= Set.new
  dep = department(cop_name)
  unless @processed_departments.include?(dep)
    @processed_departments << dep
    return true
  end
  false
end

#possible_ellipses(location) ⇒ Object



248
249
250
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 248

def possible_ellipses(location)
  location.first_line == location.last_line ? '' : " #{ELLIPSES}"
end

#sort_categories(categories) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 187

def sort_categories(categories)
  categories.sort { |a, b|
    # First sort departments by custom ordering (of importance).
    # Then sort by cop name.
    a_department, a_name = a[0].split('/')
    b_department, b_name = b[0].split('/')
    # Sort SketchUp cops at the top, then all the rest comes after.
    # First sorting by department.
    sort_order_a = SORT_ORDER.index(a_department)
    sort_order_b = SORT_ORDER.index(b_department)
    if sort_order_a.nil? && sort_order_b.nil?
      n = a_department <=> b_department
    else
      sort_order_a ||= SORT_ORDER.size
      sort_order_b ||= SORT_ORDER.size
      n = sort_order_a <=> sort_order_b
    end
    # Them sort by name if departments match.
    n == 0 ? a_name <=> b_name : n
  }.to_h
end

#source_after_highlight(offense) ⇒ Object



243
244
245
246
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 243

def source_after_highlight(offense)
  source_line = offense.location.source_line
  escape(source_line[offense.highlighted_area.end_pos..-1])
end

#source_before_highlight(offense) ⇒ Object



238
239
240
241
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 238

def source_before_highlight(offense)
  source_line = offense.location.source_line
  escape(source_line[0...offense.highlighted_area.begin_pos])
end