rubocop 0.85.1 → 0.86.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -4
  3. data/config/default.yml +29 -1
  4. data/lib/rubocop.rb +3 -0
  5. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  6. data/lib/rubocop/config.rb +1 -1
  7. data/lib/rubocop/config_loader_resolver.rb +1 -1
  8. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  9. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  10. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  11. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  12. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
  13. data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
  14. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  15. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  16. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  17. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  18. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  19. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  20. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  21. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  22. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
  23. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  24. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  25. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  26. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  27. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +6 -1
  28. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  29. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  30. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  31. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
  32. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  33. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  34. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  35. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  36. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  37. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  38. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  39. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  40. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  41. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  42. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  43. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  44. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  45. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
  46. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  47. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  48. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  49. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -2
  50. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  51. data/lib/rubocop/cop/naming/file_name.rb +1 -3
  52. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  53. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  54. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  55. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  56. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  57. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  58. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  59. data/lib/rubocop/cop/style/copyright.rb +3 -3
  60. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  61. data/lib/rubocop/cop/style/documentation.rb +2 -2
  62. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  63. data/lib/rubocop/cop/style/encoding.rb +1 -1
  64. data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
  65. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  66. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  67. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  68. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  69. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  70. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  71. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  72. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  73. data/lib/rubocop/cop/style/next.rb +2 -2
  74. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  75. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  76. data/lib/rubocop/cop/style/redundant_fetch_block.rb +103 -0
  77. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  78. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  79. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  80. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
  81. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  82. data/lib/rubocop/cop/style/sample.rb +1 -1
  83. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  84. data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
  85. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  86. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  87. data/lib/rubocop/cop/style/word_array.rb +1 -1
  88. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  89. data/lib/rubocop/cop/util.rb +2 -2
  90. data/lib/rubocop/cop/utils/format_string.rb +1 -1
  91. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  92. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  93. data/lib/rubocop/name_similarity.rb +6 -0
  94. data/lib/rubocop/path_util.rb +2 -2
  95. data/lib/rubocop/platform.rb +1 -1
  96. data/lib/rubocop/rspec/expect_offense.rb +12 -2
  97. data/lib/rubocop/target_finder.rb +1 -1
  98. data/lib/rubocop/target_ruby.rb +1 -1
  99. data/lib/rubocop/version.rb +1 -1
  100. metadata +11 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86b240fb0a653934db99c6ca12a1c53976c01f3d6de666842e9f7146f207f725
4
- data.tar.gz: 606ea84dc8c148971ba8a2dc17ef2a03bbb48802e6053f5042d180e0f6c7c15f
3
+ metadata.gz: 99c7f4b160b77fafa6fb31562b3fbd206a50b4b5201c918250b1a1d94105fb14
4
+ data.tar.gz: d709c3614f3aaf536c0acefe88effe53ff51c148e37cc57740890d4ec4abbb50
5
5
  SHA512:
6
- metadata.gz: 4e6a817ec287b216c15e8820e29192fe1152fa77bb9b6fb9377f7a9b1bc22faef07e15c1f025691107914ae211f401549ef831c755fbf6b9c21bd04198388742
7
- data.tar.gz: 2e3a6b4d35e5b24a9542ac5749247537ae2a47db242e86a189d3f719f7b1d35568eb22deaf491b2f5d1408de8415b2ee86fadfe8a56f229fbce89686744cc5e4
6
+ metadata.gz: 966a99e7d38184ccdd1f6b21c14c8d800a6f86488f9685571d60b712088ddac8218f1324e9fc61ddf37d941d6f16b6f6a8a3a1eb6f7c897780e02d1d7994fb15
7
+ data.tar.gz: 61bc95d25555286a6ed5e4fc710ce97cbd0b1cf7858e8bd876cb42bf1cbb673fde2316917abf5e2f0e89252042b1a4495cb88f5cbc579046052e71a22cef1259
data/README.md CHANGED
@@ -3,12 +3,12 @@
3
3
  </p>
4
4
 
5
5
  ----------
