cookstyle 6.2.5 → 6.6.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/config/cookstyle.yml +292 -13
  4. data/config/disable_all.yml +13 -3
  5. data/config/upstream.yml +88 -17
  6. data/lib/cookstyle/version.rb +2 -2
  7. data/lib/rubocop/chef/platform_helpers.rb +2 -1
  8. data/lib/rubocop/cop/chef/correctness/invalid_platform_family_values_in_case.rb +77 -0
  9. data/lib/rubocop/cop/chef/correctness/invalid_platform_values_in_case.rb +77 -0
  10. data/lib/rubocop/cop/chef/correctness/lazy_eval_node_attribute_defaults.rb +56 -0
  11. data/lib/rubocop/cop/chef/correctness/node_normal.rb +1 -1
  12. data/lib/rubocop/cop/chef/correctness/node_normal_unless.rb +1 -1
  13. data/lib/rubocop/cop/chef/correctness/openssl_password_helpers.rb +45 -0
  14. data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +1 -1
  15. data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +1 -1
  16. data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +10 -4
  17. data/lib/rubocop/cop/chef/deprecation/deprecated_shellout_methods.rb +65 -0
  18. data/lib/rubocop/cop/chef/deprecation/easy_install.rb +2 -2
  19. data/lib/rubocop/cop/chef/deprecation/erl_call.rb +1 -1
  20. data/lib/rubocop/cop/chef/deprecation/locale_lc_all_property.rb +2 -2
  21. data/lib/rubocop/cop/chef/deprecation/log_resource_notifications.rb +5 -2
  22. data/lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb +1 -1
  23. data/lib/rubocop/cop/chef/deprecation/node_set.rb +2 -3
  24. data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +2 -3
  25. data/lib/rubocop/cop/chef/deprecation/powershell_cookbook_helpers.rb +3 -3
  26. data/lib/rubocop/cop/chef/deprecation/ruby_27_keyword_argument_warnings.rb +59 -0
  27. data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +6 -1
  28. data/lib/rubocop/cop/chef/deprecation/xml_ruby_recipe.rb +3 -3
  29. data/lib/rubocop/cop/chef/modernize/respond_to_compile_time.rb +93 -0
  30. data/lib/rubocop/cop/chef/modernize/respond_to_provides.rb +15 -5
  31. data/lib/rubocop/cop/chef/modernize/shell_out_helper.rb +64 -0
  32. data/lib/rubocop/cop/chef/modernize/use_multipackage_installs.rb +8 -4
  33. data/lib/rubocop/cop/chef/modernize/whyrun_supported_true.rb +9 -2
  34. data/lib/rubocop/cop/chef/redundant/grouping_metadata.rb +0 -1
  35. data/lib/rubocop/cop/chef/redundant/sensitive_property_in_resource.rb +3 -3
  36. data/lib/rubocop/cop/chef/style/negating_only_if.rb +8 -3
  37. data/lib/rubocop/cop/chef/style/overly_complex_supports_depends_metadata.rb +1 -1
  38. data/lib/rubocop/cop/target_chef_version.rb +4 -0
  39. metadata +12 -4
@@ -59,6 +59,8 @@ Layout/EmptyLinesAroundAccessModifier:
59
59
  Enabled: false
60
60
  Layout/EmptyLinesAroundArguments:
61
61
  Enabled: false
62
+ Layout/EmptyLinesAroundAttributeAccessor:
63
+ Enabled: false
62
64
  Layout/EmptyLinesAroundBeginBody:
63
65
  Enabled: false
64
66
  Layout/EmptyLinesAroundBlockBody:
@@ -103,6 +105,8 @@ Layout/HeredocIndentation:
103
105
  Enabled: false
104
106
  Layout/IndentationConsistency:
105
107
  Enabled: false
108
+ Layout/IndentationStyle:
109
+ Enabled: false
106
110
  Layout/IndentationWidth:
107
111
  Enabled: false
108
112
  Layout/InitialIndentation:
@@ -155,6 +159,8 @@ Layout/SpaceAroundEqualsInParameterDefault:
155
159
  Enabled: false
