Class: GLRubocop::GLCops::NoHardcodedStringAssignmentToTextOrContentVariable

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

Overview

Ensures that variables and parameters named with _text, text, _content, or content suffixes are never assigned a hardcoded string literal. Create an i18n entry and use t() or I18n.t() instead.

Good:

title_text = t('components.title')
@label_text = I18n.t('components.label')
def initialize(button_text: t('components.button.default'))
title_text = content_tag(:span, t('components.title'))
@label_content = tag.p(t('components.label'), class: 'label')

Bad:

title_text = 'Hello'
@label_text = 'Click here'
def initialize(button_text: 'Submit')

Constant Summary collapse

MSG =
'`%<name>s` must not be assigned a hardcoded string. ' \
'Create an i18n entry and use t() instead.'

Instance Method Summary collapse

Instance Method Details

#on_ivasgn(node) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/gl_rubocop/gl_cops/no_hardcoded_string_assignment_to_text_or_content_variable.rb', line 32

def on_ivasgn(node)
  name, value = node.children
  return unless text_or_content_name?(name.to_s.delete_prefix('@'))
  return unless value&.str_type?

  add_offense(value, message: format(MSG, name:))
end

#on_kwoptarg(node) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/gl_rubocop/gl_cops/no_hardcoded_string_assignment_to_text_or_content_variable.rb', line 40

def on_kwoptarg(node)
  name, default = node.children
  return unless text_or_content_name?(name)
  return unless default&.str_type?

  add_offense(default, message: format(MSG, name:))
end

#on_lvasgn(node) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/gl_rubocop/gl_cops/no_hardcoded_string_assignment_to_text_or_content_variable.rb', line 24

def on_lvasgn(node)
  name, value = node.children
  return unless text_or_content_name?(name)
  return unless value&.str_type?

  add_offense(value, message: format(MSG, name:))
end