Skip to content

Commit a621cdb

Browse files
committed
(PUP-11993) Style/NestedTernaryOperator
This commit enables the Style/NestedTernaryOperator cop and fixes 78 autocorrectable offenses.
1 parent ed76b89 commit a621cdb

File tree

10 files changed

+67
-39
lines changed

10 files changed

+67
-39
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -629,19 +629,6 @@ Style/MultipleComparison:
629629
Style/MutableConstant:
630630
Enabled: false
631631

632-
# This cop supports safe auto-correction (--auto-correct).
633-
Style/NestedTernaryOperator:
634-
Exclude:
635-
- 'lib/puppet/external/dot.rb'
636-
- 'lib/puppet/module_tool/shared_behaviors.rb'
637-
- 'lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb'
638-
- 'lib/puppet/pops/lookup/hiera_config.rb'
639-
- 'lib/puppet/pops/parser/lexer2.rb'
640-
- 'lib/puppet/pops/types/string_converter.rb'
641-
- 'lib/puppet/pops/types/type_factory.rb'
642-
- 'lib/puppet/pops/types/types.rb'
643-
- 'lib/puppet/util/log.rb'
644-
645632
# This cop supports safe auto-correction (--auto-correct).
646633
# Configuration parameters: EnforcedStyle, MinBodyLength.
647634
# SupportedStyles: skip_modifier_ifs, always

lib/puppet/external/dot.rb

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,18 @@ def pop
196196
def to_s(t = '')
197197
# This code is totally incomprehensible; it needs to be replaced!
198198

199-
label = @options['shape'] != 'record' && @ports.length == 0 ?
200-
@options['label'] ?
201-
t + $tab + "label = #{stringify(@options['label'])}\n" :
202-
'' :
203-
t + $tab + 'label = "' + " \\\n" +
204-
t + $tab2 + "#{stringify(@options['label'])}| \\\n" +
205-
@ports.collect { |i|
206-
t + $tab2 + i.to_s
207-
}.join("| \\\n") + " \\\n" +
208-
t + $tab + '"' + "\n"
199+
label = if @options['shape'] != 'record' && @ports.length == 0
200+
@options['label'] ?
201+
t + $tab + "label = #{stringify(@options['label'])}\n" :
202+
''
203+
else
204+
t + $tab + 'label = "' + " \\\n" +
205+
t + $tab2 + "#{stringify(@options['label'])}| \\\n" +
206+
@ports.collect { |i|
207+
t + $tab2 + i.to_s
208+
}.join("| \\\n") + " \\\n" +
209+
t + $tab + '"' + "\n"
210+
end
209211

