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(0xED, 0x9C, 0x28, 1.0),
  convention: Color.new(0xED, 0x9C, 0x28, 1.0),
  warning:    Color.new(0x96, 0x28, 0xEF, 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', __FILE__)
SORT_ORDER =
%w[
  SketchupRequirements
  SketchupDeprecations
  SketchupPerformance
  SketchupSuggestions
]
DEPARTMENT_DESCRIPTIONS =
{
  'SketchupRequirements' => <<-DESCRIPTION,
  'SketchupDeprecations' => <<-DESCRIPTION,
  'SketchupPerformance' => <<-DESCRIPTION,
  'SketchupSuggestions' => <<-DESCRIPTION,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(categories, files, summary) ⇒ ERBContext

Returns a new instance of ERBContext.



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

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.



130
131
132
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 130

def categories
  @categories
end

#filesObject (readonly)

Returns the value of attribute files.



130
131
132
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 130

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary.



130
131
132
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 130

def summary
  @summary
end

Instance Method Details

#base64_encoded_logo_imageObject



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

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

#bindingObject

Make Kernel#binding public.



185
186
187
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 185

def binding
  super
end

#cop_anchor(cop_name) ⇒ Object



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

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

#decorated_message(offense) ⇒ Object



189
190
191
192
193
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 189

def decorated_message(offense)
  offense.message.gsub(/`(.+?)`/) do
    "<code>#{Regexp.last_match(1)}</code>"
  end
end

#department(cop_name) ⇒ Object



138
139
140
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 138

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

#department_description(cop_name) ⇒ Object



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

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



148
149
150
151
152
153
154
155
156
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 148

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(s) ⇒ Object



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

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

#format_plain_text(text) ⇒ Object



168
169
170
171
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 168

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



195
196
197
198
199
200
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 195

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



202
203
204
205
206
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 202

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

#new_department?(cop_name) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 158

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



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

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

#sort_categories(categories) ⇒ Object



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

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('/')
    n = SORT_ORDER.index(a_department) <=> SORT_ORDER.index(b_department)
    n == 0 ? a_name <=> b_name : n
  }.to_h
end

#source_after_highlight(offense) ⇒ Object



213
214
215
216
# File 'lib/rubocop/sketchup/formatter/extension_review.rb', line 213

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



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

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