puppet-lint-nine-check 0.4.0 → 0.5.0

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: 3dc35597d6a8c91070d3f0f897ad882a3e0d7f5baa26136c96f3c32de2447487
4
- data.tar.gz: d3e419564fb15a1728a7885ea243cd61b03ced9676c49d79f9b59f6216352abb
3
+ metadata.gz: c4708a12e4b0252d046a69e52f62ca9296daab1e6f0d356a16f712df5d56df5b
4
+ data.tar.gz: a903c154bc715a5dfd8c21a77eccff6ceb2baaeeaf4412a794d4b6bcea83bf44
5
5
  SHA512:
6
- metadata.gz: adb7afff9bca164ff1a683528169113bd8066a4ab9b4b8272ea88a71f6ae6b13205ec653484ebb721fbc6af68932a7ae7f1129dee5e6281b6bafcecb0380bd1e
7
- data.tar.gz: 1ce9cf998c731e36e11ed2a1c322905151df43c4a99aef80749c8617f29a3f4813486d432aab22d148da12cae5cfa1a4d0d13a28c3fdd3be990483712103bb33
6
+ metadata.gz: b56ffb029e88553a11a87a9c4f819cb54e985dc19e62e50032f4c5b8c2ab5efce592073405a809ed53d153aa26087fb67bb3e0553789256b16facf196cf33fee
7
+ data.tar.gz: 677c848e83b23cbc1b2488e922b75a6471e31145a9ac6c7961be63442369cfba9bc3308d8a8b387df76048f5dac936b380fa3305ad885ab719903d947ec87d57
@@ -0,0 +1,158 @@
1
+ # https://github.com/anhpt379/puppet-lint-class_alignment-check/blob/961d5457b0b6aa1eb10f3ceb543f90115b572f37/lib/puppet-lint/plugins/check_class_helper.rb
2
+ #
3
+ # Copyright (c) 2011 Tim Sharpe
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+
25
+ module ClassAlignment
26
+ class Helper
27
+ class << self
28
+ def a_param?(token)
29
+ if token&.prev_code_token&.type == :EQUALS
30
+ false
31
+ elsif token&.prev_code_token&.type == :FARROW
32
+ false
33
+ elsif %i[DQPRE DQMID].include?(token&.prev_code_token&.type)
34
+ false
35
+ elsif token&.type == :VARIABLE
36
+ # first var in the class
37
+ return true if token&.prev_token_of(:CLASS)&.next_token_of(:LPAREN)&.next_token_of(:VARIABLE) == token
38
+ return true if token&.prev_token_of(:DEFINE)&.next_token_of(:LPAREN)&.next_token_of(:VARIABLE) == token
39
+
40
+ count = 0
41
+ saw_a_comma = false
42
+ while token&.prev_token
43
+ token = token.prev_token
44
+
45
+ saw_a_comma = true if token.type == :COMMA
46
+
47
+ return false if token.type == :EQUALS && count == 0 && saw_a_comma == false
48
+
49
+ if %i[RPAREN RBRACK RBRACE].include?(token.type)
50
+ count -= 1
51
+ elsif %i[LPAREN LBRACK LBRACE].include?(token.type)
52
+ count += 1
53
+ end
54
+
55
+ break if %i[CLASS DEFINE].include?(token.type)
56
+ end
57
+
58
+ true if count == 1
59
+ end
60
+ end
61
+
62
+ def first_on_the_line?(token, type)
63
+ origin = token
64
+ while token&.prev_token
65
+ token = token.prev_token
66
+
67
+ break if token.type == :NEWLINE
68
+ end
69
+
70
+ while token&.next_token
71
+ token = token.next_token
72
+
73
+ break if token.type == type
74
+ end
75
+
76
+ origin == token
77
+ end
78
+
79
+ def the_one?(token, character)
80
+ case character
81
+ when "="
82
+ true if token.type == :EQUALS && first_on_the_line?(token, :EQUALS)
83
+ when "$"
84
+ true if a_param?(token) && first_on_the_line?(token, :VARIABLE)
85
+ end
86
+ end
87
+
88
+ def get_the_first_param(token)
89
+ while token&.prev_code_token
90
+ token = token.prev_code_token
91
+ break if token.type == :CLASS
92
+ end
93
+
94
+ while token&.next_code_token
95
+ token = token.next_code_token
96
+ return token if token.type == :VARIABLE
97
+ end
98
+ end
99
+
100
+ def get_prev_code_token(token, character)
101
+ case character
102
+ when "="
103
+ token.prev_code_token
104
+ when "$"
105
+ if token.prev_code_token
106
+ if %i[CLASSREF RBRACK].include?(token.prev_code_token.type)
107
+ token.prev_code_token
108
+ elsif token.prev_code_token.type == :LPAREN
109
+ token
110
+ elsif token.prev_code_token.type == :COMMA
111
+ get_the_first_param(token)
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ def get_param_start_token(token)
118
+ return nil unless a_param?(token)
119
+
120
+ case token&.prev_code_token&.type
121
+
122
+ # Integer $db_port
123
+ when :TYPE
124
+ token.prev_code_token
125
+
126
+ when :CLASSREF
127
+ token.prev_code_token
128
+
129
+ # Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port
130
+ when :RBRACK
131
+ count = 0
132
+ while token&.prev_code_token
133
+ token = token.prev_code_token
134
+ case token.type
135
+ when :RBRACK
136
+ count += 1
137
+ when :LBRACK
138
+ count -= 1
139
+ end
140
+
141
+ break if count.zero?
142
+ end
143
+ token.prev_code_token
144
+
145
+ else
146
+ token
147
+ end
148
+ end
149
+
150
+ def get_prev_param_token(token)
151
+ while token&.prev_code_token
152
+ token = token.prev_code_token
153
+ return token if a_param?(token)
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,155 @@
1
+ # https://github.com/anhpt379/puppet-lint-class_alignment-check/blob/b62e5c52568fca00413669df745453ff80c76a91/lib/puppet-lint/plugins/check_class_alignment.rb
2
+ #
3
+ # Copyright (c) 2011 Tim Sharpe
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+ # Parameters to classes or defined types must be uniformly indented in two
25
+ # spaces from the title. The equals sign should be aligned.
26
+ #
27
+ # https://puppet.com/docs/puppet/7/style_guide.html#style_guide_classes-param-indentation-alignment
28
+
29
+ require_relative "class_alignment/helper"
30
+
31
+ # This function is copied & modified from puppet-lint arrow_alignment check
32
+ # https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L8
33
+ def check_for(character)
34
+ # I intentionally didn't rename `arrow` to another name, to keep the code as
35
+ # similar as the original one, to easier to update in the future.
36
+ (class_indexes + defined_type_indexes).each do |res_idx|
37
+ arrow_column = [0]
38
+ level_idx = 0
39
+ level_tokens = []
40
+ param_column = [nil]
41
+ resource_tokens = res_idx[:param_tokens]
42
+ next if resource_tokens.nil?
43
+
44
+ resource_tokens.reject! do |token|
45
+ COMMENT_TYPES.include?(token.type)
46
+ end
47
+
48
+ # If this is a single line resource, skip it
49
+ first_arrow = resource_tokens.index { |r| ClassAlignment::Helper.the_one?(r, character) }
50
+ last_arrow = resource_tokens.rindex { |r| ClassAlignment::Helper.the_one?(r, character) }
51
+ next if first_arrow.nil?
52
+ next if last_arrow.nil?
53
+ next if resource_tokens[first_arrow].line == resource_tokens[last_arrow].line
54
+
55
+ resource_tokens.each do |token|
56
+ if ClassAlignment::Helper.the_one?(token, character)
57
+ param_token = ClassAlignment::Helper.get_prev_code_token(token, character)
58
+ param_token = token if param_token.nil?
59
+
60
+ param_length = param_token.to_manifest.length
61
+
62
+ param_column[level_idx] = param_token.column if param_column[level_idx].nil?
63
+
64
+ if (level_tokens[level_idx] ||= []).any? { |t| t.line == token.line }
65
+ this_arrow_column = param_column[level_idx] + param_length + 1
66
+ elsif character == "$" && param_token.type == :VARIABLE
67
+ this_arrow_column = param_token.column
68
+ else
69
+ this_arrow_column = param_token.column + param_token.to_manifest.length
70
+ this_arrow_column += 1 if param_token.type != :INDENT
71
+ end
72
+
73
+ arrow_column[level_idx] = this_arrow_column if arrow_column[level_idx] < this_arrow_column
74
+
75
+ (level_tokens[level_idx] ||= []) << token
76
+ elsif token == resource_tokens[0]
77
+ level_idx += 1
78
+ arrow_column << 0
79
+ level_tokens[level_idx] ||= []
80
+ param_column << nil
81
+ elsif token == resource_tokens[-1]
82
+ if (level_tokens[level_idx] ||= []).map(&:line).uniq.length > 1
83
+ level_tokens[level_idx].each do |arrow_tok|
84
+ next if arrow_tok.column == arrow_column[level_idx] || level_tokens[level_idx].size == 1
85
+
86
+ arrows_on_line = level_tokens[level_idx].select { |t| t.line == arrow_tok.line }
87
+ notify(
88
+ :warning,
89
+ message: "indentation of #{character} is not properly aligned (expected in column #{arrow_column[level_idx]}, but found it in column #{arrow_tok.column})",
90
+ line: arrow_tok.line,
91
+ column: arrow_tok.column,
92
+ token: arrow_tok,
93
+ arrow_column: arrow_column[level_idx],
94
+ newline: arrows_on_line.index(arrow_tok) != 0,
95
+ newline_indent: param_column[level_idx] - 1
96
+ )
97
+ end
98
+ end
99
+ arrow_column[level_idx] = 0
100
+ level_tokens[level_idx].clear
101
+ param_column[level_idx] = nil
102
+ level_idx -= 1
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ # This function is copied & modified from puppet-lint arrow_alignment fix
109
+ # https://github.com/puppetlabs/puppet-lint/blob/020143b705b023946739eb44e7c7d99fcd087527/lib/puppet-lint/plugins/check_whitespace/arrow_alignment.rb#L94
110
+ def fix_for(problem)
111
+ raise PuppetLint::NoFix if problem[:newline]
112
+
113
+ new_ws_len = if problem[:token].prev_token.type == :WHITESPACE
114
+ problem[:token].prev_token.to_manifest.length
115
+ else
116
+ 0
117
+ end
118
+ new_ws_len += (problem[:arrow_column] - problem[:token].column)
119
+
120
+ if new_ws_len.negative?
121
+ raise PuppetLint::NoFix if problem[:token].prev_token.type != :INDENT
122
+
123
+ new_ws = problem[:token].prev_token.to_manifest[0...new_ws_len]
124
+ problem[:token].prev_token.value = new_ws
125
+ else
126
+ new_ws = " " * new_ws_len
127
+
128
+ if problem[:token].prev_token.type == :WHITESPACE
129
+ problem[:token].prev_token.value = new_ws
130
+ else
131
+ index = tokens.index(problem[:token].prev_token)
132
+ tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:WHITESPACE, new_ws, 0, 0))
133
+ end
134
+ end
135
+ end
136
+
137
+ PuppetLint.new_check(:class_alignment_params) do
138
+ def check
139
+ check_for("$")
140
+ end
141
+
142
+ def fix(problem)
143
+ fix_for(problem)
144
+ end
145
+ end
146
+
147
+ PuppetLint.new_check(:class_alignment_equals) do
148
+ def check
149
+ check_for("=")
150
+ end
151
+
152
+ def fix(problem)
153
+ fix_for(problem)
154
+ end
155
+ end
@@ -0,0 +1,108 @@
1
+ # https://github.com/anhpt379/puppet-lint-class_alignment-check/blob/961d5457b0b6aa1eb10f3ceb543f90115b572f37/lib/puppet-lint/plugins/check_class_helper.rb
2
+ #
3
+ # Copyright (c) 2011 Tim Sharpe
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #
24
+
25
+ require_relative "class_alignment/helper"
26
+
27
+ PuppetLint.new_check(:class_alignment_params_newline) do
28
+ def check
29
+ (class_indexes + defined_type_indexes).each do |item|
30
+ tokens = item[:param_tokens]
31
+ next if tokens.nil?
32
+
33
+ # Skip if line length < 80 chars
34
+ first_paren = tokens[0]&.prev_token_of(:LPAREN)
35
+ last_paren = tokens[-1]&.next_token_of(:RPAREN)
36
+ next if first_paren.nil?
37
+ next if last_paren.nil?
38
+ next if first_paren.line == last_paren.line && last_paren.column < 80
39
+
40
+ tokens.each do |token|
41
+ if token == tokens[-1]
42
+ rparen = token.next_token_of(:RPAREN)
43
+
44
+ last_code_token = token
45
+ while last_code_token&.prev_token
46
+ break unless %i[WHITESPACE INDENT NEWLINE].include?(last_code_token.type)
47
+
48
+ last_code_token = last_code_token.prev_token
49
+ end
50
+
51
+ next if rparen.line != last_code_token.line
52
+
53
+ notify(
54
+ :warning,
55
+ message: "`)` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
56
+ line: rparen.line,
57
+ column: rparen.column,
58
+ token: rparen,
59
+ newline: true,
60
+ newline_indent: item[:tokens][0].column - 1
61
+ )
62
+ end
63
+
64
+ next unless ClassAlignment::Helper.a_param?(token)
65
+
66
+ if ClassAlignment::Helper.get_param_start_token(token)&.prev_code_token&.type == :LPAREN
67
+ next if token.line != ClassAlignment::Helper.get_param_start_token(token).prev_code_token.line
68
+ elsif token.line != ClassAlignment::Helper.get_prev_param_token(token)&.line
69
+ next
70
+ end
71
+
72
+ notify(
73
+ :warning,
74
+ message: "`#{token.to_manifest}` should be in a new line (expected in line #{token.line + 1}, but found it in line #{token.line})",
75
+ line: token.line,
76
+ column: token.column,
77
+ token: token,
78
+ newline: true,
79
+ newline_indent: item[:tokens][0].column + 1
80
+ )
81
+ end
82
+ end
83
+ end
84
+
85
+ def fix(problem)
86
+ token = problem[:token]
87
+ token = ClassAlignment::Helper.get_param_start_token(token) if token.type == :VARIABLE
88
+
89
+ last_non_whitespace_token = token.prev_token
90
+ while last_non_whitespace_token&.prev_token
91
+ break unless %i[WHITESPACE INDENT NEWLINE].include?(last_non_whitespace_token.type)
92
+
93
+ last_non_whitespace_token = last_non_whitespace_token.prev_token
94
+ end
95
+
96
+ index = tokens.index(last_non_whitespace_token) + 1
97
+ tokens.insert(index, PuppetLint::Lexer::Token.new(:NEWLINE, "\n", 0, 0))
98
+
99
+ # When there's no space at the beginning of the param
100
+ # e.g. class foo($bar="aaa") {}
101
+ if token.prev_code_token.next_token == token
102
+ tokens.insert(index + 1, PuppetLint::Lexer::Token.new(:INDENT, " " * problem[:newline_indent], 0, 0))
103
+
104
+ elsif %i[WHITESPACE INDENT].include?(token.prev_token.type)
105
+ token.prev_token.value = " " * problem[:newline_indent]
106
+ end
107
+ end
108
+ end