Top Level Namespace
Instance Method Summary collapse
-
#a_param?(token) ⇒ Boolean
Parameters to classes or defined types must be uniformly indented in two spaces from the title.
-
#check_for(character) ⇒ Object
This function is copied & modified from puppet-lint arrow_alignment check github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L8.
- #first_on_the_line?(token, type) ⇒ Boolean
-
#fix_for(problem) ⇒ Object
This function is copied & modified from puppet-lint arrow_alignment fix github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L94.
- #get_prev_code_token(token, character) ⇒ Object
- #get_the_first_param(token) ⇒ Object
- #prev_param_token(token) ⇒ Object
- #the_one?(token, character) ⇒ Boolean
Instance Method Details
#a_param?(token) ⇒ Boolean
Parameters to classes or defined types must be uniformly indented in two spaces from the title. The equals sign should be aligned.
puppet.com/docs/puppet/7/style_guide.html#style_guide_classes-param-indentation-alignment
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 6 def a_param?(token) if token&.prev_code_token&.type == :EQUALS false elsif token&.prev_code_token&.type == :FARROW false elsif %i[DQPRE DQMID].include?(token&.prev_code_token&.type) false elsif token&.type == :VARIABLE count = 0 while token&.prev_token token = token.prev_token if %i[LPAREN LBRACK LBRACE].include?(token.type) count += 1 elsif %i[RPAREN RBRACK RBRACE].include?(token.type) count -= 1 elsif %i[DEFINE CLASS].include?(token.type) break end end true if count == 1 end end |
#check_for(character) ⇒ Object
This function is copied & modified from puppet-lint arrow_alignment check github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L8
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 86 def check_for(character) # I intentionally didn't rename `arrow` to another name, to keep the code as # similar as the original one, to easier to update in the future. (class_indexes + defined_type_indexes).each do |res_idx| arrow_column = [0] level_idx = 0 level_tokens = [] param_column = [nil] resource_tokens = res_idx[:param_tokens] next if resource_tokens.nil? resource_tokens.reject! do |token| COMMENT_TYPES.include?(token.type) end # If this is a single line resource, skip it first_arrow = resource_tokens.index { |r| the_one?(r, character) } last_arrow = resource_tokens.rindex { |r| the_one?(r, character) } next if first_arrow.nil? next if last_arrow.nil? next if resource_tokens[first_arrow].line == resource_tokens[last_arrow].line resource_tokens.each do |token| if the_one?(token, character) param_token = get_prev_code_token(token, character) param_token = token if param_token.nil? param_length = param_token.to_manifest.length param_column[level_idx] = param_token.column if param_column[level_idx].nil? if (level_tokens[level_idx] ||= []).any? { |t| t.line == token.line } this_arrow_column = param_column[level_idx] + param_length + 1 elsif character == '$' && param_token.type == :VARIABLE this_arrow_column = param_token.column else this_arrow_column = param_token.column + param_token.to_manifest.length this_arrow_column += 1 if param_token.type != :INDENT end arrow_column[level_idx] = this_arrow_column if arrow_column[level_idx] < this_arrow_column (level_tokens[level_idx] ||= []) << token elsif token == resource_tokens[0] level_idx += 1 arrow_column << 0 level_tokens[level_idx] ||= [] param_column << nil elsif token == resource_tokens[-1] if (level_tokens[level_idx] ||= []).map(&:line).uniq.length > 1 level_tokens[level_idx].each do |arrow_tok| next if arrow_tok.column == arrow_column[level_idx] || level_tokens[level_idx].size == 1 arrows_on_line = level_tokens[level_idx].select { |t| t.line == arrow_tok.line } notify( :warning, message: "indentation of #{character} is not properly aligned (expected in column #{arrow_column[level_idx]}, but found it in column #{arrow_tok.column})", line: arrow_tok.line, column: arrow_tok.column, token: arrow_tok, arrow_column: arrow_column[level_idx], newline: arrows_on_line.index(arrow_tok) != 0, newline_indent: param_column[level_idx] - 1 ) end end arrow_column[level_idx] = 0 level_tokens[level_idx].clear param_column[level_idx] = nil level_idx -= 1 end end end end |
#first_on_the_line?(token, type) ⇒ Boolean
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 29 def first_on_the_line?(token, type) origin = token while token&.prev_token token = token.prev_token break if token.type == :NEWLINE end while token&.next_token token = token.next_token break if token.type == type end origin == token end |
#fix_for(problem) ⇒ Object
This function is copied & modified from puppet-lint arrow_alignment fix github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L94
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 163 def fix_for(problem) raise PuppetLint::NoFix if problem[:newline] new_ws_len = if problem[:token].prev_token.type == :WHITESPACE problem[:token].prev_token.to_manifest.length else 0 end new_ws_len += (problem[:arrow_column] - problem[:token].column) if new_ws_len.negative? raise PuppetLint::NoFix if problem[:token].prev_token.type != :INDENT new_ws = problem[:token].prev_token.to_manifest[0...new_ws_len] problem[:token].prev_token.value = new_ws else new_ws = ' ' * new_ws_len if problem[:token].prev_token.type == :WHITESPACE problem[:token].prev_token.value = new_ws else index = tokens.index(problem[:token].prev_token) tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, new_ws, 0, 0)) end end end |
#get_prev_code_token(token, character) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 67 def get_prev_code_token(token, character) case character when '=' token.prev_code_token when '$' if token.prev_code_token if %i[CLASSREF RBRACK].include?(token.prev_code_token.type) token.prev_code_token elsif token.prev_code_token.type == :LPAREN token elsif token.prev_code_token.type == :COMMA get_the_first_param(token) end end end end |
#get_the_first_param(token) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 55 def get_the_first_param(token) while token&.prev_code_token token = token.prev_code_token break if token.type == :CLASS end while token&.next_code_token token = token.next_code_token return token if token.type == :VARIABLE end end |
#prev_param_token(token) ⇒ Object
1 2 3 4 5 6 7 |
# File 'lib/puppet-lint/plugins/check_class_params_newline.rb', line 1 def prev_param_token(token) while token&.prev_code_token token = token.prev_code_token break if a_param?(token) end token end |
#the_one?(token, character) ⇒ Boolean
46 47 48 49 50 51 52 53 |
# File 'lib/puppet-lint/plugins/check_class_alignment.rb', line 46 def the_one?(token, character) case character when '=' true if token.type == :EQUALS && first_on_the_line?(token, :EQUALS) when '$' true if a_param?(token) && first_on_the_line?(token, :VARIABLE) end end |