|
2 | 2 | COMMANDS = Array['command', 'onlyif', 'unless']
|
3 | 3 | def check
|
4 | 4 | # Gather any exec commands' resources into an array
|
5 |
| - exec_resources = resource_indexes.map do |resource| |
| 5 | + exec_resources = resource_indexes.map { |resource| |
6 | 6 | resource_parameters = resource[:param_tokens].map(&:value)
|
7 | 7 | resource if resource[:type].value == 'exec' && !(COMMANDS & resource_parameters).empty?
|
| 8 | + }.compact |
| 9 | + |
| 10 | + # Get title tokens with modified puppet lint function |
| 11 | + title_tokens_list = get_title_tokens |
| 12 | + |
| 13 | + # Iterate over title tokens and raise a warning if any are variables |
| 14 | + title_tokens_list[:tokens].each do |token| |
| 15 | + if token.type == :VARIABLE |
| 16 | + notify_warning(token) |
| 17 | + end |
8 | 18 | end
|
9 | 19 |
|
10 | 20 | # Iterate over each command found in any exec
|
@@ -58,4 +68,49 @@ def parameterised?(token)
|
58 | 68 | current_token = current_token.next_token
|
59 | 69 | end
|
60 | 70 | end
|
| 71 | + |
| 72 | + # This function is a replacement for puppet_lint's title_tokens function which assumes titles have single quotes |
| 73 | + # This function adds a check for titles in double quotes where there could be interpolated variables |
| 74 | + def get_title_tokens |
| 75 | + tokens_array = [] |
| 76 | + result = {} |
| 77 | + tokens.each_index do |token_idx| |
| 78 | + if tokens[token_idx].type == :COLON |
| 79 | + # Check if title is an array |
| 80 | + if tokens[token_idx - 1].type == :RBRACK |
| 81 | + array_start_idx = tokens.rindex do |r| |
| 82 | + r.type == :LBRACK |
| 83 | + end |
| 84 | + title_array_tokens = tokens[(array_start_idx + 1)..(token_idx - 2)] |
| 85 | + tokens_array.concat(title_array_tokens.select do |token| |
| 86 | + { STRING: true, NAME: true }.include?(token.type) |
| 87 | + end) |
| 88 | + result = { |
| 89 | + tokens: tokens_array, |
| 90 | + resource_type: tokens[array_start_idx].prev_code_token.prev_code_token |
| 91 | + } |
| 92 | + # Check if title is double quotes string |
| 93 | + elsif tokens[token_idx - 1].type == :DQPOST |
| 94 | + # Find index of the start of the title |
| 95 | + title_start_idx = tokens.rindex do |r| |
| 96 | + r.type == :DQPRE |
| 97 | + end |
| 98 | + result = { |
| 99 | + # Title is tokens from :DQPRE to the index before :COLON |
| 100 | + tokens: tokens[title_start_idx..(token_idx - 1)], |
| 101 | + resource_type: tokens[title_start_idx].prev_code_token.prev_code_token |
| 102 | + } |
| 103 | + # Title is in single quotes |
| 104 | + else |
| 105 | + next_token = tokens[token_idx].next_code_token |
| 106 | + tokens_array.concat(tokens[..token_idx - 1]) unless next_token.type == :LBRACE |
| 107 | + result = { |
| 108 | + tokens: tokens_array, |
| 109 | + resource_type: tokens[token_idx - 1].prev_code_token.prev_code_token |
| 110 | + } |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + result |
| 115 | + end |
61 | 116 | end
|
0 commit comments