Skip to content

Commit 0d631a3

Browse files
committed
Add support for heredoc strings
Fixes #12
1 parent d0d06ea commit 0d631a3

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

lib/puppet-lint/plugins/check_strict_indent.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ def match(tokens)
77
:RBRACE => :LBRACE,
88
:RBRACK => :LBRACK,
99
:RPAREN => :LPAREN,
10+
:HEREDOC => :HEREDOC_OPEN,
11+
:HEREDOC_POST => :HEREDOC_OPEN,
1012
}
1113
open = {
1214
:LBRACE => [],
1315
:LBRACK => [],
1416
:LPAREN => [],
17+
:HEREDOC_OPEN => [],
1518
}
1619

1720
matches = {}
1821

1922
tokens.each do |token|
20-
if [:LBRACE, :LBRACK, :LPAREN].include?(token.type)
23+
if [:LBRACE, :LBRACK, :LPAREN, :HEREDOC_OPEN].include?(token.type)
2124
open[token.type] << token
22-
elsif [:RBRACE, :RBRACK, :RPAREN].include?(token.type)
25+
elsif [:RBRACE, :RBRACK, :RPAREN, :HEREDOC, :HEREDOC_POST].include?(token.type)
2326
match = open[opening_token[token.type]].pop
2427
if not match.nil?
2528
matches[token] = match
@@ -50,6 +53,9 @@ def check
5053
open_groups = 0
5154
prev_token = token.prev_token
5255
while not prev_token.nil? and prev_token.type != :NEWLINE
56+
if prev_token.type == :HEREDOC_OPEN
57+
temp_indent += 1
58+
end
5359
if [:LBRACE, :LBRACK, :LPAREN].include?(prev_token.type)
5460
if matches[prev_token].nil? or matches[prev_token].line > prev_token.line
5561
# left braces not matched in the same line increase indent
@@ -125,6 +131,10 @@ def check
125131
actual = 0
126132
if token.next_token.type == :INDENT
127133
actual = token.next_token.value.length
134+
elsif token.prev_token.type == :HEREDOC
135+
actual = token.prev_token.value.split("\n").last.length
136+
elsif token.prev_token.type == :HEREDOC_OPEN
137+
actual = next_token.prev_token.value.split("\n").last.length
128138
else
129139
actual = 0
130140
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$variable = @(EOT)
2+
This is a multiline
3+
heredoc string
4+
| EOT
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$variable = @(EOT)
2+
This is a multiline
3+
heredoc string
4+
| EOT

spec/fixtures/pass/heredoc.pp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
$variable = @(EOT)
2+
This is a multiline
3+
heredoc string
4+
| EOT
5+
6+
$variable_with_interpolation = @("EOT")
7+
Another example
8+
${variable}
9+
with
10+
${variable}
11+
with interpolation
12+
| EOT
13+
14+
case fact('os.family') {
15+
'debian': {
16+
$greeting = @(EOT)
17+
Hello
18+
World
19+
| EOT
20+
}
21+
}

0 commit comments

Comments
 (0)