puppet-lint-class_alignment-check 0.2.5 → 0.3.0

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: a097f4f616f220ca1061819f771b0e9553f89d91f2de912b271026d6a1be2640
4
- data.tar.gz: 0db8265872341ec18239a51144d2ec7fd288f71488f1b5bf2174726c17bf279d
3
+ metadata.gz: fe4a55df13a00eb32e3dab657bc0d1dc1e2982851d3992ec1329210b2642ccc5
4
+ data.tar.gz: 0d227e2da3f0dfa710ee46a11c3a9bd0e6e4fbf05728e96b553ae28a018c305e
5
5
  SHA512:
6
- metadata.gz: a5d927d414770af9512912b5ee24eeedfafbfb1987984f0ad5806e6950c1f9efc56ed542114bb1933eb9920f4fbc9367ef9f9233c2ec6c20ed170857e204010b
7
- data.tar.gz: 2dfa0a335795a1d5b21a35b9e68a01fcc19a2ef77efaa50a91c2460e12a7cae7e3fde92d772aa4fbc7f285bafbf5ceb98b62a7c146eb6cd140d9a748cdc02b97
6
+ metadata.gz: 4c33eb7910db84735f6baa86e020b604cdc272c18d726c7c53bb34b3995b6ca874fc5da985756df1f34f985cc80385f2477fe571756ff0c8da374ca0e1001de8
7
+ data.tar.gz: 9d3122d562637f655b4fdb251e59256c3a29a42b6affecf78d26d9618cacacd81d3ae698541d5fda6b0e1aa1350d71807bfbd87b5f55213d671bb7ba0f010a41
data/README.md CHANGED
@@ -11,13 +11,26 @@ A puppet-lint plugin to check and fix class params/equals alignment.
11
11
 
12
12
  ## Usage
13
13
 
14
- This plugin provides 2 checks for puppet-lint:
14
+ This plugin provides 3 checks for puppet-lint:
15
15
 
16
+ - `class_params_newline`
16
17
  - `class_params_alignment`
17
18
  - `class_equals_alignment`
18
19
 
19
20
  It supports `--fix` flag.
20
21
 
22
+ To properly reformat the code, run `puppet-lint --fix` like this:
23
+
24
+ ```bash
25
+ $ puppet-lint --only-checks class_params_newline --fix .
26
+ $ puppet-lint --only-checks class_params_alignment --fix .
27
+ $ puppet-lint --only-checks class_equals_alignment --fix .
28
+
29
+ # It's best to combine the above checks with `strict_indent` to fix all remaining issues
30
+ $ puppet-lint --only-checks strict_indent --fix .
31
+
32
+ ```
33
+
21
34
  > Parameters to classes or defined types must be uniformly indented in two
22
35
  > spaces from the title. The equals sign should be aligned.
23
36
  >
