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
].freeze
DEPARTMENT_DESCRIPTIONS =
{
  'SketchupRequirements' => "    This is the most important set of checks. They represent a large\n    part of the technical requirements an extension must pass in order\n    to be hosted on Extension Warehouse.\n\n    They have been designed to prevent extensions from conflicting with\n    each other as well as avoiding bad side-effects for the end user.\n\n    Please address these as soon as possible.\n  DESCRIPTION\n  'SketchupDeprecations' => <<-DESCRIPTION,\n    This department checks for usage of deprecated features. It's\n    recommended that you migrate your code away from deprecated features\n    of the SketchUp API.\n\n    This department is not a requirement for submission to\n    Extension Warehouse.\n  DESCRIPTION\n  'SketchupPerformance' => <<-DESCRIPTION,\n    This department looks for known patterns that have noticeable\n    performance impact on SketchUp and/or your extension. It's worth\n    looking into these warnings and investigate whether performance\n    can be improved.\n\n    This department is not a requirement for submission to\n    Extension Warehouse.\n  DESCRIPTION\n  'SketchupSuggestions' => <<-DESCRIPTION,\n    This department is a collection of suggestions for best practices\n    that aim to improve the general quality of your extension. Some of\n    these might be more noisy than the rest of the cops. Disable as\n    needed after reviewing the suggestions.\n\n    This department is not a requirement for submission to\n    Extension Warehouse.\n  DESCRIPTION\n}.freeze\n",

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(categories, files, summary) ⇒ ERBContext

Returns a new instance of ERBContext.



135
136
137
138
139
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 135

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.



133
134
135
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 133

def categories
  @categories
end

#filesObject (readonly)

Returns the value of attribute files.



133
134
135
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 133

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary.



133
134
135
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 133

def summary
  @summary
end

Instance Method Details

#base64_encoded_logo_imageObject



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

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

#bindingObject

Make Kernel#binding public.



200
201
202
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 200

def binding
  super
end

#cop_anchor(cop_name) ⇒ Object



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

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

#decorated_message(offense) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 204

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



141
142
143
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 141

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

#department_description(cop_name) ⇒ Object



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

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



151
152
153
154
155
156
157
158
159
160
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 151

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



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

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

#format_plain_text(text) ⇒ Object



172
173
174
175
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 172

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



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

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



222
223
224
225
226
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 222

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

#new_department?(cop_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#sort_categories(categories) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 177

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



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

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



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

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