Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select parameter tokens based on preceeding code token #934

Merged
merged 2 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ group :test do
end

group :development do
# For Changelog generation
if RUBY_VERSION > '1.9'
gem 'github_changelog_generator', :require => false if RUBY_VERSION >= '2.2.2'
gem 'github_changelog_generator', '~> 1.13.0', :require => false if RUBY_VERSION < '2.2.2'
gem 'rack', '~> 1.0', :require => false if RUBY_VERSION < '2.2.2'
# For Changelog generation
if RUBY_VERSION >= '2.2.2'
gem 'github_changelog_generator', :require => false
else
gem 'github_changelog_generator', '~> 1.13.0', :require => false
gem 'rack', '~> 1.0', :require => false
end

gem 'pry'
end
end
13 changes: 12 additions & 1 deletion lib/puppet-lint/plugins/check_classes/parameter_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def check
end

next unless hash_or_array_stack.empty? && paren_stack.empty?
next unless parameter?(token)
next unless required_parameter?(token)

prev_tokens = class_idx[:param_tokens][0..i]
Expand All @@ -38,9 +39,19 @@ def check
end
end

def required_parameter?(token)
def parameter?(token)
return false unless token.type == :VARIABLE
return false unless token.prev_code_token

[
:LPAREN, # First parameter, no type specification
:COMMA, # Subsequent parameter, no type specification
:TYPE, # Parameter with simple type specification
:RBRACK, # Parameter with complex type specification
].include?(token.prev_code_token.type)
end

def required_parameter?(token)
data_type = token.prev_token_of(:TYPE, :skip_blocks => true)
return false if data_type && data_type.value == 'Optional'

Expand Down
18 changes: 18 additions & 0 deletions spec/puppet-lint/plugins/check_classes/parameter_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,23 @@

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

context "#{type} parameter with array operation" do
let(:code) do
<<-END
#{type} ntp (
# XXX: remove self from list
Array[String] $ntp_servers = [
'foo',
'bar',
'baz',
] - $::fqdn,
Array[String] $pools = [],
) { }
END
end

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