156
160
  Layout/SpaceAroundKeyword:
157
161
  Enabled: false
162
+ Layout/SpaceAroundMethodCallOperator:
163
+ Enabled: false
158
164
  Layout/SpaceAroundOperators:
159
165
  Enabled: false
160
166
  Layout/SpaceBeforeBlockBraces:
@@ -187,8 +193,6 @@ Layout/SpaceInsideReferenceBrackets:
187
193
  Enabled: false
188
194
  Layout/SpaceInsideStringInterpolation:
189
195
  Enabled: false
190
- Layout/Tab:
191
- Enabled: false
192
196
  Layout/TrailingEmptyLines:
193
197
  Enabled: false
194
198
  Layout/TrailingWhitespace:
@@ -467,6 +471,8 @@ Style/DefWithParentheses:
467
471
  Enabled: false
468
472
  Style/Dir:
469
473
  Enabled: false
474
+ Style/DisableCopsWithinSourceCodeDirective:
475
+ Enabled: false
470
476
  Style/DocumentationMethod:
471
477
  Enabled: false
472
478
  Style/Documentation:
@@ -501,6 +507,8 @@ Style/EvenOdd:
501
507
  Enabled: false
502
508
  Style/ExpandPathArguments:
503
509
  Enabled: false
510
+ Style/ExponentialNotation:
511
+ Enabled: false
504
512
  Style/FloatDivision:
505
513
  Enabled: false
506
514
  Style/For:
@@ -691,6 +699,8 @@ Style/SingleLineBlockParams:
691
699
  Enabled: false
692
700
  Style/SingleLineMethods:
693
701
  Enabled: false
702
+ Style/SlicingWithRange:
703
+ Enabled: false
694
704
  Style/SpecialGlobalVars:
695
705
  Enabled: false
696
706
  Style/StabbyLambdaParentheses:
@@ -764,4 +774,4 @@ Security/MarshalLoad:
764
774
  Security/Open:
765
775
  Enabled: false
766
776
  Security/YAMLLoad:
767
- Enabled: false
777
+ Enabled: false
@@ -35,6 +35,7 @@ AllCops:
35
35
  - '**/*.watchr'
36
36
  - '**/.irbrc'
37
37
  - '**/.pryrc'
38
+ - '**/.simplecov'
38
39
  - '**/buildfile'
39
40
  - '**/Appraisals'
40
41
  - '**/Berksfile'
@@ -53,6 +54,7 @@ AllCops:
53
54
  - '**/Podfile'
54
55
  - '**/Puppetfile'
55
56
  - '**/Rakefile'
57
+ - '**/rakefile'
56
58
  - '**/Snapfile'
57
59
  - '**/Steepfile'
58
60
  - '**/Thorfile'
@@ -97,6 +99,14 @@ AllCops:
97
99
  # to true in the same configuration.
98
100
  EnabledByDefault: false
99
101
  DisabledByDefault: false
102
+ # New cops introduced between major versions are set to a special pending status
103
+ # and are not enabled by default with warning message.
104
+ # Change this behavior by overriding either `NewCops: enable` or `NewCops: disable`.
105
+ # When `NewCops` is `enable`, pending cops are enabled in bulk. Can be overridden by
106
+ # the `--enable-pending-cops` command-line option.
107
+ # When `NewCops` is `disable`, pending cops are disabled in bulk. Can be overridden by
108
+ # the `--disable-pending-cops` command-line option.
109
+ NewCops: pending
100
110
  # Enables the result cache if `true`. Can be overridden by the `--cache` command
101
111
  # line option.
102
112
  UseCache: true
@@ -125,7 +135,7 @@ AllCops:
125
135
  # followed by the Gemfile.lock or gems.locked file. (Although the Ruby version
126
136
  # is specified in the Gemfile or gems.rb file, RuboCop reads the final value
127
137
  # from the lock file.) If the Ruby version is still unresolved, RuboCop will
128
- # use the oldest officially supported Ruby version (currently Ruby 2.3).
138
+ # use the oldest officially supported Ruby version (currently Ruby 2.4).
129
139
  TargetRubyVersion: ~
