puppet-lint-class_parameter-check 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40c9f69e6438b1c1454bfb8f826922cfdd0e39d8
4
- data.tar.gz: 2e7620b8cdb14bd0e57935b066ed0a5e2968df9b
3
+ metadata.gz: 6e75015f13ea813a8d308575f028536653bc84cf
4
+ data.tar.gz: 95ed706edf7e95efb9b03e7028ebc8990aee9376
5
5
  SHA512:
6
- metadata.gz: 55e32ac4ac2a6e730571f862776c92d41f8a3c1c6b336e40557f9af83e0547349a65b3a19ab5eacc9604620c1183ef41a0f5d942c28d14192279611fd215df15
7
- data.tar.gz: 9fc4aa5d2094ae859aa9d811a783862ff7e7938266f9f1e695612da7ab952eaf2e2809e1ff89805897349a64562eeb7dc312f202c0411d568a2d2db2bb0a8367
6
+ metadata.gz: 3143d719a396c96967fe5a010114c5d344cb2ba65c4ccc2b4a877a3d43c99819e17e5cdcefa9999bd9b16992ba4cc15534b1d7553b83f5b94931cfb3a5f2d5d1
7
+ data.tar.gz: d3659ced8a9ddcc5621e11c134bd61a1eb14fcfd85b9e093cfa501883939232e043b8480a27da91bc7970ccda378c25165d6b30607a8d036c63cde0cbff32c0d
@@ -1,12 +1,10 @@
1
1
  class ClassParameter
2
- attr_reader :tokens
3
-
4
2
  def initialize
5
3
  @tokens = []
6
4
  end
7
5
 
8
6
  def is_optional?
9
- tokens.any? { |token| token.type == :EQUALS }
7
+ @tokens.any? { |token| token.type == :EQUALS }
10
8
  end
11
9
 
12
10
  def is_required?
@@ -14,21 +12,29 @@ class ClassParameter
14
12
  end
15
13
 
16
14
  def name
17
- tokens.select do |token|
15
+ @tokens.select do |token|
18
16
  token.type == :VARIABLE
19
17
  end.first.value
20
18
  end
21
19
 
22
20
  def add(token)
23
- tokens << token
21
+ # A parameter never starts with a newline token
22
+ unless @tokens.empty? && token.type == :NEWLINE
23
+ @tokens << token
24
+ end
25
+ end
26
+
27
+ def tokens
28
+ sanitized_tokens = strip_starting_newlines(@tokens)
29
+ return strip_ending_newlines(sanitized_tokens)
24
30
  end
25
31
 
26
32
  def line
27
- tokens.first.line
33
+ @tokens.first.line
28
34
  end
29
35
 
30
36
  def column
31
- tokens.first.column
37
+ @tokens.first.column
32
38
  end
33
39
 
34
40
  def <=>(other)
@@ -40,4 +46,18 @@ class ClassParameter
40
46
  return -1
41
47
  end
42
48
  end
49
+
50
+ private
51
+ def strip_starting_newlines(tokens)
52
+ tokens.inject([]) do |memo, token|
53
+ unless memo.empty? && token.type == :NEWLINE
54
+ memo << token
55
+ end
56
+ memo
57
+ end
58
+ end
59
+
60
+ def strip_ending_newlines(tokens)
61
+ strip_starting_newlines(tokens.reverse).reverse
62
+ end
43
63
  end
@@ -48,9 +48,9 @@ class ClassParameterList
48
48
  stack = []
49
49
 
50
50
  @tokens.inject([]) do |memo, token|
51
- if token.type == :LBRACK || token.type == :LPAREN
51
+ if [:LBRACK, :LPAREN].include?(token.type)
52
52
  stack.push(true)
53
- elsif token.type == :RBRACK || token.type == :RPAREN
53
+ elsif [:RBRACK, :RPAREN].include?(token.type)
54
54
  stack.pop
55
55
  end
56
56
 
@@ -59,12 +59,9 @@ class ClassParameterList
59
59
  parameter.add(token)
60
60
  end
61
61
 
62
- # always add a comma and a newline token at the end of each parameter
63
- parameter.add(PuppetLint::Lexer::Token.new(:COMMA, ",", 0,0))
64
- parameter.add(PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0,0))
65
62
  memo << parameter
66
63
  parameter = ClassParameter.new
67
- elsif token.type != :NEWLINE
64
+ else
68
65
  parameter.add(token)
69
66
  end
70
67
  memo
@@ -16,8 +16,15 @@ PuppetLint.new_check(:class_parameter) do
16
16
  class_parameter_lists.each do |class_parameter_list|
17
17
  resorted_tokens += tokens[token_index..class_parameter_list.start_index]
18
18
 
19
- class_parameter_list.sort.each do |parameter|
19
+ sorted_class_parameter_list = class_parameter_list.sort
20
+ sorted_class_parameter_list.each do |parameter|
20
21
  resorted_tokens += parameter.tokens
22
+
23
+ unless parameter == sorted_class_parameter_list.last
24
+ resorted_tokens << PuppetLint::Lexer::Token.new(:COMMA, ",", 0,0)
25
+ end
26
+
27
+ resorted_tokens << PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0,0)
21
28
  end
22
29
 
23
30
  token_index = class_parameter_list.end_index
@@ -191,7 +191,7 @@ describe 'class_parameter' do
191
191
  let(:code) { <<-EOF
192
192
  class puppet_module(
193
193
  String $alphabetical,
194
- String $non_alphabetical,
194
+ String $non_alphabetical
195
195
  ) { }
196
196
  EOF
197
197
  }
@@ -218,12 +218,12 @@ describe 'class_parameter' do
218
218
  expect(manifest).to eq(<<-EOF
219
219
  class puppet_module(
220
220
  String $alphabetical,
221
- String $non_alphabetical,
221
+ String $non_alphabetical
222
222
  ) { }
223
223
 
224
224
  class puppet_module2(
225
225
  String $alphabetical,
226
- String $non_alphabetical,
226
+ String $non_alphabetical
227
227
  ) { }
228
228
  EOF
229
229
  )
@@ -247,7 +247,7 @@ describe 'class_parameter' do
247
247
  String $alphabetical,
248
248
  String $non_alphabetical,
249
249
  String $alphabetical_optional = "default",
250
- String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional,
250
+ String $non_alphabetical_optional = $puppet_module::params::non_alphabetical_optional
251
251
  ) inherits puppet_module::params { }
252
252
  EOF
253
253
  )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-class_parameter-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roelof Reitsma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-31 00:00:00.000000000 Z
11
+ date: 2016-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint