11# Public: Check the manifest tokens for correct indent levels and
22# record a warning for each instance found.
33
4- PuppetLint . new_check ( :' strict_indent' ) do
4+ PuppetLint . new_check ( :strict_indent ) do
55 def match ( tokens )
66 opening_token = {
7- : RBRACE => :LBRACE ,
8- : RBRACK => :LBRACK ,
9- : RPAREN => :LPAREN ,
10- : HEREDOC => :HEREDOC_OPEN ,
11- : HEREDOC_POST => :HEREDOC_OPEN ,
7+ RBRACE : :LBRACE ,
8+ RBRACK : :LBRACK ,
9+ RPAREN : :LPAREN ,
10+ HEREDOC : :HEREDOC_OPEN ,
11+ HEREDOC_POST : :HEREDOC_OPEN ,
1212 }
1313 open = {
14- : LBRACE => [ ] ,
15- : LBRACK => [ ] ,
16- : LPAREN => [ ] ,
17- : HEREDOC_OPEN => [ ] ,
14+ LBRACE : [ ] ,
15+ LBRACK : [ ] ,
16+ LPAREN : [ ] ,
17+ HEREDOC_OPEN : [ ] ,
1818 }
1919
2020 matches = { }
2121
2222 tokens . each do |token |
23- if [ : LBRACE, : LBRACK, : LPAREN, : HEREDOC_OPEN] . include? ( token . type )
23+ if %i[ LBRACE LBRACK LPAREN HEREDOC_OPEN ] . include? ( token . type )
2424 open [ token . type ] << token
25- elsif [ : RBRACE, : RBRACK, : RPAREN, : HEREDOC, : HEREDOC_POST] . include? ( token . type )
25+ elsif %i[ RBRACE RBRACK RPAREN HEREDOC HEREDOC_POST ] . include? ( token . type )
2626 match = open [ opening_token [ token . type ] ] . pop
27- if not match . nil?
27+ unless match . nil?
2828 matches [ token ] = match
2929 matches [ match ] = token
3030 end
@@ -41,34 +41,31 @@ def check
4141
4242 matches = match ( tokens )
4343
44- tokens . select { |token |
44+ tokens . select do |token |
4545 token . type == :NEWLINE
46- } . reject { |token |
46+ end . reject do |token |
4747 # ignore newline at end of code
4848 token . next_token . nil?
49- } . each do |token |
49+ end . each do |token |
5050 temp_indent = 0
5151
5252 # indent for open groups in the previous line
5353 open_groups = 0
5454 prev_token = token . prev_token
55- while not prev_token . nil? and prev_token . type != :NEWLINE
56- if prev_token . type == :HEREDOC_OPEN
57- temp_indent += 1
58- end
59- if [ :LBRACE , :LBRACK , :LPAREN ] . include? ( prev_token . type )
60- if matches [ prev_token ] . nil? or matches [ prev_token ] . line > prev_token . line
61- # left braces not matched in the same line increase indent
62- open_groups += 1
63- end
55+ while !prev_token . nil? and prev_token . type != :NEWLINE
56+ temp_indent += 1 if prev_token . type == :HEREDOC_OPEN
57+ if %i[ LBRACE LBRACK
58+ LPAREN ] . include? ( prev_token . type ) && ( matches [ prev_token ] . nil? or matches [ prev_token ] . line > prev_token . line )
59+ # left braces not matched in the same line increase indent
60+ open_groups += 1
6461 end
6562 prev_token = prev_token . prev_token
6663 end
6764 indent += open_groups
6865
6966 # reset prev_token to last non-whitespace token on previous line
7067 prev_token = token . prev_token
71- while not prev_token . nil? and ( prev_token . type == :WHITESPACE or prev_token . type == :COMMENT )
68+ while ! prev_token . nil? and ( prev_token . type == :WHITESPACE or prev_token . type == :COMMENT )
7269 prev_token = prev_token . prev_token
7370 end
7471
@@ -88,7 +85,7 @@ def check
8885 end
8986 end
9087 when :SEMIC
91- if not colon_indent . nil?
88+ unless colon_indent . nil?
9289 # only unindent for a semicolon when we've indented for a colon
9390 colon_indent = nil
9491 indent -= 1
@@ -99,18 +96,16 @@ def check
9996
10097 # unindent for closing brackets in the current line
10198 next_token = token . next_token
102- while not next_token . nil? and next_token . type != :NEWLINE
103- if [ : RBRACE, : RBRACK, : RPAREN] . include? ( next_token . type )
104- if not matches [ next_token ] . nil? and matches [ next_token ] . line < next_token . line
99+ while ! next_token . nil? and next_token . type != :NEWLINE
100+ if %i[ RBRACE RBRACK RPAREN ] . include? ( next_token . type )
101+ if ! matches [ next_token ] . nil? and matches [ next_token ] . line < next_token . line
105102 # right braces matched in a previous line decrease indent
106103 indent -= 1
107104 end
108- if next_token . type == :RBRACE and not colon_indent . nil?
109- if not matches [ next_token ] . nil? and matches [ next_token ] . line < colon_indent
110- # unindent at the end of resources if needed
111- indent -= 1
112- colon_indent = nil
113- end
105+ if next_token . type == :RBRACE and !colon_indent . nil? && ( !matches [ next_token ] . nil? and matches [ next_token ] . line < colon_indent )
106+ # unindent at the end of resources if needed
107+ indent -= 1
108+ colon_indent = nil
114109 end
115110 end
116111 next_token = next_token . next_token
@@ -119,53 +114,53 @@ def check
119114 # obviously we have a problem
120115 if indent < 0
121116 notify :error , {
122- : message => 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues' ,
123- : line => token . next_token . line ,
124- : column => token . next_token . column ,
117+ message : 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues' ,
118+ line : token . next_token . line ,
119+ column : token . next_token . column ,
125120 }
126121 # stop parsing indent
127122 break
128123 end
129124
130125 # get actual indent
131126 actual = 0
132- if token . next_token . type == :INDENT
133- actual = token . next_token . value . length
134- elsif !token . prev_token . nil? and token . prev_token . type == :HEREDOC
135- actual = token . prev_token . value . split ( "\n " ) . last . length
136- elsif !token . prev_token . nil? and token . prev_token . type == :HEREDOC_OPEN
137- actual = next_token . prev_token . value . split ( "\n " ) . last . length
138- else
139- actual = 0
140- end
127+ actual = if token . next_token . type == :INDENT
128+ token . next_token . value . length
129+ elsif !token . prev_token . nil? and token . prev_token . type == :HEREDOC
130+ token . prev_token . value . split ( "\n " ) . last . length
131+ elsif !token . prev_token . nil? and token . prev_token . type == :HEREDOC_OPEN
132+ next_token . prev_token . value . split ( "\n " ) . last . length
133+ else
134+ 0
135+ end
141136
142137 # expected indent
143138 expected = ( indent + temp_indent ) * chars_per_indent
144139
145140 # oh no! incorrect indent!
146- if actual != expected
147- # no one cares if blank lines and comments are indented correctly
148- if not [ :COMMENT , :NEWLINE ] . include? ( token . next_token . type )
149- notify :warning , {
150- :message => "indent should be #{ expected } chars and is #{ actual } " ,
151- :line => token . next_token . line ,
152- :column => token . next_token . column ,
153- :token => token . next_token ,
154- :indent => expected ,
155- }
156- end
157- end
141+ next unless actual != expected
142+
143+ # no one cares if blank lines and comments are indented correctly
144+ next if %i[ COMMENT NEWLINE ] . include? ( token . next_token . type )
145+
146+ notify :warning , {
147+ message : "indent should be #{ expected } chars and is #{ actual } " ,
148+ line : token . next_token . line ,
149+ column : token . next_token . column ,
150+ token : token . next_token ,
151+ indent : expected ,
152+ }
158153 end
159154 end
160155
161156 def fix ( problem )
162157 char_for_indent = ' '
163- if [ : INDENT, : WHITESPACE] . include? ( problem [ :token ] . type )
158+ if %i[ INDENT WHITESPACE ] . include? ( problem [ :token ] . type )
164159 problem [ :token ] . value = char_for_indent * problem [ :indent ]
165160 else
166161 tokens . insert (
167162 tokens . find_index ( problem [ :token ] ) ,
168- PuppetLint ::Lexer ::Token . new ( :INDENT , char_for_indent * problem [ :indent ] , problem [ :line ] , problem [ :column ] )
163+ PuppetLint ::Lexer ::Token . new ( :INDENT , char_for_indent * problem [ :indent ] , problem [ :line ] , problem [ :column ] ) ,
169164 )
170165 end
171166 end
0 commit comments