Skip to content

Commit 35aa39a

Browse files
committed
rubocop: autofix
1 parent 254fa9d commit 35aa39a

File tree

5 files changed

+184
-86
lines changed

5 files changed

+184
-86
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
inherit_from: .rubocop_todo.yml
3+
24
# Managed by modulesync - DO NOT EDIT
35
# https://voxpupuli.org/docs/updating-files-managed-with-modulesync/
46

.rubocop_todo.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2023-04-21 12:08:55 UTC using RuboCop version 1.50.2.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 1
10+
Lint/UselessAssignment:
11+
Exclude:
12+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
13+
14+
# Offense count: 4
15+
# Configuration parameters: ForbiddenDelimiters.
16+
# ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$))
17+
Naming/HeredocDelimiterNaming:
18+
Exclude:
19+
- 'puppet-lint-strict_indent-check.gemspec'
20+
- 'spec/puppet-lint/plugins/check_strict_indent_spec.rb'
21+
22+
# Offense count: 5
23+
# Configuration parameters: MinSize.
24+
Performance/CollectionLiteralInLoop:
25+
Exclude:
26+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
27+
28+
# Offense count: 4
29+
# Configuration parameters: Prefixes, AllowedPatterns.
30+
# Prefixes: when, with, without
31+
RSpec/ContextWording:
32+
Exclude:
33+
- 'spec/puppet-lint/plugins/check_strict_indent_spec.rb'
34+
35+
# Offense count: 1
36+
# Configuration parameters: IgnoredMetadata.
37+
RSpec/DescribeClass:
38+
Exclude:
39+
- '**/spec/features/**/*'
40+
- '**/spec/requests/**/*'
41+
- '**/spec/routing/**/*'
42+
- '**/spec/system/**/*'
43+
- '**/spec/views/**/*'
44+
- 'spec/puppet-lint/plugins/check_strict_indent_spec.rb'
45+
46+
# Offense count: 10
47+
# This cop supports unsafe autocorrection (--autocorrect-all).
48+
# Configuration parameters: EnforcedStyle.
49+
# SupportedStyles: always, conditionals
50+
Style/AndOr:
51+
Exclude:
52+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
53+
54+
# Offense count: 1
55+
# This cop supports safe autocorrection (--autocorrect).
56+
# Configuration parameters: EnforcedStyle, AllowComments.
57+
# SupportedStyles: empty, nil, both
58+
Style/EmptyElse:
59+
Exclude:
60+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
61+
62+
# Offense count: 5
63+
# This cop supports unsafe autocorrection (--autocorrect-all).
64+
# Configuration parameters: EnforcedStyle.
65+
# SupportedStyles: always, always_true, never
66+
Style/FrozenStringLiteralComment:
67+
Exclude:
68+
- 'Gemfile'
69+
- 'Rakefile'
70+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
71+
- 'puppet-lint-strict_indent-check.gemspec'
72+
- 'spec/puppet-lint/plugins/check_strict_indent_spec.rb'
73+
74+
# Offense count: 4
75+
Style/MultilineBlockChain:
76+
Exclude:
77+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
78+
- 'spec/puppet-lint/plugins/check_strict_indent_spec.rb'
79+
80+
# Offense count: 2
81+
# This cop supports unsafe autocorrection (--autocorrect-all).
82+
# Configuration parameters: EnforcedStyle, AllowedMethods, AllowedPatterns.
83+
# SupportedStyles: predicate, comparison
84+
Style/NumericPredicate:
85+
Exclude:
86+
- 'spec/**/*'
87+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
88+
89+
# Offense count: 1
90+
# This cop supports unsafe autocorrection (--autocorrect-all).
91+
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
92+
# AllowedMethods: present?, blank?, presence, try, try!
93+
Style/SafeNavigation:
94+
Exclude:
95+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
96+
97+
# Offense count: 1
98+
# This cop supports safe autocorrection (--autocorrect).
99+
Style/WhileUntilModifier:
100+
Exclude:
101+
- 'lib/puppet-lint/plugins/check_strict_indent.rb'
102+
103+
# Offense count: 2
104+
# This cop supports safe autocorrection (--autocorrect).
105+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
106+
# URISchemes: http, https
107+
Layout/LineLength:
108+
Max: 137

lib/puppet-lint/plugins/check_strict_indent.rb

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# Public: Check the manifest tokens for correct indent levels and
22
# record a warning for each instance found.
33

