Skip to content

Commit

Permalink
Merge pull request #14 from smortex/add-heredoc-support
Browse files Browse the repository at this point in the history
Add support for heredoc strings
  • Loading branch information
relud authored Dec 29, 2018
2 parents 926a7ae + 0d631a3 commit 0fc7479
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/puppet-lint/plugins/check_strict_indent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@

PuppetLint.new_check(:'strict_indent') do
def match(tokens)
opening_token = {
:RBRACE => :LBRACE,
:RBRACK => :LBRACK,
:RPAREN => :LPAREN,
:HEREDOC => :HEREDOC_OPEN,
:HEREDOC_POST => :HEREDOC_OPEN,
}
open = {
:LBRACE => [],
:LBRACK => [],
:LPAREN => [],
:HEREDOC_OPEN => [],
}

matches = {}

tokens.each do |token|
if [:LBRACE, :LBRACK, :LPAREN].include?(token.type)
if [:LBRACE, :LBRACK, :LPAREN, :HEREDOC_OPEN].include?(token.type)
open[token.type] << token
elsif [:RBRACE, :RBRACK, :RPAREN].include?(token.type)
match = open[("L" + token.type.to_s[1..-1]).to_sym].pop
elsif [:RBRACE, :RBRACK, :RPAREN, :HEREDOC, :HEREDOC_POST].include?(token.type)
match = open[opening_token[token.type]].pop
if not match.nil?
matches[token] = match
matches[match] = token
Expand Down Expand Up @@ -45,6 +53,9 @@ def check
open_groups = 0
prev_token = token.prev_token
while not prev_token.nil? and prev_token.type != :NEWLINE
if prev_token.type == :HEREDOC_OPEN
temp_indent += 1
end
if [:LBRACE, :LBRACK, :LPAREN].include?(prev_token.type)
if matches[prev_token].nil? or matches[prev_token].line > prev_token.line
# left braces not matched in the same line increase indent
Expand Down Expand Up @@ -120,6 +131,10 @@ def check
actual = 0
if token.next_token.type == :INDENT
actual = token.next_token.value.length
elsif token.prev_token.type == :HEREDOC
actual = token.prev_token.value.split("\n").last.length
elsif token.prev_token.type == :HEREDOC_OPEN
actual = next_token.prev_token.value.split("\n").last.length
else
actual = 0
end
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/fail/heredoc_extra.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$variable = @(EOT)
This is a multiline
heredoc string
| EOT
4 changes: 4 additions & 0 deletions spec/fixtures/fail/heredoc_unsuficient.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$variable = @(EOT)
This is a multiline
heredoc string
| EOT
21 changes: 21 additions & 0 deletions spec/fixtures/pass/heredoc.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$variable = @(EOT)
This is a multiline
heredoc string
| EOT

$variable_with_interpolation = @("EOT")
Another example
${variable}
with
${variable}
with interpolation
| EOT

case fact('os.family') {
'debian': {
$greeting = @(EOT)
Hello
World
| EOT
}
}

0 comments on commit 0fc7479

Please sign in to comment.