Class: ParamComments

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeParamComments

Returns a new instance of ParamComments.



58
59
60
61
62
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 58

def initialize
  @workflow = ParamWorkflow.new(self)

  reset
end

Instance Attribute Details

#paramsObject (readonly)

The list of analyzed parameters in the comments



142
143
144
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 142

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



113
114
115
116
117
118
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 113

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



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 98

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 121

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_stateObject

Called when an invalid state transition would happen



137
138
139
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 137

def invalid_state
  raise InvalidCommentForState.new(@current_comment, @workflow.current)
end

#process(comments) ⇒ Object

Walk through every comment and transition the workflow fsm accordingly

Parameters:

  • comments

    A list of Comment tokens appearing before the class/defined type header



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 76

def process(comments) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  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

#resetObject



64
65
66
67
68
69
70
71
# File 'lib/puppet-lint-param_comment-check/param_comments.rb', line 64

def reset
  @current_param = nil
  @current_option = nil
  @in_option = false
  @params_have_started = false
  @params = []
  @workflow.restore!(:start)
end