4-
PuppetLint.new_check(:'strict_indent') do
4+
PuppetLint.new_check(:strict_indent) do
55
def match(tokens)
66
opening_token = {
7-
:RBRACE => :LBRACE,
8-
:RBRACK => :LBRACK,
9-
:RPAREN => :LPAREN,
10-
:HEREDOC => :HEREDOC_OPEN,
11-
:HEREDOC_POST => :HEREDOC_OPEN,
7+
RBRACE: :LBRACE,
8+
RBRACK: :LBRACK,
9+
RPAREN: :LPAREN,
10+
HEREDOC: :HEREDOC_OPEN,
11+
HEREDOC_POST: :HEREDOC_OPEN,
1212
}
1313
open = {
14-
:LBRACE => [],
15-
:LBRACK => [],
16-
:LPAREN => [],
17-
:HEREDOC_OPEN => [],
14+
LBRACE: [],
15+
LBRACK: [],
16+
LPAREN: [],
17+
HEREDOC_OPEN: [],
1818
}
1919

2020
matches = {}
2121

2222
tokens.each do |token|
23-
if [:LBRACE, :LBRACK, :LPAREN, :HEREDOC_OPEN].include?(token.type)
23+
if %i[LBRACE LBRACK LPAREN HEREDOC_OPEN].include?(token.type)
2424
open[token.type] << token
25-
elsif [:RBRACE, :RBRACK, :RPAREN, :HEREDOC, :HEREDOC_POST].include?(token.type)
25+
elsif %i[RBRACE RBRACK RPAREN HEREDOC HEREDOC_POST].include?(token.type)
2626
match = open[opening_token[token.type]].pop
27-
if not match.nil?
27+
unless match.nil?
2828
matches[token] = match
2929
matches[match] = token
3030
end
@@ -41,34 +41,31 @@ def check
4141

4242
matches = match(tokens)
4343

44-
tokens.select { |token|
44+
tokens.select do |token|
4545
token.type == :NEWLINE
46-
}.reject { |token|
46+
end.reject do |token|
4747
# ignore newline at end of code
4848
token.next_token.nil?
49-
}.each do |token|
49+
end.each do |token|
5050
temp_indent = 0
5151

5252
# indent for open groups in the previous line
5353
open_groups = 0
5454
prev_token = token.prev_token
55-
while not prev_token.nil? and prev_token.type != :NEWLINE
56-
if prev_token.type == :HEREDOC_OPEN
57-
temp_indent += 1
58-
end
59-
if [:LBRACE, :LBRACK, :LPAREN].include?(prev_token.type)
60-
if matches[prev_token].nil? or matches[prev_token].line > prev_token.line
61-
# left braces not matched in the same line increase indent
62-
open_groups += 1
63-
end
55+
while !prev_token.nil? and prev_token.type != :NEWLINE
56+
temp_indent += 1 if prev_token.type == :HEREDOC_OPEN
57+
if %i[LBRACE LBRACK
58+
LPAREN].include?(prev_token.type) && (matches[prev_token].nil? or matches[prev_token].line > prev_token.line)
59+
# left braces not matched in the same line increase indent
60+
open_groups += 1
6461
end
6562
prev_token = prev_token.prev_token
6663
end
6764
indent += open_groups
6865

6966
# reset prev_token to last non-whitespace token on previous line
7067
prev_token = token.prev_token
71-
while not prev_token.nil? and (prev_token.type == :WHITESPACE or prev_token.type == :COMMENT)
68+
while !prev_token.nil? and (prev_token.type == :WHITESPACE or prev_token.type == :COMMENT)
7269
prev_token = prev_token.prev_token
7370
end
7471

@@ -88,7 +85,7 @@ def check
8885
end
8986
end
9087
when :SEMIC
91-
if not colon_indent.nil?
88+
unless colon_indent.nil?
9289
# only unindent for a semicolon when we've indented for a colon
9390
colon_indent = nil
9491
indent -= 1
@@ -99,18 +96,16 @@ def check
9996

10097
# unindent for closing brackets in the current line
10198
next_token = token.next_token
102-
while not next_token.nil? and next_token.type != :NEWLINE
103-
if [:RBRACE, :RBRACK, :RPAREN].include?(next_token.type)
104-
if not matches[next_token].nil? and matches[next_token].line < next_token.line
99+
while !next_token.nil? and next_token.type != :NEWLINE
100+
if %i[RBRACE RBRACK RPAREN].include?(next_token.type)
101+
if !matches[next_token].nil? and matches[next_token].line < next_token.line
105102
# right braces matched in a previous line decrease indent
106103
indent -= 1
107104
end
108-
if next_token.type == :RBRACE and not colon_indent.nil?
109-
if not matches[next_token].nil? and matches[next_token].line < colon_indent
110-
# unindent at the end of resources if needed
111-
indent -= 1
112-
colon_indent = nil
113-
end
105+
if next_token.type == :RBRACE and !colon_indent.nil? && (!matches[next_token].nil? and matches[next_token].line < colon_indent)
106+
# unindent at the end of resources if needed
107+
indent -= 1
108+
colon_indent = nil
114109
end
115110
end
116111
next_token = next_token.next_token
@@ -119,53 +114,53 @@ def check
119114
# obviously we have a problem
120115
if indent < 0
121116
notify :error, {
122-
:message => 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues',
123-
:line => token.next_token.line,
124-
:column => token.next_token.column,
117+
message: 'Error calculating indent. Please file an issue at https://github.com/relud/puppet-lint-indent-check/issues',
118+
line: token.next_token.line,
119+
column: token.next_token.column,
125120
}
126121
# stop parsing indent
127122
break
128123
end
129124