210212
t + "#{@name} [\n" +
211213
@options.to_a.filter_map { |i|
@@ -253,9 +255,11 @@ def to_s(t = '')
253255
hdr = t + "#{@dot_string} #{@name} {\n"
254256

255257
options = @options.to_a.filter_map { |name, val|
256-
val && name != 'label' ?
257-
t + $tab + "#{name} = #{val}" :
258+
if val && name != 'label'
259+
t + $tab + "#{name} = #{val}"
260+
else
258261
name ? t + $tab + "#{name} = \"#{val}\"" : nil
262+
end
259263
}.join("\n") + "\n"
260264

261265
nodes = @nodes.collect { |i|
@@ -292,9 +296,11 @@ def edge_link
292296
def to_s(t = '')
293297
t + "#{@from} #{edge_link} #{to} [\n" +
294298
@options.to_a.filter_map { |i|
295-
i[1] && i[0] != 'label' ?
296-
t + $tab + "#{i[0]} = #{i[1]}" :
299+
if i[1] && i[0] != 'label'
300+
t + $tab + "#{i[0]} = #{i[1]}"
301+
else
297302
i[1] ? t + $tab + "#{i[0]} = \"#{i[1]}\"" : nil
303+
end
298304
}.join("\n") + "\n#{t}]\n"
299305
end
300306
end

lib/puppet/module_tool/shared_behaviors.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def resolve_constraints(dependencies, source = [{ :name => :you }], seen = {}, a
135135
:action => action,
136136
:previous_version => @installed[mod].empty? ? nil : @installed[mod].first.version,
137137
:file => @urls["#{mod}@#{version[:vstring]}"],
138-
:path => action == :install ? @options[:target_dir] : (@installed[mod].empty? ? @options[:target_dir] : @installed[mod].first.modulepath),
138+
:path => if action == :install
139+
@options[:target_dir]
140+
else
141+
(@installed[mod].empty? ? @options[:target_dir] : @installed[mod].first.modulepath)
142+
end,
139143
:dependencies => []
140144
}
141145
end

lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ def self.find_identity(rast)
120120
# Returns an Array; a tuple with method name and line number or "<unknown>" if either is missing, or format is not the expected
121121
#
122122
def self.extract_name_line(x)
123-
(x.is_a?(Array) ? [x[1], x[2].is_a?(Array) ? x[2][1] : nil] : [nil, nil]).map { |v| v.nil? ? UNKNOWN : v }
123+
(if x.is_a?(Array)
124+
[x[1], x[2].is_a?(Array) ? x[2][1] : nil]
125+
else
126+
[nil, nil]
127+
end).map { |v| v.nil? ? UNKNOWN : v }
124128
end
125129
private_class_method :extract_name_line
126130
end

lib/puppet/pops/lookup/hiera_config.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,13 @@ def create_configured_data_providers(lookup_invocation, parent_data_provider, us
700700
locations,
701701
{
702702
:hierarchy =>
703-
locations.nil? ? [] : locations.map do |loc|
704-
path = loc.original_location
705-
path.end_with?(".#{function_name}") ? path[0..-(function_name.length + 2)] : path
703+
if locations.nil?
704+
[]
705+
else
706+
locations.map do |loc|
707+
path = loc.original_location
708+
path.end_with?(".#{function_name}") ? path[0..-(function_name.length + 2)] : path
709+
end
706710
end,
707711
function_name.to_sym => v3options,
708712
:backends => [function_name],

lib/puppet/pops/parser/lexer2.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ def initialize
268268
'|' => lambda do
269269
scn = @scanner
270270
la = scn.peek(3)
271-
emit(la[1] == '>' ? (la[2] == '>' ? TOKEN_RRCOLLECT : TOKEN_RCOLLECT) : TOKEN_PIPE, scn.pos)
271+
emit(if la[1] == '>'
272+
la[2] == '>' ? TOKEN_RRCOLLECT : TOKEN_RCOLLECT
273+
else
274+
TOKEN_PIPE
275+
end, scn.pos)
272276
end,
273277

274278
# TOKENS =, =>, ==, =~
@@ -635,7 +639,11 @@ def lex_unquoted_string(string, locator, escapes, interpolate)
635639
@scanner = StringScanner.new(string)
636640
@locator = locator || Locator.locator(string, '')
637641
@lexing_context[:escapes] = escapes || UQ_ESCAPES
638-
@lexing_context[:uq_slurp_pattern] = interpolate ? (escapes.include?('$') ? SLURP_UQ_PATTERN : SLURP_UQNE_PATTERN) : SLURP_ALL_PATTERN
642+
@lexing_context[:uq_slurp_pattern] = if interpolate
643+
escapes.include?('$') ? SLURP_UQ_PATTERN : SLURP_UQNE_PATTERN
644+
else
645+
SLURP_ALL_PATTERN
646+
end
639647
end
640648

641649
# Convenience method, and for compatibility with older lexer. Use the lex_file instead.

lib/puppet/pops/types/string_converter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ def initialize(fmt)
119119

120120
@left = flags.include?('-')
121121
@alt = flags.include?('#')
122-
@plus = (flags.include?(' ') ? :space : (flags.include?('+') ? :plus : :ignore))
122+
@plus = (if flags.include?(' ')
123+
:space
124+
else
125+
(flags.include?('+') ? :plus : :ignore)
126+
end)
123127
@zero_pad = flags.include?('0')
124128

125129
@delimiters = nil

lib/puppet/pops/types/type_factory.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ def self.tuple(types = [], size_type = nil)
248248
# @api public
249249
#
250250
def self.boolean(value = nil)
251-
value.nil? ? PBooleanType::DEFAULT : (value ? PBooleanType::TRUE : PBooleanType::FALSE)
251+
if value.nil?
252+
PBooleanType::DEFAULT
253+
else
254+
(value ? PBooleanType::TRUE : PBooleanType::FALSE)
255+
end
252256
end
253257

254258
# Produces the Any type

lib/puppet/pops/types/types.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,11 @@ def each(&block)
10881088
# numbers are converted to zero
10891089
# @return [PIntegerType] a positive range
10901090
def to_size
1091-
@from >= 0 ? self : PIntegerType.new(0, @to < 0 ? 0 : @to)
1091+
if @from >= 0
1092+
self
1093+
else
1094+
PIntegerType.new(0, @to < 0 ? 0 : @to)
1095+
end
10921096
end
10931097

10941098
def new_function

lib/puppet/util/log.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ def Log.reopen
257257

258258
def self.setup_default
259259
Log.newdestination(
260-
(Puppet.features.syslog? ? :syslog :
261-
(Puppet.features.eventlog? ? :eventlog : Puppet[:puppetdlog]))
260+
(if Puppet.features.syslog?
261+
:syslog
262+
else
263+
(Puppet.features.eventlog? ? :eventlog : Puppet[:puppetdlog])
264+
end)
262265
)
263266
end
264267

0 commit comments

Comments
 (0)