130
140
 
131
141
  #################### Bundler ###############################
@@ -367,6 +377,7 @@ Layout/ConditionPosition:
367
377
  StyleGuide: '#same-line-condition'
368
378
  Enabled: true
369
379
  VersionAdded: '0.53'
380
+ VersionChanged: '0.83'
370
381
 
371
382
  Layout/DefEndAlignment:
372
383
  Description: 'Align ends corresponding to defs correctly.'
@@ -452,6 +463,12 @@ Layout/EmptyLinesAroundArguments:
452
463
  Enabled: true
453
464
  VersionAdded: '0.52'
454
465
 
466
+ Layout/EmptyLinesAroundAttributeAccessor:
467
+ Description: "Keep blank lines around attribute accessors."
468
+ StyleGuide: '#empty-lines-around-attribute-accessor'
469
+ Enabled: pending
470
+ VersionAdded: '0.83'
471
+
455
472
  Layout/EmptyLinesAroundBeginBody:
456
473
  Description: "Keeps track of empty lines around begin-end bodies."
457
474
  StyleGuide: '#empty-lines-around-bodies'
@@ -793,6 +810,22 @@ Layout/IndentationConsistency:
793
810
  # A reference to `EnforcedStyle: indented_internal_methods`.
794
811
  - https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
795
812
 
813
+ Layout/IndentationStyle:
814
+ Description: 'Consistent indentation either with tabs only or spaces only.'
815
+ StyleGuide: '#spaces-indentation'
816
+ Enabled: true
817
+ VersionAdded: '0.49'
818
+ VersionChanged: '0.82'
819
+ # By default, the indentation width from Layout/IndentationWidth is used
820
+ # But it can be overridden by setting this parameter
821
+ # It is used during auto-correction to determine how many spaces should
822
+ # replace each tab.
823
+ IndentationWidth: ~
824
+ EnforcedStyle: spaces
825
+ SupportedStyles:
826
+ - spaces
827
+ - tabs
828
+
796
829
  Layout/IndentationWidth:
797
830
  Description: 'Use 2 spaces for indentation.'
798
831
  StyleGuide: '#spaces-indentation'
@@ -1080,6 +1113,11 @@ Layout/SpaceAroundKeyword:
1080
1113
  Enabled: true
1081
1114
  VersionAdded: '0.49'
1082
1115
 
1116
+ Layout/SpaceAroundMethodCallOperator:
1117
+ Description: 'Checks method call operators to not have spaces around them.'
1118
+ Enabled: pending
1119
+ VersionAdded: '0.82'
1120
+
1083
1121
  Layout/SpaceAroundOperators:
1084
1122
  Description: 'Use a single space around operators.'
1085
1123
  StyleGuide: '#spaces-operators'
@@ -1250,18 +1288,6 @@ Layout/SpaceInsideStringInterpolation:
1250
1288
  - space
1251
1289
  - no_space
1252
1290
 
1253
- Layout/Tab:
1254
- Description: 'No hard tabs.'
1255
- StyleGuide: '#spaces-indentation'
1256
- Enabled: true
1257
- VersionAdded: '0.49'
1258
- VersionChanged: '0.51'
1259
- # By default, the indentation width from Layout/IndentationWidth is used
1260
- # But it can be overridden by setting this parameter
1261
- # It is used during auto-correction to determine how many spaces should
1262
- # replace each tab.
1263
- IndentationWidth: ~
1264
-
1265
1291
  Layout/TrailingEmptyLines:
1266
1292
  Description: 'Checks trailing blank lines and final newline.'
1267
1293
  StyleGuide: '#newline-eof'
@@ -1278,8 +1304,8 @@ Layout/TrailingWhitespace:
1278
1304
  StyleGuide: '#no-trailing-whitespace'
1279
1305
  Enabled: true
1280
1306
  VersionAdded: '0.49'
1281
- VersionChanged: '0.55'
1282
- AllowInHeredoc: false
1307
+ VersionChanged: '0.83'
1308
+ AllowInHeredoc: true
1283
1309
 
