Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rubocop/cop/lint/datastore_srvhost_usage.rb
36035 views
1
# frozen_string_literal: true
2
3
module RuboCop
4
module Cop
5
module Lint
6
# Detects direct access to datastore['SRVHOST'] and recommends using the srvhost method instead.
7
#
8
# The srvhost method provides a cleaner API for accessing the SRVHOST value from the datastore.
9
#
10
# @example
11
# # bad
12
# datastore['SRVHOST']
13
# datastore["SRVHOST"]
14
#
15
# # good
16
# srvhost
17
class DatastoreSrvhostUsage < Base
18
extend AutoCorrector
19
20
MSG = 'Use the `srvhost` method instead of directly accessing `datastore[\'SRVHOST\']`.'
21
22
# @!method datastore_srvhost_access?(node)
23
def_node_matcher :datastore_srvhost_access?, <<~PATTERN
24
(send
25
(send nil? :datastore) :[]
26
(str {"SRVHOST"}))
27
PATTERN
28
29
# Called for every method call in the code
30
# Checks if it's a datastore['SRVHOST'] access and registers an offense if so
31
# @param node [RuboCop::AST::SendNode] The method call node being checked
32
def on_send(node)
33
return unless datastore_srvhost_access?(node)
34
35
add_offense(node, message: MSG) do |corrector|
36
corrector.replace(node, 'srvhost')
37
end
38
end
39
end
40
end
41
end
42
end
43
44