rubocop 0.65.0 → 0.66.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 +3 -3
- data/config/default.yml +53 -4
- data/lib/rubocop.rb +4 -5
- data/lib/rubocop/cli.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -5
- data/lib/rubocop/cop/layout/class_structure.rb +59 -28
- data/lib/rubocop/cop/layout/extra_spacing.rb +18 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +25 -5
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +33 -17
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +30 -11
- data/lib/rubocop/cop/lint/else_layout.rb +1 -0
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +38 -0
- data/lib/rubocop/cop/lint/shadowed_exception.rb +14 -1
- data/lib/rubocop/cop/lint/to_json.rb +38 -0
- data/lib/rubocop/cop/lint/void.rb +1 -1
- data/lib/rubocop/cop/message_annotator.rb +4 -4
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +31 -9
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/integer_node.rb +1 -1
- data/lib/rubocop/cop/mixin/method_preference.rb +2 -1
- data/lib/rubocop/cop/naming/constant_name.rb +6 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +6 -0
- data/lib/rubocop/cop/rails/link_to_blank.rb +1 -1
- data/lib/rubocop/cop/rails/output.rb +18 -1
- data/lib/rubocop/cop/rails/reflection_class_name.rb +1 -1
- data/lib/rubocop/cop/rails/time_zone.rb +10 -0
- data/lib/rubocop/cop/rails/validation.rb +3 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +30 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +3 -1
- data/lib/rubocop/cop/style/constant_visibility.rb +66 -0
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +8 -12
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +16 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +16 -7
- data/lib/rubocop/cop/style/option_hash.rb +5 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +13 -1
- data/lib/rubocop/cop/style/redundant_self.rb +3 -1
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +9 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +5 -0
- data/lib/rubocop/cop/style/word_array.rb +0 -4
- data/lib/rubocop/cop/util.rb +4 -0
- data/lib/rubocop/core_ext/string.rb +47 -0
- data/lib/rubocop/node_pattern.rb +76 -55
- data/lib/rubocop/processed_source.rb +2 -2
- data/lib/rubocop/result_cache.rb +4 -4
- data/lib/rubocop/rspec/cop_helper.rb +5 -0
- data/lib/rubocop/rspec/expect_offense.rb +5 -5
- data/lib/rubocop/runner.rb +6 -13
- data/lib/rubocop/version.rb +1 -1
- metadata +15 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7917fb6bc9ad3c4e8e80d2931b7c275f7485ece4bcd0d18be214949e2facb5c6
|
4
|
+
data.tar.gz: e243620f2c8903c4a71eb755397cb6f0b7094c47202df5bc6310f911ec161557
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 398cf3d8e470cdece6665bd91f9960639acc9196989a80bf6189cea39bdc4f3345a3ff11c5073646d9d4c80369d5ae7e5cc132f01e7855940b546b62ea143352
|
7
|
+
data.tar.gz: bdc1aabc612fb36230023a06d8cb507a95539ecdc42386ac5f608aff02bb0d8a918f1b39412322905fab1bcb58f107296f4a84b2a34c80d64edf302ba8976d06
|
data/README.md
CHANGED
@@ -31,7 +31,7 @@ automatically fix some of the problems for you.
|
|
31
31
|
|
32
32
|
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bbatsov/rubocop?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
33
33
|
|
34
|
-
**Please consider [supporting
|
34
|
+
**Please consider [financially supporting its ongoing development](#funding).**
|
35
35
|
|
36
36
|
## Installation
|
37
37
|
|
@@ -50,10 +50,10 @@ gem 'rubocop', require: false
|
|
50
50
|
RuboCop's development is moving at a very rapid pace and there are
|
51
51
|
often backward-incompatible changes between minor releases (since we
|
52
52
|
haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
|
53
|
-
might want to use a conservative version
|
53
|
+
might want to use a conservative version lock in your `Gemfile`:
|
54
54
|
|
55
55
|
```rb
|
56
|
-
gem 'rubocop', '~> 0.
|
56
|
+
gem 'rubocop', '~> 0.66.0', require: false
|
57
57
|
```
|
58
58
|
|
59
59
|
## Quickstart
|
data/config/default.yml
CHANGED
@@ -581,6 +581,10 @@ Layout/ExtraSpacing:
|
|
581
581
|
# things with the previous or next line, not counting empty lines or comment
|
582
582
|
# lines.
|
583
583
|
AllowForAlignment: true
|
584
|
+
# When true, allows things like 'obj.meth(arg) # comment',
|
585
|
+
# rather than insisting on 'obj.meth(arg) # comment'.
|
586
|
+
# If done for alignment, either this OR AllowForAlignment will allow it.
|
587
|
+
AllowBeforeTrailingComments: false
|
584
588
|
# When true, forces the alignment of `=` in assignments on consecutive lines.
|
585
589
|
ForceEqualSignAlignment: false
|
586
590
|
|
@@ -1479,6 +1483,11 @@ Lint/SafeNavigationConsistency:
|
|
1479
1483
|
- try!
|
1480
1484
|
|
1481
1485
|
|
1486
|
+
Lint/SafeNavigationWithEmpty:
|
1487
|
+
Description: 'Avoid `foo&.empty?` in conditionals.'
|
1488
|
+
Enabled: true
|
1489
|
+
VersionAdded: '0.62'
|
1490
|
+
|
1482
1491
|
Lint/ScriptPermission:
|
1483
1492
|
Description: 'Grant script file execute permission.'
|
1484
1493
|
Enabled: true
|
@@ -1519,6 +1528,10 @@ Lint/Syntax:
|
|
1519
1528
|
VersionAdded: '0.9'
|
1520
1529
|
|
1521
1530
|
|
1531
|
+
Lint/ToJSON:
|
1532
|
+
Description: 'Ensure #to_json includes an optional argument.'
|
1533
|
+
Enabled: true
|
1534
|
+
|
1522
1535
|
Lint/UnderscorePrefixedVariableName:
|
1523
1536
|
Description: 'Do not use prefix `_` for a variable that is used.'
|
1524
1537
|
Enabled: true
|
@@ -1632,9 +1645,12 @@ Metrics/AbcSize:
|
|
1632
1645
|
Description: >-
|
1633
1646
|
A calculated magnitude based on number of assignments,
|
1634
1647
|
branches, and conditions.
|
1635
|
-
Reference:
|
1648
|
+
Reference:
|
1649
|
+
- http://c2.com/cgi/wiki?AbcMetric
|
1650
|
+
- https://en.wikipedia.org/wiki/ABC_Software_Metric'
|
1636
1651
|
Enabled: true
|
1637
1652
|
VersionAdded: '0.27'
|
1653
|
+
VersionChanged: '0.66'
|
1638
1654
|
# The ABC size is a calculated magnitude, so this number can be an Integer or
|
1639
1655
|
# a Float.
|
1640
1656
|
Max: 15
|
@@ -1643,13 +1659,15 @@ Metrics/BlockLength:
|
|
1643
1659
|
Description: 'Avoid long blocks with many lines.'
|
1644
1660
|
Enabled: true
|
1645
1661
|
VersionAdded: '0.44'
|
1646
|
-
VersionChanged: '0.
|
1662
|
+
VersionChanged: '0.66'
|
1647
1663
|
CountComments: false # count full line comments?
|
1648
1664
|
Max: 25
|
1649
1665
|
ExcludedMethods:
|
1650
1666
|
# By default, exclude the `#refine` method, as it tends to have larger
|
1651
1667
|
# associated blocks.
|
1652
1668
|
- refine
|
1669
|
+
Exclude:
|
1670
|
+
- '**/*.gemspec'
|
1653
1671
|
|
1654
1672
|
Metrics/BlockNesting:
|
1655
1673
|
Description: 'Avoid excessive block nesting'
|
@@ -2764,8 +2782,9 @@ Style/BlockDelimiters:
|
|
2764
2782
|
- line_count_based
|
2765
2783
|
# The `semantic` style enforces braces around functional blocks, where the
|
2766
2784
|
# primary purpose of the block is to return a value and do..end for
|
2767
|
-
# procedural blocks, where the primary purpose of the block is
|
2768
|
-
# side-effects.
|
2785
|
+
# multi-line procedural blocks, where the primary purpose of the block is
|
2786
|
+
# its side-effects. Single-line procedural blocks may only use do-end,
|
2787
|
+
# unless AllowBracesOnProceduralOneLiners has a truthy value (see below).
|
2769
2788
|
#
|
2770
2789
|
# This looks at the usage of a block's method to determine its type (e.g. is
|
2771
2790
|
# the result of a `map` assigned to a variable or passed to another
|
@@ -2826,6 +2845,28 @@ Style/BlockDelimiters:
|
|
2826
2845
|
- lambda
|
2827
2846
|
- proc
|
2828
2847
|
- it
|
2848
|
+
# The AllowBracesOnProceduralOneLiners option is ignored unless the
|
2849
|
+
# EnforcedStyle is set to `semantic`. If so:
|
2850
|
+
#
|
2851
|
+
# If AllowBracesOnProceduralOneLiners is unspecified, or set to any
|
2852
|
+
# falsey value, then semantic purity is maintained, so one-line
|
2853
|
+
# procedural blocks must use do-end, not braces.
|
2854
|
+
#
|
2855
|
+
# # bad
|
2856
|
+
# collection.each { |element| puts element }
|
2857
|
+
#
|
2858
|
+
# # good
|
2859
|
+
# collection.each do |element| puts element end
|
2860
|
+
#
|
2861
|
+
# If AllowBracesOnProceduralOneLiners is set to any truthy value,
|
2862
|
+
# then one-line procedural blocks may use either style.
|
2863
|
+
#
|
2864
|
+
# # good
|
2865
|
+
# collection.each { |element| puts element }
|
2866
|
+
#
|
2867
|
+
# # also good
|
2868
|
+
# collection.each do |element| puts element end
|
2869
|
+
AllowBracesOnProceduralOneLiners: false
|
2829
2870
|
|
2830
2871
|
Style/BracesAroundHashParameters:
|
2831
2872
|
Description: 'Enforce braces style around hash parameters.'
|
@@ -3020,6 +3061,13 @@ Style/ConditionalAssignment:
|
|
3020
3061
|
# Notice: 'Copyright (\(c\) )?2015 Yahoo! Inc'
|
3021
3062
|
# AutocorrectNotice: '# Copyright (c) 2015 Yahoo! Inc.'
|
3022
3063
|
#
|
3064
|
+
Style/ConstantVisibility:
|
3065
|
+
Description: >-
|
3066
|
+
Check that class- and module constants have
|
3067
|
+
visibility declarations.
|
3068
|
+
Enabled: false
|
3069
|
+
VersionAdded: '0.66'
|
3070
|
+
|
3023
3071
|
Style/Copyright:
|
3024
3072
|
Description: 'Include a copyright notice in each file before any code.'
|
3025
3073
|
Enabled: false
|
@@ -3867,6 +3915,7 @@ Style/RedundantFreeze:
|
|
3867
3915
|
Description: "Checks usages of Object#freeze on immutable objects."
|
3868
3916
|
Enabled: true
|
3869
3917
|
VersionAdded: '0.34'
|
3918
|
+
VersionChanged: '0.66'
|
3870
3919
|
|
3871
3920
|
Style/RedundantParentheses:
|
3872
3921
|
Description: "Checks for parentheses that seem not to serve any purpose."
|
data/lib/rubocop.rb
CHANGED
@@ -6,15 +6,11 @@ require 'rainbow'
|
|
6
6
|
require 'English'
|
7
7
|
require 'set'
|
8
8
|
require 'forwardable'
|
9
|
-
require 'powerpack/array/butfirst'
|
10
|
-
require 'powerpack/enumerable/drop_last'
|
11
|
-
require 'powerpack/hash/symbolize_keys'
|
12
|
-
require 'powerpack/string/blank'
|
13
|
-
require 'powerpack/string/strip_indent'
|
14
9
|
require 'unicode/display_width/no_string_ext'
|
15
10
|
|
16
11
|
require_relative 'rubocop/version'
|
17
12
|
|
13
|
+
require_relative 'rubocop/core_ext/string'
|
18
14
|
require_relative 'rubocop/path_util'
|
19
15
|
require_relative 'rubocop/file_finder'
|
20
16
|
require_relative 'rubocop/platform'
|
@@ -313,12 +309,14 @@ require_relative 'rubocop/cop/lint/rescue_type'
|
|
313
309
|
require_relative 'rubocop/cop/lint/return_in_void_context'
|
314
310
|
require_relative 'rubocop/cop/lint/safe_navigation_consistency'
|
315
311
|
require_relative 'rubocop/cop/lint/safe_navigation_chain'
|
312
|
+
require_relative 'rubocop/cop/lint/safe_navigation_with_empty'
|
316
313
|
require_relative 'rubocop/cop/lint/script_permission'
|
317
314
|
require_relative 'rubocop/cop/lint/shadowed_argument'
|
318
315
|
require_relative 'rubocop/cop/lint/shadowed_exception'
|
319
316
|
require_relative 'rubocop/cop/lint/shadowing_outer_local_variable'
|
320
317
|
require_relative 'rubocop/cop/lint/string_conversion_in_interpolation'
|
321
318
|
require_relative 'rubocop/cop/lint/syntax'
|
319
|
+
require_relative 'rubocop/cop/lint/to_json'
|
322
320
|
require_relative 'rubocop/cop/lint/underscore_prefixed_variable_name'
|
323
321
|
require_relative 'rubocop/cop/lint/unified_integer'
|
324
322
|
require_relative 'rubocop/cop/lint/unneeded_cop_disable_directive'
|
@@ -421,6 +419,7 @@ require_relative 'rubocop/cop/style/command_literal'
|
|
421
419
|
require_relative 'rubocop/cop/style/comment_annotation'
|
422
420
|
require_relative 'rubocop/cop/style/commented_keyword'
|
423
421
|
require_relative 'rubocop/cop/style/conditional_assignment'
|
422
|
+
require_relative 'rubocop/cop/style/constant_visibility'
|
424
423
|
require_relative 'rubocop/cop/style/copyright'
|
425
424
|
require_relative 'rubocop/cop/style/date_time'
|
426
425
|
require_relative 'rubocop/cop/style/def_with_parentheses'
|
data/lib/rubocop/cli.rb
CHANGED
@@ -263,7 +263,7 @@ module RuboCop
|
|
263
263
|
|
264
264
|
def config_lines(cop)
|
265
265
|
cnf = @config_store.for(Dir.pwd).for_cop(cop)
|
266
|
-
cnf.to_yaml.lines.to_a.
|
266
|
+
cnf.to_yaml.lines.to_a.drop(1).map { |line| ' ' + line }
|
267
267
|
end
|
268
268
|
|
269
269
|
def display_warning_summary(warnings)
|
data/lib/rubocop/config.rb
CHANGED
@@ -31,7 +31,7 @@ module RuboCop
|
|
31
31
|
attr_reader :block_node, :method, :arguments
|
32
32
|
|
33
33
|
def remove_unparenthesized_whitespace(corrector)
|
34
|
-
return
|
34
|
+
return if arguments.empty? || arguments.parenthesized_call?
|
35
35
|
|
36
36
|
remove_leading_whitespace(corrector)
|
37
37
|
remove_trailing_whitespace(corrector)
|
@@ -69,10 +69,8 @@ module RuboCop
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def remove_trailing_whitespace(corrector)
|
72
|
-
|
73
|
-
|
74
|
-
block_begin.begin_pos - arguments.source_range.end_pos - 1
|
75
|
-
)
|
72
|
+
size = block_begin.begin_pos - arguments.source_range.end_pos - 1
|
73
|
+
corrector.remove_preceding(block_begin, size) if size > 0
|
76
74
|
end
|
77
75
|
|
78
76
|
def replace_delimiters(corrector)
|
@@ -8,35 +8,43 @@ module RuboCop
|
|
8
8
|
# `Categories` allows us to map macro names into a category.
|
9
9
|
#
|
10
10
|
# Consider an example of code style that covers the following order:
|
11
|
+
# - Module inclusion (include, prepend, extend)
|
11
12
|
# - Constants
|
12
13
|
# - Associations (has_one, has_many)
|
13
|
-
# -
|
14
|
+
# - Public attribute macros (attr_accessor, attr_writer, attr_reader)
|
15
|
+
# - Other macros (validates, validate)
|
16
|
+
# - Public class methods
|
14
17
|
# - Initializer
|
15
|
-
# -
|
16
|
-
# - Protected
|
17
|
-
# -
|
18
|
+
# - Public instance methods
|
19
|
+
# - Protected attribute macros (attr_accessor, attr_writer, attr_reader)
|
20
|
+
# - Protected instance methods
|
21
|
+
# - Private attribute macros (attr_accessor, attr_writer, attr_reader)
|
22
|
+
# - Private instance methods
|
18
23
|
#
|
19
24
|
# You can configure the following order:
|
20
25
|
#
|
21
26
|
# ```yaml
|
22
27
|
# Layout/ClassStructure:
|
23
|
-
# Categories:
|
24
|
-
# module_inclusion:
|
25
|
-
# - include
|
26
|
-
# - prepend
|
27
|
-
# - extend
|
28
28
|
# ExpectedOrder:
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
29
|
+
# - module_inclusion
|
30
|
+
# - constants
|
31
|
+
# - association
|
32
|
+
# - public_attribute_macros
|
33
|
+
# - public_delegate
|
34
|
+
# - macros
|
35
|
+
# - public_class_methods
|
36
|
+
# - initializer
|
37
|
+
# - public_methods
|
38
|
+
# - protected_attribute_macros
|
39
|
+
# - protected_methods
|
40
|
+
# - private_attribute_macros
|
41
|
+
# - private_delegate
|
42
|
+
# - private_methods
|
37
43
|
# ```
|
44
|
+
#
|
38
45
|
# Instead of putting all literals in the expected order, is also
|
39
|
-
# possible to group categories of macros.
|
46
|
+
# possible to group categories of macros. Visibility levels are handled
|
47
|
+
# automatically.
|
40
48
|
#
|
41
49
|
# ```yaml
|
42
50
|
# Layout/ClassStructure:
|
@@ -44,10 +52,17 @@ module RuboCop
|
|
44
52
|
# association:
|
45
53
|
# - has_many
|
46
54
|
# - has_one
|
47
|
-
#
|
55
|
+
# attribute_macros:
|
48
56
|
# - attr_accessor
|
49
57
|
# - attr_reader
|
50
58
|
# - attr_writer
|
59
|
+
# macros:
|
60
|
+
# - validates
|
61
|
+
# - validate
|
62
|
+
# module_inclusion:
|
63
|
+
# - include
|
64
|
+
# - prepend
|
65
|
+
# - extend
|
51
66
|
# ```
|
52
67
|
#
|
53
68
|
# @example
|
@@ -73,12 +88,15 @@ module RuboCop
|
|
73
88
|
# # constants are next
|
74
89
|
# SOME_CONSTANT = 20
|
75
90
|
#
|
76
|
-
# # afterwards we have attribute macros
|
91
|
+
# # afterwards we have public attribute macros
|
77
92
|
# attr_reader :name
|
78
93
|
#
|
79
94
|
# # followed by other macros (if any)
|
80
95
|
# validates :name
|
81
96
|
#
|
97
|
+
# # then we have public delegate macros
|
98
|
+
# delegate :to_s, to: :name
|
99
|
+
#
|
82
100
|
# # public class methods are next in line
|
83
101
|
# def self.some_method
|
84
102
|
# end
|
@@ -91,14 +109,22 @@ module RuboCop
|
|
91
109
|
# def some_method
|
92
110
|
# end
|
93
111
|
#
|
94
|
-
# # protected and
|
112
|
+
# # protected attribute macros and methods go next
|
95
113
|
# protected
|
96
114
|
#
|
115
|
+
# attr_reader :protected_name
|
116
|
+
#
|
97
117
|
# def some_protected_method
|
98
118
|
# end
|
99
119
|
#
|
120
|
+
# # private attribute macros, delegate macros and methods
|
121
|
+
# # are grouped near the end
|
100
122
|
# private
|
101
123
|
#
|
124
|
+
# attr_reader :private_name
|
125
|
+
#
|
126
|
+
# delegate :some_private_delegate, to: :name
|
127
|
+
#
|
102
128
|
# def some_private_method
|
103
129
|
# end
|
104
130
|
# end
|
@@ -108,7 +134,8 @@ module RuboCop
|
|
108
134
|
HUMANIZED_NODE_TYPE = {
|
109
135
|
casgn: :constants,
|
110
136
|
defs: :class_methods,
|
111
|
-
def: :public_methods
|
137
|
+
def: :public_methods,
|
138
|
+
sclass: :class_singleton
|
112
139
|
}.freeze
|
113
140
|
|
114
141
|
VISIBILITY_SCOPES = %i[private protected public].freeze
|
@@ -167,19 +194,23 @@ module RuboCop
|
|
167
194
|
when :block
|
168
195
|
classify(node.send_node)
|
169
196
|
when :send
|
170
|
-
find_category(node
|
197
|
+
find_category(node)
|
171
198
|
else
|
172
199
|
humanize_node(node)
|
173
200
|
end.to_s
|
174
201
|
end
|
175
202
|
|
176
|
-
# Categorize a
|
177
|
-
#
|
203
|
+
# Categorize a node according to the {expected_order}
|
204
|
+
# Try to match {categories} values against the node's method_name given
|
205
|
+
# also its visibility.
|
206
|
+
# @param node to be analysed.
|
178
207
|
# @return [String] with the key category or the `method_name` as string
|
179
|
-
def find_category(
|
180
|
-
name = method_name.to_s
|
208
|
+
def find_category(node)
|
209
|
+
name = node.method_name.to_s
|
181
210
|
category, = categories.find { |_, names| names.include?(name) }
|
182
|
-
category || name
|
211
|
+
key = category || name
|
212
|
+
visibility_key = "#{node_visibility(node)}_#{key}"
|
213
|
+
expected_order.include?(visibility_key) ? visibility_key : key
|
183
214
|
end
|
184
215
|
|
185
216
|
def walk_over_nested_class_definition(class_node)
|
@@ -19,6 +19,17 @@ module RuboCop
|
|
19
19
|
# # bad for any configuration
|
20
20
|
# set_app("RuboCop")
|
21
21
|
# website = "https://github.com/rubocop-hq/rubocop"
|
22
|
+
#
|
23
|
+
# # good only if AllowBeforeTrailingComments is true
|
24
|
+
# object.method(arg) # this is a comment
|
25
|
+
#
|
26
|
+
# # good even if AllowBeforeTrailingComments is false or not set
|
27
|
+
# object.method(arg) # this is a comment
|
28
|
+
#
|
29
|
+
# # good with either AllowBeforeTrailingComments or AllowForAlignment
|
30
|
+
# object.method(arg) # this is a comment
|
31
|
+
# another_object.method(arg) # this is another comment
|
32
|
+
# some_object.method(arg) # this is some comment
|
22
33
|
class ExtraSpacing < Cop
|
23
34
|
include PrecedingFollowingAlignment
|
24
35
|
include RangeHelp
|
@@ -98,6 +109,9 @@ module RuboCop
|
|
98
109
|
end
|
99
110
|
|
100
111
|
def check_other(token1, token2, ast)
|
112
|
+
return false if allow_for_trailing_comments? &&
|
113
|
+
token2.text.start_with?('#')
|
114
|
+
|
101
115
|
extra_space_range(token1, token2) do |range|
|
102
116
|
# Unary + doesn't appear as a token and needs special handling.
|
103
117
|
next if ignored_range?(ast, range.begin_pos)
|
@@ -222,6 +236,10 @@ module RuboCop
|
|
222
236
|
optarg_eql = optargs.map { |o| o.loc.operator.begin_pos }.to_set
|
223
237
|
asgn_tokens.reject { |t| optarg_eql.include?(t.begin_pos) }
|
224
238
|
end
|
239
|
+
|
240
|
+
def allow_for_trailing_comments?
|
241
|
+
cop_config['AllowBeforeTrailingComments']
|
242
|
+
end
|
225
243
|
end
|
226
244
|
end
|
227
245
|
end
|
@@ -54,6 +54,10 @@ module RuboCop
|
|
54
54
|
|
55
55
|
SPECIAL_MODIFIERS = %w[private protected].freeze
|
56
56
|
|
57
|
+
def_node_matcher :access_modifier?, <<-PATTERN
|
58
|
+
[(send ...) access_modifier?]
|
59
|
+
PATTERN
|
60
|
+
|
57
61
|
def on_rescue(node)
|
58
62
|
_begin_node, *_rescue_nodes, else_node = *node
|
59
63
|
check_indentation(node.loc.else, else_node)
|
@@ -161,15 +165,12 @@ module RuboCop
|
|
161
165
|
private
|
162
166
|
|
163
167
|
def check_members(base, members)
|
164
|
-
check_indentation(base, members.first)
|
168
|
+
check_indentation(base, select_check_member(members.first))
|
165
169
|
|
166
170
|
return unless members.any? && members.first.begin_type?
|
167
171
|
|
168
172
|
if indentation_consistency_style == 'rails'
|
169
|
-
|
170
|
-
check_indentation(previous_modifier, member,
|
171
|
-
indentation_consistency_style)
|
172
|
-
end
|
173
|
+
check_members_for_rails_style(members)
|
173
174
|
else
|
174
175
|
members.first.children.each do |member|
|
175
176
|
next if member.send_type? && member.access_modifier?
|
@@ -179,6 +180,23 @@ module RuboCop
|
|
179
180
|
end
|
180
181
|
end
|
181
182
|
|
183
|
+
def select_check_member(member)
|
184
|
+
return unless member
|
185
|
+
|
186
|
+
if access_modifier?(member.children.first)
|
187
|
+
member.children.first
|
188
|
+
else
|
189
|
+
member
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def check_members_for_rails_style(members)
|
194
|
+
each_member(members) do |member, previous_modifier|
|
195
|
+
check_indentation(previous_modifier, member,
|
196
|
+
indentation_consistency_style)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
182
200
|
def each_member(members)
|
183
201
|
previous_modifier = nil
|
184
202
|
members.first.children.each do |member|
|
@@ -326,6 +344,8 @@ module RuboCop
|
|
326
344
|
return unless body_node.begin_type?
|
327
345
|
|
328
346
|
starting_node = body_node.children.first
|
347
|
+
return unless starting_node
|
348
|
+
|
329
349
|
starting_node.send_type? && starting_node.bare_access_modifier?
|
330
350
|
end
|
331
351
|
|