1284
1310
  #################### Lint ##################################
1285
1311
  ### Warnings
@@ -1299,6 +1325,7 @@ Lint/AmbiguousOperator:
1299
1325
  StyleGuide: '#method-invocation-parens'
1300
1326
  Enabled: true
1301
1327
  VersionAdded: '0.17'
1328
+ VersionChanged: '0.83'
1302
1329
 
1303
1330
  Lint/AmbiguousRegexpLiteral:
1304
1331
  Description: >-
@@ -1306,6 +1333,7 @@ Lint/AmbiguousRegexpLiteral:
1306
1333
  a method invocation without parentheses.
1307
1334
  Enabled: true
1308
1335
  VersionAdded: '0.17'
1336
+ VersionChanged: '0.83'
1309
1337
 
1310
1338
  Lint/AssignmentInCondition:
1311
1339
  Description: "Don't use assignment in conditions."
@@ -1322,8 +1350,9 @@ Lint/BigDecimalNew:
1322
1350
  Lint/BooleanSymbol:
1323
1351
  Description: 'Check for `:true` and `:false` symbols.'
1324
1352
  Enabled: true
1353
+ Safe: false
1325
1354
  VersionAdded: '0.50'
1326
- VersionChanged: '0.81'
1355
+ VersionChanged: '0.83'
1327
1356
 
1328
1357
  Lint/CircularArgumentReference:
1329
1358
  Description: "Default values in optional keyword arguments and optional ordinal arguments should not refer back to the name of the argument."
@@ -1394,13 +1423,16 @@ Lint/EmptyInterpolation:
1394
1423
  Lint/EmptyWhen:
1395
1424
  Description: 'Checks for `when` branches with empty bodies.'
1396
1425
  Enabled: true
1426
+ AllowComments: true
1397
1427
  VersionAdded: '0.45'
1428
+ VersionChanged: '0.83'
1398
1429
 
1399
1430
  Lint/EnsureReturn:
1400
1431
  Description: 'Do not use return in an ensure block.'
1401
1432
  StyleGuide: '#no-return-ensure'
1402
1433
  Enabled: true
1403
1434
  VersionAdded: '0.9'
1435
+ VersionChanged: '0.83'
1404
1436
 
1405
1437
  Lint/ErbNewArguments:
1406
1438
  Description: 'Use `:trim_mode` and `:eoutvar` keyword arguments to `ERB.new`.'
@@ -1548,6 +1580,7 @@ Lint/ParenthesesAsGroupedExpression:
1548
1580
  StyleGuide: '#parens-no-spaces'
1549
1581
  Enabled: true
1550
1582
  VersionAdded: '0.12'
1583
+ VersionChanged: '0.83'
1551
1584
 
1552
1585
  Lint/PercentStringArray:
1553
1586
  Description: >-
@@ -1567,6 +1600,8 @@ Lint/RaiseException:
1567
1600
  StyleGuide: '#raise-exception'
1568
1601
  Enabled: pending
1569
1602
  VersionAdded: '0.81'
1603
+ AllowedImplicitNamespaces:
1604
+ - 'Gem'
1570
1605
 
1571
1606
  Lint/RandOne:
1572
1607
  Description: >-
@@ -1789,7 +1824,7 @@ Lint/UselessAccessModifier:
1789
1824
  Description: 'Checks for useless access modifiers.'
1790
1825
  Enabled: true
1791
1826
  VersionAdded: '0.20'
1792
- VersionChanged: '0.47'
1827
+ VersionChanged: '0.83'
1793
1828
  ContextCreatingMethods: []
1794
1829
  MethodCreatingMethods: []
1795
1830
 
@@ -2409,6 +2444,15 @@ Style/CaseEquality:
2409
2444
  StyleGuide: '#no-case-equality'
2410
2445
  Enabled: true
2411
2446
  VersionAdded: '0.9'
