Skip to content

Commit 0fc7479

Browse files
authored
Merge pull request #14 from smortex/add-heredoc-support
Add support for heredoc strings
2 parents 926a7ae + 0d631a3 commit 0fc7479

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

lib/puppet-lint/plugins/check_strict_indent.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33

44
PuppetLint.new_check(:'strict_indent') do
55
def match(tokens)
6+
opening_token = {
7+
:RBRACE => :LBRACE,
8+
:RBRACK => :LBRACK,
9+
:RPAREN => :LPAREN,
10+
:HEREDOC => :HEREDOC_OPEN,
11+
:HEREDOC_POST => :HEREDOC_OPEN,
12+
}
613
open = {
714
:LBRACE => [],
815
:LBRACK => [],
916
:LPAREN => [],
17+
:HEREDOC_OPEN => [],
1018
}
1119

1220
matches = {}
1321

1422
tokens.each do |token|
15-
if [:LBRACE, :LBRACK, :LPAREN].include?(token.type)
23+
if [:LBRACE, :LBRACK, :LPAREN, :HEREDOC_OPEN].include?(token.type)
1624
open[token.type] << token
17-
elsif [:RBRACE, :RBRACK, :RPAREN].include?(token.type)
18-
match = open[("L" + token.type.to_s[1..-1]).to_sym].pop
25+
elsif [:RBRACE, :RBRACK, :RPAREN, :HEREDOC, :HEREDOC_POST].include?(token.type)
26+
match = open[opening_token[token.type]].pop
1927
if not match.nil?
2028
matches[token] = match
2129
matches[match] = token
@@ -45,6 +53,9 @@ def check
4553
open_groups = 0
4654
prev_token = token.prev_token
4755
while not prev_token.nil? and prev_token.type != :NEWLINE
56+
if prev_token.type == :HEREDOC_OPEN
57+
temp_indent += 1
58+
end
4859
if [:LBRACE, :LBRACK, :LPAREN].include?(prev_token.type)
4960
if matches[prev_token].nil? or matches[prev_token].line > prev_token.line
5061
# left braces not matched in the same line increase indent
@@ -120,6 +131,10 @@ def check
120131
actual = 0
121132
if token.next_token.type == :INDENT
122133
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
123138
else
124139
actual = 0
125140
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)