Class: GLRubocop::GLCops::NoHardcodedStringsInErbTextElements

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
ErbContentHelper
Defined in:
lib/gl_rubocop/gl_cops/no_hardcoded_strings_in_erb_text_elements.rb

Overview

Ensures that text-containing HTML elements in ERB files do not have literal text outside of ERB expressions. All visible text must be wrapped in <%= t() %> or a variable expression.

Good:

<p><%= t('components.message') %></p>
<span><%= @label_text %></span>
<h1><%= @title_text %></h1>
<%= content_tag(:p, t('components.message')) %>

Bad:

<p>Hello World</p>
<span>Click here</span>
<h1>Page Title</h1>

Constant Summary collapse

MSG =
'Hardcoded string in <%<tag>s> on line %<line>d. ' \
'Use an i18n expression (t()) or a variable.'
TEXT_TAGS =
%w[
  a span strong em b i p
  h1 h2 h3 h4 h5 h6
  blockquote li td th
  label button dt dd caption
].freeze

Instance Method Summary collapse

Methods included from ErbContentHelper

#erb_file?, #read_erb_file

Instance Method Details

#investigate(processed_source) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/gl_rubocop/gl_cops/no_hardcoded_strings_in_erb_text_elements.rb', line 34

def investigate(processed_source)
  return unless erb_file?

  content = read_erb_file
  return unless content

  find_and_report_hardcoded_strings(content, processed_source)
end