Module: Gitlab::Styles::Common::BannedConstants

Included in:
Rubocop::Cop::Fips::MD5, Rubocop::Cop::Fips::OpenSSL, Rubocop::Cop::Fips::SHA1
Defined in:
lib/gitlab/styles/common/banned_constants.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#autocorrectObject (readonly)

Returns the value of attribute autocorrect.



7
8
9
# File 'lib/gitlab/styles/common/banned_constants.rb', line 7

def autocorrect
  @autocorrect
end

#message_templateObject (readonly)

Returns the value of attribute message_template.



7
8
9
# File 'lib/gitlab/styles/common/banned_constants.rb', line 7

def message_template
  @message_template
end

#replacementsObject (readonly)

Returns the value of attribute replacements.



7
8
9
# File 'lib/gitlab/styles/common/banned_constants.rb', line 7

def replacements
  @replacements
end

Instance Method Details

#on_const(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/styles/common/banned_constants.rb', line 9

def on_const(node)
  constant = node.source.delete_prefix('::')

  return unless replacements.key?(constant)

  replacement = replacements.fetch(constant)
  message = format(message_template, { replacement: replacement })

  add_offense(node, message: message) do |corrector|
    next unless autocorrect

    replacement = "::#{replacement}" if node.source.start_with?("::")

    corrector.replace(node, replacement)
  end
end