Skip to content

Commit

Permalink
Merge pull request #577 from paran1/issue-565
Browse files Browse the repository at this point in the history
Count lines in comments and double quoted strings
  • Loading branch information
rnelson0 authored Dec 14, 2016
2 parents 041b430 + c6777f4 commit 874fd90
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/puppet-lint/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def new_token(type, value, length, opts = {})
@line_no += 1
@column = 1
end
if type == :SSTRING and /(?:\r\n|\r|\n)/.match(value)
lines = value.split(/(?:\r\n|\r|\n)/)
if [:MLCOMMENT, :SSTRING, :STRING].include? type and /(?:\r\n|\r|\n)/.match(value)
lines = value.split(/(?:\r\n|\r|\n)/, -1)
@line_no += lines.length-1
@column = lines.last.length
end
Expand Down
30 changes: 30 additions & 0 deletions spec/puppet-lint/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@
expect(token.line).to eq(1)
end

it 'should get correct line number after double quoted multi line string' do
token = @lexer.new_token(:STRING, "test\ntest", 9)
token = @lexer.new_token(:TEST, 'test', 4)
expect(token.line).to eq(2)
end

it 'should get correct line number after a multi line comment' do
token = @lexer.new_token(:MLCOMMENT, "test\ntest", 9)
token = @lexer.new_token(:TEST, 'test', 4)
expect(token.line).to eq(2)
end

it 'should calculate the line number for a multi line string' do
token = @lexer.new_token(:SSTRING, "test\ntest", 9)
token = @lexer.new_token(:TEST, 'test', 4)
expect(token.line).to eq(2)
end

it 'should calculate line number for string that ends with newline' do
token = @lexer.new_token(:SSTRING, "test\n", 5)
token = @lexer.new_token(:TEST, 'test', 4)
expect(token.line).to eq(2)
end

it 'should calculate the column number for an empty string' do
token = @lexer.new_token(:TEST, 'test', 4)
expect(token.column).to eq(1)
Expand Down Expand Up @@ -780,6 +798,12 @@
expect(token.type).to eq(:SSTRING)
expect(token.value).to eq(%{foo\\\\})
end

it "should match single quoted string containing a line break" do
token = @lexer.tokenise("'\n'").first
expect(token.type).to eq(:SSTRING)
expect(token.value).to eq("\n")
end
end

context ':REGEX' do
Expand Down Expand Up @@ -824,5 +848,11 @@
@lexer.tokenise("exec { \"/bin/echo \\\\\\\"${environment}\\\\\\\"\": }")
}.to_not raise_error
end

it "should match double quoted string containing a line break" do
token = @lexer.tokenise(%Q{"\n"}).first
expect(token.type).to eq(:STRING)
expect(token.value).to eq("\n")
end
end
end

0 comments on commit 874fd90

Please sign in to comment.