rubocop 0.52.1 → 0.53.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +2 -2
- data/config/default.yml +118 -46
- data/config/disabled.yml +8 -8
- data/config/enabled.yml +84 -28
- data/lib/rubocop.rb +28 -8
- data/lib/rubocop/ast/builder.rb +35 -37
- data/lib/rubocop/ast/node.rb +16 -1
- data/lib/rubocop/ast/node/and_node.rb +0 -8
- data/lib/rubocop/ast/node/block_node.rb +1 -9
- data/lib/rubocop/ast/node/case_node.rb +0 -8
- data/lib/rubocop/ast/node/ensure_node.rb +0 -8
- data/lib/rubocop/ast/node/for_node.rb +0 -8
- data/lib/rubocop/ast/node/or_node.rb +0 -8
- data/lib/rubocop/ast/node/pair_node.rb +0 -8
- data/lib/rubocop/ast/node/resbody_node.rb +0 -8
- data/lib/rubocop/ast/node/send_node.rb +0 -8
- data/lib/rubocop/ast/node/symbol_node.rb +0 -8
- data/lib/rubocop/ast/node/until_node.rb +0 -8
- data/lib/rubocop/ast/node/when_node.rb +0 -8
- data/lib/rubocop/ast/node/while_node.rb +0 -8
- data/lib/rubocop/cli.rb +17 -7
- data/lib/rubocop/comment_config.rb +24 -3
- data/lib/rubocop/config.rb +75 -6
- data/lib/rubocop/config_loader.rb +18 -28
- data/lib/rubocop/config_loader_resolver.rb +61 -9
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -1
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +4 -2
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/commissioner.rb +2 -2
- data/lib/rubocop/cop/cop.rb +4 -0
- data/lib/rubocop/cop/corrector.rb +11 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +59 -0
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +13 -0
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -5
- data/lib/rubocop/cop/generator.rb +29 -8
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +2 -0
- data/lib/rubocop/cop/layout/align_hash.rb +106 -37
- data/lib/rubocop/cop/{lint → layout}/block_alignment.rb +8 -5
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -17
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +6 -7
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/{lint → layout}/condition_position.rb +3 -3
- data/lib/rubocop/cop/{lint → layout}/def_end_alignment.rb +2 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +140 -0
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -5
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +20 -10
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +20 -0
- data/lib/rubocop/cop/{lint → layout}/end_alignment.rb +37 -6
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -0
- data/lib/rubocop/cop/layout/extra_spacing.rb +30 -37
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/indent_heredoc.rb +38 -2
- data/lib/rubocop/cop/layout/indentation_consistency.rb +105 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +4 -3
- data/lib/rubocop/cop/layout/initial_indentation.rb +15 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +4 -2
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +62 -29
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +74 -33
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +16 -2
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
- data/lib/rubocop/cop/layout/space_after_method_name.rb +2 -0
- data/lib/rubocop/cop/layout/space_after_not.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -0
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +15 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +15 -13
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +13 -1
- data/lib/rubocop/cop/layout/space_before_comment.rb +6 -4
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +30 -45
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -2
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +48 -18
- data/lib/rubocop/cop/layout/space_inside_parens.rb +8 -7
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +57 -11
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -0
- data/lib/rubocop/cop/layout/tab.rb +42 -16
- data/lib/rubocop/cop/layout/trailing_blank_lines.rb +46 -13
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +12 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +5 -3
- data/lib/rubocop/cop/lint/big_decimal_new.rb +44 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +2 -2
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -2
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +5 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +20 -9
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +4 -3
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +16 -10
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +5 -4
- data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +4 -3
- data/lib/rubocop/cop/lint/literal_as_condition.rb +2 -2
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +2 -0
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +7 -5
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +59 -0
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +86 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -0
- data/lib/rubocop/cop/lint/percent_string_array.rb +0 -2
- data/lib/rubocop/cop/lint/rand_one.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +2 -0
- data/lib/rubocop/cop/lint/rescue_type.rb +6 -3
- data/lib/rubocop/cop/lint/return_in_void_context.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -21
- data/lib/rubocop/cop/lint/script_permission.rb +30 -10
- data/lib/rubocop/cop/lint/shadowed_argument.rb +3 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unified_integer.rb +2 -2
- data/lib/rubocop/cop/lint/{unneeded_disable.rb → unneeded_cop_disable_directive.rb} +13 -7
- data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +97 -0
- data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +3 -3
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +11 -10
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -5
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +49 -10
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/line_length.rb +5 -2
- data/lib/rubocop/cop/mixin/alignment.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -1
- data/lib/rubocop/cop/mixin/def_node.rb +4 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +11 -3
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +20 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
- data/lib/rubocop/cop/mixin/hash_alignment.rb +2 -2
- data/lib/rubocop/cop/mixin/match_range.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +6 -0
- data/lib/rubocop/cop/mixin/nil_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +57 -9
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -5
- data/lib/rubocop/cop/mixin/range_help.rb +102 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +8 -7
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -9
- data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -10
- data/lib/rubocop/cop/mixin/surrounding_space.rb +38 -8
- data/lib/rubocop/cop/mixin/trailing_body.rb +26 -0
- data/lib/rubocop/cop/mixin/trailing_comma.rb +15 -3
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +104 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +3 -1
- data/lib/rubocop/cop/naming/file_name.rb +5 -10
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +76 -0
- data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +48 -0
- data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +57 -0
- data/lib/rubocop/cop/offense.rb +3 -2
- data/lib/rubocop/cop/performance/case_when_splat.rb +1 -0
- data/lib/rubocop/cop/performance/casecmp.rb +17 -8
- data/lib/rubocop/cop/performance/compare_with_block.rb +2 -0
- data/lib/rubocop/cop/performance/count.rb +1 -0
- data/lib/rubocop/cop/performance/fixed_size.rb +41 -0
- data/lib/rubocop/cop/performance/flat_map.rb +2 -0
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -0
- data/lib/rubocop/cop/performance/redundant_merge.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -0
- data/lib/rubocop/cop/performance/regexp_match.rb +4 -0
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +2 -0
- data/lib/rubocop/cop/rails/active_record_aliases.rb +46 -0
- data/lib/rubocop/cop/rails/blank.rb +3 -3
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +6 -0
- data/lib/rubocop/cop/rails/delegate.rb +6 -6
- data/lib/rubocop/cop/rails/file_path.rb +7 -1
- data/lib/rubocop/cop/rails/find_by.rb +2 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +17 -5
- data/lib/rubocop/cop/rails/inverse_of.rb +21 -2
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +45 -9
- data/lib/rubocop/cop/rails/presence.rb +8 -2
- data/lib/rubocop/cop/rails/present.rb +5 -5
- data/lib/rubocop/cop/rails/read_write_attribute.rb +4 -3
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -0
- data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -3
- data/lib/rubocop/cop/rails/request_referer.rb +3 -2
- data/lib/rubocop/cop/rails/reversible_migration.rb +9 -8
- data/lib/rubocop/cop/rails/safe_navigation.rb +3 -2
- data/lib/rubocop/cop/rails/save_bang.rb +11 -12
- data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -2
- data/lib/rubocop/cop/rails/time_zone.rb +38 -16
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +26 -16
- data/lib/rubocop/cop/rails/validation.rb +30 -2
- data/lib/rubocop/cop/security/open.rb +48 -0
- data/lib/rubocop/cop/style/and_or.rb +1 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +3 -1
- data/lib/rubocop/cop/style/attr.rb +2 -0
- data/lib/rubocop/cop/style/block_comments.rb +3 -1
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +2 -5
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -0
- data/lib/rubocop/cop/style/class_vars.rb +23 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
- data/lib/rubocop/cop/style/comment_annotation.rb +6 -4
- data/lib/rubocop/cop/style/commented_keyword.rb +3 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
- data/lib/rubocop/cop/style/copyright.rb +3 -1
- data/lib/rubocop/cop/style/each_with_object.rb +15 -1
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -0
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -0
- data/lib/rubocop/cop/style/empty_else.rb +9 -5
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -0
- data/lib/rubocop/cop/style/empty_line_after_guard_clause.rb +80 -0
- data/lib/rubocop/cop/style/empty_literal.rb +1 -0
- data/lib/rubocop/cop/style/encoding.rb +2 -0
- data/lib/rubocop/cop/style/expand_path_arguments.rb +194 -0
- data/lib/rubocop/cop/style/for.rb +33 -0
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/format_string_token.rb +4 -5
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/lambda.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +2 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -0
- data/lib/rubocop/cop/style/missing_else.rb +72 -7
- data/lib/rubocop/cop/style/mixin_usage.rb +3 -5
- data/lib/rubocop/cop/style/module_function.rb +10 -0
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -0
- data/lib/rubocop/cop/style/nested_modifier.rb +2 -0
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -0
- data/lib/rubocop/cop/style/next.rb +1 -0
- data/lib/rubocop/cop/style/not.rb +2 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/redundant_exception.rb +8 -3
- data/lib/rubocop/cop/style/redundant_return.rb +37 -3
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +74 -32
- data/lib/rubocop/cop/style/semicolon.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +14 -23
- data/lib/rubocop/cop/style/stderr_puts.rb +2 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +12 -0
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +29 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +43 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +7 -54
- data/lib/rubocop/cop/style/trailing_body_on_module.rb +43 -0
- data/lib/rubocop/cop/style/{trailing_comma_in_literal.rb → trailing_comma_in_array_literal.rb} +2 -20
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +56 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +17 -20
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -0
- data/lib/rubocop/cop/style/unless_else.rb +2 -0
- data/lib/rubocop/cop/style/word_array.rb +0 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
- data/lib/rubocop/cop/team.rb +5 -5
- data/lib/rubocop/cop/util.rb +23 -188
- data/lib/rubocop/cop/variable_force.rb +1 -1
- data/lib/rubocop/file_finder.rb +45 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +23 -14
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +12 -5
- data/lib/rubocop/formatter/json_formatter.rb +1 -1
- data/lib/rubocop/node_pattern.rb +8 -5
- data/lib/rubocop/options.rb +40 -33
- data/lib/rubocop/path_util.rb +5 -8
- data/lib/rubocop/processed_source.rb +53 -0
- data/lib/rubocop/remote_config.rb +1 -1
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/cop_helper.rb +0 -4
- data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -4
- data/lib/rubocop/rspec/shared_contexts.rb +3 -1
- data/lib/rubocop/rspec/shared_examples.rb +23 -25
- data/lib/rubocop/rspec/support.rb +5 -0
- data/lib/rubocop/runner.rb +3 -2
- data/lib/rubocop/string_util.rb +10 -9
- data/lib/rubocop/target_finder.rb +4 -1
- data/lib/rubocop/token.rb +26 -16
- data/lib/rubocop/version.rb +6 -4
- metadata +31 -17
- data/lib/rubocop/cop/performance/hash_each_methods.rb +0 -129
@@ -45,7 +45,7 @@ module RuboCop
|
|
45
45
|
|
46
46
|
def aligned_with_line?(line_nos, range, indent = nil)
|
47
47
|
line_nos.each do |lineno|
|
48
|
-
next if
|
48
|
+
next if aligned_comment_lines.include?(lineno + 1)
|
49
49
|
line = processed_source.lines[lineno]
|
50
50
|
index = line =~ /\S/
|
51
51
|
next unless index
|
@@ -55,10 +55,11 @@ module RuboCop
|
|
55
55
|
false
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
59
|
-
@
|
60
|
-
|
61
|
-
|
58
|
+
def aligned_comment_lines
|
59
|
+
@aligned_comment_lines ||=
|
60
|
+
processed_source.comments.map(&:loc).select do |r|
|
61
|
+
begins_its_line?(r.expression)
|
62
|
+
end.map(&:line)
|
62
63
|
end
|
63
64
|
|
64
65
|
def aligned_token?(range, line)
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Methods that calculate and return Parser::Source::Ranges
|
6
|
+
module RangeHelp
|
7
|
+
private
|
8
|
+
|
9
|
+
def source_range(source_buffer, line_number, column, length = 1)
|
10
|
+
if column.is_a?(Range)
|
11
|
+
column_index = column.begin
|
12
|
+
length = column.size
|
13
|
+
else
|
14
|
+
column_index = column
|
15
|
+
end
|
16
|
+
|
17
|
+
line_begin_pos = if line_number.zero?
|
18
|
+
0
|
19
|
+
else
|
20
|
+
source_buffer.line_range(line_number).begin_pos
|
21
|
+
end
|
22
|
+
begin_pos = line_begin_pos + column_index
|
23
|
+
end_pos = begin_pos + length
|
24
|
+
|
25
|
+
Parser::Source::Range.new(source_buffer, begin_pos, end_pos)
|
26
|
+
end
|
27
|
+
|
28
|
+
def range_between(start_pos, end_pos)
|
29
|
+
Parser::Source::Range.new(processed_source.buffer, start_pos, end_pos)
|
30
|
+
end
|
31
|
+
|
32
|
+
def range_with_surrounding_comma(range, side = :both)
|
33
|
+
buffer = @processed_source.buffer
|
34
|
+
src = buffer.source
|
35
|
+
|
36
|
+
go_left, go_right = directions(side)
|
37
|
+
|
38
|
+
begin_pos = range.begin_pos
|
39
|
+
end_pos = range.end_pos
|
40
|
+
begin_pos = move_pos(src, begin_pos, -1, go_left, /,/)
|
41
|
+
end_pos = move_pos(src, end_pos, 1, go_right, /,/)
|
42
|
+
|
43
|
+
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
44
|
+
end
|
45
|
+
|
46
|
+
def range_with_surrounding_space(range:, side: :both,
|
47
|
+
newlines: true, whitespace: false)
|
48
|
+
buffer = @processed_source.buffer
|
49
|
+
src = buffer.source
|
50
|
+
|
51
|
+
go_left, go_right = directions(side)
|
52
|
+
|
53
|
+
begin_pos = range.begin_pos
|
54
|
+
if go_left
|
55
|
+
begin_pos =
|
56
|
+
final_pos(src, begin_pos, -1, newlines, whitespace)
|
57
|
+
end
|
58
|
+
end_pos = range.end_pos
|
59
|
+
end_pos = final_pos(src, end_pos, 1, newlines, whitespace) if go_right
|
60
|
+
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
61
|
+
end
|
62
|
+
|
63
|
+
def range_by_whole_lines(range, include_final_newline: false)
|
64
|
+
buffer = @processed_source.buffer
|
65
|
+
|
66
|
+
begin_pos = range.begin_pos
|
67
|
+
begin_offset = range.column
|
68
|
+
begin_of_first_line = begin_pos - begin_offset
|
69
|
+
|
70
|
+
last_line = buffer.source_line(range.last_line)
|
71
|
+
end_pos = range.end_pos
|
72
|
+
end_offset = last_line.length - range.last_column
|
73
|
+
end_offset += 1 if include_final_newline
|
74
|
+
end_of_last_line = end_pos + end_offset
|
75
|
+
|
76
|
+
Parser::Source::Range.new(buffer, begin_of_first_line, end_of_last_line)
|
77
|
+
end
|
78
|
+
|
79
|
+
## Helpers for above range methods. Do not use inside Cops.
|
80
|
+
|
81
|
+
def directions(side)
|
82
|
+
if side == :both
|
83
|
+
[true, true]
|
84
|
+
else
|
85
|
+
[side == :left, side == :right]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def final_pos(src, pos, increment, newlines, whitespace)
|
90
|
+
pos = move_pos(src, pos, increment, true, /[ \t]/)
|
91
|
+
pos = move_pos(src, pos, increment, newlines, /\n/)
|
92
|
+
move_pos(src, pos, increment, whitespace, /\s/)
|
93
|
+
end
|
94
|
+
|
95
|
+
def move_pos(src, pos, step, condition, regexp)
|
96
|
+
offset = step == -1 ? -1 : 0
|
97
|
+
pos += step while condition && src[pos + offset] =~ regexp
|
98
|
+
pos < 0 ? 0 : pos
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -17,17 +17,18 @@ module RuboCop
|
|
17
17
|
private
|
18
18
|
|
19
19
|
def each_missing_space(tokens)
|
20
|
-
tokens.each_cons(2) do |
|
21
|
-
next unless kind(
|
22
|
-
next unless space_missing?(
|
23
|
-
next unless space_required_before?(
|
20
|
+
tokens.each_cons(2) do |token1, token2|
|
21
|
+
next unless kind(token1)
|
22
|
+
next unless space_missing?(token1, token2)
|
23
|
+
next unless space_required_before?(token2)
|
24
24
|
|
25
|
-
yield
|
25
|
+
yield token1
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
def space_missing?(
|
30
|
-
|
29
|
+
def space_missing?(token1, token2)
|
30
|
+
token1.line == token2.line &&
|
31
|
+
token2.column == token1.column + offset
|
31
32
|
end
|
32
33
|
|
33
34
|
def space_required_before?(token)
|
@@ -5,6 +5,8 @@ module RuboCop
|
|
5
5
|
# Common functionality for cops checking for space before
|
6
6
|
# punctuation.
|
7
7
|
module SpaceBeforePunctuation
|
8
|
+
include RangeHelp
|
9
|
+
|
8
10
|
MSG = 'Space found before %<token>s.'.freeze
|
9
11
|
|
10
12
|
def investigate(processed_source)
|
@@ -17,20 +19,20 @@ module RuboCop
|
|
17
19
|
private
|
18
20
|
|
19
21
|
def each_missing_space(tokens)
|
20
|
-
tokens.each_cons(2) do |
|
21
|
-
next unless kind(
|
22
|
-
next unless space_missing?(
|
23
|
-
next if space_required_after?(
|
22
|
+
tokens.each_cons(2) do |token1, token2|
|
23
|
+
next unless kind(token2)
|
24
|
+
next unless space_missing?(token1, token2)
|
25
|
+
next if space_required_after?(token1)
|
24
26
|
|
25
|
-
pos_before_punctuation = range_between(
|
26
|
-
|
27
|
+
pos_before_punctuation = range_between(token1.end_pos,
|
28
|
+
token2.begin_pos)
|
27
29
|
|
28
|
-
yield
|
30
|
+
yield token2, pos_before_punctuation
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
def space_missing?(
|
33
|
-
|
34
|
+
def space_missing?(token1, token2)
|
35
|
+
token1.line == token2.line && token2.begin_pos > token1.end_pos
|
34
36
|
end
|
35
37
|
|
36
38
|
def space_required_after?(token)
|
@@ -16,14 +16,15 @@ module RuboCop
|
|
16
16
|
|
17
17
|
def non_eligible_node?(node)
|
18
18
|
node.nonempty_line_count > 3 ||
|
19
|
-
!node.modifier_form? &&
|
19
|
+
!node.modifier_form? &&
|
20
|
+
processed_source.commented?(node.loc.end)
|
20
21
|
end
|
21
22
|
|
22
23
|
def non_eligible_body?(body)
|
23
24
|
body.nil? ||
|
24
25
|
body.empty_source? ||
|
25
26
|
body.begin_type? ||
|
26
|
-
commented?(body.source_range)
|
27
|
+
processed_source.commented?(body.source_range)
|
27
28
|
end
|
28
29
|
|
29
30
|
def non_eligible_condition?(condition)
|
@@ -48,14 +49,6 @@ module RuboCop
|
|
48
49
|
def max_line_length
|
49
50
|
config.for_cop('Metrics/LineLength')['Max']
|
50
51
|
end
|
51
|
-
|
52
|
-
def commented?(source)
|
53
|
-
comment_lines.include?(source.line)
|
54
|
-
end
|
55
|
-
|
56
|
-
def comment_lines
|
57
|
-
@comment_lines ||= processed_source.comments.map { |c| c.location.line }
|
58
|
-
end
|
59
52
|
end
|
60
53
|
end
|
61
54
|
end
|
@@ -4,6 +4,8 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
# Common functionality for checking and correcting surrounding whitespace.
|
6
6
|
module SurroundingSpace
|
7
|
+
include RangeHelp
|
8
|
+
|
7
9
|
NO_SPACE_COMMAND = 'Do not use'.freeze
|
8
10
|
SPACE_COMMAND = 'Use'.freeze
|
9
11
|
|
@@ -51,14 +53,6 @@ module RuboCop
|
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
54
|
-
def tokens(node)
|
55
|
-
@tokens ||= {}
|
56
|
-
@tokens[node.object_id] ||= processed_source.tokens.select do |token|
|
57
|
-
token.end_pos <= node.source_range.end_pos &&
|
58
|
-
token.begin_pos >= node.source_range.begin_pos
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
56
|
def no_space_offenses(node, # rubocop:disable Metrics/ParameterLists
|
63
57
|
left_token,
|
64
58
|
right_token,
|
@@ -105,6 +99,42 @@ module RuboCop
|
|
105
99
|
add_offense(node, location: range,
|
106
100
|
message: format(message, command: command))
|
107
101
|
end
|
102
|
+
|
103
|
+
def empty_offenses(node, left, right, message)
|
104
|
+
range = range_between(left.begin_pos, right.end_pos)
|
105
|
+
if offending_empty_space?(empty_config, left, right)
|
106
|
+
empty_offense(node, range, message, 'Use one')
|
107
|
+
end
|
108
|
+
return unless offending_empty_no_space?(empty_config, left, right)
|
109
|
+
empty_offense(node, range, message, 'Do not use')
|
110
|
+
end
|
111
|
+
|
112
|
+
def empty_offense(node, range, message, command)
|
113
|
+
add_offense(node, location: range,
|
114
|
+
message: format(message, command: command))
|
115
|
+
end
|
116
|
+
|
117
|
+
def empty_brackets?(left_bracket_token, right_bracket_token)
|
118
|
+
left_index = processed_source.tokens.index(left_bracket_token)
|
119
|
+
right_index = processed_source.tokens.index(right_bracket_token)
|
120
|
+
right_index && left_index == right_index - 1
|
121
|
+
end
|
122
|
+
|
123
|
+
def offending_empty_space?(config, left_token, right_token)
|
124
|
+
config == 'space' && !space_between?(left_token, right_token)
|
125
|
+
end
|
126
|
+
|
127
|
+
def offending_empty_no_space?(config, left_token, right_token)
|
128
|
+
config == 'no_space' && !no_space_between?(left_token, right_token)
|
129
|
+
end
|
130
|
+
|
131
|
+
def space_between?(left_bracket_token, right_bracket_token)
|
132
|
+
left_bracket_token.end_pos + 1 == right_bracket_token.begin_pos
|
133
|
+
end
|
134
|
+
|
135
|
+
def no_space_between?(left_bracket_token, right_bracket_token)
|
136
|
+
left_bracket_token.end_pos == right_bracket_token.begin_pos
|
137
|
+
end
|
108
138
|
end
|
109
139
|
end
|
110
140
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Common methods shared by TrailingBody cops
|
6
|
+
module TrailingBody
|
7
|
+
def trailing_body?(node)
|
8
|
+
body = node.to_a.reverse[0]
|
9
|
+
body && node.multiline? &&
|
10
|
+
body_on_first_line?(node, body)
|
11
|
+
end
|
12
|
+
|
13
|
+
def body_on_first_line?(node, body)
|
14
|
+
node.source_range.first_line == body.source_range.first_line
|
15
|
+
end
|
16
|
+
|
17
|
+
def first_part_of(body)
|
18
|
+
if body.begin_type?
|
19
|
+
body.children.first.source_range
|
20
|
+
else
|
21
|
+
body.source_range
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -6,6 +6,7 @@ module RuboCop
|
|
6
6
|
# Style/TrailingCommaInLiteral
|
7
7
|
module TrailingComma
|
8
8
|
include ConfigurableEnforcedStyle
|
9
|
+
include RangeHelp
|
9
10
|
|
10
11
|
MSG = '%<command>s comma after the last %<unit>s.'.freeze
|
11
12
|
|
@@ -35,6 +36,17 @@ module RuboCop
|
|
35
36
|
avoid_comma(kind, comma_pos, extra_avoid_comma_info)
|
36
37
|
end
|
37
38
|
|
39
|
+
def check_literal(node, kind)
|
40
|
+
return if node.children.empty?
|
41
|
+
# A braceless hash is the last parameter of a method call and will be
|
42
|
+
# checked as such.
|
43
|
+
return unless brackets?(node)
|
44
|
+
|
45
|
+
check(node, node.children, kind,
|
46
|
+
node.children.last.source_range.end_pos,
|
47
|
+
node.loc.end.begin_pos)
|
48
|
+
end
|
49
|
+
|
38
50
|
def extra_avoid_comma_info
|
39
51
|
case style
|
40
52
|
when :comma
|
@@ -111,8 +123,8 @@ module RuboCop
|
|
111
123
|
items.each_cons(2).none? { |a, b| on_same_line?(a, b) }
|
112
124
|
end
|
113
125
|
|
114
|
-
def on_same_line?(
|
115
|
-
|
126
|
+
def on_same_line?(range1, range2)
|
127
|
+
range1.last_line == range2.line
|
116
128
|
end
|
117
129
|
|
118
130
|
def avoid_comma(kind, comma_begin_pos, extra_info)
|
@@ -152,7 +164,7 @@ module RuboCop
|
|
152
164
|
end
|
153
165
|
|
154
166
|
# By default, there's no reason to avoid auto-correct.
|
155
|
-
def avoid_autocorrect?(
|
167
|
+
def avoid_autocorrect?(_nodes)
|
156
168
|
false
|
157
169
|
end
|
158
170
|
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Common functionality shared by Uncommunicative cops
|
6
|
+
module UncommunicativeName
|
7
|
+
CASE_MSG = 'Only use lowercase characters for %<name_type>s.'.freeze
|
8
|
+
NUM_MSG = 'Do not end %<name_type>s with a number.'.freeze
|
9
|
+
LENGTH_MSG = '%<name_type>s must be longer than %<min>s ' \
|
10
|
+
'characters.'.freeze
|
11
|
+
FORBIDDEN_MSG = 'Do not use %<name>s as a name for a ' \
|
12
|
+
'%<name_type>s.'.freeze
|
13
|
+
|
14
|
+
def check(node, args)
|
15
|
+
args.each do |arg|
|
16
|
+
name = arg.children.first.to_s
|
17
|
+
next if arg.restarg_type? && name.empty?
|
18
|
+
next if allowed_names.include?(name)
|
19
|
+
range = arg_range(arg, name.size)
|
20
|
+
issue_offenses(node, range, name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def issue_offenses(node, range, name)
|
27
|
+
forbidden_offense(node, range, name) if forbidden_names.include?(name)
|
28
|
+
case_offense(node, range) if uppercase?(name)
|
29
|
+
length_offense(node, range) unless long_enough?(name)
|
30
|
+
return if allow_nums
|
31
|
+
num_offense(node, range) if ends_with_num?(name)
|
32
|
+
end
|
33
|
+
|
34
|
+
def case_offense(node, range)
|
35
|
+
add_offense(node, location: range,
|
36
|
+
message: format(CASE_MSG, name_type: name_type(node)))
|
37
|
+
end
|
38
|
+
|
39
|
+
def uppercase?(name)
|
40
|
+
name =~ /[[:upper:]]/
|
41
|
+
end
|
42
|
+
|
43
|
+
def name_type(node)
|
44
|
+
@name_type ||= begin
|
45
|
+
case node.type
|
46
|
+
when :block then 'block parameter'
|
47
|
+
when :def, :defs then 'method parameter'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def num_offense(node, range)
|
53
|
+
add_offense(node, location: range,
|
54
|
+
message: format(NUM_MSG, name_type: name_type(node)))
|
55
|
+
end
|
56
|
+
|
57
|
+
def ends_with_num?(name)
|
58
|
+
name[-1] =~ /\d/
|
59
|
+
end
|
60
|
+
|
61
|
+
def length_offense(node, range)
|
62
|
+
add_offense(node, location: range,
|
63
|
+
message: format(LENGTH_MSG,
|
64
|
+
name_type: name_type(node).capitalize,
|
65
|
+
min: min_length))
|
66
|
+
end
|
67
|
+
|
68
|
+
def long_enough?(name)
|
69
|
+
name.size >= min_length
|
70
|
+
end
|
71
|
+
|
72
|
+
def arg_range(arg, length)
|
73
|
+
begin_pos = arg.source_range.begin_pos
|
74
|
+
Parser::Source::Range.new(processed_source.buffer,
|
75
|
+
begin_pos,
|
76
|
+
begin_pos + length)
|
77
|
+
end
|
78
|
+
|
79
|
+
def forbidden_offense(node, range, name)
|
80
|
+
add_offense(
|
81
|
+
node,
|
82
|
+
location: range,
|
83
|
+
message: format(FORBIDDEN_MSG, name: name, name_type: name_type(node))
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
def allowed_names
|
88
|
+
cop_config['AllowedNames']
|
89
|
+
end
|
90
|
+
|
91
|
+
def forbidden_names
|
92
|
+
cop_config['ForbiddenNames']
|
93
|
+
end
|
94
|
+
|
95
|
+
def allow_nums
|
96
|
+
cop_config['AllowNamesEndingInNumbers']
|
97
|
+
end
|
98
|
+
|
99
|
+
def min_length
|
100
|
+
cop_config['MinNameLength']
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|