puppet-lint-class_alignment-check 0.3.3 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3dc0a012b0d80156eaad90609fb62d8f32d580d6acbc5f28e6981d71137fc301
4
- data.tar.gz: 72378622329307ce0c0631caebec4fef365fe137b89581bfd01bae1e8bad9537
3
+ metadata.gz: 4cfe3bc16f68ebdeb6b63a01f2bdefdba07052baa618e1090b25c23ea133a1b6
4
+ data.tar.gz: b75630c6a6c37634f011ad13fe6147bf3c12753780a5f417ba074bbc1a05aedb
5
5
  SHA512:
6
- metadata.gz: f7750b39ff5ef57ec5fca519a77e1aa0a1b0def9d906814c4c6b2cf096229a8cde51bc75d796e174c4f351e3fe17f4a5baeb79b3649d468e255f88433d3691a2
7
- data.tar.gz: 79e3897e36b000f944ebe5d90bbdd671215e2fe94d4a6956a3ddd258fbced0ec4ecd5c8bee7e94b9a69fbc1df88fff411b87872f74babce420f870fc3ac84339
6
+ metadata.gz: 465e680a7fc27debc8fbd2243ed8c68deae99d405ba5e82cc42aef2716f9865b198120f756cd815149b1fc43309a784b08e4662d2a49407a70d49b048ea2adf7
7
+ data.tar.gz: 467601075b7fd04ccee1b490809ff719f3a8129141a9afe03c93f2744b412cba0a8fcc6b2c54aeedc12358586d608ca0653cd6bcfa8929ccca8423192e1f482a
@@ -3,84 +3,6 @@
3
3
  #
4
4
  # https://puppet.com/docs/puppet/7/style_guide.html#style_guide_classes-param-indentation-alignment
5
5
 
6
- def a_param?(token)
7
- if token&.prev_code_token&.type == :EQUALS
8
- false
9
- elsif token&.prev_code_token&.type == :FARROW
10
- false
11
- elsif %i[DQPRE DQMID].include?(token&.prev_code_token&.type)
12
- false
13
- elsif token&.type == :VARIABLE
14
- count = 0
15
- while token&.prev_token
16
- token = token.prev_token
17
- if %i[LPAREN LBRACK LBRACE].include?(token.type)
18
- count += 1
19
- elsif %i[RPAREN RBRACK RBRACE].include?(token.type)
20
- count -= 1
21
- elsif %i[DEFINE CLASS].include?(token.type)
22
- break
23
- end
24
- end
25
- true if count == 1
26
- end
27
- end
28
-
29
- def first_on_the_line?(token, type)
30
- origin = token
31
- while token&.prev_token
32
- token = token.prev_token
33
-
34
- break if token.type == :NEWLINE
35
- end
36
-
37
- while token&.next_token
38
- token = token.next_token
39
-
40
- break if token.type == type
41
- end
42
-
43
- origin == token
44
- end
45
-
46
- def the_one?(token, character)
47
- case character
48
- when '='
49
- true if token.type == :EQUALS && first_on_the_line?(token, :EQUALS)
50
- when '$'
51
- true if a_param?(token) && first_on_the_line?(token, :VARIABLE)
52
- end
53
- end
54
-
55
- def get_the_first_param(token)
56
- while token&.prev_code_token
57
- token = token.prev_code_token
58
- break if token.type == :CLASS
59
- end
60
-
61
- while token&.next_code_token
62
- token = token.next_code_token
63
- return token if token.type == :VARIABLE
64
- end
65
- end
66
-
67
- def get_prev_code_token(token, character)
68
- case character
69
- when '='
70
- token.prev_code_token
71
- when '$'
72
- if token.prev_code_token
73
- if %i[CLASSREF RBRACK].include?(token.prev_code_token.type)
74
- token.prev_code_token
75
- elsif token.prev_code_token.type == :LPAREN
76
- token
77
- elsif token.prev_code_token.type == :COMMA
78
- get_the_first_param(token)
79
- end
80
- end
81
- end
82
- end
83
-
84
6
  # This function is copied & modified from puppet-lint arrow_alignment check
85
7
  # https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L8
86
8
  def check_for(character)
