Path: blob/master/lib/rubocop/cop/lint/datastore_srvhost_usage.rb
36035 views
# frozen_string_literal: true12module RuboCop3module Cop4module Lint5# Detects direct access to datastore['SRVHOST'] and recommends using the srvhost method instead.6#7# The srvhost method provides a cleaner API for accessing the SRVHOST value from the datastore.8#9# @example10# # bad11# datastore['SRVHOST']12# datastore["SRVHOST"]13#14# # good15# srvhost16class DatastoreSrvhostUsage < Base17extend AutoCorrector1819MSG = 'Use the `srvhost` method instead of directly accessing `datastore[\'SRVHOST\']`.'2021# @!method datastore_srvhost_access?(node)22def_node_matcher :datastore_srvhost_access?, <<~PATTERN23(send24(send nil? :datastore) :[]25(str {"SRVHOST"}))26PATTERN2728# Called for every method call in the code29# Checks if it's a datastore['SRVHOST'] access and registers an offense if so30# @param node [RuboCop::AST::SendNode] The method call node being checked31def on_send(node)32return unless datastore_srvhost_access?(node)3334add_offense(node, message: MSG) do |corrector|35corrector.replace(node, 'srvhost')36end37end38end39end40end41end424344