puppet-lint-class_alignment-check 0.2.5 → 0.3.2

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
  SHA256:
3
- metadata.gz: a097f4f616f220ca1061819f771b0e9553f89d91f2de912b271026d6a1be2640
4
- data.tar.gz: 0db8265872341ec18239a51144d2ec7fd288f71488f1b5bf2174726c17bf279d
3
+ metadata.gz: d8c9035498fac5688a9587ff1b5eaaf15027974e85ac46c9745419b3cd309319
4
+ data.tar.gz: c0d6f86ab41d272eb2f2e1883ebd2f9502888a1ba21d592523c8038d1f0dc5a4
5
5
  SHA512:
6
- metadata.gz: a5d927d414770af9512912b5ee24eeedfafbfb1987984f0ad5806e6950c1f9efc56ed542114bb1933eb9920f4fbc9367ef9f9233c2ec6c20ed170857e204010b
7
- data.tar.gz: 2dfa0a335795a1d5b21a35b9e68a01fcc19a2ef77efaa50a91c2460e12a7cae7e3fde92d772aa4fbc7f285bafbf5ceb98b62a7c146eb6cd140d9a748cdc02b97
6
+ metadata.gz: b1bd3766aa9650219858800d4374434fba8fca275fe905d1734917ddcfb17fe1ff64efb33ffdeaa98ca48ff85feff8242fe45127c2460f6cd752e854d5c18952
7
+ data.tar.gz: 9d1e88aa315c31b44660589c183f6beca1399a84219a6f6f57ad741fb1eae633d4bd7c92e537c3b6b2c73767cbb0a3f70084040540cbb0681030272bdca5bee6
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,106 @@
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
+ next if tokens.nil?
14
+
15
+ first_param = tokens.index { |token| a_param?(token) }
16
+ last_param = tokens.rindex { |token| a_param?(token) }
17
+ next if first_param.nil?
18
+ next if last_param.nil?
19
+
20
+ # Skip if there's only 1 param
21
+ next if tokens[first_param] == tokens[last_param]
22
+
23
+ tokens.each do |token|
24
+ if token == tokens[-1]
25
+ rparen = token
26
+ while rparen&.next_token
27
+ rparen = rparen.next_token
28
+ break if rparen.type == :RPAREN
29
+ end
30
+
31
+ last_code_token = token
32
+ while last_code_token&.prev_token
33
+ break unless %i[WHITESPACE INDENT NEWLINE].include?(last_code_token.type)
34
+
35
+ last_code_token = last_code_token.prev_token
36
+ end
37
+
38
+ next if rparen.line != last_code_token.line
39
+
40
+ notify(
41
+ :warning,
42
+ message: "`)` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
43
+ line: rparen.line,
44
+ column: rparen.column,
45
+ token: rparen,
46
+ newline: true,
47
+ newline_indent: item[:tokens][0].column - 1
48
+ )
49
+ break
50
+ end
51
+
52
+ next unless a_param?(token)
53
+
54
+ if token.prev_code_token.type == :LPAREN
55
+ next if token.line != token.prev_code_token.line
56
+ elsif token.line != prev_param_token(token).line
57
+ next
58
+ end
59
+
60
+ notify(
61
+ :warning,
62
+ message: "`#{token.to_manifest}` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
63
+ line: token.line,
64
+ column: token.column,
65
+ token: token,
66
+ newline: true,
67
+ newline_indent: item[:tokens][0].column + 1
68
+ )
69
+ end
70
+ end
71
+ end
72
+
73
+ def fix(problem)
74
+ token = problem[:token]
75
+ case token&.prev_code_token&.type
76
+ when :TYPE
77
+ token = token.prev_code_token
78
+ when :RBRACK
79
+ count = 0
80
+ while token&.prev_code_token
81
+ token = token.prev_code_token
82
+ case token.type
83
+ when :RBRACK
84
+ count += 1
85
+ when :LBRACK
86
+ count -= 1
87
+ end
88
+
89
+ break if count.zero?
90
+ end
91
+ token = token.prev_code_token
92
+ end
93
+
94
+ index = tokens.index(token.prev_code_token.next_token)
95
+ tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
96
+
97
+ # When there's no space at the beginning of the param
98
+ # e.g. class foo($bar="aaa") {}
99
+ if token.prev_code_token.next_token == token
100
+ tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:INDENT, ' ' * problem[:newline_indent], 0, 0))
101
+
102
+ elsif %i[WHITESPACE INDENT].include?(token.prev_token.type)
103
+ token.prev_token.value = ' ' * problem[:newline_indent]
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'class_params_newline' do
4
+ let(:msg) { '`%s` should be in a new 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
+
94
+ context 'in a single line' do
95
+ let(:code) do
96
+ <<-END
97
+ class foo ($foo = 1, $bar = $a) {}
98
+
99
+ class bar ($foo = 1, $bar = $a,) {}
100
+
101
+ class aaa ( $foo = 1, $bar = $a,) {}
102
+
103
+ class bbb ( $foo = 1, $bar = $a, ) {}
104
+
105
+ class ccc ($foo = 1) {}
106
+
107
+ class ddd {}
108
+ END
109
+ end
110
+
111
+ let(:fixed) do
112
+ <<-END
113
+ class foo (
114
+ $foo = 1,
115
+ $bar = $a
116
+ ) {}
117
+
118
+ class bar (
119
+ $foo = 1,
120
+ $bar = $a,
121
+ ) {}
122
+
123
+ class aaa (
124
+ $foo = 1,
125
+ $bar = $a,
126
+ ) {}
127
+
128
+ class bbb (
129
+ $foo = 1,
130
+ $bar = $a,
131
+ ) {}
132
+
133
+ class ccc ($foo = 1) {}
134
+
135
+ class ddd {}
136
+ END
137
+ end
138
+
139
+ it 'should fix the problems' do
140
+ expect(manifest).to eq(fixed)
141
+ end
142
+ end
143
+ end
144
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anh Pham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-08 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puppet-lint
@@ -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