puppet-lint-manifest_whitespace-check 0.0.1 → 0.1.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.
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ PuppetLint.new_check(:manifest_whitespace_opening_bracket_before) do
4
+ def check
5
+ tokens.select { |token| token.type == :LBRACK }.each do |bracket_token|
6
+ prev_token = bracket_token.prev_token
7
+ prev_code_token = prev_non_space_token(bracket_token)
8
+
9
+ next unless prev_token && prev_code_token
10
+ next if %i[LBRACK LBRACE COMMA SEMIC].include?(prev_code_token.type)
11
+ next unless %i[WHITESPACE NEWLINE INDENT].include?(prev_token.type)
12
+ next unless tokens.index(prev_code_token) != tokens.index(bracket_token) - 2 ||
13
+ !is_single_space(prev_token)
14
+
15
+ notify(
16
+ :error,
17
+ message: 'there should be a single space before an opening bracket',
18
+ line: bracket_token.line,
19
+ column: bracket_token.column,
20
+ token: bracket_token,
21
+ )
22
+ end
23
+ end
24
+
25
+ def fix(problem)
26
+ token = problem[:token]
27
+ prev_token = token.prev_token
28
+ prev_code_token = prev_non_space_token(token)
29
+
30
+ while prev_code_token != prev_token
31
+ unless %i[WHITESPACE INDENT NEWLINE].include?(prev_token.type)
32
+ raise PuppetLint::NoFix
33
+ end
34
+
35
+ remove_token(prev_token)
36
+ prev_token = prev_token.prev_token
37
+ end
38
+
39
+ add_token(tokens.index(token), new_single_space)
40
+ end
41
+ end
42
+
43
+ PuppetLint.new_check(:manifest_whitespace_opening_bracket_after) do
44
+ def check
45
+ tokens.select { |token| token.type == :LBRACK }.each do |bracket_token|
46
+ next_token = bracket_token.next_token
47
+
48
+ next unless next_token
49
+ next unless %i[WHITESPACE NEWLINE INDENT].include?(next_token.type)
50
+
51
+ if next_token.type == :NEWLINE
52
+ next_token = next_token.next_token
53
+ next if next_token.type != :NEWLINE
54
+ end
55
+
56
+ notify(
57
+ :error,
58
+ message: 'there should be no whitespace or a single newline after an opening bracket',
59
+ line: next_token.line,
60
+ column: next_token.column,
61
+ token: next_token,
62
+ )
63
+ end
64
+ end
65
+
66
+ def fix(problem)
67
+ token = problem[:token]
68
+
69
+ if token.type == :WHITESPACE
70
+ remove_token(token)
71
+ return
72
+ end
73
+
74
+ while token.next_token.type == :NEWLINE || (token.next_token.type == :INDENT && token.next_token.next_token.type != :NEWLINE)
75
+ remove_token(token)
76
+ token = token.next_token
77
+ end
78
+ end
79
+ end
@@ -7,3 +7,19 @@ end
7
7
  def new_single_space
8
8
  PuppetLint::Lexer::Token.new(:WHITESPACE, ' ', 0, 0)
9
9
  end
10
+
11
+ def after_bracket_tokens
12
+ %i[RBRACE RBRACK RPAREN SEMIC COMMA COLON NEWLINE DQPOST LBRACK]
13
+ end
14
+
15
+ def prev_non_space_token(token)
16
+ while token = token.prev_token
17
+ return token unless %i[WHITESPACE INDENT NEWLINE].include?(token.type)
18
+ end
19
+ end
20
+
21
+ def next_non_space_token(token)
22
+ while token = token.next_token
23
+ return token unless %i[WHITESPACE INDENT NEWLINE].include?(token.type)
24
+ end
25
+ end
@@ -69,9 +69,9 @@ describe 'manifest_whitespace_class_name_single_space_before' do
69
69
  end
70
70
 
71
71
  describe 'manifest_whitespace_class_name_single_space_after' do
