rubocop 0.77.0 → 0.81.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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +3 -3
  4. data/config/default.yml +136 -60
  5. data/lib/rubocop.rb +20 -4
  6. data/lib/rubocop/ast/builder.rb +45 -42
  7. data/lib/rubocop/ast/node.rb +11 -18
  8. data/lib/rubocop/ast/node/block_node.rb +5 -1
  9. data/lib/rubocop/ast/node/case_match_node.rb +56 -0
  10. data/lib/rubocop/ast/node/def_node.rb +11 -0
  11. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  12. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  13. data/lib/rubocop/ast/traversal.rb +29 -10
  14. data/lib/rubocop/cli/command/auto_genenerate_config.rb +7 -7
  15. data/lib/rubocop/cli/command/show_cops.rb +11 -4
  16. data/lib/rubocop/comment_config.rb +6 -1
  17. data/lib/rubocop/config.rb +28 -10
  18. data/lib/rubocop/config_loader.rb +19 -19
  19. data/lib/rubocop/config_obsoletion.rb +6 -4
  20. data/lib/rubocop/config_validator.rb +55 -95
  21. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  22. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  23. data/lib/rubocop/cop/cop.rb +3 -1
  24. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +3 -4
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  28. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  29. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  30. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  31. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
  32. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
  33. data/lib/rubocop/cop/layout/heredoc_indentation.rb +4 -4
  34. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -2
  35. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +35 -79
  36. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_around_operators.rb +49 -6
  40. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  41. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  42. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  43. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  44. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  45. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  46. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  47. data/lib/rubocop/cop/lint/loop.rb +6 -4
  48. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  49. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  50. data/lib/rubocop/cop/lint/raise_exception.rb +39 -0
  51. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
  52. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +13 -8
  53. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  54. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  55. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
  56. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  57. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  58. data/lib/rubocop/cop/migration/department_name.rb +47 -6
  59. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  60. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  61. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  62. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  63. data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
  64. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  65. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  66. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  67. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  68. data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -12
  69. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  70. data/lib/rubocop/cop/naming/method_name.rb +30 -0
  71. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  72. data/lib/rubocop/cop/offense.rb +11 -0
  73. data/lib/rubocop/cop/registry.rb +7 -2
  74. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  75. data/lib/rubocop/cop/style/attr.rb +8 -0
  76. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  77. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  78. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  79. data/lib/rubocop/cop/style/documentation.rb +43 -5
  80. data/lib/rubocop/cop/style/end_block.rb +6 -0
  81. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  82. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  83. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  84. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  85. data/lib/rubocop/cop/style/hash_transform_values.rb +83 -0
  86. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -3
  87. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  88. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  89. data/lib/rubocop/cop/style/lambda.rb +1 -0
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  91. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  92. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  93. data/lib/rubocop/cop/style/module_function.rb +56 -10
  94. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  95. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  96. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  97. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  98. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -2
  99. data/lib/rubocop/cop/style/or_assignment.rb +3 -2
  100. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  101. data/lib/rubocop/cop/style/redundant_condition.rb +17 -4
  102. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  103. data/lib/rubocop/cop/style/symbol_array.rb +2 -2
  104. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  105. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +34 -22
  106. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  107. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +85 -0
  108. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  109. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  110. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  111. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  112. data/lib/rubocop/cop/variable_force.rb +4 -1
  113. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  114. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  115. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  116. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  117. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  118. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  119. data/lib/rubocop/node_pattern.rb +97 -11
  120. data/lib/rubocop/options.rb +8 -8
  121. data/lib/rubocop/processed_source.rb +1 -1
  122. data/lib/rubocop/result_cache.rb +2 -0
  123. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  124. data/lib/rubocop/runner.rb +5 -1
  125. data/lib/rubocop/target_ruby.rb +151 -0
  126. data/lib/rubocop/version.rb +1 -1
  127. metadata +38 -10
  128. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  129. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '0.77.0'
6
+ STRING = '0.81.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, running on ' \
9
9
  '%<ruby_engine>s %<ruby_version>s %<ruby_platform>s)'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.77.0
4
+ version: 0.81.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2019-11-27 00:00:00.000000000 Z
13
+ date: 2020-04-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: jaro_winkler
@@ -46,14 +46,14 @@ dependencies:
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '2.6'
49
+ version: 2.7.0.1
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: '2.6'
56
+ version: 2.7.0.1
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: rainbow
59
59
  requirement: !ruby/object:Gem::Requirement
@@ -74,6 +74,20 @@ dependencies:
74
74
  - - "<"
75
75
  - !ruby/object:Gem::Version
