Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for spaces in the heredoc tag #841

Merged
merged 1 commit into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/puppet-lint/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def tokenise(code)
interpolate_string(str_contents, lines_parsed.count, lines_parsed.last.length)
length = str_contents.size + 1

elsif heredoc_name = chunk[%r{\A@\(("?.+?"?(:.+?)?(/.*?)?)\)}, 1]
elsif heredoc_name = chunk[%r{\A@\(("?.+?"?(:.+?)?#{WHITESPACE_RE}*(/.*?)?)\)}, 1]
heredoc_queue << heredoc_name
tokens << new_token(:HEREDOC_OPEN, heredoc_name)
length = heredoc_name.size + 3
Expand Down Expand Up @@ -267,7 +267,7 @@ def tokenise(code)
length += indent.size
else
heredoc_tag = heredoc_queue.shift
heredoc_name = heredoc_tag[%r{\A"?(.+?)"?(:.+?)?(/.*)?\Z}, 1]
heredoc_name = heredoc_tag[%r{\A"?(.+?)"?(:.+?)?#{WHITESPACE_RE}*(/.*)?\Z}, 1]
str_contents = StringScanner.new(code[(i + length)..-1]).scan_until(%r{\|?\s*-?\s*#{heredoc_name}})
interpolate_heredoc(str_contents, heredoc_tag)
length += str_contents.size
Expand All @@ -283,7 +283,7 @@ def tokenise(code)

unless heredoc_queue.empty?
heredoc_tag = heredoc_queue.shift
heredoc_name = heredoc_tag[%r{\A"?(.+?)"?(:.+?)?(/.*)?\Z}, 1]
heredoc_name = heredoc_tag[%r{\A"?(.+?)"?(:.+?)?#{WHITESPACE_RE}*(/.*)?\Z}, 1]
str_contents = StringScanner.new(code[(i + length)..-1]).scan_until(%r{\|?\s*-?\s*#{heredoc_name}})
_ = code[0..(i + length)].split(LINE_END_RE)
interpolate_heredoc(str_contents, heredoc_tag)
Expand Down Expand Up @@ -493,7 +493,7 @@ def interpolate_string(string, line, column)
# Returns nothing.
def interpolate_heredoc(string, name)
ss = StringScanner.new(string)
eos_text = name[%r{\A"?(.+?)"?(:.+?)?(/.*)?\Z}, 1]
eos_text = name[%r{\A"?(.+?)"?(:.+?)?#{WHITESPACE_RE}*(/.*)?\Z}, 1]
first = true
interpolate = name.start_with?('"')
value, terminator = get_heredoc_segment(ss, eos_text, interpolate)
Expand Down
15 changes: 15 additions & 0 deletions spec/puppet-lint/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,21 @@
expect(tokens[7].line).to eq(5)
expect(tokens[7].column).to eq(8)
end

it 'should handle a heredoc with spaces in the tag' do
manifest = <<-END.gsub(%r{^ {6}}, '')
$str = @("myheredoc" /)
foo
|-myheredoc
END
tokens = @lexer.tokenise(manifest)
expect(tokens.length).to eq(8)

expect(tokens[4].type).to eq(:HEREDOC_OPEN)
expect(tokens[4].value).to eq('"myheredoc" /')
expect(tokens[6].type).to eq(:HEREDOC)
expect(tokens[6].value).to eq(" foo\n ")
end
end

context ':HEREDOC with interpolation' do
Expand Down