rubocop 0.52.0 → 0.52.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -5
  3. data/config/default.yml +1 -11
  4. data/config/disabled.yml +5 -0
  5. data/config/enabled.yml +6 -8
  6. data/lib/rubocop.rb +13 -2
  7. data/lib/rubocop/ast/node.rb +23 -15
  8. data/lib/rubocop/cli.rb +25 -2
  9. data/lib/rubocop/config.rb +23 -8
  10. data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -2
  11. data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -0
  12. data/lib/rubocop/cop/commissioner.rb +1 -1
  13. data/lib/rubocop/cop/correctors/alignment_corrector.rb +121 -0
  14. data/lib/rubocop/cop/correctors/condition_corrector.rb +28 -0
  15. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +26 -0
  16. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +62 -0
  17. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +44 -0
  18. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +31 -0
  19. data/lib/rubocop/cop/correctors/punctuation_corrector.rb +29 -0
  20. data/lib/rubocop/cop/correctors/space_corrector.rb +34 -0
  21. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +25 -0
  22. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +31 -0
  23. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  24. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -0
  25. data/lib/rubocop/cop/generator.rb +18 -87
  26. data/lib/rubocop/cop/generator/require_file_injector.rb +78 -0
  27. data/lib/rubocop/cop/layout/access_modifier_indentation.rb +5 -1
  28. data/lib/rubocop/cop/layout/align_array.rb +5 -1
  29. data/lib/rubocop/cop/layout/align_hash.rb +1 -1
  30. data/lib/rubocop/cop/layout/align_parameters.rb +5 -1
  31. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  32. data/lib/rubocop/cop/layout/class_structure.rb +2 -2
  33. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -1
  34. data/lib/rubocop/cop/layout/comment_indentation.rb +5 -1
  35. data/lib/rubocop/cop/layout/else_alignment.rb +5 -1
  36. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +3 -3
  37. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +17 -19
  38. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +4 -0
  39. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +4 -0
  40. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +4 -0
  41. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +4 -0
  42. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +4 -0
  43. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +4 -0
  44. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +4 -0
  45. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +4 -0
  46. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +4 -0
  47. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +4 -0
  48. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -2
  49. data/lib/rubocop/cop/layout/indent_array.rb +6 -2
  50. data/lib/rubocop/cop/layout/indent_assignment.rb +6 -2
  51. data/lib/rubocop/cop/layout/indent_hash.rb +5 -1
  52. data/lib/rubocop/cop/layout/indentation_consistency.rb +5 -1
  53. data/lib/rubocop/cop/layout/indentation_width.rb +5 -1
  54. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +4 -0
  55. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +3 -3
  56. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -2
  57. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +4 -0
  58. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +4 -0
  59. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +6 -2
  60. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +4 -0
  61. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -2
  62. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
  63. data/lib/rubocop/cop/layout/space_after_comma.rb +4 -0
  64. data/lib/rubocop/cop/layout/space_after_semicolon.rb +4 -0
  65. data/lib/rubocop/cop/layout/space_before_comma.rb +4 -0
  66. data/lib/rubocop/cop/layout/space_before_semicolon.rb +4 -0
  67. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +3 -2
  68. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +16 -7
  69. data/lib/rubocop/cop/layout/tab.rb +1 -1
  70. data/lib/rubocop/cop/lint/block_alignment.rb +1 -1
  71. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
  72. data/lib/rubocop/cop/lint/end_alignment.rb +3 -1
  73. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  74. data/lib/rubocop/cop/lint/unused_block_argument.rb +4 -0
  75. data/lib/rubocop/cop/lint/unused_method_argument.rb +6 -0
  76. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -1
  77. data/lib/rubocop/cop/mixin/alignment.rb +70 -0
  78. data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -0
  79. data/lib/rubocop/cop/mixin/array_syntax.rb +2 -0
  80. data/lib/rubocop/cop/mixin/code_length.rb +2 -0
  81. data/lib/rubocop/cop/mixin/configurable_max.rb +2 -0
  82. data/lib/rubocop/cop/mixin/def_node.rb +3 -1
  83. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  84. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -15
  85. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -25
  86. data/lib/rubocop/cop/mixin/enforce_superclass.rb +0 -6
  87. data/lib/rubocop/cop/mixin/first_element_line_break.rb +5 -9
  88. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +2 -2
  89. data/lib/rubocop/cop/mixin/ignored_pattern.rb +2 -0
  90. data/lib/rubocop/cop/mixin/integer_node.rb +2 -0
  91. data/lib/rubocop/cop/mixin/match_range.rb +2 -0
  92. data/lib/rubocop/cop/mixin/min_body_length.rb +2 -0
  93. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
  94. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +9 -48
  95. data/lib/rubocop/cop/mixin/negative_conditional.rb +2 -16
  96. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +12 -31
  97. data/lib/rubocop/cop/mixin/parentheses.rb +2 -19
  98. data/lib/rubocop/cop/mixin/percent_literal.rb +3 -3
  99. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +2 -0
  100. data/lib/rubocop/cop/mixin/rescue_node.rb +2 -0
  101. data/lib/rubocop/cop/mixin/safe_assignment.rb +2 -0
  102. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -3
  103. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -3
  104. data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -2
  105. data/lib/rubocop/cop/mixin/string_help.rb +2 -0
  106. data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -13
  107. data/lib/rubocop/cop/mixin/surrounding_space.rb +4 -21
  108. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -10
  109. data/lib/rubocop/cop/mixin/unused_argument.rb +2 -15
  110. data/lib/rubocop/cop/performance/case_when_splat.rb +1 -1
  111. data/lib/rubocop/cop/rails/action_filter.rb +3 -2
  112. data/lib/rubocop/cop/rails/active_support_aliases.rb +3 -2
  113. data/lib/rubocop/cop/rails/application_job.rb +6 -0
  114. data/lib/rubocop/cop/rails/application_record.rb +6 -0
  115. data/lib/rubocop/cop/rails/blank.rb +10 -9
  116. data/lib/rubocop/cop/rails/date.rb +22 -14
  117. data/lib/rubocop/cop/rails/delegate.rb +1 -1
  118. data/lib/rubocop/cop/rails/dynamic_find_by.rb +3 -2
  119. data/lib/rubocop/cop/rails/enum_uniqueness.rb +4 -2
  120. data/lib/rubocop/cop/rails/environment_comparison.rb +2 -2
  121. data/lib/rubocop/cop/rails/file_path.rb +1 -1
  122. data/lib/rubocop/cop/rails/find_by.rb +2 -2
  123. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +15 -7
  124. data/lib/rubocop/cop/rails/http_positional_arguments.rb +2 -2
  125. data/lib/rubocop/cop/rails/inverse_of.rb +130 -8
  126. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +3 -3
  127. data/lib/rubocop/cop/rails/pluralization_grammar.rb +3 -2
  128. data/lib/rubocop/cop/rails/presence.rb +31 -18
  129. data/lib/rubocop/cop/rails/present.rb +11 -8
  130. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +52 -10
  131. data/lib/rubocop/cop/rails/request_referer.rb +2 -3
  132. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +9 -2
  133. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +38 -10
  134. data/lib/rubocop/cop/style/class_and_module_children.rb +76 -0
  135. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  136. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  137. data/lib/rubocop/cop/style/format_string_token.rb +24 -4
  138. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +46 -0
  139. data/lib/rubocop/cop/style/hash_syntax.rb +4 -3
  140. data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -0
  141. data/lib/rubocop/cop/style/method_def_parentheses.rb +79 -0
  142. data/lib/rubocop/cop/style/mixin_usage.rb +13 -2
  143. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +19 -0
  145. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  146. data/lib/rubocop/cop/style/negated_while.rb +6 -4
  147. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
  148. data/lib/rubocop/cop/style/parentheses_around_condition.rb +4 -0
  149. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  150. data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
  151. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  152. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  153. data/lib/rubocop/cop/style/string_literals.rb +4 -0
  154. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +4 -0
  155. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -1
  156. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -0
  157. data/lib/rubocop/cop/style/trailing_comma_in_literal.rb +4 -0
  158. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
  159. data/lib/rubocop/formatter/disabled_config_formatter.rb +33 -24
  160. data/lib/rubocop/options.rb +33 -10
  161. data/lib/rubocop/path_util.rb +7 -0
  162. data/lib/rubocop/token.rb +4 -0
  163. data/lib/rubocop/version.rb +1 -1
  164. metadata +14 -4
  165. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +0 -149
  166. data/lib/rubocop/cop/style/extend_self.rb +0 -92
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbbe943a5ed0fd35d07ba728c5569d279eaffbb8
4
- data.tar.gz: 0fd2d58d865e457479f1d6ce0d8f12c7cda1dba3
3
+ metadata.gz: 9c220d66052a5e9bbd4c5c4b8152f9eb198def00
4
+ data.tar.gz: 3b3e355cef47c417d03265e565f7bf1fe94c7b9e
5
5
  SHA512:
6
- metadata.gz: 4253ebe0cf732ca876078c01852f99cbfb9cc44f8bdb053bc368f6d18839c90152b87322738c052e15b8b128fa4a2e7f85902d8445d961b21df232a3b4fbb184
7
- data.tar.gz: 9deb27503042d598d22d3f748a72fea9900b37a4fb95400180fada165e4ee393631d1b53456230d1a0aa1a69b8d26899c2b2e6253363912f2813de1eb6ac9f7b
6
+ metadata.gz: cba8acef3a405e92862d9ae1e1783bbeb6f9d4675fe978068a519a759598a26b2b79b10e0bc688fe0ee2c1b8a0c640316395c65f94903179b0b45a9b091f63b9
7
+ data.tar.gz: 3e31e510ef57bd3ccd4b2085398c22b1da1a0a137efa487c4f110b5c1d0d8ad568197d899f05a3b79bd9d281c9933ee8c6d19f134b0d052344ca778e50c07739
data/README.md CHANGED
@@ -6,7 +6,8 @@
6
6
  [![Code Climate](https://codeclimate.com/github/bbatsov/rubocop/badges/gpa.svg)](https://codeclimate.com/github/bbatsov/rubocop)
7
7
  [![Inline docs](http://inch-ci.org/github/bbatsov/rubocop.svg)](http://inch-ci.org/github/bbatsov/rubocop)
8
8
 
9
- [![Gratipay Team](https://img.shields.io/gratipay/team/rubocop.svg?maxAge=2592000)](https://gratipay.com/rubocop/)
9
+ [![Patreon](https://img.shields.io/badge/patreon-donate-orange.svg)](https://www.patreon.com/bbatsov)
10
+ [![Liberapay](https://liberapay.com/assets/widgets/donate.svg)](https://liberapay.com/bbatsov/donate)
10
11
  [![OpenCollective](https://opencollective.com/rubocop/backers/badge.svg)](#open-collective-backers)
11
12
  [![OpenCollective](https://opencollective.com/rubocop/sponsors/badge.svg)](#open-collective-sponsors)
12
13
 
@@ -51,7 +52,7 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
51
52
  might want to use a conservative version locking in your `Gemfile`:
52
53
 
53
54
  ```rb
54
- gem 'rubocop', '~> 0.52.0', require: false
55
+ gem 'rubocop', '~> 0.52.1', require: false
55
56
  ```
56
57
 
57
58
  ## Quickstart
@@ -129,9 +130,9 @@ to become a RuboCop sponsor.
129
130
 
130
131
  You can support the development of RuboCop via
131
132
  [Salt](https://salt.bountysource.com/teams/rubocop),
132
- [Gratipay](https://www.gratipay.com/rubocop) and [Open Collective](https://opencollective.com/rubocop).
133
-
134
- [![Support via Gratipay](https://cdn.rawgit.com/gratipay/gratipay-badge/2.1.3/dist/gratipay.png)](https://gratipay.com/rubocop)
133
+ [Patreon](https://www.patreon.com/bbatsov),
134
+ [Liberapay](https://liberapay.com/bbatsov/donate),
135
+ and [Open Collective](https://opencollective.com/rubocop).
135
136
 
136
137
  ### Open Collective Backers
137
138
 
@@ -490,7 +490,7 @@ Layout/SpaceBeforeBlockBraces:
490
490
  SupportedStyles:
491
491
  - space
492
492
  - no_space
493
- EnforcedStyleForEmptyBraces: no_space
493
+ EnforcedStyleForEmptyBraces: space
494
494
  SupportedStylesForEmptyBraces:
495
495
  - space
496
496
  - no_space
@@ -552,9 +552,6 @@ Layout/SpaceInsideStringInterpolation:
552
552
  - no_space
553
553
 
554
554
  Layout/ClassStructure:
555
- Description: 'Enforces a configured order of definitions within a class body.'
556
- StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-classes'
557
- Enabled: false
558
555
  Categories:
559
556
  module_inclusion:
560
557
  - include
@@ -930,12 +927,6 @@ Style/EmptyMethod:
930
927
  - compact
931
928
  - expanded
932
929
 
933
- Style/ExtendSelf:
934
- EnforcedStyle: module_function
935
- SupportedStyles:
936
- - module_function
937
- - extend_self
938
-
939
930
  # Checks use of for or each in multiline loops.
940
931
  Style/For:
941
932
  EnforcedStyle: each
@@ -1003,7 +994,6 @@ Style/HashSyntax:
1003
994
  PreferHashRocketsForNonAlnumEndingSymbols: false
1004
995
 
1005
996
  Style/InverseMethods:
1006
- Enabled: true
1007
997
  # `InverseMethods` are methods that can be inverted by a not (`not` or `!`)
1008
998
  # The relationship of inverse methods only needs to be defined in one direction.
1009
999
  # Keys and values both need to be defined as symbols.
@@ -1,5 +1,10 @@
1
1
  # These are all the cops that are disabled in the default configuration.
2
2
 
3
+ Layout/ClassStructure:
4
+ Description: 'Enforces a configured order of definitions within a class body.'
5
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-classes'
6
+ Enabled: false
7
+
3
8
  Layout/FirstArrayElementLineBreak:
4
9
  Description: >-
5
10
  Checks for a line break before the first element in a
@@ -1327,6 +1327,12 @@ Style/CharacterLiteral:
1327
1327
  Style/ClassAndModuleChildren:
1328
1328
  Description: 'Checks style of children classes and modules.'
1329
1329
  StyleGuide: '#namespace-definition'
1330
+ # Moving from compact to nested children requires knowledge of whether the
1331
+ # outer parent is a module or a class. Moving from nested to compact requires
1332
+ # verification that the outer parent is defined elsewhere. Rubocop does not
1333
+ # have the knowledge to perform either operation safely and thus requires
1334
+ # manual oversight.
1335
+ AutoCorrect: false
1330
1336
  Enabled: true
1331
1337
 
1332
1338
  Style/ClassCheck:
@@ -1459,14 +1465,6 @@ Style/EvenOdd:
1459
1465
  StyleGuide: '#predicate-methods'
1460
1466
  Enabled: true
1461
1467
 
1462
- Style/ExtendSelf:
1463
- Description: 'Checks for uses of extend self.'
1464
- StyleGuide: '#module-function'
1465
- Enabled: true
1466
- # Using `module_function` results in private visibility scope when the
1467
- # module is included, so auto-correction is unsafe.
1468
- Autocorrect: false
1469
-
1470
1468
  Style/FlipFlop:
1471
1469
  Description: 'Checks for flip flops'
1472
1470
  StyleGuide: '#no-flip-flops'
@@ -74,6 +74,7 @@ require_relative 'rubocop/cop/corrector'
74
74
  require_relative 'rubocop/cop/force'
75
75
  require_relative 'rubocop/cop/severity'
76
76
  require_relative 'rubocop/cop/generator'
77
+ require_relative 'rubocop/cop/generator/require_file_injector'
77
78
 
78
79
  require_relative 'rubocop/cop/variable_force'
79
80
  require_relative 'rubocop/cop/variable_force/branch'
@@ -88,7 +89,7 @@ require_relative 'rubocop/cop/mixin/annotation_comment'
88
89
  require_relative 'rubocop/cop/mixin/array_hash_indentation'
89
90
  require_relative 'rubocop/cop/mixin/array_min_size'
90
91
  require_relative 'rubocop/cop/mixin/array_syntax'
91
- require_relative 'rubocop/cop/mixin/autocorrect_alignment'
92
+ require_relative 'rubocop/cop/mixin/alignment'
92
93
  require_relative 'rubocop/cop/mixin/check_assignment'
93
94
  require_relative 'rubocop/cop/mixin/configurable_max'
94
95
  require_relative 'rubocop/cop/mixin/code_length' # relies on configurable_max
@@ -139,6 +140,17 @@ require_relative 'rubocop/cop/mixin/too_many_lines'
139
140
  require_relative 'rubocop/cop/mixin/trailing_comma'
140
141
  require_relative 'rubocop/cop/mixin/unused_argument'
141
142
 
143
+ require_relative 'rubocop/cop/correctors/alignment_corrector'
144
+ require_relative 'rubocop/cop/correctors/condition_corrector'
145
+ require_relative 'rubocop/cop/correctors/empty_line_corrector'
146
+ require_relative 'rubocop/cop/correctors/multiline_literal_brace_corrector'
147
+ require_relative 'rubocop/cop/correctors/ordered_gem_corrector'
148
+ require_relative 'rubocop/cop/correctors/parentheses_corrector'
149
+ require_relative 'rubocop/cop/correctors/punctuation_corrector'
150
+ require_relative 'rubocop/cop/correctors/space_corrector'
151
+ require_relative 'rubocop/cop/correctors/string_literal_corrector'
152
+ require_relative 'rubocop/cop/correctors/unused_arg_corrector'
153
+
142
154
  require_relative 'rubocop/cop/bundler/duplicated_gem'
143
155
  require_relative 'rubocop/cop/bundler/insecure_protocol_source'
144
156
  require_relative 'rubocop/cop/bundler/ordered_gems'
@@ -390,7 +402,6 @@ require_relative 'rubocop/cop/style/encoding'
390
402
  require_relative 'rubocop/cop/style/end_block'
391
403
  require_relative 'rubocop/cop/style/eval_with_location'
392
404
  require_relative 'rubocop/cop/style/even_odd'
393
- require_relative 'rubocop/cop/style/extend_self'
394
405
  require_relative 'rubocop/cop/style/flip_flop'
395
406
  require_relative 'rubocop/cop/style/for'
396
407
  require_relative 'rubocop/cop/style/format_string'
@@ -259,6 +259,27 @@ module RuboCop
259
259
  loc.expression
260
260
  end
261
261
 
262
+ def first_line
263
+ loc.line
264
+ end
265
+
266
+ def last_line
267
+ loc.last_line
268
+ end
269
+
270
+ def line_count
271
+ return 0 unless source_range
272
+ source_range.last_line - source_range.first_line + 1
273
+ end
274
+
275
+ def nonempty_line_count
276
+ source.lines.grep(/\S/).size
277
+ end
278
+
279
+ def source_length
280
+ source_range ? source_range.size : 0
281
+ end
282
+
262
283
  ## Destructuring
263
284
 
264
285
  def_node_matcher :receiver, <<-PATTERN
@@ -322,21 +343,8 @@ module RuboCop
322
343
  line_count == 1
323
344
  end
324
345
 
325
- def line_count
326
- return 0 unless source_range
327
- source_range.last_line - source_range.first_line + 1
328
- end
329
-
330
- def nonempty_line_count
331
- source.lines.grep(/\S/).size
332
- end
333
-
334
- def empty?
335
- length.zero?
336
- end
337
-
338
- def length
339
- source_range ? source_range.size : 0
346
+ def empty_source?
347
+ source_length.zero?
340
348
  end
341
349
 
342
350
  def asgn_method_call?
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rubocop:disable Metrics/ClassLength
3
4
  module RuboCop
4
5
  # The CLI is a class responsible of handling all the command line interface
5
6
  # logic.
6
7
  class CLI
7
8
  include Formatter::TextUtil
8
9
 
10
+ SKIPPED_PHASE_1 = 'Phase 1 of 2: run Metrics/LineLength cop (skipped ' \
11
+ 'because the default Metrics/LineLength:Max is ' \
12
+ 'overridden)'.freeze
13
+
9
14
  class Finished < RuntimeError; end
10
15
 
11
16
  attr_reader :options, :config_store
@@ -23,6 +28,8 @@ module RuboCop
23
28
  #
24
29
  # @param args [Array<String>] command line arguments
25
30
  # @return [Integer] UNIX exit code
31
+ #
32
+ # rubocop:disable Metrics/MethodLength
26
33
  def run(args = ARGV)
27
34
  @options, paths = Options.new.parse(args)
28
35
  validate_options_vs_config
@@ -34,11 +41,15 @@ module RuboCop
34
41
  return 2
35
42
  rescue Finished
36
43
  return 0
44
+ rescue IncorrectCopNameError => e
45
+ warn e.message
46
+ return 2
37
47
  rescue StandardError, SyntaxError, LoadError => e
38
48
  warn e.message
39
49
  warn e.backtrace
40
50
  return 2
41
51
  end
52
+ # rubocop:enable Metrics/MethodLength
42
53
 
43
54
  def trap_interrupt(runner)
44
55
  Signal.trap('INT') do
@@ -54,13 +65,24 @@ module RuboCop
54
65
  def execute_runners(paths)
55
66
  if @options[:auto_gen_config]
56
67
  reset_config_and_auto_gen_file
57
- line_length_contents = run_line_length_cop_auto_gen_config(paths)
68
+ line_length_contents =
69
+ if max_line_length(@config_store.for(Dir.pwd)) ==
70
+ max_line_length(ConfigLoader.default_configuration)
71
+ run_line_length_cop_auto_gen_config(paths)
72
+ else
73
+ puts Rainbow(SKIPPED_PHASE_1).yellow
74
+ ''
75
+ end
58
76
  run_all_cops_auto_gen_config(line_length_contents, paths)
59
77
  else
60
78
  execute_runner(paths)
61
79
  end
62
80
  end
63
81
 
82
+ def max_line_length(config)
83
+ config.for_cop('Metrics/LineLength')['Max']
84
+ end
85
+
64
86
  # Do an initial run with only Metrics/LineLength so that cops that depend
65
87
  # on Metrics/LineLength:Max get the correct value for that parameter.
66
88
  def run_line_length_cop_auto_gen_config(paths)
@@ -89,7 +111,7 @@ module RuboCop
89
111
 
90
112
  def reset_config_and_auto_gen_file
91
113
  @config_store = ConfigStore.new
92
- File.open(ConfigLoader::AUTO_GENERATED_FILE, 'w'){}
114
+ File.open(ConfigLoader::AUTO_GENERATED_FILE, 'w') {}
93
115
  ConfigLoader.add_inheritance_from_auto_generated_file
94
116
  end
95
117
 
@@ -243,3 +265,4 @@ module RuboCop
243
265
  end
244
266
  end
245
267
  end
268
+ # rubocop:enable Metrics/ClassLength
@@ -19,6 +19,7 @@ module RuboCop
19
19
  DEFAULT_RUBY_VERSION = 2.1
20
20
  KNOWN_RUBIES = [2.1, 2.2, 2.3, 2.4, 2.5].freeze
21
21
  OBSOLETE_RUBIES = { 1.9 => '0.50', 2.0 => '0.50' }.freeze
22
+ RUBY_VERSION_FILENAME = '.ruby-version'.freeze
22
23
  DEFAULT_RAILS_VERSION = 5.0
23
24
  OBSOLETE_COPS = {
24
25
  'Style/TrailingComma' =>
@@ -195,7 +196,7 @@ module RuboCop
195
196
 
196
197
  def check
197
198
  deprecation_check do |deprecation_message|
198
- warn("#{path} - #{deprecation_message}")
199
+ warn("#{loaded_path} - #{deprecation_message}")
199
200
  end
200
201
  validate
201
202
  make_excludes_absolute
@@ -383,12 +384,10 @@ module RuboCop
383
384
  @target_ruby_version_source = :rubocop_yml
384
385
 
385
386
  for_all_cops['TargetRubyVersion']
386
- elsif File.file?('.ruby-version') &&
387
- /\A(ruby-)?(?<version>\d+\.\d+)/ =~ File.read('.ruby-version')
387
+ elsif target_ruby_version_from_version_file
388
+ @target_ruby_version_source = :ruby_version_file
388
389
 
389
- @target_ruby_version_source = :dot_ruby_version
390
-
391
- version.to_f
390
+ target_ruby_version_from_version_file
392
391
  else
393
392
  DEFAULT_RUBY_VERSION
394
393
  end
@@ -513,13 +512,29 @@ module RuboCop
513
512
 
514
513
  def target_ruby_source
515
514
  case @target_ruby_version_source
516
- when :dot_ruby_version
517
- '`.ruby-version`'
515
+ when :ruby_version_file
516
+ "`#{RUBY_VERSION_FILENAME}`"
518
517
  when :rubocop_yml
519
518
  "`TargetRubyVersion` parameter (in #{smart_loaded_path})"
520
519
  end
521
520
  end
522
521
 
522
+ def ruby_version_file
523
+ @ruby_version_file ||=
524
+ PathUtil.find_file_upwards(RUBY_VERSION_FILENAME,
525
+ base_dir_for_path_parameters)
526
+ end
527
+
528
+ def target_ruby_version_from_version_file
529
+ file = ruby_version_file
530
+ return unless file && File.file?(file)
531
+
532
+ @target_ruby_version_from_version_file ||=
533
+ File.read(file).match(/\A(ruby-)?(?<version>\d+\.\d+)/) do |md|
534
+ md[:version].to_f
535
+ end
536
+ end
537
+
523
538
  def reject_mutually_exclusive_defaults
524
539
  disabled_by_default = for_all_cops['DisabledByDefault']
525
540
  enabled_by_default = for_all_cops['EnabledByDefault']
@@ -37,7 +37,7 @@ module RuboCop
37
37
  register_offense(
38
38
  node,
39
39
  node.first_argument.to_a.first,
40
- nodes.first.loc.line
40
+ nodes.first.first_line
41
41
  )
42
42
  end
43
43
  end
@@ -57,7 +57,7 @@ module RuboCop
57
57
  def register_offense(node, gem_name, line_of_first_occurrence)
58
58
  line_range = node.loc.column...node.loc.last_column
59
59
  offense_location =
60
- source_range(processed_source.buffer, node.loc.line, line_range)
60
+ source_range(processed_source.buffer, node.first_line, line_range)
61
61
  message = format(
62
62
  MSG,
63
63
  gem_name: gem_name,
@@ -45,6 +45,15 @@ module RuboCop
45
45
  end
46
46
  end
47
47
 
48
+ def autocorrect(node)
49
+ OrderedGemCorrector.correct(
50
+ processed_source,
51
+ node,
52
+ previous_declaration(node),
53
+ treat_comments_as_separators
54
+ )
55
+ end
56
+
48
57
  private
49
58
 
50
59
  def previous_declaration(node)
@@ -110,7 +110,7 @@ module RuboCop
110
110
  rescue StandardError => e
111
111
  raise e if @options[:raise_error]
112
112
  if node
113
- line = node.loc.line
113
+ line = node.first_line
114
114
  column = node.loc.column
115
115
  end
116
116
  error = CopError.new(e, line, column)
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # This class does auto-correction of nodes that should just be moved to
6
+ # the left or to the right, amount being determined by the instance
7
+ # variable column_delta.
8
+ class AlignmentCorrector
9
+ extend Util
10
+ extend Alignment
11
+
12
+ class << self
13
+ attr_reader :processed_source
14
+
15
+ def correct(processed_source, node, column_delta)
16
+ return unless node
17
+ @processed_source = processed_source
18
+ expr = node.respond_to?(:loc) ? node.loc.expression : node
19
+ return if block_comment_within?(expr)
20
+
21
+ lambda do |corrector|
22
+ each_line(expr) do |line_begin_pos|
23
+ autocorrect_line(corrector, line_begin_pos, expr, column_delta,
24
+ heredoc_ranges(node))
25
+ end
26
+ end
27
+ end
28
+
29
+ def align_end(processed_source, node, align_to)
30
+ @processed_source = processed_source
31
+ whitespace = whitespace_range(node)
32
+ return false unless whitespace.source.strip.empty?
33
+
34
+ column = alignment_column(align_to)
35
+ ->(corrector) { corrector.replace(whitespace, ' ' * column) }
36
+ end
37
+
38
+ private
39
+
40
+ def autocorrect_line(corrector, line_begin_pos, expr, column_delta,
41
+ heredoc_ranges)
42
+ range = calculate_range(expr, line_begin_pos, column_delta)
43
+ # We must not change indentation of heredoc strings.
44
+ return if heredoc_ranges.any? { |h| within?(range, h) }
45
+
46
+ if column_delta > 0
47
+ unless range.source == "\n"
48
+ corrector.insert_before(range, ' ' * column_delta)
49
+ end
50
+ elsif range.source =~ /\A[ \t]+\z/
51
+ remove(range, corrector)
52
+ end
53
+ end
54
+
55
+ def heredoc_ranges(node)
56
+ return [] unless node.is_a?(Parser::AST::Node)
57
+
58
+ node.each_node(:dstr)
59
+ .select { |n| n.loc.respond_to?(:heredoc_body) }
60
+ .map { |n| n.loc.heredoc_body.join(n.loc.heredoc_end) }
61
+ end
62
+
63
+ def block_comment_within?(expr)
64
+ processed_source.comments.select(&:document?).any? do |c|
65
+ within?(c.loc.expression, expr)
66
+ end
67
+ end
68
+
69
+ def calculate_range(expr, line_begin_pos, column_delta)
70
+ starts_with_space = expr.source_buffer.source[line_begin_pos] =~ / /
71
+ pos_to_remove = if column_delta > 0 || starts_with_space
72
+ line_begin_pos
73
+ else
74
+ line_begin_pos - column_delta.abs
75
+ end
76
+
77
+ range_between(pos_to_remove, pos_to_remove + column_delta.abs)
78
+ end
79
+
80
+ def remove(range, corrector)
81
+ original_stderr = $stderr
82
+ $stderr = StringIO.new # Avoid error messages on console
83
+ corrector.remove(range)
84
+ rescue RuntimeError
85
+ range = range_between(range.begin_pos + 1, range.end_pos + 1)
86
+ retry if range.source =~ /^ +$/
87
+ ensure
88
+ $stderr = original_stderr
89
+ end
90
+
91
+ def each_line(expr)
92
+ line_begin_pos = expr.begin_pos
93
+ expr.source.each_line do |line|
94
+ yield line_begin_pos
95
+ line_begin_pos += line.length
96
+ end
97
+ end
98
+
99
+ def begins_its_line?(range)
100
+ (range.source_line =~ /\S/) == range.column
101
+ end
102
+
103
+ def whitespace_range(node)
104
+ begin_pos = node.loc.end.begin_pos
105
+
106
+ range_between(begin_pos - node.loc.end.column, begin_pos)
107
+ end
108
+
109
+ def alignment_column(align_to)
110
+ if !align_to
111
+ 0
112
+ elsif align_to.respond_to?(:loc)
113
+ align_to.source_range.column
114
+ else
115
+ align_to.column
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end