Class: PuppetLint::Lexer::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-lint/plugins/check_wmf_styleguide.rb

Overview

Add some utility functions to the PuppetLint::Lexer::Token class

Instance Method Summary collapse

Instance Method Details

#class_include?Boolean

Returns:

  • (Boolean)


168
169
170
171
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 168

def class_include?
  # Check for include-like objects
  @type == :NAME && ['include', 'require', 'contain'].include?(@value) && @next_code_token.type != :FARROW
end

#declared_classObject



180
181
182
183
184
185
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 180

def declared_class
  return unless @type == :CLASS
  # In a class declaration, the first token is the class declaration itself.
  return if @next_code_token.type != :LBRACE
  @next_code_token.next_code_token
end

#declared_type?Boolean

Returns:

  • (Boolean)


187
188
189
190
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 187

def declared_type?
  # The token is a name and the next token is a {, while the previous one is not "class"
  @type == :NAME && @next_code_token.type == :LBRACE && @prev_code_token.type != :CLASS
end

#function?Boolean

Extend the basic token with utility functions

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 148

def function?
  # A function is something that has a name and is followed by a left parens
  [:NAME, :FUNCTION_NAME].include?(@type) && @next_code_token.type == :LPAREN
end

#included_classObject



173
174
175
176
177
178
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 173

def included_class
  # Fetch the token describing the included class
  return unless class_include?
  return @next_code_token.next_code_token if @next_code_token.type == :LPAREN
  @next_code_token
end

#legacy_hiera?Boolean

Returns:

  • (Boolean)


153
154
155
156
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 153

def legacy_hiera?
  # Using old hiera call
  function? && ['hiera', 'hiera_array', 'hiera_hash'].include?(@value)
end

#legacy_validate?Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 163

def legacy_validate?
  # A function calling one of the legacy stdlib validate functions
  function? && @value.start_with?('validate_')
end

#lookup?Boolean

Returns:

  • (Boolean)


158
159
160
161
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 158

def lookup?
  # A function call specifically calling lookup
  function? && ['lookup'].include?(@value)
end

#node_def?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 192

def node_def?
  [:SSTRING, :STRING, :NAME, :REGEX, :DEFAULT].include?(@type)
end

#role_keyword?Boolean

Returns:

  • (Boolean)


196
197
198
199
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 196

def role_keyword?
  # This is a function with name "role"
  @type == :NAME && @value = 'role' && @next_code_token.type == :LPAREN
end