2447
+ # If AllowOnConstant is enabled, the cop will ignore violations when the receiver of
2448
+ # the case equality operator is a constant.
2449
+ #
2450
+ # # bad
2451
+ # /string/ === "string"
2452
+ #
2453
+ # # good
2454
+ # String === "string"
2455
+ AllowOnConstant: false
2412
2456
 
2413
2457
  Style/CharacterLiteral:
2414
2458
  Description: 'Checks for uses of character literals.'
@@ -2449,6 +2493,7 @@ Style/ClassAndModuleChildren:
2449
2493
 
2450
2494
  Style/ClassCheck:
2451
2495
  Description: 'Enforces consistent use of `Object#is_a?` or `Object#kind_of?`.'
2496
+ StyleGuide: '#is-a-vs-kind-of'
2452
2497
  Enabled: true
2453
2498
  VersionAdded: '0.24'
2454
2499
  EnforcedStyle: is_a?
@@ -2616,6 +2661,12 @@ Style/Dir:
2616
2661
  Enabled: true
2617
2662
  VersionAdded: '0.50'
2618
2663
 
2664
+ Style/DisableCopsWithinSourceCodeDirective:
2665
+ Description: >-
2666
+ Forbids disabling/enabling cops within source code.
2667
+ Enabled: false
2668
+ VersionAdded: '0.82'
2669
+
2619
2670
  Style/Documentation:
2620
2671
  Description: 'Document classes and non-namespace modules.'
2621
2672
  Enabled: true
@@ -2734,6 +2785,17 @@ Style/ExpandPathArguments:
2734
2785
  Enabled: true
2735
2786
  VersionAdded: '0.53'
2736
2787
 
2788
+ Style/ExponentialNotation:
2789
+ Description: 'When using exponential notation, favor a mantissa between 1 (inclusive) and 10 (exclusive).'
2790
+ StyleGuide: '#exponential-notation'
2791
+ Enabled: pending
2792
+ VersionAdded: '0.82'
2793
+ EnforcedStyle: scientific
2794
+ SupportedStyles:
2795
+ - scientific
2796
+ - engineering
2797
+ - integral
2798
+
2737
2799
  Style/FloatDivision:
2738
2800
  Description: 'For performing float division, coerce one side only.'
2739
2801
  StyleGuide: '#float-division'
@@ -2902,6 +2964,7 @@ Style/IfWithSemicolon:
2902
2964
  StyleGuide: '#no-semicolon-ifs'
2903
2965
  Enabled: true
2904
2966
  VersionAdded: '0.9'
2967
+ VersionChanged: '0.83'
2905
2968
 
2906
2969
  Style/ImplicitRuntimeError:
2907
2970
  Description: >-
@@ -3379,7 +3442,9 @@ Style/OptionalArguments:
3379
3442
  of the argument list.
3380
3443
  StyleGuide: '#optional-arguments'
3381
3444
  Enabled: true
3445
+ Safe: false
3382
3446
  VersionAdded: '0.33'
3447
+ VersionChanged: '0.83'
3383
3448
 
3384
3449
  Style/OrAssignment:
3385
3450
  Description: 'Recommend usage of double pipe equals (||=) where applicable.'
@@ -3684,6 +3749,12 @@ Style/SingleLineMethods:
3684
3749
  VersionChanged: '0.19'
3685
3750
  AllowIfMethodIsEmpty: true
3686
3751
 
3752
+ Style/SlicingWithRange:
3753
+ Description: 'Checks array slicing is done with endless ranges when suitable.'
3754
+ Enabled: pending
3755
+ VersionAdded: '0.83'
3756
+ Safe: false
3757
+
3687
3758
  Style/SpecialGlobalVars:
3688
3759
  Description: 'Avoid Perl-style global variables.'
3689
3760
  StyleGuide: '#no-cryptic-perlisms'
@@ -1,4 +1,4 @@
1
1
  module Cookstyle