@@ -0,0 +1,70 @@
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
+ PuppetLint.new_check(:class_params_newline) do
10
+ def check
11
+ (class_indexes + defined_type_indexes).each do |item|
12
+ tokens = item[:param_tokens]
13
+
14
+ first_param = tokens.index { |token| a_param?(token) }
15
+ last_param = tokens.rindex { |token| a_param?(token) }
16
+ next if first_param.nil?
17
+ next if last_param.nil?
18
+
19
+ # Skip if there's only 1 param
20
+ next if tokens[first_param] == tokens[last_param]
21
+
22
+ tokens.each do |token|
23
+ next unless a_param?(token)
24
+
25
+ if token.prev_code_token == :LPAREN
26
+ next if token.line != token.prev_code_token.line
27
+ elsif token.line != prev_param_token(token).line
28
+ next
29
+ end
30
+
31
+ notify(
32
+ :warning,
33
+ message: "Parameter #{token.to_manifest} should have its own line (expected in line #{token.line + 1}, but found it in line #{token.line})",
34
+ line: token.line,
35
+ column: token.column,
36
+ token: token,
37
+ newline: true,
38
+ newline_indent: item[:tokens][0].column + 1
39
+ )
40
+ end
41
+ end
42
+ end
43
+
44
+ def fix(problem)
45
+ token = problem[:token]
46
+ case token&.prev_code_token&.type
47
+ when :TYPE
48
+ token = token.prev_code_token
49
+ when :RBRACK
50
+ count = 0
51
+ while token&.prev_code_token
52
+ token = token.prev_code_token
53
+ case token.type
54
+ when :RBRACK
55
+ count += 1
56
+ when :LBRACK
57
+ count -= 1
58
+ end
59
+
60
+ break if count.zero?
61
+ end
62
+ token = token.prev_code_token
63
+ end
64
+
65
+ index = tokens.index(token.prev_code_token.next_token)
66
+ tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
67
+
68
+ token.prev_token.value = ' ' * problem[:newline_indent] if %i[WHITESPACE INDENT].include?(token.prev_token.type)
69
+ end
70
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'class_params_newline' do
4
+ let(:msg) { 'Parameter %s should have its own line (expected in line %d, but found it in line %d)' }
5
+
6
+ context 'with fix disabled' do
7
+ context 'tidy code' do
8
+ let(:code) do
9
+ <<-END
10
+ class bar(
11
+ $someparam = 'somevalue',
12
+ $another,
13
+ String $nope,
14
+ $foo,
15
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
16
+ # $aaa,
17
+ String $third = 'meh',
18
+ Array[Int, Int] $asdf = 'asdf',
19
+ ) { }
20
+ END
21
+ end
22
+
23
+ it 'should detect 0 problems' do
24
+ expect(problems).to have(0).problems
25
+ end
26
+ end
27
+
28
+ context 'messy code' do
29
+ let(:code) do
30
+ <<-END
31
+ class bar( $someparam = 'somevalue',
32
+ $another, String $nope, $foo,
33
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
34
+ # $aaa,
35
+ String $third = 'meh', Array[Int, Int] $asdf = 'asdf',
36
+ ) { }
37
+ END
38
+ end
39
+
40
+ it 'should detect 4 problems' do
41
+ expect(problems).to have(4).problems
42
+ end
43
+
44
+ it 'should create four warnings' do
45
+ expect(problems).to contain_warning(format(msg, '$someparam', 2, 1)).on_line(1).in_column(77)
46
+ expect(problems).to contain_warning(format(msg, '$nope', 3, 2)).on_line(2).in_column(30)
47
+ expect(problems).to contain_warning(format(msg, '$foo', 3, 2)).on_line(2).in_column(37)
48
+ expect(problems).to contain_warning(format(msg, '$asdf', 6, 5)).on_line(5).in_column(101)
49
+ end
50
+ end
51
+ end
52
+
53
+ context 'with fix enabled' do
54
+ before do
55
+ PuppetLint.configuration.fix = true
56
+ end
57
+
58
+ after do
59
+ PuppetLint.configuration.fix = false
60
+ end
61
+
62
+ context 'messy code' do
63
+ let(:code) do
64
+ <<-END
65
+ class bar( $someparam = 'somevalue',
66
+ $another, String $nope, $foo,
67
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
68
+ # $aaa,
69
+ String $third = 'meh', Array[Int, Int] $asdf = 'asdf',
70
+ ) { }
71
+ END
72
+ end
73
+
74
+ let(:fixed) do
75
+ <<-END
76
+ class bar(
77
+ $someparam = 'somevalue',
78
+ $another,
79
+ String $nope,
80
+ $foo,
81
+ Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port,
82
+ # $aaa,
83
+ String $third = 'meh',
84
+ Array[Int, Int] $asdf = 'asdf',
85
+ ) { }
86
+ END
87
+ end
88
+
89
+ it 'should fix the problems' do
90
+ expect(manifest).to eq(fixed)
91
+ end
92
+ end
93
+ end
94
+ end
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.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anh Pham
@@ -123,8 +123,10 @@ 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_params_newline.rb
126
127
  - spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
127
128
  - spec/puppet-lint/plugins/check_class_params_alignment_spec.rb
129
+ - spec/puppet-lint/plugins/check_class_params_newline_spec.rb
128
130
  - spec/spec_helper.rb
129
131
  homepage: https://github.com/anhpt379/puppet-lint-class_alignment-check
130
132
  licenses:
@@ -152,4 +154,5 @@ summary: A puppet-lint plugin to check & fix class params/equals alignment.
152
154
  test_files:
153
155
  - spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
154
156
  - spec/puppet-lint/plugins/check_class_params_alignment_spec.rb
157
+ - spec/puppet-lint/plugins/check_class_params_newline_spec.rb
155
158
  - spec/spec_helper.rb