Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Skip param tokens in parens when checking order

For the sake of checking parameter ordering

    define foo($bar, $baz=function())

Is exactly the same as

    define foo($bar, $baz=function($gronk))

Except the former has a lot simpler logic

Closes #132
  • Loading branch information...
commit 40896373b80c344fa7fb987eadacffe6d373237f 1 parent 2b9c203
Tim Sharpe authored
34 lib/puppet-lint/plugins/check_classes.rb
View
@@ -68,26 +68,34 @@ class PuppetLint::Plugins::CheckClasses < PuppetLint::CheckPlugin
end
unless lparen_idx.nil? or rparen_idx.nil?
- param_tokens = tokens[lparen_idx..rparen_idx].reject { |r|
+ param_tokens = tokens[lparen_idx+1..rparen_idx-1].reject { |r|
formatting_tokens.include? r.type
}
+
+ paren_stack = []
param_tokens.each_index do |param_tokens_idx|
this_token = param_tokens[param_tokens_idx]
next_token = param_tokens[param_tokens_idx+1]
prev_token = param_tokens[param_tokens_idx-1]
+
+ if this_token.type == :LPAREN
+ paren_stack.push(true)
+ elsif this_token.type == :RPAREN
+ paren_stack.pop
+ end
+ next unless paren_stack.empty?
+
if this_token.type == :VARIABLE
- unless next_token.nil?
- if next_token.type == :COMMA or next_token.type == :RPAREN
- prev_tokens = param_tokens[0..param_tokens_idx]
- unless prev_tokens.rindex { |r| r.type == :EQUALS }.nil?
- unless prev_token.nil? or prev_token.type == :EQUALS
- msg = 'optional parameter listed before required parameter'
- notify :warning, {
- :message => msg,
- :linenumber => this_token.line,
- :column => this_token.column,
- }
- end
+ if next_token.nil? || next_token.type == :COMMA
+ prev_tokens = param_tokens[0..param_tokens_idx]
+ unless prev_tokens.rindex { |r| r.type == :EQUALS }.nil?
+ unless prev_token.nil? or prev_token.type == :EQUALS
+ msg = 'optional parameter listed before required parameter'
+ notify :warning, {
+ :message => msg,
+ :linenumber => this_token.line,
+ :column => this_token.column,
+ }
end
end
end
6 spec/puppet-lint/plugins/check_classes_spec.rb
View
@@ -119,6 +119,12 @@ class foo {
its(:problems) { should be_empty }
end
+ describe 'define with parameter that calls a function' do
+ let(:code) { "define foo($bar=extlookup($name)) {}" }
+
+ its(:problems) { should == [] }
+ end
+
describe 'define with attrs out of order' do
let(:code) { "define foo($bar='baz', $gronk) { }" }
Please sign in to comment.
Something went wrong with that request. Please try again.