72
- let(:single_space_msg) { 'there should be a single space between the class or resource name and the first bracket' }
72
+ let(:single_space_msg) { 'there should be a single space between the class or resource name and the first brace' }
73
73
 
74
- context 'with no spaces' do
74
+ context 'with parameters and no spaces' do
75
75
  let(:code) do
76
76
  <<~EOF
77
77
  # example
@@ -79,7 +79,9 @@ describe 'manifest_whitespace_class_name_single_space_after' do
79
79
  # Main class, includes all other classes.
80
80
  #
81
81
 
82
- class example{
82
+ class myclass(
83
+ $param1,
84
+ ) {
83
85
  class { 'example2':
84
86
  param1 => 'value1',
85
87
  }
@@ -122,7 +124,9 @@ describe 'manifest_whitespace_class_name_single_space_after' do
122
124
  # Main class, includes all other classes.
123
125
  #
124
126
 
125
- class example {
127
+ class myclass (
128
+ $param1,
129
+ ) {
126
130
  class { 'example2':
127
131
  param1 => 'value1',
128
132
  }
@@ -133,7 +137,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
133
137
  end
134
138
  end
135
139
 
136
- context 'with two spaces' do
140
+ context 'with scope and no spaces' do
137
141
  let(:code) do
138
142
  <<~EOF
139
143
  # example
@@ -141,7 +145,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
141
145
  # Main class, includes all other classes.
142
146
  #
143
147
 
144
- class example {
148
+ class mymodule::example{
145
149
  class { 'example2':
146
150
  param1 => 'value1',
147
151
  }
@@ -155,7 +159,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
155
159
  end
156
160
 
157
161
  it 'should create a error' do
158
- expect(problems).to contain_error(single_space_msg).on_line(6).in_column(14)
162
+ expect(problems).to contain_error(single_space_msg).on_line(6).in_column(24)
159
163
  end
160
164
  end
161
165
 
@@ -176,7 +180,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
176
180
  expect(problems).to contain_fixed(single_space_msg)
177
181
  end
178
182
 
179
- it 'should fix the space' do
183
+ it 'should fix the newline' do
180
184
  expect(manifest).to eq(
181
185
  <<~EOF,
182
186
  # example
@@ -184,7 +188,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
184
188
  # Main class, includes all other classes.
185
189
  #
186
190
 
187
- class example {
191
+ class mymodule::example {
188
192
  class { 'example2':
189
193
  param1 => 'value1',
190
194
  }
@@ -195,7 +199,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
195
199
  end
196
200
  end
197
201
 
198
- context 'with newline' do
202
+ context 'with no spaces' do
199
203
  let(:code) do
200
204
  <<~EOF
201
205
  # example
@@ -203,10 +207,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
203
207
  # Main class, includes all other classes.
204
208
  #
205
209
 
206
- class example
207
-
208
-
209
- {
210
+ class example{
210
211
  class { 'example2':
211
212
  param1 => 'value1',
212
213
  }
@@ -260,7 +261,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
260
261
  end
261
262
  end
262
263
 
263
- context 'with comment' do
264
+ context 'with two spaces' do
264
265
  let(:code) do
265
266
  <<~EOF
266
267
  # example
@@ -268,8 +269,7 @@ describe 'manifest_whitespace_class_name_single_space_after' do
268
269
  # Main class, includes all other classes.
269
270
  #
270
271
 
271
- class example # the class
272
- {
272
+ class example {
273
273
  class { 'example2':
274
274
  param1 => 'value1',
275
275
  }
@@ -300,62 +300,11 @@ describe 'manifest_whitespace_class_name_single_space_after' do
300
300
  expect(problems).to have(1).problem
301
301
  end
302
302
 
303
- it 'should not fix the manifest' do
304
- expect(problems).to contain_error(single_space_msg).on_line(6).in_column(14)
305
- end
306
- end
307
- end
308
- end
309
-
310
- describe 'manifest_whitespace_class_opening_curly_brace' do
311
- let(:opening_curly_brace_same_line_msg) { 'there should be a single space before the opening curly bracket of a class body' }
312
-
313
- context 'with no spaces' do
314
- let(:code) do
315
- <<~EOF
316
- # example
317
- #
318
- # Main class, includes all other classes.
319
- #
320
-
321
- class example (
322
- String $content,
323
- ){
324
- class { 'example2':
325
- param1 => 'value1',
326
- }
327
- }
328
- EOF
329
- end
330
-
331
- context 'with fix disabled' do
332
- it 'should detect a single problem' do
333
- expect(problems).to have(1).problem
334
- end
335
-
336
- it 'should create a error' do
337
- expect(problems).to contain_error(opening_curly_brace_same_line_msg).on_line(8).in_column(2)
338
- end
339
- end
340
-
341
- context 'with fix enabled' do
342
- before do
343
- PuppetLint.configuration.fix = true
344
- end
345
-
346
- after do
347
- PuppetLint.configuration.fix = false
348
- end
349
-
350
- it 'should detect a single problem' do
351
- expect(problems).to have(1).problem
352
- end
353
-
354
- it 'should create a error' do
355
- expect(problems).to contain_fixed(opening_curly_brace_same_line_msg)
303
+ it 'should fix the manifest' do
304
+ expect(problems).to contain_fixed(single_space_msg)
356
305
  end
357
306
 
358
- it 'should add a space' do
307
+ it 'should fix the space' do
359
308
  expect(manifest).to eq(
360
309
  <<~EOF,
361
310
  # example
@@ -363,10 +312,8 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
363
312
  # Main class, includes all other classes.
364
313
  #
365
314
 
366
- class example (
367
- String $content,
368
- ) {
369
- class { 'example2':
315
+ class example {
316
+ class { 'example2':
370
317
  param1 => 'value1',
371
318
  }
372
319
  }
@@ -376,7 +323,7 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
376
323
  end
377
324
  end
378
325
 
379
- context 'with two spaces' do
326
+ context 'with newline' do
380
327
  let(:code) do
381
328
  <<~EOF
382
329
  # example
@@ -384,77 +331,11 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
384
331
  # Main class, includes all other classes.
385
332
  #
386
333
 
387
- class example (
388
- String $content,
389
- ) {
390
- class { 'example2':
391
- param1 => 'value1',
392
- }
393
- }
394
- EOF
395
- end
396
-
397
- context 'with fix disabled' do
398
- it 'should detect a single problem' do
399
- expect(problems).to have(1).problem
400
- end
401
-
402
- it 'should create a error' do
403
- expect(problems).to contain_error(opening_curly_brace_same_line_msg).on_line(8).in_column(4)
404
- end
405
- end
406
-
407
- context 'with fix enabled' do
408
- before do
409
- PuppetLint.configuration.fix = true
410
- end
411
-
412
- after do
413
- PuppetLint.configuration.fix = false
414
- end
415
-
416
- it 'should detect a single problem' do
417
- expect(problems).to have(1).problem
418
- end
419
-
420
- it 'should create a error' do
421
- expect(problems).to contain_fixed(opening_curly_brace_same_line_msg)
422
- end
423
-
424
- it 'should remove a space' do
425
- expect(manifest).to eq(
426
- <<~EOF,
427
- # example
428
- #
429
- # Main class, includes all other classes.
430
- #
431
-
432
- class example (
433
- String $content,
434
- ) {
435
- class { 'example2':
436
- param1 => 'value1',
437
- }
438
- }
439
- EOF
440
- )
441
- end
442
- end
443
- end
334
+ class example
444
335
 
445
- context 'with newline' do
446
- let(:code) do
447
- <<~EOF
448
- # example
449
- #
450
- # Main class, includes all other classes.
451
- #
452
336
 
453
- class example (
454
- String $content,
455
- )
456
- {
457
- class { 'example2':
337
+ {
338
+ class { 'example2':
458
339
  param1 => 'value1',
459
340
  }
460
341
  }
@@ -467,7 +348,7 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
467
348
  end
468
349
 
469
350
  it 'should create a error' do
470
- expect(problems).to contain_error(opening_curly_brace_same_line_msg).on_line(9).in_column(1)
351
+ expect(problems).to contain_error(single_space_msg).on_line(6).in_column(14)
471
352
  end
472
353
  end
473
354
 
@@ -484,8 +365,8 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
484
365
  expect(problems).to have(1).problem
485
366
  end
486
367
 
487
- it 'should create a error' do
488
- expect(problems).to contain_fixed(opening_curly_brace_same_line_msg)
368
+ it 'should fix the manifest' do
369
+ expect(problems).to contain_fixed(single_space_msg)
489
370
  end
490
371
 
491
372
  it 'should fix the newline' do
@@ -496,10 +377,8 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
496
377
  # Main class, includes all other classes.
497
378
  #
498
379
 
499
- class example (
500
- String $content,
501
- ) {
502
- class { 'example2':
380
+ class example {
381
+ class { 'example2':
503
382
  param1 => 'value1',
504
383
  }
505
384
  }
@@ -517,11 +396,9 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
517
396
  # Main class, includes all other classes.
518
397
  #
519
398
 
520
- class example (
521
- String $content,
522
- ) # the class
399
+ class example # the class
523
400
  {
524
- class { 'example2':
401
+ class { 'example2':
525
402
  param1 => 'value1',
526
403
  }
527
404
  }
@@ -534,7 +411,7 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
534
411
  end
535
412
 
536
413
  it 'should create a error' do
537
- expect(problems).to contain_error(opening_curly_brace_same_line_msg).on_line(9).in_column(1)
414
+ expect(problems).to contain_error(single_space_msg).on_line(6).in_column(14)
538
415
  end
539
416
  end
540
417
 
@@ -552,7 +429,7 @@ describe 'manifest_whitespace_class_opening_curly_brace' do
552
429
  end
553
430
 
554
431
  it 'should not fix the manifest' do
555
- expect(problems).to contain_error(opening_curly_brace_same_line_msg).on_line(9).in_column(1)
432
+ expect(problems).to contain_error(single_space_msg).on_line(6).in_column(14)
556
433
  end
557
434
  end
558
435
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'manifest_whitespace_inherits_name_single_space_before' do
6
+ let(:single_space_msg) { 'there should be a single space between the inherits statement and the name' }
7
+
8
+ context 'with two spaces' do
9
+ let(:code) do
10
+ <<~EOF
11
+ # example
12
+ #
13
+ # Main class, includes all other classes.
14
+ #
15
+
16
+ class example inherits other::example {
17
+ class { 'example2':
18
+ param1 => 'value1',
19
+ }
20
+ }
21
+ EOF
22
+ end
23
+
24
+ context 'with fix disabled' do
25
+ it 'should detect a single problem' do
26
+ expect(problems).to have(1).problem
27
+ end
28
+
29
+ it 'should create a error' do
30
+ expect(problems).to contain_error(single_space_msg).on_line(6).in_column(23)
31
+ end
32
+ end
33
+
34
+ context 'with fix enabled' do
35
+ before do
36
+ PuppetLint.configuration.fix = true
37
+ end
38
+
39
+ after do
40
+ PuppetLint.configuration.fix = false
41
+ end
42
+
43
+ it 'should detect a single problem' do
44
+ expect(problems).to have(1).problem
45
+ end
46
+
47
+ it 'should fix the manifest' do
48
+ expect(problems).to contain_fixed(single_space_msg)
49
+ end
50
+
51
+ it 'should fix the space' do
52
+ expect(manifest).to eq(
53
+ <<~EOF,
54
+ # example
55
+ #
56
+ # Main class, includes all other classes.
57
+ #
58
+
59
+ class example inherits other::example {
60
+ class { 'example2':
61
+ param1 => 'value1',
62
+ }
63
+ }
64
+ EOF
65
+ )
66
+ end
67
+ end
68
+ end
69
+ end