76
76
  version: '4.0'
77
+ - !ruby/object:Gem::Dependency
78
+ name: rexml
79
+ requirement: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
77
91
  - !ruby/object:Gem::Dependency
78
92
  name: ruby-progressbar
79
93
  requirement: !ruby/object:Gem::Requirement
@@ -97,7 +111,7 @@ dependencies:
97
111
  version: 1.4.0
98
112
  - - "<"
99
113
  - !ruby/object:Gem::Version
100
- version: '1.7'
114
+ version: '2.0'
101
115
  type: :runtime
102
116
  prerelease: false
103
117
  version_requirements: !ruby/object:Gem::Requirement
@@ -107,7 +121,7 @@ dependencies:
107
121
  version: 1.4.0
108
122
  - - "<"
109
123
  - !ruby/object:Gem::Version
110
- version: '1.7'
124
+ version: '2.0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: bundler
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -156,6 +170,7 @@ files:
156
170
  - lib/rubocop/ast/node/array_node.rb
157
171
  - lib/rubocop/ast/node/block_node.rb
158
172
  - lib/rubocop/ast/node/break_node.rb
173
+ - lib/rubocop/ast/node/case_match_node.rb
159
174
  - lib/rubocop/ast/node/case_node.rb
160
175
  - lib/rubocop/ast/node/class_node.rb
161
176
  - lib/rubocop/ast/node/def_node.rb
@@ -163,6 +178,7 @@ files:
163
178
  - lib/rubocop/ast/node/ensure_node.rb
164
179
  - lib/rubocop/ast/node/float_node.rb
165
180
  - lib/rubocop/ast/node/for_node.rb
181
+ - lib/rubocop/ast/node/forward_args_node.rb
166
182
  - lib/rubocop/ast/node/hash_node.rb
167
183
  - lib/rubocop/ast/node/if_node.rb
168
184
  - lib/rubocop/ast/node/int_node.rb
@@ -302,6 +318,7 @@ files:
302
318
  - lib/rubocop/cop/layout/initial_indentation.rb
303
319
  - lib/rubocop/cop/layout/leading_comment_space.rb
304
320
  - lib/rubocop/cop/layout/leading_empty_lines.rb
321
+ - lib/rubocop/cop/layout/line_length.rb
305
322
  - lib/rubocop/cop/layout/multiline_array_brace_layout.rb
306
323
  - lib/rubocop/cop/layout/multiline_array_line_breaks.rb
307
324
  - lib/rubocop/cop/layout/multiline_assignment_layout.rb
@@ -361,7 +378,6 @@ files:
361
378
  - lib/rubocop/cop/lint/empty_expression.rb
362
379
  - lib/rubocop/cop/lint/empty_interpolation.rb
363
380
  - lib/rubocop/cop/lint/empty_when.rb
364
- - lib/rubocop/cop/lint/end_in_method.rb
365
381
  - lib/rubocop/cop/lint/ensure_return.rb
366
382
  - lib/rubocop/cop/lint/erb_new_arguments.rb
367
383
  - lib/rubocop/cop/lint/flip_flop.rb
@@ -380,12 +396,14 @@ files:
380
396
  - lib/rubocop/cop/lint/nested_method_definition.rb
381
397
  - lib/rubocop/cop/lint/nested_percent_literal.rb
382
398
  - lib/rubocop/cop/lint/next_without_accumulator.rb
399
+ - lib/rubocop/cop/lint/non_deterministic_require_order.rb
383
400
  - lib/rubocop/cop/lint/non_local_exit_from_iterator.rb
384
401
  - lib/rubocop/cop/lint/number_conversion.rb
385
402
  - lib/rubocop/cop/lint/ordered_magic_comments.rb
386
403
  - lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
387
404
  - lib/rubocop/cop/lint/percent_string_array.rb
388
405
  - lib/rubocop/cop/lint/percent_symbol_array.rb
406
+ - lib/rubocop/cop/lint/raise_exception.rb
389
407
  - lib/rubocop/cop/lint/rand_one.rb
390
408
  - lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
391
409
  - lib/rubocop/cop/lint/redundant_cop_enable_directive.rb
@@ -407,6 +425,7 @@ files:
407
425
  - lib/rubocop/cop/lint/shadowed_argument.rb
408
426
  - lib/rubocop/cop/lint/shadowed_exception.rb
409
427
  - lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
428
+ - lib/rubocop/cop/lint/struct_new_override.rb
410
429
  - lib/rubocop/cop/lint/suppressed_exception.rb
411
430
  - lib/rubocop/cop/lint/syntax.rb
