Class: Param

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-lint-param_comment-check/param.rb

Overview

A helper to analyze parameter comments using the ParamWorkflow fsm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParam

Returns a new instance of Param.



42
43
44
45
46
# File 'lib/puppet-lint-param_comment-check/param.rb', line 42

def initialize
  @workflow = ParamWorkflow.new(self)

  reset
end

Instance Attribute Details

#paramsObject (readonly)

The list of analyzed parameters in the comments



131
132
133
# File 'lib/puppet-lint-param_comment-check/param.rb', line 131

def params
  @params
end

Instance Method Details

#got_end_trigger(_, default_tokens) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet-lint-param_comment-check/param.rb', line 112

def got_end_trigger(_, default_tokens) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
  raise InvalidDefaultForOptional if @current_param[:param_type] == PARAM_TYPE_ENUM[:optional] &&
                                     !default_tokens.empty? &&
                                     default_tokens[0].value != 'undef'

  @current_param[:default] = @default_tokens.map(&:value).join('') unless @default_tokens.empty?
  @current_param[:param_type] = PARAM_TYPE_ENUM[:with_default] unless
      @current_param[:param_type] == PARAM_TYPE_ENUM[:optional] || default_tokens.empty?
  @params.append(@current_param)
  @current_param = EMPTY_PARAM.dup
  @in_default, @in_type = false
end

#got_name_trigger(_, token, type_tokens) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/puppet-lint-param_comment-check/param.rb', line 104

def got_name_trigger(_, token, type_tokens)
  @current_param[:type] = type_tokens.map(&:value).join('')
  if !@type_tokens.empty? && @type_tokens[0].value == 'OPTIONAL'
    @current_param[:param_type] = PARAM_TYPE_ENUM[:optional]
  end
  @current_param[:name] = token.value
end

#invalid_stateObject

Called when an invalid state transition would happen



126
127
128
# File 'lib/puppet-lint-param_comment-check/param.rb', line 126

def invalid_state
  raise InvalidTokenForState.new(@current_token, @workflow.current)
end

#process(tokens) ⇒ Object

Walk through every parameter and transition the workflow fsm accordingly

Parameters:

  • tokens

    A list of parameter tokens



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/puppet-lint-param_comment-check/param.rb', line 62

def process(tokens) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  reset
  brackets = 0
  tokens.reject { |token| %i[NEWLINE INDENT].include? token.type }.each do |token| # rubocop:disable Metrics/BlockLength
    @current_token = token
    case token.type
    when :TYPE
      if @in_default
        @default_tokens.append(token)
        next
      end
      @workflow.got_type unless @in_type
      @in_type = true
      @type_tokens.append(token)
    when :VARIABLE
      @workflow.got_name(token, @type_tokens) unless @in_default
      @in_type = false unless @in_default
      @type_tokens = [] unless @in_default
      @default_tokens.append(token) if @in_default
    when :EQUALS
      @in_default = true
    when :COMMA
      @workflow.got_end(@default_tokens) unless @in_type || brackets.positive?
      @default_tokens = [] unless @in_type && brackets.positive?
      @in_default = false unless @in_type && brackets.positive?
      @type_tokens.append(token) if @in_type
    when :LBRACE, :LBRACK
      brackets += 1
      @type_tokens.append(token) if @in_type
      @default_tokens.append(token) if @in_default
    when :RBRACE, :RBRACK
      brackets -= 1
      @type_tokens.append(token) if @in_type
      @default_tokens.append(token) if @in_default
    else
      @type_tokens.append(token) if @in_type
      @default_tokens.append(token) if @in_default
    end
  end
  @workflow.got_end(@default_tokens) unless @workflow.current == :start
end

#resetObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet-lint-param_comment-check/param.rb', line 48

def reset
  @params = []
  @in_default = false
  @default_tokens = []
  @in_type = false
  @type_tokens = []
  @current_param = EMPTY_PARAM.dup
  @current_token = nil
  @workflow.restore!(:start)
end