rubocop 1.50.2 → 1.57.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/config/default.yml +110 -14
- data/config/obsoletion.yml +5 -0
- data/lib/rubocop/cli/command/lsp.rb +19 -0
- data/lib/rubocop/cli.rb +4 -1
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_finder.rb +2 -2
- data/lib/rubocop/config_loader_resolver.rb +4 -3
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
- data/lib/rubocop/config_obsoletion.rb +2 -2
- data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
- data/lib/rubocop/cop/base.rb +6 -2
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
- data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
- data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
- data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
- data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +32 -8
- data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
- data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/layout/class_structure.rb +7 -0
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
- data/lib/rubocop/cop/layout/dot_position.rb +1 -5
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -0
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +3 -3
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
- data/lib/rubocop/cop/layout/redundant_line_break.rb +14 -4
- data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
- data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
- data/lib/rubocop/cop/lint/debugger.rb +18 -5
- data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
- data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
- data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/missing_super.rb +34 -5
- data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
- data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +12 -3
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
- data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
- data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +5 -3
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
- data/lib/rubocop/cop/lint/void.rb +57 -7
- data/lib/rubocop/cop/metrics/block_length.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +2 -2
- data/lib/rubocop/cop/metrics/method_length.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +32 -4
- data/lib/rubocop/cop/migration/department_name.rb +2 -2
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/comments_help.rb +7 -3
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +5 -7
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/string_help.rb +4 -2
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +25 -10
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
- data/lib/rubocop/cop/naming/variable_name.rb +6 -1
- data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
- data/lib/rubocop/cop/style/alias.rb +9 -8
- data/lib/rubocop/cop/style/arguments_forwarding.rb +280 -63
- data/lib/rubocop/cop/style/array_intersect.rb +13 -5
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/begin_block.rb +1 -2
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +5 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +24 -39
- data/lib/rubocop/cop/style/collection_compact.rb +16 -6
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +30 -8
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +5 -3
- data/lib/rubocop/cop/style/copyright.rb +5 -2
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/dir_empty.rb +8 -14
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
- data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
- data/lib/rubocop/cop/style/file_read.rb +2 -2
- data/lib/rubocop/cop/style/for.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +24 -3
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -1
- data/lib/rubocop/cop/style/guard_clause.rb +28 -0
- data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
- data/lib/rubocop/cop/style/hash_except.rb +19 -8
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +23 -5
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -6
- data/lib/rubocop/cop/style/lambda.rb +3 -3
- data/lib/rubocop/cop/style/lambda_call.rb +5 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -5
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +6 -0
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +6 -1
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +10 -2
- data/lib/rubocop/cop/style/redundant_conditional.rb +2 -10
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +86 -5
- data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +117 -0
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +7 -3
- data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -7
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
- data/lib/rubocop/cop/style/redundant_return.rb +7 -2
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +11 -5
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
- data/lib/rubocop/cop/style/return_nil.rb +6 -2
- data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +95 -0
- data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
- data/lib/rubocop/cop/style/semicolon.rb +12 -4
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +65 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +6 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
- data/lib/rubocop/cop/style/symbol_array.rb +35 -15
- data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
- data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
- data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/regexp_ranges.rb +113 -0
- data/lib/rubocop/cop/variable_force/assignment.rb +45 -4
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
- data/lib/rubocop/cop/variable_force.rb +1 -0
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/ext/regexp_parser.rb +4 -1
- data/lib/rubocop/file_finder.rb +4 -7
- data/lib/rubocop/formatter/junit_formatter.rb +1 -1
- data/lib/rubocop/lsp/logger.rb +22 -0
- data/lib/rubocop/lsp/routes.rb +246 -0
- data/lib/rubocop/lsp/runtime.rb +99 -0
- data/lib/rubocop/lsp/server.rb +68 -0
- data/lib/rubocop/lsp/severity.rb +27 -0
- data/lib/rubocop/magic_comment.rb +12 -10
- data/lib/rubocop/options.rb +11 -1
- data/lib/rubocop/result_cache.rb +5 -1
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/shared_contexts.rb +2 -3
- data/lib/rubocop/runner.rb +5 -3
- data/lib/rubocop/server/cache.rb +1 -0
- data/lib/rubocop/server/client_command/exec.rb +3 -2
- data/lib/rubocop/string_interpreter.rb +3 -3
- data/lib/rubocop/target_finder.rb +7 -3
- data/lib/rubocop/target_ruby.rb +12 -7
- data/lib/rubocop/version.rb +10 -6
- data/lib/rubocop.rb +15 -0
- metadata +63 -15
data/lib/rubocop/cop/util.rb
CHANGED
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Utils
|
6
|
+
# Helper to abstract complexity of building range pairs
|
7
|
+
# with octal escape reconstruction (needed for regexp_parser < 2.7).
|
8
|
+
class RegexpRanges
|
9
|
+
attr_reader :root
|
10
|
+
|
11
|
+
def initialize(root)
|
12
|
+
@root = root
|
13
|
+
@compound_token = []
|
14
|
+
@pairs = []
|
15
|
+
@populated = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def compound_token
|
19
|
+
populate_all unless @populated
|
20
|
+
|
21
|
+
@compound_token
|
22
|
+
end
|
23
|
+
|
24
|
+
def pairs
|
25
|
+
populate_all unless @populated
|
26
|
+
|
27
|
+
@pairs
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def populate_all
|
33
|
+
populate(@root)
|
34
|
+
|
35
|
+
# If either bound is a compound the first one is an escape
|
36
|
+
# and that's all we need to work with.
|
37
|
+
# If there are any cops that wanted to operate on the compound
|
38
|
+
# expression we could wrap it with a facade class.
|
39
|
+
@pairs.map! { |pair| pair.map(&:first) }
|
40
|
+
|
41
|
+
@populated = true
|
42
|
+
end
|
43
|
+
|
44
|
+
def populate(expr)
|
45
|
+
expressions = expr.expressions.to_a
|
46
|
+
|
47
|
+
until expressions.empty?
|
48
|
+
current = expressions.shift
|
49
|
+
|
50
|
+
if escaped_octal?(current)
|
51
|
+
@compound_token << current
|
52
|
+
@compound_token.concat(pop_octal_digits(expressions))
|
53
|
+
# If we have all the digits we can discard.
|
54
|
+
end
|
55
|
+
|
56
|
+
next unless current.type == :set
|
57
|
+
|
58
|
+
process_set(expressions, current)
|
59
|
+
@compound_token.clear
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def process_set(expressions, current)
|
64
|
+
case current.token
|
65
|
+
when :range
|
66
|
+
@pairs << compose_range(expressions, current)
|
67
|
+
when :character
|
68
|
+
# Child expressions may include the range we are looking for.
|
69
|
+
populate(current)
|
70
|
+
when :intersection
|
71
|
+
# Each child expression could have child expressions that lead to ranges.
|
72
|
+
current.expressions.each do |intersected|
|
73
|
+
populate(intersected)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def compose_range(expressions, current)
|
79
|
+
range_start, range_end = current.expressions
|
80
|
+
range_start = if @compound_token.size.between?(1, 2) && octal_digit?(range_start.text)
|
81
|
+
@compound_token.dup << range_start
|
82
|
+
else
|
83
|
+
[range_start]
|
84
|
+
end
|
85
|
+
range_end = [range_end]
|
86
|
+
range_end.concat(pop_octal_digits(expressions)) if escaped_octal?(range_end.first)
|
87
|
+
[range_start, range_end]
|
88
|
+
end
|
89
|
+
|
90
|
+
def escaped_octal?(expr)
|
91
|
+
expr.text =~ /^\\[0-7]$/
|
92
|
+
end
|
93
|
+
|
94
|
+
def octal_digit?(char)
|
95
|
+
('0'..'7').cover?(char)
|
96
|
+
end
|
97
|
+
|
98
|
+
def pop_octal_digits(expressions)
|
99
|
+
digits = []
|
100
|
+
|
101
|
+
2.times do
|
102
|
+
next unless (next_child = expressions.first)
|
103
|
+
next unless next_child.type == :literal && next_child.text =~ /^[0-7]$/
|
104
|
+
|
105
|
+
digits << expressions.shift
|
106
|
+
end
|
107
|
+
|
108
|
+
digits
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -47,6 +47,10 @@ module RuboCop
|
|
47
47
|
@node.type == REGEXP_NAMED_CAPTURE_TYPE
|
48
48
|
end
|
49
49
|
|
50
|
+
def exception_assignment?
|
51
|
+
node.parent&.resbody_type? && node.parent.exception_variable == node
|
52
|
+
end
|
53
|
+
|
50
54
|
def operator_assignment?
|
51
55
|
return false unless meta_assignment_node
|
52
56
|
|
@@ -59,6 +63,18 @@ module RuboCop
|
|
59
63
|
meta_assignment_node.type == MULTIPLE_ASSIGNMENT_TYPE
|
60
64
|
end
|
61
65
|
|
66
|
+
def rest_assignment?
|
67
|
+
return false unless meta_assignment_node
|
68
|
+
|
69
|
+
meta_assignment_node.type == REST_ASSIGNMENT_TYPE
|
70
|
+
end
|
71
|
+
|
72
|
+
def for_assignment?
|
73
|
+
return false unless meta_assignment_node
|
74
|
+
|
75
|
+
meta_assignment_node.for_type?
|
76
|
+
end
|
77
|
+
|
62
78
|
def operator
|
63
79
|
assignment_node = meta_assignment_node || @node
|
64
80
|
assignment_node.loc.operator.source
|
@@ -66,7 +82,10 @@ module RuboCop
|
|
66
82
|
|
67
83
|
def meta_assignment_node
|
68
84
|
unless instance_variable_defined?(:@meta_assignment_node)
|
69
|
-
@meta_assignment_node = operator_assignment_node ||
|
85
|
+
@meta_assignment_node = operator_assignment_node ||
|
86
|
+
multiple_assignment_node ||
|
87
|
+
rest_assignment_node ||
|
88
|
+
for_assignment_node
|
70
89
|
end
|
71
90
|
|
72
91
|
@meta_assignment_node
|
@@ -83,13 +102,35 @@ module RuboCop
|
|
83
102
|
end
|
84
103
|
|
85
104
|
def multiple_assignment_node
|
86
|
-
grandparent_node = node.parent&.parent
|
87
|
-
|
105
|
+
return nil unless (grandparent_node = node.parent&.parent)
|
106
|
+
if (node = find_multiple_assignment_node(grandparent_node))
|
107
|
+
return node
|
108
|
+
end
|
88
109
|
return nil unless grandparent_node.type == MULTIPLE_ASSIGNMENT_TYPE
|
89
|
-
return nil unless node.parent.type == MULTIPLE_LEFT_HAND_SIDE_TYPE
|
90
110
|
|
91
111
|
grandparent_node
|
92
112
|
end
|
113
|
+
|
114
|
+
def rest_assignment_node
|
115
|
+
return nil unless node.parent
|
116
|
+
return nil unless node.parent.type == REST_ASSIGNMENT_TYPE
|
117
|
+
|
118
|
+
node.parent
|
119
|
+
end
|
120
|
+
|
121
|
+
def for_assignment_node
|
122
|
+
node.ancestors.find(&:for_type?)
|
123
|
+
end
|
124
|
+
|
125
|
+
def find_multiple_assignment_node(grandparent_node)
|
126
|
+
return unless grandparent_node.type == MULTIPLE_LEFT_HAND_SIDE_TYPE
|
127
|
+
return if grandparent_node.children.any?(&:splat_type?)
|
128
|
+
|
129
|
+
parent = grandparent_node.parent
|
130
|
+
return parent if parent.type == MULTIPLE_ASSIGNMENT_TYPE
|
131
|
+
|
132
|
+
find_multiple_assignment_node(parent)
|
133
|
+
end
|
93
134
|
end
|
94
135
|
end
|
95
136
|
end
|
@@ -113,14 +113,14 @@ module RuboCop
|
|
113
113
|
def accessible_variables
|
114
114
|
scope_stack.reverse_each.with_object([]) do |scope, variables|
|
115
115
|
variables.concat(scope.variables.values)
|
116
|
-
break variables unless scope.node.block_type?
|
116
|
+
break variables unless scope.node.block_type? || scope.node.numblock_type?
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
120
|
private
|
121
121
|
|
122
122
|
def mark_variable_as_captured_by_block_if_so(variable)
|
123
|
-
return unless current_scope.node.block_type?
|
123
|
+
return unless current_scope.node.block_type? || current_scope.node.numblock_type?
|
124
124
|
return if variable.scope == current_scope
|
125
125
|
|
126
126
|
variable.capture_with_block!
|
@@ -198,7 +198,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
198
198
|
table = ['|===', "| #{header.join(' | ')}\n\n"].join("\n")
|
199
199
|
marked_contents = content.map do |plain_content|
|
200
200
|
# Escape `|` with backslash to prevent the regexp `|` is not used as a table separator.
|
201
|
-
plain_content.map { |c| "| #{c.gsub(
|
201
|
+
plain_content.map { |c| "| #{c.gsub('|', '\|')}" }.join("\n")
|
202
202
|
end
|
203
203
|
table << marked_contents.join("\n\n")
|
204
204
|
table << "\n|===\n"
|
@@ -68,7 +68,9 @@ module RuboCop
|
|
68
68
|
return { body: expression } unless (q = quantifier)
|
69
69
|
|
70
70
|
body = expression.adjust(end_pos: -q.text.length)
|
71
|
-
|
71
|
+
q.origin = origin
|
72
|
+
q.source = source if q.respond_to?(:source=) # for regexp_parser 1.8
|
73
|
+
q_loc = q.expression
|
72
74
|
{ body: body, quantifier: q_loc }
|
73
75
|
end
|
74
76
|
end
|
@@ -86,6 +88,7 @@ module RuboCop
|
|
86
88
|
end
|
87
89
|
end
|
88
90
|
::Regexp::Expression::Base.include Expression::Base
|
91
|
+
::Regexp::Expression::Quantifier.include Expression::Base
|
89
92
|
::Regexp::Expression::CharacterSet.include Expression::CharacterSet
|
90
93
|
end
|
91
94
|
end
|
data/lib/rubocop/file_finder.rb
CHANGED
@@ -6,12 +6,8 @@ module RuboCop
|
|
6
6
|
# Common methods for finding files.
|
7
7
|
# @api private
|
8
8
|
module FileFinder
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.root_level?(path, stop_dir)
|
14
|
-
(@root_level || stop_dir) == path.to_s
|
9
|
+
class << self
|
10
|
+
attr_accessor :root_level
|
15
11
|
end
|
16
12
|
|
17
13
|
def find_file_upwards(filename, start_dir, stop_dir = nil)
|
@@ -34,7 +30,8 @@ module RuboCop
|
|
34
30
|
file = dir + filename
|
35
31
|
yield(file.to_s) if file.exist?
|
36
32
|
|
37
|
-
|
33
|
+
dir = dir.to_s
|
34
|
+
break if dir == stop_dir || dir == FileFinder.root_level
|
38
35
|
end
|
39
36
|
end
|
40
37
|
end
|
@@ -63,7 +63,7 @@ module RuboCop
|
|
63
63
|
|
64
64
|
def classname_attribute_value(file)
|
65
65
|
@classname_attribute_value_cache ||= Hash.new do |hash, key|
|
66
|
-
hash[key] = key.
|
66
|
+
hash[key] = key.delete_suffix('.rb').gsub("#{Dir.pwd}/", '').tr('/', '.')
|
67
67
|
end
|
68
68
|
@classname_attribute_value_cache[file]
|
69
69
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# This code is based on https://github.com/standardrb/standard.
|
5
|
+
#
|
6
|
+
# Copyright (c) 2023 Test Double, Inc.
|
7
|
+
#
|
8
|
+
# The MIT License (MIT)
|
9
|
+
#
|
10
|
+
# https://github.com/standardrb/standard/blob/main/LICENSE.txt
|
11
|
+
#
|
12
|
+
module RuboCop
|
13
|
+
module Lsp
|
14
|
+
# Log for Language Server Protocol of RuboCop.
|
15
|
+
# @api private
|
16
|
+
class Logger
|
17
|
+
def self.log(message)
|
18
|
+
warn("[server] #{message}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,246 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'severity'
|
4
|
+
|
5
|
+
#
|
6
|
+
# This code is based on https://github.com/standardrb/standard.
|
7
|
+
#
|
8
|
+
# Copyright (c) 2023 Test Double, Inc.
|
9
|
+
#
|
10
|
+
# The MIT License (MIT)
|
11
|
+
#
|
12
|
+
# https://github.com/standardrb/standard/blob/main/LICENSE.txt
|
13
|
+
#
|
14
|
+
module RuboCop
|
15
|
+
module Lsp
|
16
|
+
# Routes for Language Server Protocol of RuboCop.
|
17
|
+
# @api private
|
18
|
+
class Routes
|
19
|
+
def self.handle(name, &block)
|
20
|
+
define_method("handle_#{name}", &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
private_class_method :handle
|
24
|
+
|
25
|
+
def initialize(server)
|
26
|
+
@server = server
|
27
|
+
|
28
|
+
@text_cache = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def for(name)
|
32
|
+
name = "handle_#{name}"
|
33
|
+
return unless respond_to?(name)
|
34
|
+
|
35
|
+
method(name)
|
36
|
+
end
|
37
|
+
|
38
|
+
handle 'initialize' do |request|
|
39
|
+
initialization_options = extract_initialization_options_from(request)
|
40
|
+
|
41
|
+
@server.configure(initialization_options)
|
42
|
+
|
43
|
+
@server.write(
|
44
|
+
id: request[:id],
|
45
|
+
result: LanguageServer::Protocol::Interface::InitializeResult.new(
|
46
|
+
capabilities: LanguageServer::Protocol::Interface::ServerCapabilities.new(
|
47
|
+
document_formatting_provider: true,
|
48
|
+
diagnostic_provider: LanguageServer::Protocol::Interface::DiagnosticOptions.new(
|
49
|
+
inter_file_dependencies: false,
|
50
|
+
workspace_diagnostics: false
|
51
|
+
),
|
52
|
+
text_document_sync: LanguageServer::Protocol::Interface::TextDocumentSyncOptions.new(
|
53
|
+
change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL,
|
54
|
+
open_close: true
|
55
|
+
)
|
56
|
+
)
|
57
|
+
)
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
handle 'initialized' do |_request|
|
62
|
+
version = RuboCop::Version::STRING
|
63
|
+
|
64
|
+
Logger.log("RuboCop #{version} language server initialized, PID #{Process.pid}")
|
65
|
+
end
|
66
|
+
|
67
|
+
handle 'shutdown' do |request|
|
68
|
+
Logger.log('Client asked to shutdown RuboCop language server.')
|
69
|
+
@server.stop do
|
70
|
+
@server.write(id: request[:id], result: nil)
|
71
|
+
Logger.log('Exiting...')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
handle 'textDocument/diagnostic' do |request|
|
76
|
+
doc = request[:params][:textDocument]
|
77
|
+
result = diagnostic(doc[:uri], doc[:text])
|
78
|
+
@server.write(result)
|
79
|
+
end
|
80
|
+
|
81
|
+
handle 'textDocument/didChange' do |request|
|
82
|
+
params = request[:params]
|
83
|
+
result = diagnostic(params[:textDocument][:uri], params[:contentChanges][0][:text])
|
84
|
+
@server.write(result)
|
85
|
+
end
|
86
|
+
|
87
|
+
handle 'textDocument/didOpen' do |request|
|
88
|
+
doc = request[:params][:textDocument]
|
89
|
+
result = diagnostic(doc[:uri], doc[:text])
|
90
|
+
@server.write(result)
|
91
|
+
end
|
92
|
+
|
93
|
+
handle 'textDocument/didClose' do |request|
|
94
|
+
@text_cache.delete(request.dig(:params, :textDocument, :uri))
|
95
|
+
end
|
96
|
+
|
97
|
+
handle 'textDocument/formatting' do |request|
|
98
|
+
uri = request[:params][:textDocument][:uri]
|
99
|
+
@server.write(id: request[:id], result: format_file(uri))
|
100
|
+
end
|
101
|
+
|
102
|
+
handle 'workspace/didChangeConfiguration' do |_request|
|
103
|
+
Logger.log('Ignoring workspace/didChangeConfiguration')
|
104
|
+
end
|
105
|
+
|
106
|
+
handle 'workspace/didChangeWatchedFiles' do |request|
|
107
|
+
changed = request[:params][:changes].any? do |change|
|
108
|
+
change[:uri].end_with?(RuboCop::ConfigFinder::DOTFILE)
|
109
|
+
end
|
110
|
+
|
111
|
+
if changed
|
112
|
+
Logger.log('Configuration file changed; restart required')
|
113
|
+
@server.stop
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
handle 'workspace/executeCommand' do |request|
|
118
|
+
case (command = request[:params][:command])
|
119
|
+
when 'rubocop.formatAutocorrects'
|
120
|
+
label = 'Format with RuboCop autocorrects'
|
121
|
+
when 'rubocop.formatAutocorrectsAll'
|
122
|
+
label = 'Format all with RuboCop autocorrects'
|
123
|
+
else
|
124
|
+
handle_unsupported_method(request, command)
|
125
|
+
return
|
126
|
+
end
|
127
|
+
|
128
|
+
uri = request[:params][:arguments][0][:uri]
|
129
|
+
@server.write(
|
130
|
+
id: request[:id],
|
131
|
+
method: 'workspace/applyEdit',
|
132
|
+
params: {
|
133
|
+
label: label,
|
134
|
+
edit: {
|
135
|
+
changes: {
|
136
|
+
uri => format_file(uri, command: command)
|
137
|
+
}
|
138
|
+
}
|
139
|
+
}
|
140
|
+
)
|
141
|
+
end
|
142
|
+
|
143
|
+
handle 'textDocument/willSave' do |_request|
|
144
|
+
# Nothing to do
|
145
|
+
end
|
146
|
+
|
147
|
+
handle 'textDocument/didSave' do |_request|
|
148
|
+
# Nothing to do
|
149
|
+
end
|
150
|
+
|
151
|
+
handle '$/cancelRequest' do |_request|
|
152
|
+
# Can't cancel anything because single-threaded
|
153
|
+
end
|
154
|
+
|
155
|
+
handle '$/setTrace' do |_request|
|
156
|
+
# No-op, we log everything
|
157
|
+
end
|
158
|
+
|
159
|
+
def handle_unsupported_method(request, method = request[:method])
|
160
|
+
@server.write(
|
161
|
+
id: request[:id],
|
162
|
+
error: LanguageServer::Protocol::Interface::ResponseError.new(
|
163
|
+
code: LanguageServer::Protocol::Constant::ErrorCodes::METHOD_NOT_FOUND,
|
164
|
+
message: "Unsupported Method: #{method}"
|
165
|
+
)
|
166
|
+
)
|
167
|
+
Logger.log("Unsupported Method: #{method}")
|
168
|
+
end
|
169
|
+
|
170
|
+
def handle_method_missing(request)
|
171
|
+
return unless request.key?(:id)
|
172
|
+
|
173
|
+
@server.write(id: request[:id], result: nil)
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
def extract_initialization_options_from(request)
|
179
|
+
safe_autocorrect = request.dig(:params, :initializationOptions, :safeAutocorrect)
|
180
|
+
|
181
|
+
{
|
182
|
+
safe_autocorrect: safe_autocorrect.nil? || safe_autocorrect == true,
|
183
|
+
lint_mode: request.dig(:params, :initializationOptions, :lintMode) == true,
|
184
|
+
layout_mode: request.dig(:params, :initializationOptions, :layoutMode) == true
|
185
|
+
}
|
186
|
+
end
|
187
|
+
|
188
|
+
def format_file(file_uri, command: nil)
|
189
|
+
unless (text = @text_cache[file_uri])
|
190
|
+
Logger.log("Format request arrived before text synchronized; skipping: `#{file_uri}'")
|
191
|
+
|
192
|
+
return []
|
193
|
+
end
|
194
|
+
|
195
|
+
new_text = @server.format(remove_file_protocol_from(file_uri), text, command: command)
|
196
|
+
|
197
|
+
return [] if new_text == text
|
198
|
+
|
199
|
+
[
|
200
|
+
newText: new_text,
|
201
|
+
range: {
|
202
|
+
start: { line: 0, character: 0 },
|
203
|
+
end: { line: text.count("\n") + 1, character: 0 }
|
204
|
+
}
|
205
|
+
]
|
206
|
+
end
|
207
|
+
|
208
|
+
def diagnostic(file_uri, text)
|
209
|
+
@text_cache[file_uri] = text
|
210
|
+
offenses = @server.offenses(remove_file_protocol_from(file_uri), text)
|
211
|
+
diagnostics = offenses.map { |offense| to_diagnostic(offense) }
|
212
|
+
|
213
|
+
{
|
214
|
+
method: 'textDocument/publishDiagnostics',
|
215
|
+
params: {
|
216
|
+
uri: file_uri,
|
217
|
+
diagnostics: diagnostics
|
218
|
+
}
|
219
|
+
}
|
220
|
+
end
|
221
|
+
|
222
|
+
def remove_file_protocol_from(uri)
|
223
|
+
uri.delete_prefix('file://')
|
224
|
+
end
|
225
|
+
|
226
|
+
def to_diagnostic(offense)
|
227
|
+
code = offense[:cop_name]
|
228
|
+
message = offense[:message]
|
229
|
+
loc = offense[:location]
|
230
|
+
rubocop_severity = offense[:severity]
|
231
|
+
severity = Severity.find_by(rubocop_severity)
|
232
|
+
|
233
|
+
{
|
234
|
+
code: code, message: message, range: to_range(loc), severity: severity, source: 'rubocop'
|
235
|
+
}
|
236
|
+
end
|
237
|
+
|
238
|
+
def to_range(location)
|
239
|
+
{
|
240
|
+
start: { character: location[:start_column] - 1, line: location[:start_line] - 1 },
|
241
|
+
end: { character: location[:last_column] - 1, line: location[:last_line] - 1 }
|
242
|
+
}
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# This code is based on https://github.com/standardrb/standard.
|
5
|
+
#
|
6
|
+
# Copyright (c) 2023 Test Double, Inc.
|
7
|
+
#
|
8
|
+
# The MIT License (MIT)
|
9
|
+
#
|
10
|
+
# https://github.com/standardrb/standard/blob/main/LICENSE.txt
|
11
|
+
#
|
12
|
+
module RuboCop
|
13
|
+
module Lsp
|
14
|
+
# Runtime for Language Server Protocol of RuboCop.
|
15
|
+
# @api private
|
16
|
+
class Runtime
|
17
|
+
attr_writer :safe_autocorrect, :lint_mode, :layout_mode
|
18
|
+
|
19
|
+
def initialize(config_store)
|
20
|
+
@config_store = config_store
|
21
|
+
@logged_paths = []
|
22
|
+
@safe_autocorrect = true
|
23
|
+
@lint_mode = false
|
24
|
+
@layout_mode = false
|
25
|
+
end
|
26
|
+
|
27
|
+
# This abuses the `--stdin` option of rubocop and reads the formatted text
|
28
|
+
# from the `options[:stdin]` that rubocop mutates. This depends on
|
29
|
+
# `parallel: false` as well as the fact that RuboCop doesn't otherwise dup
|
30
|
+
# or reassign that options object. Risky business!
|
31
|
+
#
|
32
|
+
# Reassigning `options[:stdin]` is done here:
|
33
|
+
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/cop/team.rb#L131
|
34
|
+
# Printing `options[:stdin]`
|
35
|
+
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/cli/command/execute_runner.rb#L95
|
36
|
+
# Setting `parallel: true` would break this here:
|
37
|
+
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/runner.rb#L72
|
38
|
+
def format(path, text, command:)
|
39
|
+
safe_autocorrect = if command
|
40
|
+
command == 'rubocop.formatAutocorrects'
|
41
|
+
else
|
42
|
+
@safe_autocorrect
|
43
|
+
end
|
44
|
+
|
45
|
+
formatting_options = {
|
46
|
+
stdin: text, force_exclusion: true, autocorrect: true, safe_autocorrect: safe_autocorrect
|
47
|
+
}
|
48
|
+
formatting_options[:only] = config_only_options if @lint_mode || @layout_mode
|
49
|
+
|
50
|
+
redirect_stdout { run_rubocop(formatting_options, path) }
|
51
|
+
|
52
|
+
formatting_options[:stdin]
|
53
|
+
end
|
54
|
+
|
55
|
+
def offenses(path, text)
|
56
|
+
diagnostic_options = {
|
57
|
+
stdin: text, force_exclusion: true, formatters: ['json'], format: 'json'
|
58
|
+
}
|
59
|
+
diagnostic_options[:only] = config_only_options if @lint_mode || @layout_mode
|
60
|
+
|
61
|
+
json = redirect_stdout { run_rubocop(diagnostic_options, path) }
|
62
|
+
results = JSON.parse(json, symbolize_names: true)
|
63
|
+
|
64
|
+
if results[:files].empty?
|
65
|
+
unless @logged_paths.include?(path)
|
66
|
+
Logger.log "Ignoring file, per configuration: #{path}"
|
67
|
+
@logged_paths << path
|
68
|
+
end
|
69
|
+
return []
|
70
|
+
end
|
71
|
+
|
72
|
+
results.dig(:files, 0, :offenses)
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def config_only_options
|
78
|
+
only_options = []
|
79
|
+
only_options << 'Lint' if @lint_mode
|
80
|
+
only_options << 'Layout' if @layout_mode
|
81
|
+
only_options
|
82
|
+
end
|
83
|
+
|
84
|
+
def redirect_stdout(&block)
|
85
|
+
stdout = StringIO.new
|
86
|
+
|
87
|
+
RuboCop::Server::Helper.redirect(stdout: stdout, &block)
|
88
|
+
|
89
|
+
stdout.string
|
90
|
+
end
|
91
|
+
|
92
|
+
def run_rubocop(options, path)
|
93
|
+
runner = RuboCop::Runner.new(options, @config_store)
|
94
|
+
|
95
|
+
runner.run([path])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|