rubocop 0.85.1 → 0.86.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -4
- data/config/default.yml +29 -1
- data/lib/rubocop.rb +3 -0
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +6 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -1
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -2
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
- data/lib/rubocop/cop/style/copyright.rb +3 -3
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +103 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
- data/lib/rubocop/cop/style/redundant_self.rb +6 -9
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/utils/format_string.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/name_similarity.rb +6 -0
- data/lib/rubocop/path_util.rb +2 -2
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +12 -2
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99c7f4b160b77fafa6fb31562b3fbd206a50b4b5201c918250b1a1d94105fb14
|
4
|
+
data.tar.gz: d709c3614f3aaf536c0acefe88effe53ff51c148e37cc57740890d4ec4abbb50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
10
|
-
[![
|
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.
|
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
|
|
data/config/default.yml
CHANGED
@@ -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:
|
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.'
|
data/lib/rubocop.rb
CHANGED
@@ -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://
|
30
|
+
# See https://docs.rubocop.org/rubocop/configuration
|
31
31
|
DESC
|
32
32
|
|
33
33
|
File.open(DOTFILE, 'w') do |f|
|
data/lib/rubocop/config.rb
CHANGED
@@ -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
|
-
|
155
|
+
/[A-Z]/.match?(pattern.to_s) &&
|
156
156
|
(match_path?(pattern, relative_path) ||
|
157
157
|
match_path?(pattern, absolute_path))
|
158
158
|
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
|
-
|
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
|
-
|
132
|
+
/^\s*(end\b|[)}\]])/.match?(line)
|
133
133
|
end
|
134
134
|
|
135
135
|
def two_alternatives?(line)
|
136
|
-
|
136
|
+
/^\s*(else|elsif|when|rescue|ensure)\b/.match?(line)
|
137
137
|
end
|
138
138
|
end
|
139
139
|
end
|
@@ -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
|
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
|
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)'
|
@@ -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
|
@@ -186,7 +186,7 @@ module RuboCop
|
|
186
186
|
pos = range.begin_pos - 1
|
187
187
|
return false if pos.negative?
|
188
188
|
|
189
|
-
|
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
|
-
|
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
|
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
|
@@ -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 &&
|
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
|
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]
|
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
|