@@ -0,0 +1,128 @@
1
+ def a_param?(token)
2
+ if token&.prev_code_token&.type == :EQUALS
3
+ false
4
+ elsif token&.prev_code_token&.type == :FARROW
5
+ false
6
+ elsif %i[DQPRE DQMID].include?(token&.prev_code_token&.type)
7
+ false
8
+ elsif token&.type == :VARIABLE
9
+ # first var in the class
10
+ return true if token&.prev_token_of(:CLASS)&.next_token_of(:LPAREN)&.next_token_of(:VARIABLE) == token
11
+ return true if token&.prev_token_of(:DEFINE)&.next_token_of(:LPAREN)&.next_token_of(:VARIABLE) == token
12
+
13
+ count = 0
14
+ saw_a_comma = false
15
+ while token&.prev_token
16
+ token = token.prev_token
17
+
18
+ saw_a_comma = true if token.type == :COMMA
19
+
20
+ return false if token.type == :EQUALS && count == 0 && saw_a_comma == false
21
+
22
+ if %i[RPAREN RBRACK RBRACE].include?(token.type)
23
+ count -= 1
24
+ elsif %i[LPAREN LBRACK LBRACE].include?(token.type)
25
+ count += 1
26
+ end
27
+
28
+ break if %i[CLASS DEFINE].include?(token.type)
29
+ end
30
+
31
+ true if count == 1
32
+ end
33
+ end
34
+
35
+ def first_on_the_line?(token, type)
36
+ origin = token
37
+ while token&.prev_token
38
+ token = token.prev_token
39
+
40
+ break if token.type == :NEWLINE
41
+ end
42
+
43
+ while token&.next_token
44
+ token = token.next_token
45
+
46
+ break if token.type == type
47
+ end
48
+
49
+ origin == token
50
+ end
51
+
52
+ def the_one?(token, character)
53
+ case character
54
+ when '='
55
+ true if token.type == :EQUALS && first_on_the_line?(token, :EQUALS)
56
+ when '$'
57
+ true if a_param?(token) && first_on_the_line?(token, :VARIABLE)
58
+ end
59
+ end
60
+
61
+ def get_the_first_param(token)
62
+ while token&.prev_code_token
63
+ token = token.prev_code_token
64
+ break if token.type == :CLASS
65
+ end
66
+
67
+ while token&.next_code_token
68
+ token = token.next_code_token
69
+ return token if token.type == :VARIABLE
70
+ end
71
+ end
72
+
73
+ def get_prev_code_token(token, character)
74
+ case character
75
+ when '='
76
+ token.prev_code_token
77
+ when '$'
78
+ if token.prev_code_token
79
+ if %i[CLASSREF RBRACK].include?(token.prev_code_token.type)
80
+ token.prev_code_token
81
+ elsif token.prev_code_token.type == :LPAREN
82
+ token
83
+ elsif token.prev_code_token.type == :COMMA
84
+ get_the_first_param(token)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def get_param_start_token(token)
91
+ return nil unless a_param?(token)
92
+
93
+ case token&.prev_code_token&.type
94
+
95
+ # Integer $db_port
96
+ when :TYPE
97
+ token.prev_code_token
98
+
99
+ when :CLASSREF
100
+ token.prev_code_token
101
+
102
+ # Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port
103
+ when :RBRACK
104
+ count = 0
105
+ while token&.prev_code_token
106
+ token = token.prev_code_token
107
+ case token.type
108
+ when :RBRACK
109
+ count += 1
110
+ when :LBRACK
111
+ count -= 1
112
+ end
113
+
114
+ break if count.zero?
115
+ end
116
+ token.prev_code_token
117
+
118
+ else
119
+ token
120
+ end
121
+ end
122
+
123
+ def get_prev_param_token(token)
124
+ while token&.prev_code_token
125
+ token = token.prev_code_token
126
+ return token if a_param?(token)
127
+ end
128
+ end
@@ -1,11 +1,3 @@
1
- def prev_param_token(token)
2
- while token&.prev_code_token
3
- token = token.prev_code_token
4
- break if a_param?(token)
5
- end
6
- token
7
- end
8
-
9
1
  PuppetLint.new_check(:class_params_newline) do
10
2
  def check
11
3
  (class_indexes + defined_type_indexes).each do |item|
@@ -45,13 +37,12 @@ PuppetLint.new_check(:class_params_newline) do
45
37
 
46
38
  next unless a_param?(token)
47
39
 
