puppet-lint-class_alignment-check 0.1.0 → 0.2.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 +4 -4
- data/README.md +3 -0
- data/spec/spec_helper.rb +24 -0
- metadata +20 -7
- data/lib/puppet-lint/plugins/check_class_params_alignment.rb +0 -213
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bea9133383ac53f7e568261f0b752c267b49773265c6dd71cbf76b7082cf5326
|
4
|
+
data.tar.gz: 7db3d124fb3714902d13da0db06f2ba31baa469b16e0b236cf124f77258b7b2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 538a2f9bb3dbe6575673a5ee8a5207a6ec09f2c1c3f245957f5f4f5149434f24ba7af08da337df97f6fa853158de9459b0332e2df4eceed91a094ec974716ebc
|
7
|
+
data.tar.gz: ec62f9ae3a83cbcf445dfd7607ae3aeb4bd70dedf266767a4b26385d18e516689db06dc70db43fbd0f7f7cb6ba87913a15e703a6b92edfd8da41f769e5f4485d
|
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
[](https://github.com/anhpt379/puppet-lint-class_alignment-check/blob/master/LICENSE)
|
4
4
|
[](https://github.com/anhpt379/puppet-lint-class_alignment-check/actions/workflows/test.yml)
|
5
|
+
[](https://codecov.io/gh/anhpt379/puppet-lint-class_alignment-check)
|
6
|
+
[](https://rubygems.org/gems/puppet-lint-class_alignment-check)
|
7
|
+
[](https://rubygems.org/gems/puppet-lint-class_alignment-check)
|
5
8
|
|
6
9
|
A puppet-lint plugin to check and fix class params/equals alignment.
|
7
10
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
begin
|
4
|
+
require 'simplecov'
|
5
|
+
require 'simplecov-console'
|
6
|
+
require 'codecov'
|
7
|
+
rescue LoadError
|
8
|
+
else
|
9
|
+
SimpleCov.start do
|
10
|
+
track_files 'lib/**/*.rb'
|
11
|
+
|
12
|
+
add_filter '/spec'
|
13
|
+
|
14
|
+
enable_coverage :branch
|
15
|
+
|
16
|
+
# do not track vendored files
|
17
|
+
add_filter '/vendor'
|
18
|
+
add_filter '/.vendor'
|
19
|
+
end
|
20
|
+
|
21
|
+
SimpleCov.formatters = [
|
22
|
+
SimpleCov::Formatter::Console,
|
23
|
+
SimpleCov::Formatter::Codecov
|
24
|
+
]
|
25
|
+
end
|
26
|
+
|
3
27
|
require 'puppet-lint'
|
4
28
|
|
5
29
|
PuppetLint::Plugins.load_spec_helper
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puppet-lint-class_alignment-check
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anh Pham
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2022-05-08 00:00:00.000000000 Z
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '3.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: codecov
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: rake
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,7 +101,7 @@ dependencies:
|
|
87
101
|
- !ruby/object:Gem::Version
|
88
102
|
version: '1.0'
|
89
103
|
- !ruby/object:Gem::Dependency
|
90
|
-
name: simplecov
|
104
|
+
name: simplecov-console
|
91
105
|
requirement: !ruby/object:Gem::Requirement
|
92
106
|
requirements:
|
93
107
|
- - ">="
|
@@ -108,7 +122,6 @@ extra_rdoc_files: []
|
|
108
122
|
files:
|
109
123
|
- LICENSE
|
110
124
|
- README.md
|
111
|
-
- lib/puppet-lint/plugins/check_class_params_alignment.rb
|
112
125
|
- spec/puppet-lint/plugins/check_class_equals_alignment_spec.rb
|
113
126
|
- spec/puppet-lint/plugins/check_class_params_alignment_spec.rb
|
114
127
|
- spec/spec_helper.rb
|
@@ -116,7 +129,7 @@ homepage: https://github.com/anhpt379/puppet-lint-class_alignment-check
|
|
116
129
|
licenses:
|
117
130
|
- MIT
|
118
131
|
metadata: {}
|
119
|
-
post_install_message:
|
132
|
+
post_install_message:
|
120
133
|
rdoc_options: []
|
121
134
|
require_paths:
|
122
135
|
- lib
|
@@ -131,8 +144,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
144
|
- !ruby/object:Gem::Version
|
132
145
|
version: '0'
|
133
146
|
requirements: []
|
134
|
-
rubygems_version: 3.2.
|
135
|
-
signing_key:
|
147
|
+
rubygems_version: 3.2.33
|
148
|
+
signing_key:
|
136
149
|
specification_version: 4
|
137
150
|
summary: A puppet-lint plugin to check & fix class params/equals alignment.
|
138
151
|
test_files:
|
@@ -1,213 +0,0 @@
|
|
1
|
-
# Parameters to classes or defined types must be uniformly indented in two
|
2
|
-
# spaces from the title. The equals sign should be aligned.
|
3
|
-
#
|
4
|
-
# https://puppet.com/docs/puppet/7/style_guide.html#style_guide_classes-param-indentation-alignment
|
5
|
-
|
6
|
-
require 'debug'
|
7
|
-
|
8
|
-
def a_param?(token)
|
9
|
-
if token.prev_code_token.type == :EQUALS
|
10
|
-
false
|
11
|
-
elsif token.prev_code_token.type == :FARROW
|
12
|
-
false
|
13
|
-
elsif token.type == :VARIABLE && !%i[DQPRE DQMID].include?(token.prev_code_token.type)
|
14
|
-
true
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def first_param_on_the_line?(token)
|
19
|
-
origin = token
|
20
|
-
while token&.prev_token
|
21
|
-
token = token.prev_token
|
22
|
-
|
23
|
-
break if token.type == :NEWLINE
|
24
|
-
end
|
25
|
-
|
26
|
-
while token&.next_token
|
27
|
-
token = token.next_token
|
28
|
-
|
29
|
-
break if token.type == :VARIABLE
|
30
|
-
end
|
31
|
-
|
32
|
-
origin == token
|
33
|
-
end
|
34
|
-
|
35
|
-
def the_one?(token, character)
|
36
|
-
case character
|
37
|
-
when '='
|
38
|
-
true if token.type == :EQUALS
|
39
|
-
when '$'
|
40
|
-
true if token.type == :VARIABLE && a_param?(token) && first_param_on_the_line?(token)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def get_the_first_param(token)
|
45
|
-
while token&.prev_code_token
|
46
|
-
token = token.prev_code_token
|
47
|
-
break if token.type == :CLASS
|
48
|
-
end
|
49
|
-
|
50
|
-
while token&.next_code_token
|
51
|
-
token = token.next_code_token
|
52
|
-
return token if token.type == :VARIABLE
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def get_prev_code_token(token, character)
|
57
|
-
case character
|
58
|
-
when '='
|
59
|
-
token.prev_code_token
|
60
|
-
when '$'
|
61
|
-
if token.prev_code_token
|
62
|
-
if %i[CLASSREF RBRACK].include?(token.prev_code_token.type)
|
63
|
-
token.prev_code_token
|
64
|
-
elsif token.prev_code_token.type == :LPAREN
|
65
|
-
token
|
66
|
-
elsif token.prev_code_token.type == :COMMA
|
67
|
-
get_the_first_param(token)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
# This function is copied & modified from puppet-lint arrow_alignment check
|
74
|
-
# https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L8
|
75
|
-
def check_for(character)
|
76
|
-
# I intentionally didn't rename `arrow` to another name, to keep the code as
|
77
|
-
# similar as the original one, to easier to update in the future.
|
78
|
-
(class_indexes + defined_type_indexes).each do |res_idx|
|
79
|
-
arrow_column = [0]
|
80
|
-
level_idx = 0
|
81
|
-
level_tokens = []
|
82
|
-
param_column = [nil]
|
83
|
-
resource_tokens = res_idx[:param_tokens]
|
84
|
-
next if resource_tokens.nil?
|
85
|
-
|
86
|
-
resource_tokens.reject! do |token|
|
87
|
-
COMMENT_TYPES.include?(token.type)
|
88
|
-
end
|
89
|
-
|
90
|
-
# If this is a single line resource, skip it
|
91
|
-
first_arrow = resource_tokens.index { |r| the_one?(r, character) }
|
92
|
-
last_arrow = resource_tokens.rindex { |r| the_one?(r, character) }
|
93
|
-
next if first_arrow.nil?
|
94
|
-
next if last_arrow.nil?
|
95
|
-
next if resource_tokens[first_arrow].line == resource_tokens[last_arrow].line
|
96
|
-
|
97
|
-
resource_tokens.each do |token|
|
98
|
-
if the_one?(token, character)
|
99
|
-
param_token = get_prev_code_token(token, character)
|
100
|
-
param_token = token if param_token.nil?
|
101
|
-
|
102
|
-
param_length = param_token.to_manifest.length
|
103
|
-
|
104
|
-
param_column[level_idx] = param_token.column if param_column[level_idx].nil?
|
105
|
-
|
106
|
-
if (level_tokens[level_idx] ||= []).any? { |t| t.line == token.line }
|
107
|
-
this_arrow_column = param_column[level_idx] + param_length + 1
|
108
|
-
elsif character == '$' && param_token.type == :VARIABLE
|
109
|
-
this_arrow_column = param_token.column
|
110
|
-
else
|
111
|
-
this_arrow_column = param_token.column + param_token.to_manifest.length
|
112
|
-
this_arrow_column += 1 if param_token.type != :INDENT
|
113
|
-
end
|
114
|
-
|
115
|
-
arrow_column[level_idx] = this_arrow_column if arrow_column[level_idx] < this_arrow_column
|
116
|
-
|
117
|
-
(level_tokens[level_idx] ||= []) << token
|
118
|
-
elsif token.prev_token.type == :LPAREN
|
119
|
-
level_idx += 1
|
120
|
-
arrow_column << 0
|
121
|
-
level_tokens[level_idx] ||= []
|
122
|
-
param_column << nil
|
123
|
-
elsif token.next_token.type == :RPAREN
|
124
|
-
if (level_tokens[level_idx] ||= []).map(&:line).uniq.length > 1
|
125
|
-
level_tokens[level_idx].each do |arrow_tok|
|
126
|
-
next if arrow_tok.column == arrow_column[level_idx] || level_tokens[level_idx].size == 1
|
127
|
-
|
128
|
-
arrows_on_line = level_tokens[level_idx].select { |t| t.line == arrow_tok.line }
|
129
|
-
notify(
|
130
|
-
:warning,
|
131
|
-
message: "indentation of #{character} is not properly aligned (expected in column #{arrow_column[level_idx]}, but found it in column #{arrow_tok.column})",
|
132
|
-
line: arrow_tok.line,
|
133
|
-
column: arrow_tok.column,
|
134
|
-
token: arrow_tok,
|
135
|
-
arrow_column: arrow_column[level_idx],
|
136
|
-
newline: arrows_on_line.index(arrow_tok) != 0,
|
137
|
-
newline_indent: param_column[level_idx] - 1
|
138
|
-
)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
arrow_column[level_idx] = 0
|
142
|
-
level_tokens[level_idx].clear
|
143
|
-
param_column[level_idx] = nil
|
144
|
-
level_idx -= 1
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
# This function is copied & modified from puppet-lint arrow_alignment fix
|
151
|
-
# https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L94
|
152
|
-
def fix_for(problem)
|
153
|
-
if problem[:newline]
|
154
|
-
index = tokens.index(problem[:token].prev_code_token.prev_token)
|
155
|
-
|
156
|
-
# insert newline
|
157
|
-
tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
|
158
|
-
|
159
|
-
# indent the parameter to the correct depth
|
160
|
-
problem[:token].prev_code_token.prev_token.type = :INDENT
|
161
|
-
problem[:token].prev_code_token.prev_token.value = ' ' * problem[:newline_indent]
|
162
|
-
|
163
|
-
end_param_idx = tokens.index(problem[:token].prev_code_token)
|
164
|
-
start_param_idx = tokens.index(problem[:token].prev_token_of(%i[INDENT NEWLINE]))
|
165
|
-
param_length = tokens[start_param_idx..end_param_idx].map do |r|
|
166
|
-
r.to_manifest.length
|
167
|
-
end.reduce(0) { |sum, x| sum + x } + 1
|
168
|
-
new_ws_len = problem[:arrow_column] - param_length
|
169
|
-
else
|
170
|
-
new_ws_len = if problem[:token].prev_token.type == :WHITESPACE
|
171
|
-
problem[:token].prev_token.to_manifest.length
|
172
|
-
else
|
173
|
-
0
|
174
|
-
end
|
175
|
-
new_ws_len += (problem[:arrow_column] - problem[:token].column)
|
176
|
-
end
|
177
|
-
|
178
|
-
if new_ws_len.negative?
|
179
|
-
raise PuppetLint::NoFix if problem[:token].prev_token.type != :INDENT
|
180
|
-
|
181
|
-
new_ws = problem[:token].prev_token.to_manifest[0...new_ws_len]
|
182
|
-
problem[:token].prev_token.value = new_ws
|
183
|
-
else
|
184
|
-
new_ws = ' ' * new_ws_len
|
185
|
-
|
186
|
-
if problem[:token].prev_token.type == :WHITESPACE
|
187
|
-
problem[:token].prev_token.value = new_ws
|
188
|
-
else
|
189
|
-
index = tokens.index(problem[:token].prev_token)
|
190
|
-
tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, new_ws, 0, 0))
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
PuppetLint.new_check(:class_params_alignment) do
|
196
|
-
def check
|
197
|
-
check_for('$')
|
198
|
-
end
|
199
|
-
|
200
|
-
def fix(problem)
|
201
|
-
fix_for(problem)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
PuppetLint.new_check(:class_equals_alignment) do
|
206
|
-
def check
|
207
|
-
check_for('=')
|
208
|
-
end
|
209
|
-
|
210
|
-
def fix(problem)
|
211
|
-
fix_for(problem)
|
212
|
-
end
|
213
|
-
end
|