1+ # frozen_string_literal: true
2+
13# Public: Check the manifest tokens for correct indent levels and
24# record a warning for each instance found.
35
@@ -50,12 +52,12 @@ def check
5052 # indent for open groups in the previous line
5153 open_groups = 0
5254 prev_token = token . prev_token
53- while !prev_token . nil? and prev_token . type != :NEWLINE
55+ while !prev_token . nil? && ( prev_token . type != :NEWLINE )
5456 temp_indent += 1 if prev_token . type == :HEREDOC_OPEN
5557 if %i[ LBRACE LBRACK LPAREN ] . include? ( prev_token . type )
5658 if matches [ prev_token ] . nil?
5759 open_groups += 1
58- elsif matches [ prev_token ] . line > prev_token . line and !matched_open_lines . include? ( matches [ prev_token ] . line )
60+ elsif ( matches [ prev_token ] . line > prev_token . line ) && !matched_open_lines . include? ( matches [ prev_token ] . line )
5961 # increase indent for left braces not matched in the same line or in a line we have already matched a brace to
6062 open_groups += 1
6163 matched_open_lines << matches [ prev_token ] . line
@@ -67,17 +69,15 @@ def check
6769
6870 # reset prev_token to last non-whitespace token on previous line
6971 prev_token = token . prev_token
70- while !prev_token . nil? and ( prev_token . type == :WHITESPACE or prev_token . type == :COMMENT )
71- prev_token = prev_token . prev_token
72- end
72+ prev_token = prev_token . prev_token while !prev_token . nil? && ( ( prev_token . type == :WHITESPACE ) || ( prev_token . type == :COMMENT ) )
7373
7474 # get type if available
75- prev_type = prev_token . nil? ? nil : prev_token . type
75+ prev_type = prev_token & .type
7676
7777 # handle change in indent based on last token
7878 case prev_type
7979 when :COLON
80- if open_groups == 0
80+ if open_groups . zero?
8181 if colon_indent . nil?
8282 # only indent for a colon when you haven't indented yet
8383 colon_indent = prev_token . line
@@ -99,23 +99,23 @@ def check
9999 # unindent for closing brackets in the current line
100100 next_token = token . next_token
101101 matched_close_lines = [ ]
102- while !next_token . nil? and next_token . type != :NEWLINE
102+ while !next_token . nil? && ( next_token . type != :NEWLINE )
103103 if %i[ RBRACE RBRACK RPAREN ] . include? ( next_token . type )
104- if !matches [ next_token ] . nil? and
105- matches [ next_token ] . line < next_token . line and
104+ if !matches [ next_token ] . nil? &&
105+ ( matches [ next_token ] . line < next_token . line ) &&
106106 !matched_close_lines . include? ( matches [ next_token ] . line )
107107 # right braces matched in a previous line decrease indent unless we already matched a brace in the same line
108108 indent -= 1
109109 matched_close_lines << matches [ next_token ] . line
110110 end
111- if next_token . type == :RBRACE and !colon_indent . nil? && ( !matches [ next_token ] . nil? and matches [ next_token ] . line < colon_indent )
111+ if ( next_token . type == :RBRACE ) && !colon_indent . nil? && !matches [ next_token ] . nil? && ( matches [ next_token ] . line < colon_indent )
112112 # unindent at the end of resources if needed
113113 indent -= 1
114114 colon_indent = nil
115115 end
116- elsif next_token . type == :SEMIC and !colon_indent . nil? and
117- %i[ INDENT NEWLINE ] . include? ( next_token . prev_token . type ) and
118- ( next_token . next_token . nil? or next_token . next_token . type == :NEWLINE )
116+ elsif ( next_token . type == :SEMIC ) && !colon_indent . nil? &&
117+ %i[ INDENT NEWLINE ] . include? ( next_token . prev_token . type ) &&
118+ ( next_token . next_token . nil? || ( next_token . next_token . type == :NEWLINE ) )
119119 # For a lone semicolon within a block decrement immediately. Use temp_indent because
120120 # indent will be decremented in the next line by the prev_token logic above.
121121 temp_indent -= 1
@@ -124,7 +124,7 @@ def check
124124 end
125125
126126 # obviously we have a problem
127- if indent < 0
127+ if indent . negative?
128128 notify :error , {
129129 message : 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues' ,
130130 line : token . next_token . line ,
@@ -151,7 +151,7 @@ def check
151151 # For interpolated heredocs the pipe whitespace is in the HEREDOC_POST token so we need scan forward
152152 # to this and get its length.
153153 next_token = token . next_token
154- while !next_token . nil? and next_token . type != :NEWLINE and next_token . type != :HEREDOC_POST
154+ while !next_token . nil? && ( next_token . type != :NEWLINE ) && ( next_token . type != :HEREDOC_POST )
155155 next_token = next_token . next_token
156156 end
157157 if next_token . type == :HEREDOC_POST
@@ -195,7 +195,7 @@ def fix(problem)
195195 change = problem [ :indent ] - problem [ :actual ]
196196 indent_heredoc ( problem [ :token ] , change )
197197 next_token = problem [ :token ] . next_token
198- while !next_token . nil? and next_token . type != :HEREDOC_POST
198+ while !next_token . nil? && ( next_token . type != :HEREDOC_POST )
199199 indent_heredoc ( next_token , change ) if next_token . type == :HEREDOC_MID
200200 next_token = next_token . next_token
201201 end
@@ -211,10 +211,10 @@ def fix(problem)
211211 def map_heredoc_lines ( value , change , skip )
212212 char_for_indent = ' '
213213 value . split ( "\n " ) . map! do |line |
214- if skip or line . empty?
214+ if skip || line . empty?
215215 skip = false
216216 line
217- elsif change < 0
217+ elsif change . negative?
218218 line [ -change ..]
219219 else
220220 ( char_for_indent * change ) + line
0 commit comments