412
431
  - lib/rubocop/cop/lint/to_json.rb
@@ -429,7 +448,6 @@ files:
429
448
  - lib/rubocop/cop/metrics/block_nesting.rb
430
449
  - lib/rubocop/cop/metrics/class_length.rb
431
450
  - lib/rubocop/cop/metrics/cyclomatic_complexity.rb
432
- - lib/rubocop/cop/metrics/line_length.rb
433
451
  - lib/rubocop/cop/metrics/method_length.rb
434
452
  - lib/rubocop/cop/metrics/module_length.rb
435
453
  - lib/rubocop/cop/metrics/parameter_lists.rb
@@ -459,11 +477,13 @@ files:
459
477
  - lib/rubocop/cop/mixin/first_element_line_break.rb
460
478
  - lib/rubocop/cop/mixin/frozen_string_literal.rb
461
479
  - lib/rubocop/cop/mixin/hash_alignment_styles.rb
480
+ - lib/rubocop/cop/mixin/hash_transform_method.rb
462
481
  - lib/rubocop/cop/mixin/heredoc.rb
463
482
  - lib/rubocop/cop/mixin/ignored_methods.rb
464
483
  - lib/rubocop/cop/mixin/ignored_pattern.rb
465
484
  - lib/rubocop/cop/mixin/integer_node.rb
466
485
  - lib/rubocop/cop/mixin/interpolation.rb
486
+ - lib/rubocop/cop/mixin/line_length_help.rb
467
487
  - lib/rubocop/cop/mixin/match_range.rb
468
488
  - lib/rubocop/cop/mixin/method_complexity.rb
469
489
  - lib/rubocop/cop/mixin/method_preference.rb
@@ -483,6 +503,7 @@ files:
483
503
  - lib/rubocop/cop/mixin/preceding_following_alignment.rb
484
504
  - lib/rubocop/cop/mixin/preferred_delimiters.rb
485
505
  - lib/rubocop/cop/mixin/range_help.rb
506
+ - lib/rubocop/cop/mixin/rational_literal.rb
486
507
  - lib/rubocop/cop/mixin/rescue_node.rb
487
508
  - lib/rubocop/cop/mixin/safe_assignment.rb
488
509
  - lib/rubocop/cop/mixin/space_after_punctuation.rb
@@ -532,7 +553,6 @@ files:
532
553
  - lib/rubocop/cop/style/begin_block.rb
533
554
  - lib/rubocop/cop/style/block_comments.rb
534
555
  - lib/rubocop/cop/style/block_delimiters.rb
535
- - lib/rubocop/cop/style/braces_around_hash_parameters.rb
536
556
  - lib/rubocop/cop/style/case_equality.rb
537
557
  - lib/rubocop/cop/style/character_literal.rb
538
558
  - lib/rubocop/cop/style/class_and_module_children.rb
@@ -575,7 +595,10 @@ files:
575
595
  - lib/rubocop/cop/style/frozen_string_literal_comment.rb
576
596
  - lib/rubocop/cop/style/global_vars.rb
577
597
  - lib/rubocop/cop/style/guard_clause.rb
598
+ - lib/rubocop/cop/style/hash_each_methods.rb
578
599
  - lib/rubocop/cop/style/hash_syntax.rb
600
+ - lib/rubocop/cop/style/hash_transform_keys.rb
601
+ - lib/rubocop/cop/style/hash_transform_values.rb
579
602
  - lib/rubocop/cop/style/identical_conditional_branches.rb
580
603
  - lib/rubocop/cop/style/if_inside_else.rb
581
604
  - lib/rubocop/cop/style/if_unless_modifier.rb
@@ -590,6 +613,8 @@ files:
590
613
  - lib/rubocop/cop/style/lambda_call.rb
591
614
  - lib/rubocop/cop/style/line_end_concatenation.rb
592
615
  - lib/rubocop/cop/style/method_call_with_args_parentheses.rb
616
+ - lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb
617
+ - lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb
593
618
  - lib/rubocop/cop/style/method_call_without_args_parentheses.rb
594
619
  - lib/rubocop/cop/style/method_called_on_do_end_block.rb
595
620
  - lib/rubocop/cop/style/method_def_parentheses.rb
@@ -678,6 +703,7 @@ files:
678
703
  - lib/rubocop/cop/style/trailing_body_on_module.rb
679
704
  - lib/rubocop/cop/style/trailing_comma_in_arguments.rb
680
705
  - lib/rubocop/cop/style/trailing_comma_in_array_literal.rb
706
+ - lib/rubocop/cop/style/trailing_comma_in_block_args.rb
681
707
  - lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb
