Skip to content

Commit

Permalink
(#771) Handle arrow alignment when arrow column < opening brace column
Browse files Browse the repository at this point in the history
  • Loading branch information
rodjek committed Apr 10, 2018
1 parent 122e256 commit 211a0fd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def check

resource_tokens.each do |token|
if token.type == :FARROW
(level_tokens[level_idx] ||= []) << token
param_token = token.prev_code_token

if param_token.type == :DQPOST
Expand All @@ -48,12 +47,18 @@ def check
end
end

this_arrow_column = param_column[level_idx] + param_length + 1
if (level_tokens[level_idx] ||= []).any? { |t| t.line == token.line }
this_arrow_column = param_column[level_idx] + param_length + 1
else
this_arrow_column = param_token.column + param_token.to_manifest.length
this_arrow_column += 1 unless param_token.type == :DQPOST
end

if arrow_column[level_idx] < this_arrow_column
arrow_column[level_idx] = this_arrow_column
end

(level_tokens[level_idx] ||= []) << token
elsif token.type == :LBRACE
level_idx += 1
arrow_column << 0
Expand Down Expand Up @@ -96,12 +101,21 @@ def fix(problem)
# indent the parameter to the correct depth
problem[:token].prev_code_token.prev_token.type = :INDENT
problem[:token].prev_code_token.prev_token.value = ' ' * problem[:newline_indent]

end_param_idx = tokens.index(problem[:token].prev_code_token)
start_param_idx = tokens.index(problem[:token].prev_token_of([:INDENT, :NEWLINE]))
param_length = tokens[start_param_idx..end_param_idx].map { |r| r.to_manifest.length }.reduce(0) { |sum, x| sum + x } + 1
new_ws_len = problem[:arrow_column] - param_length
else
new_ws_len = if problem[:token].prev_token.type == :WHITESPACE
problem[:token].prev_token.to_manifest.length
else
0
end
new_ws_len += (problem[:arrow_column] - problem[:token].column)
end

end_param_idx = tokens.index(problem[:token].prev_code_token)
start_param_idx = tokens.index(problem[:token].prev_token_of([:INDENT, :NEWLINE])) + 1
param_length = tokens[start_param_idx..end_param_idx].map { |r| r.to_manifest.length }.reduce(0) { |sum, x| sum + x }
new_ws_len = (problem[:arrow_column] - (problem[:newline_indent] + param_length + 1))
raise PuppetLint::NoFix if new_ws_len < 0
new_ws = ' ' * new_ws_len

if problem[:token].prev_token.type == :WHITESPACE
Expand Down
37 changes: 37 additions & 0 deletions spec/puppet-lint/plugins/check_whitespace/arrow_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -967,5 +967,42 @@ class { 'puppetdb':
expect(manifest).to eq(fixed)
end
end

context 'negative argument' do
let(:code) do
<<-END
res { 'a':
x => { 'a' => '',
'ab' => '',
}
}
END
end

# TODO: This is not the desired behaviour, but adjusting the check to
# properly format the hashes will need to wait until a major version
# bump.
let(:fixed) do
<<-END
res { 'a':
x => { 'a' => '',
'ab' => '',
}
}
END
end

it 'should detect a problem' do
expect(problems).to have(1).problem
end

it 'should fix the problems' do
expect(problems).to contain_fixed(format(msg, 24, 20)).on_line(3).in_column(20)
end

it 'should realign the arrows' do
expect(manifest).to eq(fixed)
end
end
end
end

0 comments on commit 211a0fd

Please sign in to comment.