Class: ParamComments
- Inherits:
-
Object
- Object
- ParamComments
- Defined in:
- lib/puppet-lint-param_comment-check/param_comments.rb
Overview
A helper to analyze parameter comments using the ParamWorkflow fsm
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
The list of analyzed parameters in the comments.
Instance Method Summary collapse
-
#got_description_trigger(_, comment) ⇒ Object
Called before either the got_description or get_option_description event.
-
#got_header_trigger(_, comment) ⇒ Object
Called before the got_header event.
-
#got_option_header_trigger(_, comment) ⇒ Object
Called before the got_option_header event.
-
#initialize ⇒ ParamComments
constructor
A new instance of ParamComments.
-
#invalid_state ⇒ Object
Called when an invalid state transition would happen.
-
#process(comments) ⇒ Object
Walk through every comment and transition the workflow fsm accordingly.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ ParamComments
Returns a new instance of ParamComments.
60 61 62 63 64 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 60 def initialize @workflow = ParamCommentsWorkflow.new(self) reset end |
Instance Attribute Details
#params ⇒ Object (readonly)
The list of analyzed parameters in the comments
144 145 146 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 144 def params @params end |
Instance Method Details
#got_description_trigger(_, comment) ⇒ Object
Called before either the got_description or get_option_description event. Add a description to the current parameter or hash option
115 116 117 118 119 120 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 115 def got_description_trigger(_, comment) return unless @params_have_started @current_option[:description] += comment.value.strip if @in_option @current_param[:description] += comment.value.strip unless @in_option end |
#got_header_trigger(_, comment) ⇒ Object
Called before the got_header event. Interpret the parameter header comment
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 100 def got_header_trigger(_, comment) # rubocop:disable Metrics/AbcSize @params_have_started = true @current_param[:options].append(@current_option) if @in_option && !@current_option.nil? @params.append(@current_param) unless @current_param.nil? @current_param = EMPTY_PARAM_COMMENT.dup @current_option = nil @in_option = false comment.value.strip.match(REGEXP_PARAM_HEADER) do |match| @current_param[:name] = match.named_captures['name'].strip @current_param[:line] = comment.line end end |
#got_option_header_trigger(_, comment) ⇒ Object
Called before the got_option_header event. Interpret a hash option comment
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 123 def got_option_header_trigger(_, comment) # rubocop:disable Metrics/AbcSize return unless @params_have_started @current_param[:options].append(@current_option) if @in_option && !@current_option.nil? @in_option = true @current_option = EMPTY_OPTION_COMMENT.dup comment.value.strip.match(REGEXP_OPTION_HEADER) do |match| raise OptionDoesntMatchHash, comment unless match.named_captures['hash_name'] == @current_param[:name] @current_option[:name] = match.named_captures['name'] @current_option[:type] = match.named_captures['type'] @current_param[:line] = comment.line end end |
#invalid_state ⇒ Object
Called when an invalid state transition would happen
139 140 141 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 139 def invalid_state raise InvalidCommentForState.new(@current_comment, @workflow.current) end |
#process(comments) ⇒ Object
Walk through every comment and transition the workflow fsm accordingly
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 78 def process(comments) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/AbcSize reset @current_comment = PuppetLint::Lexer::Token.new(:COMMENT, '', 1, 1) comments.each do |comment| @current_comment = comment # noinspection RubyCaseWithoutElseBlockInspection case comment.value when /@param/ # A parameter comment header @workflow.got_header(comment) when /@option/ # A hash option @workflow.got_option_header(comment) if @params_have_started when /^\s*$/ # An empty or whitespace-only comment, thus interpreted as a separator @workflow.got_separator(comment) if @params_have_started when / {2}[^ ]+/ # A description. Either for the parameter or a hash option @workflow.got_description(comment) if @params_have_started && !@in_option @workflow.got_option_description(comment) if @params_have_started && @in_option end end @params.append(@current_param) unless @current_param.nil? end |
#reset ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 66 def reset @current_param = nil @current_option = nil @in_option = false @params_have_started = false @params = [] @workflow.restore!(:start) end |