682
708
  - lib/rubocop/cop/style/trailing_method_end_statement.rb
683
709
  - lib/rubocop/cop/style/trailing_underscore_variable.rb
@@ -717,6 +743,7 @@ files:
717
743
  - lib/rubocop/formatter/fuubar_style_formatter.rb
718
744
  - lib/rubocop/formatter/html_formatter.rb
719
745
  - lib/rubocop/formatter/json_formatter.rb
746
+ - lib/rubocop/formatter/junit_formatter.rb
720
747
  - lib/rubocop/formatter/offense_count_formatter.rb
721
748
  - lib/rubocop/formatter/pacman_formatter.rb
722
749
  - lib/rubocop/formatter/progress_formatter.rb
@@ -744,6 +771,7 @@ files:
744
771
  - lib/rubocop/string_interpreter.rb
745
772
  - lib/rubocop/string_util.rb
746
773
  - lib/rubocop/target_finder.rb
774
+ - lib/rubocop/target_ruby.rb
747
775
  - lib/rubocop/token.rb
748
776
  - lib/rubocop/version.rb
749
777
  - lib/rubocop/warning.rb
@@ -772,7 +800,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
772
800
  - !ruby/object:Gem::Version
773
801
  version: '0'
774
802
  requirements: []
775
- rubygems_version: 3.0.3
803
+ rubygems_version: 3.1.2
776
804
  signing_key:
777
805
  specification_version: 4
