Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rubocop/cop/lint/module_enforce_notes.rb
Views: 11784
# frozen_string_literal: true12module RuboCop3module Cop4module Lint5class ModuleEnforceNotes < Base67NO_NOTES_MSG = 'Module is missing the Notes section which must include Stability, Reliability and SideEffects] - https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html'8MISSING_KEY_MSG = 'Module is missing %s from the Notes section - https://docs.metasploit.com/docs/development/developing-modules/module-metadata/definition-of-module-reliability-side-effects-and-stability.html'9REQUIRED_KEYS = %w[Stability Reliability SideEffects]1011def_node_matcher :find_update_info_node, <<~PATTERN12(def :initialize _args (begin (super $(send nil? {:update_info :merge_info} (lvar :info) (hash ...))) ...))13PATTERN1415def_node_matcher :find_nested_update_info_node, <<~PATTERN16(def :initialize _args (super $(send nil? {:update_info :merge_info} (lvar :info) (hash ...)) ...))17PATTERN1819def on_def(node)20update_info_node = find_update_info_node(node) || find_nested_update_info_node(node)21return if update_info_node.nil?2223hash = update_info_node.arguments.find { |argument| hash_arg?(argument) }24notes_present = false25last_key = nil26notes = nil27hash.each_pair do |key, value|28if key.value == 'Notes'29notes_present = true30notes = value31end32last_key = key33end3435if notes_present36check_for_required_keys(notes)37else38add_offense(last_key || hash, message: NO_NOTES_MSG)39end40end4142private4344def check_for_required_keys(notes)45last_key = nil46keys_present = []47notes.each_pair do |key, _value|48if REQUIRED_KEYS.include? key.value49keys_present << key.value50end51last_key = key52end5354missing_keys = REQUIRED_KEYS - keys_present55unless missing_keys.empty?56if missing_keys.length == 157msg = missing_keys[0]58else59msg = missing_keys[0...-1].join(', ') + ' and ' + missing_keys[-1]60end61add_offense(last_key || notes, message: MISSING_KEY_MSG % msg)62end63end6465def hash_arg?(node)66node.type == :hash67end68end69end70end71end727374