2
- VERSION = "6.2.5".freeze # rubocop: disable Style/StringLiterals
3
- RUBOCOP_VERSION = '0.81.0'.freeze
2
+ VERSION = "6.6.9".freeze # rubocop: disable Style/StringLiterals
3
+ RUBOCOP_VERSION = '0.83.0'.freeze
4
4
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright 2019, Chef Software Inc.
2
+ # Copyright:: Copyright 2019-2020, Chef Software Inc.
3
3
  # Author:: Tim Smith (<tsmith@chef.io>)
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,6 +32,7 @@ module RuboCop
32
32
  'mswin' => 'windows',
33
33
  'opensuse' => 'suse',
34
34
  'opensuseleap' => 'suse',
35
+ 'oracle' => 'rhel',
35
36
  'redhat' => 'rhel',
36
37
  'scientific' => 'rhel',
37
38
  'sles' => 'suse',
@@ -0,0 +1,77 @@
1
+ #
2
+ # Copyright:: Copyright 2020, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ module ChefCorrectness
21
+ # Use valid platform family values in case statements.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # case node['platform_family']
27
+ # when 'redhat'
28
+ # puts "I'm on a RHEL-like system"
29
+ # end
30
+ #
31
+ class InvalidPlatformFamilyInCase < Cop
32
+ include RangeHelp
33
+ include ::RuboCop::Chef::PlatformHelpers
34
+
35
+ MSG = 'Use valid platform family values in case statements.'.freeze
36
+
37
+ def_node_matcher :node_platform_family?, <<-PATTERN
38
+ (send (send nil? :node) :[] (str "platform_family") )
39
+ PATTERN
40
+
41
+ def on_case(node)
42
+ node_platform_family?(node.condition) do
43
+ node.each_when do |when_node|
44
+ when_node.each_condition do |con|
45
+ next unless con.str_type? # if the condition isn't a string we can't check so skip
46
+
47
+ if INVALID_PLATFORM_FAMILIES[con.str_content]
48
+ add_offense(con, location: :expression, message: MSG, severity: :refactor)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def autocorrect(node)
56
+ new_value = INVALID_PLATFORM_FAMILIES[node.str_content]
57
+
58
+ # some invalid platform families have no direct correction value and return nil instead
59
+ return unless new_value
60
+
61
+ # if the correct value already exists in the when statement then we just want to delete this node
62
+ if node.parent.conditions.any? { |x| x.str_content == new_value }
63
+ lambda do |corrector|
64
+ range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :both)
65
+ corrector.remove(range)
66
+ end
67
+ else
68
+ lambda do |corrector|
69
+ corrector.replace(node.loc.expression, "'#{new_value}'")
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,77 @@
1
+ #
2
+ # Copyright:: Copyright 2020, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ module ChefCorrectness
21
+ # Use valid platform values in case statements.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # case node['platform']
27
+ # when 'rhel'
28
+ # puts "I'm on a Red Hat system!"
29
+ # end
30
+ #
31
+ class InvalidPlatformInCase < Cop
32
+ include RangeHelp
33
+ include ::RuboCop::Chef::PlatformHelpers
34
+
35
+ MSG = 'Use valid platform values in case statements.'.freeze
36
+
37
+ def_node_matcher :node_platform?, <<-PATTERN
38
+ (send (send nil? :node) :[] (str "platform") )
39
+ PATTERN
40
+
41
+ def on_case(node)
42
+ node_platform?(node.condition) do
43
+ node.each_when do |when_node|
44
+ when_node.each_condition do |con|
45
+ next unless con.str_type? # if the condition isn't a string we can't check so skip
46
+
47
+ if INVALID_PLATFORMS[con.str_content]
48
+ add_offense(con, location: :expression, message: MSG, severity: :refactor)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def autocorrect(node)
56
+ new_value = INVALID_PLATFORMS[node.str_content]
57
+
58
+ # some invalid platform have no direct correction value and return nil instead
59
+ return unless new_value
60
+
61
+ # if the correct value already exists in the when statement then we just want to delete this node
62
+ if node.parent.conditions.any? { |x| x.str_content == new_value }
63
+ lambda do |corrector|
64
+ range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :both)
65
+ corrector.remove(range)
66
+ end
67
+ else
68
+ lambda do |corrector|
69
+ corrector.replace(node.loc.expression, "'#{new_value}'")
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end