130125
# get actual indent
131126
actual = 0
132-
if token.next_token.type == :INDENT
133-
actual = token.next_token.value.length
134-
elsif !token.prev_token.nil? and token.prev_token.type == :HEREDOC
135-
actual = token.prev_token.value.split("\n").last.length
136-
elsif !token.prev_token.nil? and token.prev_token.type == :HEREDOC_OPEN
137-
actual = next_token.prev_token.value.split("\n").last.length
138-
else
139-
actual = 0
140-
end
127+
actual = if token.next_token.type == :INDENT
128+
token.next_token.value.length
129+
elsif !token.prev_token.nil? and token.prev_token.type == :HEREDOC
130+
token.prev_token.value.split("\n").last.length
131+
elsif !token.prev_token.nil? and token.prev_token.type == :HEREDOC_OPEN
132+
next_token.prev_token.value.split("\n").last.length
133+
else
134+
0
135+
end
141136

142137
# expected indent
143138
expected = (indent + temp_indent) * chars_per_indent
144139

145140
# oh no! incorrect indent!
146-
if actual != expected
147-
# no one cares if blank lines and comments are indented correctly
148-
if not [:COMMENT, :NEWLINE].include?(token.next_token.type)
149-
notify :warning, {
150-
:message => "indent should be #{expected} chars and is #{actual}",
151-
:line => token.next_token.line,
152-
:column => token.next_token.column,
153-
:token => token.next_token,
154-
:indent => expected,
155-
}
156-
end
157-
end
141+
next unless actual != expected
142+
143+
# no one cares if blank lines and comments are indented correctly
144+
next if %i[COMMENT NEWLINE].include?(token.next_token.type)
145+
146+
notify :warning, {
147+
message: "indent should be #{expected} chars and is #{actual}",
148+
line: token.next_token.line,
149+
column: token.next_token.column,
150+
token: token.next_token,
151+
indent: expected,
152+
}
158153
end
159154
end
160155

161156
def fix(problem)
162157
char_for_indent = ' '
163-
if [:INDENT,:WHITESPACE].include?(problem[:token].type)
158+
if %i[INDENT WHITESPACE].include?(problem[:token].type)
164159
problem[:token].value = char_for_indent * problem[:indent]
165160
else
166161
tokens.insert(
167162
tokens.find_index(problem[:token]),
168-
PuppetLint::Lexer::Token.new(:INDENT, char_for_indent * problem[:indent], problem[:line], problem[:column])
163+
PuppetLint::Lexer::Token.new(:INDENT, char_for_indent * problem[:indent], problem[:line], problem[:column]),
169164
)
170165
end
171166
end
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Gem::Specification.new do |spec|
2-
spec.name = 'puppet-lint-strict_indent-check'
2+
spec.name = 'puppet-lint-strict_indent-check'
33
spec.version = '2.1.0'
44
spec.homepage = 'https://github.com/voxpupuli/puppet-lint-strict_indent-check'
55
spec.license = 'Mozilla 2.0'
@@ -12,19 +12,12 @@ Gem::Specification.new do |spec|
1212
'spec/**/*',
1313
'CHANGELOG.md',
1414
]
15-
spec.test_files = Dir['spec/**/*']
1615
spec.summary = 'puppet-lint strict indent check'
1716
spec.description = <<-EOF
1817
Extends puppet-lint to ensure that your manifests follow a strict indentation pattern.
1918
EOF
2019

2120
spec.required_ruby_version = '>= 2.7.0'
2221

23-
spec.add_dependency 'puppet-lint', '>= 3', '< 5'
24-
spec.add_development_dependency 'rspec', '~> 3.0'
25-
spec.add_development_dependency 'rspec-its', '~> 1.0'
26-
spec.add_development_dependency 'rspec-collection_matchers', '~> 1.0'
27-
spec.add_development_dependency 'rspec-json_expectations'
28-
spec.add_development_dependency 'rake'
29-
spec.add_development_dependency 'simplecov'
22+
spec.add_dependency 'puppet-lint', '>= 3', '< 5'
3023
end

0 commit comments

Comments
 (0)