778
806
  summary: Automatic Ruby code style checking tool.
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Lint
6
- # This cop checks for END blocks in method definitions.
7
- #
8
- # @example
9
- #
10
- # # bad
11
- #
12
- # def some_method
13
- # END { do_something }
14
- # end
15
- #
16
- # @example
17
- #
18
- # # good
19
- #
20
- # def some_method
21
- # at_exit { do_something }
22
- # end
23
- #
24
- # @example
25
- #
26
- # # good
27
- #
28
- # # outside defs
29
- # END { do_something }
30
- class EndInMethod < Cop
31
- MSG = '`END` found in method definition. Use `at_exit` instead.'
32
-
33
- def on_postexe(node)
34
- inside_of_method = node.each_ancestor(:def, :defs).count.nonzero?
35
- add_offense(node, location: :keyword) if inside_of_method
36
- end
37
- end
38
- end
39
- end
40
- end
@@ -1,209 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Style
6
- # This cop checks for braces around the last parameter in a method call
7
- # if the last parameter is a hash.
8
- # It supports `braces`, `no_braces` and `context_dependent` styles.
9
- #
10
- # @example EnforcedStyle: braces
11
- # # The `braces` style enforces braces around all method
12
- # # parameters that are hashes.
13
- #
14
- # # bad
15
- # some_method(x, y, a: 1, b: 2)
16
- #
17
- # # good
18
- # some_method(x, y, {a: 1, b: 2})
19
- #
20
- # @example EnforcedStyle: no_braces (default)
21
- # # The `no_braces` style checks that the last parameter doesn't
22
- # # have braces around it.
23
- #
24
- # # bad
25
- # some_method(x, y, {a: 1, b: 2})
26
- #
27
- # # good
28
- # some_method(x, y, a: 1, b: 2)
29
- #
30
- # @example EnforcedStyle: context_dependent
31
- # # The `context_dependent` style checks that the last parameter
32
- # # doesn't have braces around it, but requires braces if the
33
- # # second to last parameter is also a hash literal.
34
- #
35
- # # bad
36
- # some_method(x, y, {a: 1, b: 2})
37
- # some_method(x, y, {a: 1, b: 2}, a: 1, b: 2)
38
- #
39
- # # good
40
- # some_method(x, y, a: 1, b: 2)
41
- # some_method(x, y, {a: 1, b: 2}, {a: 1, b: 2})
42
- class BracesAroundHashParameters < Cop
43
- include ConfigurableEnforcedStyle
44
- include RangeHelp
45
-
46
- MSG = '%<type>s curly braces around a hash parameter.'
47
-
48
- def on_send(node)
49
- return if node.assignment_method? || node.operator_method?
50
-
51
- return unless node.arguments? && node.last_argument.hash_type? &&
52
- !node.last_argument.empty?
53
-
54
- check(node.last_argument, node.arguments)
55
- end
56
- alias on_csend on_send
57
-
58
- def autocorrect(send_node)
59
- hash_node = send_node.last_argument
60
-
61
- lambda do |corrector|
62
- if hash_node.braces?
63
- remove_braces_with_whitespace(corrector,
64
- hash_node,
65
- extra_space(hash_node))
66
- else
67
- add_braces(corrector, hash_node)
68
- end
69
- end
70
- end
71
-
72
- private
73
-
74
- def check(arg, args)
75
- case style
76
- when :braces
77
- check_braces(arg)
78
- when :no_braces
79
- check_no_braces(arg)
80
- when :context_dependent
81
- check_context_dependent(arg, args)
82
- end
83
- end
84
-
85
- def check_braces(arg)
86
- add_arg_offense(arg, :missing) unless arg.braces?
87
- end
88
-
89
- def check_no_braces(arg)
90
- return unless arg.braces? && !braces_needed_for_semantics?(arg)
91
-
92
- add_arg_offense(arg, :redundant)
93
- end
94
-
95
- def check_context_dependent(arg, args)
96
- braces_around_second_from_end = args.size > 1 && args[-2].hash_type?
97
-
98
- if arg.braces?
99
- unless braces_around_second_from_end ||
100
- braces_needed_for_semantics?(arg)
101
- add_arg_offense(arg, :redundant)
102
- end
103
- elsif braces_around_second_from_end
104
- add_arg_offense(arg, :missing)
105
- end
106
- end
107
-
108
- # Returns true if there's block inside the braces of the given hash arg
109
- # and that block uses do..end. The reason for wanting to check this is
110
- # that the do..end could bind to a different method invocation if the
111
- # hash braces were removed.
112
- def braces_needed_for_semantics?(arg)
113
- arg.each_pair do |_key, value|
114
- return true if value.block_type? && !value.braces?
115
- end
116
- false
117
- end
118
-
119
- def add_arg_offense(arg, type)
120
- add_offense(arg.parent, location: arg.source_range,
121
- message: format(MSG,
122
- type: type.to_s.capitalize))
123
- end
124
-
125
- def extra_space(hash_node)
126
- {
127
- newlines: extra_left_space?(hash_node) &&
128
- extra_right_space?(hash_node),
129
- left: extra_left_space?(hash_node),
130
- right: extra_right_space?(hash_node)
131
- }
132
- end
133
-
134
- def extra_left_space?(hash_node)
135
- @extra_left_space ||= begin
136
- top_line = hash_node.source_range.source_line
137
- top_line.delete(' ') == '{'
138
- end
139
- end
140
-
141
- def extra_right_space?(hash_node)
142
- @extra_right_space ||= begin
143
- bottom_line_number = hash_node.source_range.last_line
144
- bottom_line = processed_source.lines[bottom_line_number - 1]
145
- bottom_line.delete(' ') == '}'
146
- end
147
- end
148
-
149
- def remove_braces_with_whitespace(corrector, node, space)
150
- loc = node.loc
151
-
152
- if node.multiline?
153
- remove_braces_with_range(corrector,
154
- left_whole_line_range(loc.begin),
155
- right_whole_line_range(loc.end))
156
- else
157
- remove_braces_with_range(corrector,
158
- left_brace_and_space(loc.begin, space),
159
- right_brace_and_space(loc.end, space))
160
- end
161
- end
162
-
163
- def remove_braces_with_range(corrector, left_range, right_range)
164
- corrector.remove(left_range)
165
- corrector.remove(right_range)
166
- end
167
-
168
- def left_whole_line_range(loc_begin)
169
- if range_by_whole_lines(loc_begin).source.strip == '{'
170
- range_by_whole_lines(loc_begin, include_final_newline: true)
171
- else
172
- loc_begin
173
- end
174
- end
175
-
176
- def right_whole_line_range(loc_end)
177
- if range_by_whole_lines(loc_end).source.strip =~ /\A}\s*,?\z/
178
- range_by_whole_lines(loc_end, include_final_newline: true)
179
- else
180
- loc_end
181
- end
182
- end
183
-
184
- def left_brace_and_space(loc_begin, space)
185
- range_with_surrounding_space(range: loc_begin,
186
- side: :right,
187
- newlines: space[:newlines],
188
- whitespace: space[:left])
189
- end
190
-
191
- def right_brace_and_space(loc_end, space)
192
- brace_and_space =
193
- range_with_surrounding_space(
194
- range: loc_end,
195
- side: :left,
196
- newlines: space[:newlines],
197
- whitespace: space[:right]
198
- )
199
- range_with_surrounding_comma(brace_and_space, :left)
200
- end
201
-
202
- def add_braces(corrector, node)
203
- corrector.insert_before(node.source_range, '{')
204
- corrector.insert_after(node.source_range, '}')
205
- end
206
- end
207
- end
208
- end
209
- end