forked from rodjek/puppet-lint
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from gerases/space_before_arrow
Create a new plugin for ensuring a single space before a '=>'
- Loading branch information
Showing
3 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
lib/puppet-lint/plugins/check_whitespace/space_before_arrow.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
# Check the manifest tokens for any arrows (=>) that have too much space | ||
# before them in situations when a given resource has at most one line with | ||
# such arrows. For example: | ||
|
||
# file { | ||
# # too much space after "foo" | ||
# foo⎵⎵=>⎵'bar' | ||
# } | ||
# | ||
# file { | ||
# # too much space after 'bar' | ||
# foo⎵=>⎵{ bar⎵⎵=>⎵'baz' } | ||
# } | ||
# | ||
|
||
# Linting multi-parameter resources like this: | ||
# | ||
# package { 'xxx': | ||
# foo => 'bar', | ||
# bar => 'baz', | ||
# } | ||
# | ||
# is handled by the "arrow_alignment" plugin. | ||
|
||
PuppetLint.new_check(:space_before_arrow) do | ||
def check | ||
resource_indexes.each do |res_idx| | ||
resource_tokens = res_idx[:tokens] | ||
resource_tokens.reject! do |token| | ||
Set[:COMMENT, :SLASH_COMMENT, :MLCOMMENT].include?(token.type) | ||
end | ||
|
||
first_arrow = resource_tokens.index { |r| r.type == :FARROW } | ||
last_arrow = resource_tokens.rindex { |r| r.type == :FARROW } | ||
next if first_arrow.nil? | ||
next if last_arrow.nil? | ||
|
||
# If this is a single line resource, skip it | ||
next unless resource_tokens[first_arrow].line == resource_tokens[last_arrow].line | ||
|
||
resource_tokens.select { |token| token.type == :FARROW }.each do |token| | ||
prev_token = token.prev_token | ||
next unless prev_token | ||
next if prev_token.value == ' ' | ||
|
||
line = prev_token.line | ||
column = prev_token.column | ||
|
||
notify( | ||
:warning, | ||
message: "there should be a single space before '=>' on line #{line}, column #{column}", | ||
line: line, | ||
column: column, | ||
token: prev_token, | ||
) | ||
end | ||
end | ||
end | ||
|
||
def fix(problem) | ||
token = problem[:token] | ||
|
||
if token.type == :WHITESPACE | ||
token.value = ' ' | ||
return | ||
end | ||
|
||
add_token(tokens.index(token), PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0)) | ||
end | ||
end |
126 changes: 126 additions & 0 deletions
126
spec/unit/puppet-lint/plugins/check_whitespace/space_before_arrow_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
require 'spec_helper' | ||
|
||
describe 'space_before_arrow' do | ||
let(:msg) { "there should be a single space before '=>' on line %d, column %d" } | ||
|
||
context 'with code that should not trigger any warnings' do | ||
context 'resource with multiple parameters on different lines' do | ||
let(:code) do | ||
<<-END | ||
file { 'foo': | ||
foo => bar, | ||
bar => buzz, | ||
} | ||
END | ||
end | ||
|
||
it 'does not detect any problems' do | ||
expect(problems).to be_empty | ||
end | ||
end | ||
|
||
context 'resource with single param and normal spacing' do | ||
let(:code) do | ||
<<-END | ||
file { 'foo': | ||
foo => bar, | ||
} | ||
END | ||
end | ||
|
||
it 'does not detect any problems' do | ||
expect(problems).to be_empty | ||
end | ||
end | ||
|
||
context 'resource with multiple params and normal spacing' do | ||
let(:code) do | ||
<<-END | ||
file { 'foo': | ||
foo => { "bar" => "baz" }, | ||
} | ||
END | ||
end | ||
|
||
it 'does not detect any problems' do | ||
expect(problems).to be_empty | ||
end | ||
end | ||
end | ||
|
||
context 'resource with a single param and simple value with too much space before arrow' do | ||
let(:code) do | ||
<<-END | ||
file { 'foo': | ||
foo => bar, | ||
} | ||
END | ||
end | ||
|
||
context 'with fix disabled' do | ||
it 'detects extra space before arrow' do | ||
expect(problems.size).to eq(1) | ||
end | ||
|
||
it 'produces 1 warning' do | ||
expect(problems).to contain_warning(msg % [2, 14]).on_line(2).in_column(14) | ||
end | ||
end | ||
|
||
context 'with fix enabled' do | ||
before(:each) do | ||
PuppetLint.configuration.fix = true | ||
end | ||
|
||
after(:each) do | ||
PuppetLint.configuration.fix = false | ||
end | ||
|
||
it 'detects the problem' do | ||
expect(problems.size).to eq(1) | ||
end | ||
|
||
it 'fixes the manifest' do | ||
expect(problems).to contain_fixed(msg % [2, 14]) | ||
end | ||
end | ||
end | ||
|
||
context 'resource with a single param, a hash as value and bad spacing within the hash' do | ||
let(:code) do | ||
<<-END | ||
file { 'foo': | ||
foo => { "bar" => "baz" }, | ||
} | ||
END | ||
end | ||
|
||
context 'with fix disabled' do | ||
it 'detects extra space before arrow' do | ||
expect(problems.size).to eq(1) | ||
end | ||
|
||
it 'produces a warning' do | ||
expect(problems).to contain_warning(msg % [2, 25]).on_line(2).in_column(25) | ||
end | ||
end | ||
|
||
context 'with fix enabled' do | ||
before(:each) do | ||
PuppetLint.configuration.fix = true | ||
end | ||
|
||
after(:each) do | ||
PuppetLint.configuration.fix = false | ||
end | ||
|
||
it 'detects the problem' do | ||
expect(problems.size).to eq(1) | ||
end | ||
|
||
it 'fixes the manifest' do | ||
expect(problems).to contain_fixed(msg % [2, 25]) | ||
end | ||
end | ||
end | ||
end |