48
- if token.prev_code_token.type == :LPAREN
49
- next if token.line != token.prev_code_token.line
50
- elsif token.line != prev_param_token(token).line
40
+ if get_param_start_token(token)&.prev_code_token.type == :LPAREN
41
+ next if token.line != get_param_start_token(token).prev_code_token.line
42
+ elsif token.line != get_prev_param_token(token)&.line
51
43
  next
52
44
  end
53
45
 
54
- # binding.break
55
46
  notify(
56
47
  :warning,
57
48
  message: "`#{token.to_manifest}` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
@@ -67,28 +58,7 @@ PuppetLint.new_check(:class_params_newline) do
67
58
 
68
59
  def fix(problem)
69
60
  token = problem[:token]
70
- if token.type == :VARIABLE
71
- # Integer $db_port
72
- if token&.prev_code_token&.type == :TYPE
73
- token = token.prev_code_token
74
-
75
- # Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port
76
- elsif token&.prev_code_token&.type == :RBRACK
77
- count = 0
78
- while token&.prev_code_token
79
- token = token.prev_code_token
80
- case token.type
81
- when :RBRACK
82
- count += 1
83
- when :LBRACK
84
- count -= 1
85
- end
86
-
87
- break if count.zero?
88
- end
89
- token = token.prev_code_token
90
- end
91
- end
61
+ token = get_param_start_token(token) if token.type == :VARIABLE
92
62
 
93
63
  last_non_whitespace_token = token.prev_token
94
64
  while last_non_whitespace_token&.prev_token
@@ -101,7 +101,7 @@ describe 'class_params_newline' do
101
101
  class bar ($foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
102
102
 
103
103
  class aaa ( $foo = 1, $bar = $a,) {}
104
- class aaa ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
104
+ class aaa ( Integer $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a,) {}
105
105
 
106
106
  class bbb ( $foo = 1, $bar = $a, ) {}
107
107
  class bbb ( $foo = 1, $bar = $a, $foo = 1, $bar = $a, $foo = 1, $bar = $a, ) {}
@@ -123,6 +123,17 @@ describe 'class_params_newline' do
123
123
  define long_ggg ($foo, $bar=[], $foo, $bar=[], $foo, $bar=[], $foo, $bar=[]) {}
124
124
 
125
125
  define asdf ($prefix, $pattern, $expire, $port, $prefix, $pattern, $expire, $port) { }
126
+
127
+ define asdf ($prefix, $pattern, $expire, $port, $prefix, $pattern=$a and $b, $epe=-$foo, $port=!$foo) { }
128
+
129
+ class aaa (
130
+ $aaa = func('bbb', $ccc), $bar = $a, $foo = 1, $bar = $a,
131
+ ) {}
132
+
133
+ class name (
134
+ Boolean $foo,
135
+ Optional[String[1]] $bar,
136
+ ) { }
126
137
  END
127
138
  end
128
139
 
@@ -150,7 +161,7 @@ describe 'class_params_newline' do
150
161
 
151
162
  class aaa ( $foo = 1, $bar = $a,) {}
152
163
  class aaa (
153
- $foo = 1,
164
+ Integer $foo = 1,
154
165
  $bar = $a,
155
166
  $foo = 1,
156
167
  $bar = $a,
@@ -212,6 +223,29 @@ describe 'class_params_newline' do
212
223
  $expire,
213
224
  $port
214
225
  ) { }
226
+
227
+ define asdf (
228
+ $prefix,
229
+ $pattern,
230
+ $expire,
231
+ $port,
232
+ $prefix,
233
+ $pattern=$a and $b,
234
+ $epe=-$foo,
235
+ $port=!$foo
236
+ ) { }
237
+
238
+ class aaa (
239
+ $aaa = func('bbb', $ccc),
240
+ $bar = $a,
241
+ $foo = 1,
242
+ $bar = $a,
243
+ ) {}
244
+
245
+ class name (
246
+ Boolean $foo,
247
+ Optional[String[1]] $bar,
248
+ ) { }
215
249
  END
216
250
  end
217
251
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-lint-class_alignment-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anh Pham
@@ -123,6 +123,7 @@ files:
123
123
  - LICENSE
124
124
  - README.md
125
125
  - lib/puppet-lint/plugins/check_class_alignment.rb
126
+ - lib/puppet-lint/plugins/check_class_helper.rb
126
127
  - lib/puppet-lint/plugins/check_class_params_newline.rb
127
128
  - spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
128
129
  - spec/puppet-lint/plugins/check_class_params_alignment_spec.rb