From 64dc7ddebc9ef3d0445153cd3505aebdacf75401 Mon Sep 17 00:00:00 2001 From: Jarret Lavallee Date: Thu, 1 Nov 2018 13:10:27 -0700 Subject: [PATCH] Allow for spaces in the heredoc tag Prior to this commit, the regex for the heredoc name would not handle spaces in the heredoc tag. This resulted in the end of the heredoc not being found when tokenizing. This commit modifies the regex to allow for white space after the heredoc name. --- lib/puppet-lint/lexer.rb | 8 ++++---- spec/puppet-lint/lexer_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/puppet-lint/lexer.rb b/lib/puppet-lint/lexer.rb index aa48193e..14eba584 100644 --- a/lib/puppet-lint/lexer.rb +++ b/lib/puppet-lint/lexer.rb @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/spec/puppet-lint/lexer_spec.rb b/spec/puppet-lint/lexer_spec.rb index b213bc19..9da6d849 100644 --- a/spec/puppet-lint/lexer_spec.rb +++ b/spec/puppet-lint/lexer_spec.rb @@ -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