Skip to content

Commit

Permalink
Merge pull request #672 from Darhazer/arrow-on-right-hand-operand-line
Browse files Browse the repository at this point in the history
Check that arrow is on the line of right operand
  • Loading branch information
rnelson0 authored Mar 29, 2017
2 parents b91a2fa + 4adb5b0 commit 9ee701e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/puppet-lint/plugins/check_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@ def check
end
end

# Public: Test the manifest tokens for chaining arrow that is
# on the line of the left operand when the right operand is on another line.
#
# https://docs.puppet.com/guides/style_guide.html#chaining-arrow-syntax
PuppetLint.new_check(:arrow_on_right_operand_line) do
def check
tokens.select { |r| Set[:IN_EDGE, :IN_EDGE_SUB].include?(r.type) }.each do |token|
if token.next_code_token.line != token.line
notify :warning, {
:message => 'arrow should be on the right operand\'s line',
:line => token.line,
:column => token.column,
:token => token,
}
end
end
end

def fix(problem)
token = problem[:token]
tokens.delete(token)
# remove any excessive whitespace on the line
temp_token = token.prev_code_token
while (temp_token = temp_token.next_token)
tokens.delete(temp_token) if whitespace?(temp_token)
break if temp_token.type == :NEWLINE
end

index = tokens.index(token.next_code_token)
tokens.insert(index, token)
tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0))
end

def whitespace?(token)
Set[:INDENT, :WHITESPACE].include?(token.type)
end
end

# Public: Test the manifest tokens for any classes or defined types that are
# not in an appropriately named file for the autoloader to detect and record
# an error of each instance found.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'

describe 'arrow_on_right_operand_line' do
{'chain' => '->', 'subscribe chain' => '~>'}.each do |name, operator|
context "#{name} operator" do
context 'both operands on same line' do
let(:code) { "Package['httpd'] #{operator} Service['httpd']" }

it { expect(problems).to have(0).problems }
end

context 'arrow on the line of left operand' do
let(:code) do
"Package['httpd'] #{operator}
Service['httpd']"
end

it { expect(problems).to have(1).problems }

context 'with fix enabled' do
before do
PuppetLint.configuration.fix = true
end

after do
PuppetLint.configuration.fix = false
end

let(:fixed) do
"Package['httpd']
#{operator} Service['httpd']"
end

it { expect(manifest).to eq (fixed) }
end
end

context 'arrow on the line of right operand' do
let(:code) { "Package['httpd']
#{operator} Service['httpd']" }

it { expect(problems).to have(0).problems }
end
end
end
end

0 comments on commit 9ee701e

Please sign in to comment.