Class: Reference

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

Overview

A utility class to process tokens and analyze includes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReference

Returns a new instance of Reference.



26
27
28
29
30
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 26

def initialize
  @workflow = ReferenceWorkflow.new(self)

  reset
end

Instance Attribute Details

#referencesObject (readonly)

Returns the value of attribute references.



113
114
115
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 113

def references
  @references
end

Instance Method Details

#get_body_start(tokens) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 37

def get_body_start(tokens)
  params_started = false
  params_ended = false
  params_brackets = 0
  tokens.each_with_index do |token, index|
    params_started = true if token.type == :LPAREN && !params_started
    params_brackets += 1 if token.type == :LPAREN && params_started
    params_brackets -= 1 if token.type == :RPAREN && params_started
    params_ended = true if params_started && params_brackets.zero?
    return index + 1 if params_ended && token.type == :LBRACE
  end
  warn('No class or type body found')
end

#got_class_name_trigger(class_name) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 87

def got_class_name_trigger(class_name)
  @references.append(
    {
      type: class_name.match(INTERNAL_MODULE_REGEXP) ? REF_TYPE_ENUM[:internal] : REF_TYPE_ENUM[:component],
      name: class_name,
      token: @current_token
    }
  )
end

#got_features_end_trigger(feature_includes) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 97

def got_features_end_trigger(feature_includes)
  feature_includes.each do |include_name|
    @references.append(
      {
        type: REF_TYPE_ENUM[:feature],
        name: include_name,
        token: @current_token
      }
    )
  end
end

#got_include_name_trigger(include_name) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 77

def got_include_name_trigger(include_name)
  @references.append(
    {
      type: include_name.match(INTERNAL_MODULE_REGEXP) ? REF_TYPE_ENUM[:internal] : REF_TYPE_ENUM[:component],
      name: include_name,
      token: @current_token
    }
  )
end

#invalid_stateObject



109
110
111
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 109

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

#process(tokens) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 51

def process(tokens)
  tokens = tokens.drop(get_body_start(tokens))
  feature_includes = []
  tokens.reject { |token| %i[WHITESPACE NEWLINE INDENT].include? token.type }.each do |token|
    @current_token = token
    @workflow.got_include if token.type == :NAME && token.value == 'include' && token.next_code_token&.type == :NAME
    @workflow.got_class if token.value == 'class'
    @workflow.got_features_start if token.value == 'role::include_features'
    @workflow.got_feature if token.type == :LBRACK && @workflow.current == :awaiting_feature
    @workflow.got_feature_end if token.type == :RBRACK && @workflow.current == :awaiting_feature_include
    @workflow.got_features_end(feature_includes) if token.type == :RBRACE && @workflow.current == :got_feature_start
    next unless %i[NAME SSTRING].include?(token.type)
    next if %w[include class role::include_features].include?(token.value)

    # noinspection RubyCaseWithoutElseBlockInspection
    case @workflow.current
    when :awaiting_include_name
      @workflow.got_include_name(token.value)
    when :awaiting_class_name
      @workflow.got_class_name(token.value)
    when :awaiting_feature_include
      feature_includes.append(token.value)
    end
  end
end

#resetObject



32
33
34
35
# File 'lib/puppet-lint-module_reference-check/reference.rb', line 32

def reset
  @current_token = nil
  @references = []
end