class Object
Constants
- FORMATTING_TOKENS
- NAME_INDEX
- OPTINAL_CONTENT
- TYPE_SEQUENCE_END
- TYPE_SEQUENCE_START
- VALUE_SEQUENCE
Public Instance Methods
check()
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 36 def check if_indexes.each do |cond| next if check_if_else(cond) next if check_if(cond) notify :warning, message: 'ensure_packages should be used', line: cond[:tokens].first.line, column: cond[:tokens].first.column end end
check_if(cond)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 57 def check_if(cond) tokens = filter_code_tokens(cond[:tokens]) # Test start of patterns return true unless match_tokens(tokens, TYPE_SEQUENCE_START, VALUE_SEQUENCE) # Test end of pattern return true unless match_tokens(tokens.last(2), TYPE_SEQUENCE_END, {}) tokens = tokens.slice(Range.new(TYPE_SEQUENCE_START.size, -TYPE_SEQUENCE_END.size - 1)) return false if tokens.empty? return true unless OPTINAL_CONTENT.index do |c| next unless tokens.length == c[:sequence].length match_tokens(tokens, c[:sequence], c[:values]) end false end
check_if_else(cond)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 48 def check_if_else(cond) tokens = filter_code_tokens(cond[:tokens]) return true if tokens.last.next_code_token && [:ELSE, :ELSIF].include?(tokens.last.next_code_token.type) false end
ensure_packages_tokens(name)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 144 def ensure_packages_tokens(name) [ PuppetLint::Lexer::Token.new(:NAME, 'ensure_packages', nil, nil), PuppetLint::Lexer::Token.new(:LPAREN, '(', nil, nil), PuppetLint::Lexer::Token.new(:LBRACK, '[', nil, nil), PuppetLint::Lexer::Token.new(:SSTRING, name, nil, nil), PuppetLint::Lexer::Token.new(:RBRACK, ']', nil, nil), PuppetLint::Lexer::Token.new(:RPAREN, ')', nil, nil) ] end
filter_code_tokens(tokens)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 167 def filter_code_tokens(tokens) tokens.delete_if { |token| FORMATTING_TOKENS.key? token.type } end
filter_formating_tokens(tokens)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 171 def filter_formating_tokens(tokens) tokens.select { |token| !FORMATTING_TOKENS.key? token.type } end
find_function(name)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 122 def find_function(name) PuppetLint::Data.function_indexes.keep_if do |func| func[:tokens].first.type == :NAME && func[:tokens].first.value == name end end
fix(problem)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 84 def fix(problem) cond = if_indexes.select do |c| c[:tokens].first.line == problem[:line] && c[:tokens].first.column == problem[:column] end.first package_name = filter_code_tokens(cond[:tokens])[NAME_INDEX].value remove_tokens(cond[:start], cond[:end]) new_tokens = ensure_packages_tokens(package_name) insert_tokens(cond[:start], new_tokens) PuppetLint::Data.tokens = tokens merge_if_possible(cond[:start]) end
fix_linked_list()
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 155 def fix_linked_list tokens.each_cons(2) do |a, b| a.next_token = b b.prev_token = a end filter_formating_tokens(tokens).each_cons(2) do |a, b| a.next_code_token = b b.prev_code_token = a end end
if_indexes()
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 175 def if_indexes PuppetLint::Data.definition_indexes(:IF) end
insert_tokens(idx, new_tokens)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 139 def insert_tokens(idx, new_tokens) tokens.insert(idx, *new_tokens) fix_linked_list end
match_tokens(tokens, type, value)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 79 def match_tokens(tokens, type, value) tokens.first(type.size).map(&:type) == type && value.values == value.keys.map { |i| tokens[i].value } end
merge_if_possible(idx)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 103 def merge_if_possible(idx) target = find_function('ensure_packages').keep_if do |func| func[:tokens].last.next_code_token == tokens[idx] && func[:tokens].last.next_code_token != func[:tokens].first end return if target.empty? # Count non :SSTRING, :COMMA tokens to ensure there are no parameters return unless 5 == filter_code_tokens(target.first[:tokens]).count do |t| ![:SSTRING, :COMMA].include?(t.type) end start_idx = tokens.first(idx).rindex { |t| t.type == :SSTRING } + 1 remove_tokens(start_idx, idx + 2) insert_tokens(start_idx, [PuppetLint::Lexer::Token.new(:COMMA, ',', 0, 0)]) end
remove_tokens(from, to)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 133 def remove_tokens(from, to) num = to - from + 1 tokens.slice!(from, num) fix_linked_list end
tokens_idx(obj)
click to toggle source
# File lib/puppet-lint/plugins/check_use_ensure_packages.rb, line 129 def tokens_idx(obj) tokens.index(obj) end