6
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
6
7
  [![Gem Version](https://badge.fury.io/rb/rubocop.svg)](https://badge.fury.io/rb/rubocop)
7
8
  [![CircleCI Status](https://circleci.com/gh/rubocop-hq/rubocop/tree/master.svg?style=svg)](https://circleci.com/gh/rubocop-hq/rubocop/tree/master)
8
9
  [![Actions Status](https://github.com/rubocop-hq/rubocop/workflows/CI/badge.svg?branch=master)](https://github.com/rubocop-hq/rubocop/actions?query=workflow%3ACI)
9
- [![Coverage Status](https://api.codeclimate.com/v1/badges/ad6e76460499c8c99697/test_coverage)](https://codeclimate.com/github/bbatsov/rubocop)
10
- [![Code Climate](https://codeclimate.com/github/bbatsov/rubocop/badges/gpa.svg)](https://codeclimate.com/github/bbatsov/rubocop)
11
- [![Inline docs](https://inch-ci.org/github/bbatsov/rubocop.svg)](https://inch-ci.org/github/bbatsov/rubocop)
10
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/d2d67f728e88ea84ac69/test_coverage)](https://codeclimate.com/github/rubocop-hq/rubocop/test_coverage)
11
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d2d67f728e88ea84ac69/maintainability)](https://codeclimate.com/github/rubocop-hq/rubocop/maintainability)
12
12
  [![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=rubocop&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=rubocop&package-manager=bundler&version-scheme=semver)
13
13
 
14
14
  [![Patreon](https://img.shields.io/badge/patreon-donate-orange.svg)](https://www.patreon.com/bbatsov)
@@ -49,7 +49,7 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
49
49
  might want to use a conservative version lock in your `Gemfile`:
50
50
 
51
51
  ```rb
52
- gem 'rubocop', '~> 0.85.1', require: false
52
+ gem 'rubocop', '~> 0.86.0', require: false
53
53
  ```
54
54
 
55
55
  ## Quickstart
@@ -72,6 +72,16 @@ RuboCop supports the following Ruby implementations:
72
72
  * MRI 2.4+
73
73
  * JRuby 9.2+
74
74
 
75
+ See [compatibility](https://docs.rubocop.org/rubocop/compatibility.html) for further details.
76
+
77
+ ## Readme Badge
78
+
79
+ If you use RuboCop in your project, you can include one of these badges in your readme to let people know that your code is written following the community Ruby Style Guide.
80
+
81
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
82
+
83
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rubystyle.guide)
84
+
75
85
  ## Team
76
86
 
77
87
  Here's a list of RuboCop's core developers:
@@ -84,6 +94,8 @@ Here's a list of RuboCop's core developers:
84
94
  * [Masataka Kuwabara](https://github.com/pocke)
85
95
  * [Koichi Ito](https://github.com/koic)
86
96
  * [Maxim Krizhanovski](https://github.com/darhazer)
97
+ * [Benjamin Quorning](https://github.com/bquorning)
98
+ * [Marc-André Lafortune](https://github.com/marcandre)
87
99
 
88
100
  ## Logo
89
101
 
@@ -1361,6 +1361,15 @@ Lint/CircularArgumentReference:
1361
1361
  Enabled: true
1362
1362
  VersionAdded: '0.33'
1363
1363
 
1364
+ Lint/ConstantResolution:
1365
+ Description: 'Check that constants are fully qualified with `::`.'
1366
+ Enabled: false
1367
+ VersionAdded: '0.86'
1368
+ # Restrict this cop to only looking at certain names
1369
+ Only: []
1370
+ # Restrict this cop from only looking at certain names
1371
+ Ignore: []
1372
+
1364
1373
  Lint/Debugger:
1365
1374
  Description: 'Check for debugger calls.'
1366
1375
  Enabled: true
@@ -1611,7 +1620,9 @@ Lint/RaiseException:
1611
1620
  Description: Checks for `raise` or `fail` statements which are raising `Exception` class.
1612
1621
  StyleGuide: '#raise-exception'
1613
1622
  Enabled: pending
1623
+ Safe: false
1614
1624
  VersionAdded: '0.81'
1625
+ VersionChanged: '0.86'
1615
1626
  AllowedImplicitNamespaces:
1616
1627
  - 'Gem'
1617
1628
 
@@ -1668,6 +1679,7 @@ Lint/RegexpAsCondition:
1668
1679
  The regexp literal matches `$_` implicitly.
1669
1680
  Enabled: true
1670
1681
  VersionAdded: '0.51'
1682
+ VersionChanged: '0.86'
1671
1683
 
1672
1684
  Lint/RequireParentheses:
1673
1685
  Description: >-
@@ -1925,7 +1937,7 @@ Metrics/CyclomaticComplexity:
1925
1937
  VersionAdded: '0.25'
1926
1938
  VersionChanged: '0.81'
1927
1939
  IgnoredMethods: []
1928
- Max: 6
1940
+ Max: 7
1929
1941
 
1930
1942
  Metrics/MethodLength:
1931
1943
  Description: 'Avoid methods longer than 10 lines of code.'
@@ -3239,6 +3251,7 @@ Style/MultilineTernaryOperator:
3239
3251
  StyleGuide: '#no-multiline-ternary'
3240
3252
  Enabled: true
3241
3253
  VersionAdded: '0.9'
3254
+ VersionChanged: '0.86'
3242
3255
 
3243
3256
  Style/MultilineWhenThen:
3244
3257
  Description: 'Do not use then for multi-line when statement.'
@@ -3343,6 +3356,7 @@ Style/NestedTernaryOperator:
3343
3356
  StyleGuide: '#no-nested-ternary'
3344
3357
  Enabled: true
3345
3358
  VersionAdded: '0.9'
3359
+ VersionChanged: '0.86'
3346
3360
 
3347
3361
  Style/Next:
3348
3362
  Description: 'Use `next` to skip iteration instead of a condition at the end.'
@@ -3596,6 +3610,19 @@ Style/RedundantException:
3596
3610
  VersionAdded: '0.14'
3597
3611
  VersionChanged: '0.29'
3598
3612
 
3613
+ Style/RedundantFetchBlock:
3614
+ Description: >-
3615
+ Use `fetch(key, value)` instead of `fetch(key) { value }`
3616
+ when value has Numeric, Rational, Complex, Symbol or String type, `false`, `true`, `nil` or is a constant.
3617
+ Reference: 'https://github.com/JuanitoFatas/fast-ruby#hashfetch-with-argument-vs-hashfetch--block-code'
3618
+ Enabled: 'pending'
3619
+ Safe: false
3620
+ # If enabled, this cop will autocorrect usages of
3621
+ # `fetch` being called with block returning a constant.
3622
+ # This can be dangerous since constants will not be defined at that moment.
3623
+ SafeForConstants: false
3624
+ VersionAdded: '0.86'
3625
+
3599
3626
  Style/RedundantFreeze:
3600
3627
  Description: "Checks usages of Object#freeze on immutable objects."
3601
3628
  Enabled: true
@@ -3875,6 +3902,7 @@ Style/StructInheritance:
3875
3902
  StyleGuide: '#no-extend-struct-new'
3876
3903
  Enabled: true
3877
3904
  VersionAdded: '0.29'
3905
+ VersionChanged: '0.86'
3878
3906
 
3879
3907
  Style/SymbolArray:
3880
3908
  Description: 'Use %i or %I for arrays of symbols.'
@@ -242,6 +242,7 @@ require_relative 'rubocop/cop/lint/assignment_in_condition'
242
242
  require_relative 'rubocop/cop/lint/big_decimal_new'
243
243
  require_relative 'rubocop/cop/lint/boolean_symbol'
244
244
  require_relative 'rubocop/cop/lint/circular_argument_reference'
245
+ require_relative 'rubocop/cop/lint/constant_resolution'
245
246
  require_relative 'rubocop/cop/lint/debugger'
246
247
  require_relative 'rubocop/cop/lint/deprecated_class_methods'
247
248
  require_relative 'rubocop/cop/lint/deprecated_open_ssl_constant'
@@ -321,6 +322,7 @@ require_relative 'rubocop/cop/lint/useless_else_without_rescue'
321
322
  require_relative 'rubocop/cop/lint/useless_setter_call'
322
323
  require_relative 'rubocop/cop/lint/void'
323
324
 
325
+ require_relative 'rubocop/cop/metrics/utils/iterating_block'
324
326
  require_relative 'rubocop/cop/metrics/cyclomatic_complexity'
325
327
  # relies on cyclomatic_complexity
326
328
  require_relative 'rubocop/cop/metrics/utils/abc_size_calculator'
@@ -424,6 +426,7 @@ require_relative 'rubocop/cop/style/lambda_call'
424
426
  require_relative 'rubocop/cop/style/line_end_concatenation'
425
427
  require_relative 'rubocop/cop/style/method_call_without_args_parentheses'
426
428
  require_relative 'rubocop/cop/style/method_call_with_args_parentheses'
429
+ require_relative 'rubocop/cop/style/redundant_fetch_block'
427
430
  require_relative 'rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses'
428
431
  require_relative 'rubocop/cop/style/method_call_with_args_parentheses/require_parentheses'
429
432
  require_relative 'rubocop/cop/style/method_called_on_do_end_block'
@@ -27,7 +27,7 @@ module RuboCop
27
27
  # RuboCop will start looking for the configuration file in the directory
28
28
  # where the inspected file is and continue its way up to the root directory.
29
29
  #
30
- # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
30
+ # See https://docs.rubocop.org/rubocop/configuration
31
31
  DESC
32
32
 
33
33
  File.open(DOTFILE, 'w') do |f|
@@ -152,7 +152,7 @@ module RuboCop
152
152
  return true if File.extname(file) == '.gemspec'
153
153
 
154
154
  file_to_include?(file) do |pattern, relative_path, absolute_path|
155
- pattern.to_s =~ /[A-Z]/ &&
155
+ /[A-Z]/.match?(pattern.to_s) &&
156
156
  (match_path?(pattern, relative_path) ||
157
157
  match_path?(pattern, absolute_path))
158
158
  end
@@ -192,7 +192,7 @@ module RuboCop
192
192
 
193
193
  def remote_file?(uri)
194
194
  regex = URI::DEFAULT_PARSER.make_regexp(%w[http https])
195
- uri =~ /\A#{regex}\z/
195
+ /\A#{regex}\z/.match?(uri)
196
196
  end
197
197
 
198
198
  def handle_disabled_by_default(config, new_default_configuration)
@@ -64,7 +64,7 @@ module RuboCop
64
64
  def extract_ruby_version(required_ruby_version)
65
65
  if required_ruby_version.array_type?
66
66
  required_ruby_version = required_ruby_version.children.detect do |v|
67
- v.str_content =~ /[>=]/
67
+ /[>=]/.match?(v.str_content)
68
68
  end
69
69
  end
70
70
 
@@ -58,7 +58,7 @@ module RuboCop
58
58
  end
59
59
 
60
60
  def cop_name_line?(yaml)
61
- yaml !~ /^[\s#]/
61
+ !/^[\s#]/.match?(yaml)
62
62
  end
63
63
  end
64
64
  end
@@ -109,7 +109,7 @@ module RuboCop
109
109
 
110
110
  def own_line_comment?(comment)
111
111
  own_line = processed_source.lines[comment.loc.line - 1]
112
- own_line =~ /\A\s*#/
112
+ /\A\s*#/.match?(own_line)
113
113
  end
114
114
 
115
115
  def line_after_comment(comment)
@@ -129,11 +129,11 @@ module RuboCop
129
129
  end
130
130
 
131
131
  def less_indented?(line)
132
- line =~ /^\s*(end\b|[)}\]])/
132
+ /^\s*(end\b|[)}\]])/.match?(line)
133
133
  end
134
134
 
135
135
  def two_alternatives?(line)
136
- line =~ /^\s*(else|elsif|when|rescue|ensure)\b/
136
+ /^\s*(else|elsif|when|rescue|ensure)\b/.match?(line)
137
137
  end
138
138
  end
139
139
  end
@@ -119,7 +119,7 @@ module RuboCop
119
119
  /\A(#+\n)+\z/
120
120
  end
121
121
 
122
- !(comment_text =~ empty_comment_pattern).nil?
122
+ empty_comment_pattern.match?(comment_text)
123
123
  end
124
124
 
125
125
  def comment_text(comment)
@@ -96,6 +96,8 @@ module RuboCop
96
96
  end
97
97
 
98
98
  def next_line_node(node)
99
+ return if node.parent.if_type?
100
+
99
101
  node.parent.children[node.sibling_index + 1]
100
102
  end
101
103
 
@@ -65,7 +65,7 @@ module RuboCop
65
65
 
66
66
  # If there is no LF on the last line, we don't care if there's no CR.
67
67
  def unimportant_missing_cr?(index, last_line, line)
68
- style == :crlf && index == last_line - 1 && line !~ /\n$/
68
+ style == :crlf && index == last_line - 1 && !/\n$/.match?(line)
69
69
  end
70
70
 
71
71
  def offense_message(line)
@@ -168,7 +168,7 @@ module RuboCop
168
168
 
169
169
  send_node = arg_node.parent
170
170
  text = base_range(send_node, arg_node).source.strip
171
- base = if text !~ /\n/ && special_inner_call_indentation?(send_node)
171
+ base = if !/\n/.match?(text) && special_inner_call_indentation?(send_node)
172
172
  "`#{text}`"
173
173
  elsif comment_line?(text.lines.reverse_each.first)
174
174
  'the start of the previous line (not counting the comment)'
@@ -37,7 +37,7 @@ module RuboCop
37
37
 
38
38
  def assignment_on_same_line?(node)
39
39
  source = node.source_range.source_line[0...node.loc.column]
40
- source =~ /\s*=\s*$/
40
+ /\s*=\s*$/.match?(source)
41
41
  end
42
42
  end
43
43
  end
@@ -200,7 +200,7 @@ module RuboCop
200
200
  alias on_super on_send
201
201
  alias on_yield on_send
202
202
 
203
- def on_hash(node)
203
+ def on_hash(node) # rubocop:todo Metrics/CyclomaticComplexity
204
204
  return if ignored_node?(node)
205
205
  return if node.pairs.empty? || node.single_line?
206
206
 
@@ -121,7 +121,7 @@ module RuboCop
121
121
  end
122
122
 
123
123
  def autocorrect_body(corrector, node, block_body)
124
- first_node = if block_body.begin_type?
124
+ first_node = if block_body.begin_type? && !block_body.source.start_with?('(')
125
125
  block_body.children.first
126
126
  else
127
127
  block_body
@@ -39,7 +39,7 @@ module RuboCop
39
39
  private
40
40
 
41
41
  def followed_by_space?(colon)
42
- colon.source_buffer.source[colon.end_pos] =~ /\s/
42
+ /\s/.match?(colon.source_buffer.source[colon.end_pos])
43
43
  end
44
44
  end
45
45
  end
@@ -186,7 +186,7 @@ module RuboCop
186
186
  pos = range.begin_pos - 1
187
187
  return false if pos.negative?
188
188
 
189
- range.source_buffer.source[pos] !~ /[\s(|{\[;,*=]/
189
+ !/[\s(|{\[;,*=]/.match?(range.source_buffer.source[pos])
190
190
  end
191
191
 
192
192
  def space_after_missing?(range)
@@ -198,7 +198,7 @@ module RuboCop
198
198
  return false if accept_namespace_operator?(range) &&
199
199
  namespace_operator?(range, pos)
200
200
 
201
- char !~ /[\s;,#\\)}\].]/
201
+ !/[\s;,#\\)}\].]/.match?(char)
202
202
  end
203
203
 
204
204
  def accepted_opening_delimiter?(range, char)
@@ -134,7 +134,7 @@ module RuboCop
134
134
 
135
135
  def autocorrect(range)
136
136
  lambda do |corrector|
137
- if range.source =~ /\*\*/ && !space_around_exponent_operator?
137
+ if /\*\*/.match?(range.source) && !space_around_exponent_operator?
138
138
  corrector.replace(range, '**')
139
139
  elsif range.source.end_with?("\n")
140
140
  corrector.replace(range, " #{range.source.strip}\n")
@@ -27,6 +27,20 @@ module RuboCop
27
27
  # foo.map{ |a|
28
28
  # a.bar.to_s
29
29
  # }
30
+ #
31
+ # @example EnforcedStyleForEmptyBraces: space (default)
32
+ # # bad
33
+ # 7.times{}
34
+ #
35
+ # # good
36
+ # 7.times {}
37
+ #
38
+ # @example EnforcedStyleForEmptyBraces: no_space
39
+ # # bad
40
+ # 7.times {}
41
+ #
42
+ # # good
43
+ # 7.times{}
30
44
  class SpaceBeforeBlockBraces < Cop
31
45
  include ConfigurableEnforcedStyle
32
46
  include RangeHelp
@@ -130,7 +130,7 @@ module RuboCop
130
130
  line, col = line_and_column_for(token)
131
131
  return true if col == -1
132
132
 
133
- processed_source.lines[line][0..col] !~ /\S/
133
+ !/\S/.match?(processed_source.lines[line][0..col])
134
134
  end
135
135
 
136
136
  def index_for(node, token)
@@ -150,7 +150,7 @@ module RuboCop
150
150
  end
151
151
 
152
152
  def check_right_brace(inner, left_brace, right_brace, single_line)
153
- if single_line && inner =~ /\S$/
153
+ if single_line && /\S$/.match?(inner)
154
154
  no_space(right_brace.begin_pos, right_brace.end_pos,
155
155
  'Space missing inside }.')
156
156
  else
@@ -182,7 +182,7 @@ module RuboCop
182
182
  def range_of_space_to_the_right(range)
183
183
  src = range.source_buffer.source
184
184
  end_pos = range.end_pos
185
- end_pos += 1 while src[end_pos] =~ /[ \t]/
185
+ end_pos += 1 while /[ \t]/.match?(src[end_pos])
186
186
 
187
187
  range_between(range.begin_pos + 1, end_pos)
188
188
  end
@@ -190,7 +190,7 @@ module RuboCop
190
190
  def range_of_space_to_the_left(range)
191
191
  src = range.source_buffer.source
192
192
  begin_pos = range.begin_pos
193
- begin_pos -= 1 while src[begin_pos - 1] =~ /[ \t]/
193
+ begin_pos -= 1 while /[ \t]/.match?(src[begin_pos - 1])
194
194
 
195
195
  range_between(begin_pos, range.end_pos - 1)
196
196
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Check that certain constants are fully qualified.
7
+ #
8
+ # This is not enabled by default because it would mark a lot of offenses
9
+ # unnecessarily.
10
+ #
11
+ # Generally, gems should fully qualify all constants to avoid conflicts with
12
+ # the code that uses the gem. Enable this cop without using `Only`/`Ignore`
13
+ #
14
+ # Large projects will over time end up with one or two constant names that
15
+ # are problematic because of a conflict with a library or just internally
16
+ # using the same name a namespace and a class. To avoid too many unnecessary
17
+ # offenses, Enable this cop with `Only: [The, Constant, Names, Causing, Issues]`
18
+ #
19
+ # @example
20
+ # # By default checks every constant
21
+ #
22
+ # # bad
23
+ # User
24
+ #
25
+ # # bad
26
+ # User::Login
27
+ #
28
+ # # good
29
+ # ::User
30
+ #
31
+ # # good
32
+ # ::User::Login
33
+ #
34
+ # @example Only: ['Login']
35
+ # # Restrict this cop to only being concerned about certain constants
36
+ #
37
+ # # bad
38
+ # Login
39
+ #
40
+ # # good
41
+ # ::Login
42
+ #
43
+ # # good
44
+ # User::Login
45
+ #
46
+ # @example Ignore: ['Login']
47
+ # # Restrict this cop not being concerned about certain constants
48
+ #
49
+ # # bad
50
+ # User
51
+ #
52
+ # # good
53
+ # ::User::Login
54
+ #
55
+ # # good
56
+ # Login
57
+ #
58
+ class ConstantResolution < Cop
59
+ MSG = 'Fully qualify this constant to avoid possibly ambiguous resolution.'
60
+
61
+ def_node_matcher :unqualified_const?, <<~PATTERN
62
+ (const nil? #const_name?)
63
+ PATTERN
64
+
65
+ def on_const(node)
66
+ return unless unqualified_const?(node)
67
+
68
+ add_offense(node)
69
+ end
70
+
71
+ private
72
+
73
+ def const_name?(name)
74
+ name = name.to_s
75
+ (allowed_names.empty? || allowed_names.include?(name)) &&
76
+ !ignored_names.include?(name)
77
+ end
78
+
79
+ def allowed_names
80
+ cop_config['Only']
81
+ end
82
+
83
+ def ignored_names
84
+ cop_config['Ignore']
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end