rubocop 0.75.0 → 0.79.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 +2 -2
- data/config/default.yml +341 -316
- data/lib/rubocop.rb +48 -31
- data/lib/rubocop/ast/builder.rb +43 -41
- data/lib/rubocop/ast/node.rb +5 -1
- data/lib/rubocop/ast/node/block_node.rb +2 -0
- data/lib/rubocop/ast/node/def_node.rb +11 -0
- data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
- data/lib/rubocop/ast/node/return_node.rb +24 -0
- data/lib/rubocop/ast/traversal.rb +11 -3
- data/lib/rubocop/cli.rb +11 -227
- data/lib/rubocop/cli/command.rb +21 -0
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
- data/lib/rubocop/cli/command/base.rb +33 -0
- data/lib/rubocop/cli/command/execute_runner.rb +76 -0
- data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
- data/lib/rubocop/cli/command/show_cops.rb +80 -0
- data/lib/rubocop/cli/command/version.rb +17 -0
- data/lib/rubocop/cli/environment.rb +21 -0
- data/lib/rubocop/comment_config.rb +2 -2
- data/lib/rubocop/config.rb +8 -1
- data/lib/rubocop/config_loader.rb +20 -20
- data/lib/rubocop/config_loader_resolver.rb +2 -1
- data/lib/rubocop/config_obsoletion.rb +73 -11
- data/lib/rubocop/config_validator.rb +77 -110
- data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
- data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
- data/lib/rubocop/cop/commissioner.rb +15 -7
- data/lib/rubocop/cop/cop.rb +31 -6
- data/lib/rubocop/cop/corrector.rb +8 -7
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/generator.rb +3 -4
- data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
- data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +3 -2
- data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
- data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
- data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
- data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
- data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
- data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
- data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
- data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
- data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
- data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
- data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
- data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
- data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
- data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
- data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +10 -12
- data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
- data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
- data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
- data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
- data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
- data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
- data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +7 -26
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/method_length.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
- data/lib/rubocop/cop/migration/department_name.rb +16 -1
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
- data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
- data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
- data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
- data/lib/rubocop/cop/mixin/trailing_comma.rb +14 -9
- data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +12 -5
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
- data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
- data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
- data/lib/rubocop/cop/offense.rb +11 -0
- data/lib/rubocop/cop/registry.rb +8 -3
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +10 -2
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
- data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/copyright.rb +11 -7
- data/lib/rubocop/cop/style/documentation_method.rb +44 -0
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +2 -2
- data/lib/rubocop/cop/style/empty_method.rb +5 -5
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +10 -7
- data/lib/rubocop/cop/style/format_string_token.rb +15 -34
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
- data/lib/rubocop/cop/style/guard_clause.rb +3 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
- data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
- data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
- data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
- data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +24 -227
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
- data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
- data/lib/rubocop/cop/style/option_hash.rb +3 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
- data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
- data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
- data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
- data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +39 -29
- data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
- data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
- data/lib/rubocop/cop/style/semicolon.rb +13 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
- data/lib/rubocop/cop/team.rb +5 -0
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +10 -18
- data/lib/rubocop/cop/variable_force.rb +7 -5
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
- data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
- data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +16 -16
- data/lib/rubocop/formatter/json_formatter.rb +6 -5
- data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
- data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
- data/lib/rubocop/formatter/tap_formatter.rb +9 -6
- data/lib/rubocop/node_pattern.rb +4 -2
- data/lib/rubocop/options.rb +20 -26
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/rake_task.rb +1 -0
- data/lib/rubocop/result_cache.rb +24 -8
- data/lib/rubocop/rspec/shared_contexts.rb +5 -0
- data/lib/rubocop/runner.rb +50 -29
- data/lib/rubocop/target_finder.rb +12 -6
- data/lib/rubocop/target_ruby.rb +151 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +50 -34
- data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -33,13 +33,7 @@ module RuboCop
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def autocorrect(token)
|
36
|
-
|
37
|
-
|
38
|
-
regex = Regexp.new(notice)
|
39
|
-
unless autocorrect_notice =~ regex
|
40
|
-
raise Warning, "AutocorrectNotice '#{autocorrect_notice}' must " \
|
41
|
-
"match Notice /#{notice}/"
|
42
|
-
end
|
36
|
+
verify_autocorrect_notice!
|
43
37
|
|
44
38
|
lambda do |corrector|
|
45
39
|
range = token.nil? ? range_between(0, 0) : token.pos
|
@@ -57,6 +51,16 @@ module RuboCop
|
|
57
51
|
cop_config['AutocorrectNotice']
|
58
52
|
end
|
59
53
|
|
54
|
+
def verify_autocorrect_notice!
|
55
|
+
raise Warning, AUTOCORRECT_EMPTY_WARNING if autocorrect_notice.empty?
|
56
|
+
|
57
|
+
regex = Regexp.new(notice)
|
58
|
+
return if autocorrect_notice =~ regex
|
59
|
+
|
60
|
+
raise Warning, "AutocorrectNotice '#{autocorrect_notice}' must " \
|
61
|
+
"match Notice /#{notice}/"
|
62
|
+
end
|
63
|
+
|
60
64
|
def insert_notice_before(processed_source)
|
61
65
|
token_index = 0
|
62
66
|
token_index += 1 if shebang_token?(processed_source, token_index)
|
@@ -47,6 +47,50 @@ module RuboCop
|
|
47
47
|
# def foo.bar
|
48
48
|
# puts baz
|
49
49
|
# end
|
50
|
+
#
|
51
|
+
# @example RequireForNonPublicMethods: false (default)
|
52
|
+
# # good
|
53
|
+
# class Foo
|
54
|
+
# protected
|
55
|
+
# def do_something
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# class Foo
|
60
|
+
# private
|
61
|
+
# def do_something
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# @example RequireForNonPublicMethods: true
|
66
|
+
# # bad
|
67
|
+
# class Foo
|
68
|
+
# protected
|
69
|
+
# def do_something
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# class Foo
|
74
|
+
# private
|
75
|
+
# def do_something
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# # good
|
80
|
+
# class Foo
|
81
|
+
# protected
|
82
|
+
# # Documentation
|
83
|
+
# def do_something
|
84
|
+
# end
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# class Foo
|
88
|
+
# private
|
89
|
+
# # Documentation
|
90
|
+
# def do_something
|
91
|
+
# end
|
92
|
+
# end
|
93
|
+
#
|
50
94
|
class DocumentationMethod < Cop
|
51
95
|
include DocumentationComment
|
52
96
|
include DefNode
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# rubocop:disable Lint/
|
3
|
+
# rubocop:disable Lint/RedundantCopDisableDirective
|
4
4
|
# rubocop:disable Style/DoubleCopDisableDirective
|
5
5
|
|
6
6
|
module RuboCop
|
@@ -26,7 +26,7 @@ module RuboCop
|
|
26
26
|
#
|
27
27
|
class DoubleCopDisableDirective < Cop
|
28
28
|
# rubocop:enable Style/For, Style/DoubleCopDisableDirective
|
29
|
-
# rubocop:enable Lint/
|
29
|
+
# rubocop:enable Lint/RedundantCopDisableDirective, Metrics/AbcSize
|
30
30
|
MSG = 'More than one disable comment on one line.'
|
31
31
|
|
32
32
|
def investigate(processed_source)
|
@@ -84,8 +84,8 @@ module RuboCop
|
|
84
84
|
|
85
85
|
next unless conditions.size > 1
|
86
86
|
|
87
|
-
range = range_between(conditions.first.
|
88
|
-
conditions.last.
|
87
|
+
range = range_between(conditions.first.source_range.begin_pos,
|
88
|
+
conditions.last.source_range.end_pos)
|
89
89
|
|
90
90
|
corrector.replace(range, conditions.map(&:source).join(' || '))
|
91
91
|
end
|
@@ -84,8 +84,8 @@ module RuboCop
|
|
84
84
|
# to rewrite the arguments to wrap them in parenthesis.
|
85
85
|
args = node.parent.arguments
|
86
86
|
|
87
|
-
range_between(args[0].
|
88
|
-
args[-1].
|
87
|
+
range_between(args[0].source_range.begin_pos - 1,
|
88
|
+
args[-1].source_range.end_pos)
|
89
89
|
else
|
90
90
|
node.source_range
|
91
91
|
end
|
@@ -73,11 +73,11 @@ module RuboCop
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def corrected(node)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
scope
|
76
|
+
if node.arguments?
|
77
|
+
arguments = node.arguments.source
|
78
|
+
extra_space = ' ' unless parentheses?(node.arguments)
|
79
|
+
end
|
80
|
+
scope = node.receiver ? "#{node.receiver.source}." : ''
|
81
81
|
|
82
82
|
signature = [scope, node.method_name, extra_space, arguments].join
|
83
83
|
|
@@ -85,7 +85,7 @@ module RuboCop
|
|
85
85
|
# FIXME: It's a Style/ConditionalAssignment's false positive.
|
86
86
|
# rubocop:disable Style/ConditionalAssignment
|
87
87
|
def with_lineno?(node)
|
88
|
-
if node.
|
88
|
+
if node.method?(:eval)
|
89
89
|
node.arguments.size == 4
|
90
90
|
else
|
91
91
|
node.arguments.size == 3
|
@@ -94,10 +94,12 @@ module RuboCop
|
|
94
94
|
private
|
95
95
|
|
96
96
|
def autocorrect_from_percent(corrector, node)
|
97
|
-
|
98
|
-
|
97
|
+
percent_rhs = node.first_argument
|
98
|
+
args = case percent_rhs.type
|
99
|
+
when :array, :hash
|
100
|
+
percent_rhs.children.map(&:source).join(', ')
|
99
101
|
else
|
100
|
-
|
102
|
+
percent_rhs.source
|
101
103
|
end
|
102
104
|
|
103
105
|
corrected = "#{style}(#{node.receiver.source}, #{args})"
|
@@ -106,14 +108,15 @@ module RuboCop
|
|
106
108
|
end
|
107
109
|
|
108
110
|
def autocorrect_to_percent(corrector, node)
|
109
|
-
|
111
|
+
format_arg, *param_args = node.arguments
|
112
|
+
format = format_arg.source
|
110
113
|
|
111
|
-
args = if
|
112
|
-
arg =
|
114
|
+
args = if param_args.one?
|
115
|
+
arg = param_args.last
|
113
116
|
|
114
117
|
arg.hash_type? ? "{ #{arg.source} }" : arg.source
|
115
118
|
else
|
116
|
-
"[#{
|
119
|
+
"[#{param_args.map(&:source).join(', ')}]"
|
117
120
|
end
|
118
121
|
|
119
122
|
corrector.replace(node.loc.expression, "#{format} % #{args}")
|
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
# **Note:**
|
9
9
|
# `unannotated` style cop only works for strings
|
10
10
|
# which are passed as arguments to those methods:
|
11
|
-
# `sprintf`, `format`, `%`.
|
11
|
+
# `printf`, `sprintf`, `format`, `%`.
|
12
12
|
# The reason is that *unannotated* format is very similar
|
13
13
|
# to encoded URLs or Date/Time formatting strings.
|
14
14
|
#
|
@@ -41,10 +41,7 @@ module RuboCop
|
|
41
41
|
class FormatStringToken < Cop
|
42
42
|
include ConfigurableEnforcedStyle
|
43
43
|
|
44
|
-
FORMAT_STRING_METHODS = %i[sprintf format %].freeze
|
45
|
-
|
46
44
|
def on_str(node)
|
47
|
-
return if placeholder_argument?(node)
|
48
45
|
return if node.each_ancestor(:xstr, :regexp).any?
|
49
46
|
|
50
47
|
tokens(node) do |detected_style, token_range|
|
@@ -61,14 +58,16 @@ module RuboCop
|
|
61
58
|
|
62
59
|
private
|
63
60
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
61
|
+
def_node_matcher :format_string_in_typical_context?, <<~PATTERN
|
62
|
+
{
|
63
|
+
^(send _ {:format :sprintf :printf} %0 ...)
|
64
|
+
^(send %0 :% _)
|
65
|
+
}
|
66
|
+
PATTERN
|
69
67
|
|
70
68
|
def unannotated_format?(node, detected_style)
|
71
|
-
detected_style == :unannotated &&
|
69
|
+
detected_style == :unannotated &&
|
70
|
+
!format_string_in_typical_context?(node)
|
72
71
|
end
|
73
72
|
|
74
73
|
def message(detected_style)
|
@@ -95,11 +94,7 @@ module RuboCop
|
|
95
94
|
if source_map.is_a?(Parser::Source::Map::Heredoc)
|
96
95
|
source_map.heredoc_body
|
97
96
|
elsif source_map.begin
|
98
|
-
|
99
|
-
source_map.expression,
|
100
|
-
source_map.expression.begin_pos + 1,
|
101
|
-
source_map.expression.end_pos - 1
|
102
|
-
)
|
97
|
+
source_map.expression.adjust(begin_pos: +1, end_pos: -1)
|
103
98
|
else
|
104
99
|
source_map.expression
|
105
100
|
end
|
@@ -109,31 +104,17 @@ module RuboCop
|
|
109
104
|
format_string = RuboCop::Cop::Utils::FormatString.new(contents.source)
|
110
105
|
|
111
106
|
format_string.format_sequences.each do |seq|
|
107
|
+
next if seq.percent?
|
108
|
+
|
112
109
|
detected_style = seq.style
|
113
|
-
token =
|
114
|
-
|
115
|
-
|
116
|
-
contents.begin_pos + seq.end_pos
|
110
|
+
token = contents.begin.adjust(
|
111
|
+
begin_pos: seq.begin_pos,
|
112
|
+
end_pos: seq.end_pos
|
117
113
|
)
|
118
114
|
|
119
115
|
yield(detected_style, token)
|
120
116
|
end
|
121
117
|
end
|
122
|
-
|
123
|
-
def slice_source(source_range, new_begin, new_end)
|
124
|
-
Parser::Source::Range.new(
|
125
|
-
source_range.source_buffer,
|
126
|
-
new_begin,
|
127
|
-
new_end
|
128
|
-
)
|
129
|
-
end
|
130
|
-
|
131
|
-
def placeholder_argument?(node)
|
132
|
-
return false unless node.parent
|
133
|
-
return true if node.parent.pair_type?
|
134
|
-
|
135
|
-
placeholder_argument?(node.parent)
|
136
|
-
end
|
137
118
|
end
|
138
119
|
end
|
139
120
|
end
|
@@ -11,6 +11,9 @@ module RuboCop
|
|
11
11
|
# encoding comment. The frozen string literal comment is only valid in
|
12
12
|
# Ruby 2.3+.
|
13
13
|
#
|
14
|
+
# Note that the cop will ignore files where the comment exists but is set
|
15
|
+
# to `false` instead of `true`.
|
16
|
+
#
|
14
17
|
# @example EnforcedStyle: always (default)
|
15
18
|
# # The `always` style will always add the frozen string literal comment
|
16
19
|
# # to a file, regardless of the Ruby version or if `freeze` or `<<` are
|
@@ -27,6 +30,13 @@ module RuboCop
|
|
27
30
|
# # ...
|
28
31
|
# end
|
29
32
|
#
|
33
|
+
# # good
|
34
|
+
# # frozen_string_literal: false
|
35
|
+
#
|
36
|
+
# module Bar
|
37
|
+
# # ...
|
38
|
+
# end
|
39
|
+
#
|
30
40
|
# @example EnforcedStyle: never
|
31
41
|
# # The `never` will enforce that the frozen string literal comment does
|
32
42
|
# # not exist in a file.
|
@@ -49,8 +49,9 @@ module RuboCop
|
|
49
49
|
|
50
50
|
if body.if_type?
|
51
51
|
check_ending_if(body)
|
52
|
-
elsif body.begin_type?
|
53
|
-
|
52
|
+
elsif body.begin_type?
|
53
|
+
final_expression = body.children.last
|
54
|
+
check_ending_if(final_expression) if final_expression&.if_type?
|
54
55
|
end
|
55
56
|
end
|
56
57
|
alias on_defs on_def
|
@@ -168,10 +168,10 @@ module RuboCop
|
|
168
168
|
end
|
169
169
|
|
170
170
|
def autocorrect_ruby19(corrector, pair_node)
|
171
|
-
key = pair_node.key
|
171
|
+
key = pair_node.key.source_range
|
172
172
|
op = pair_node.loc.operator
|
173
173
|
|
174
|
-
range =
|
174
|
+
range = key.join(op)
|
175
175
|
range = range_with_surrounding_space(range: range, side: :right)
|
176
176
|
|
177
177
|
space = argument_without_space?(pair_node.parent) ? ' ' : ''
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
# written as modifier `if`/`unless`. The cop also checks for modifier
|
8
8
|
# `if`/`unless` lines that exceed the maximum line length.
|
9
9
|
#
|
10
|
-
# The maximum line length is configured in the `
|
10
|
+
# The maximum line length is configured in the `Layout/LineLength`
|
11
11
|
# cop. The tab size is configured in the `IndentationWidth` of the
|
12
12
|
# `Layout/Tab` cop.
|
13
13
|
#
|
@@ -32,6 +32,8 @@ module RuboCop
|
|
32
32
|
# end
|
33
33
|
class IfUnlessModifier < Cop
|
34
34
|
include StatementModifier
|
35
|
+
include LineLengthHelp
|
36
|
+
include IgnoredPattern
|
35
37
|
|
36
38
|
MSG_USE_MODIFIER = 'Favor modifier `%<keyword>s` usage when having a ' \
|
37
39
|
'single-line body. Another good alternative is ' \
|
@@ -66,12 +68,52 @@ module RuboCop
|
|
66
68
|
|
67
69
|
private
|
68
70
|
|
71
|
+
def ignored_patterns
|
72
|
+
config.for_cop('Layout/LineLength')['IgnoredPatterns'] || []
|
73
|
+
end
|
74
|
+
|
69
75
|
def too_long_single_line?(node)
|
70
76
|
return false unless max_line_length
|
71
77
|
|
72
78
|
range = node.source_range
|
73
|
-
range.first_line == range.last_line
|
74
|
-
|
79
|
+
return false unless range.first_line == range.last_line
|
80
|
+
return false unless line_length_enabled_at_line?(range.first_line)
|
81
|
+
|
82
|
+
line = range.source_line
|
83
|
+
return false if line_length(line) <= max_line_length
|
84
|
+
|
85
|
+
too_long_line_based_on_config?(range, line)
|
86
|
+
end
|
87
|
+
|
88
|
+
def too_long_line_based_on_config?(range, line)
|
89
|
+
return false if matches_ignored_pattern?(line)
|
90
|
+
|
91
|
+
too_long = too_long_line_based_on_ignore_cop_directives?(range, line)
|
92
|
+
return too_long unless too_long == :undetermined
|
93
|
+
|
94
|
+
too_long_line_based_on_allow_uri?(line)
|
95
|
+
end
|
96
|
+
|
97
|
+
def too_long_line_based_on_ignore_cop_directives?(range, line)
|
98
|
+
if ignore_cop_directives? && directive_on_source_line?(range.line - 1)
|
99
|
+
return line_length_without_directive(line) > max_line_length
|
100
|
+
end
|
101
|
+
|
102
|
+
:undetermined
|
103
|
+
end
|
104
|
+
|
105
|
+
def too_long_line_based_on_allow_uri?(line)
|
106
|
+
if allow_uri?
|
107
|
+
uri_range = find_excessive_uri_range(line)
|
108
|
+
return false if uri_range && allowed_uri_position?(line, uri_range)
|
109
|
+
end
|
110
|
+
|
111
|
+
true
|
112
|
+
end
|
113
|
+
|
114
|
+
def line_length_enabled_at_line?(line)
|
115
|
+
processed_source.comment_config
|
116
|
+
.cop_enabled_at_line?('Layout/LineLength', line)
|
75
117
|
end
|
76
118
|
|
77
119
|
def named_capture_in_condition?(node)
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
58
58
|
# `loop do` without further modification. The reason is that a
|
59
59
|
# variable that's introduced inside a while/until loop is in scope
|
60
60
|
# outside of that loop too, but a variable that's assigned for the
|
61
|
-
# first time inside a block
|
61
|
+
# first time inside a block cannot be accessed after the block. In
|
62
62
|
# those more complicated cases we don't report an offense.
|
63
63
|
return if @variables.any? do |var|
|
64
64
|
assigned_inside_loop?(var, range) &&
|
@@ -97,12 +97,13 @@ module RuboCop
|
|
97
97
|
end
|
98
98
|
|
99
99
|
def modifier_replacement(node)
|
100
|
+
body = node.body
|
100
101
|
if node.single_line?
|
101
|
-
'loop { ' +
|
102
|
+
'loop { ' + body.source + ' }'
|
102
103
|
else
|
103
|
-
indentation =
|
104
|
+
indentation = body.source_range.source_line[LEADING_SPACE]
|
104
105
|
|
105
|
-
['loop do',
|
106
|
+
['loop do', body.source.gsub(/^/, configured_indent),
|
106
107
|
'end'].join("\n#{indentation}")
|
107
108
|
end
|
108
109
|
end
|