rubocop 0.12.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- data/CHANGELOG.md +40 -0
- data/README.md +49 -8
- data/config/default.yml +40 -0
- data/config/enabled.yml +37 -9
- data/lib/rubocop.rb +24 -10
- data/lib/rubocop/cli.rb +41 -106
- data/lib/rubocop/config.rb +3 -2
- data/lib/rubocop/cop/commissioner.rb +15 -5
- data/lib/rubocop/cop/cop.rb +47 -32
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +2 -2
- data/lib/rubocop/cop/lint/block_alignment.rb +30 -9
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +4 -4
- data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/eval.rb +1 -3
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_condition.rb +6 -4
- data/lib/rubocop/cop/lint/loop.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +5 -8
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +57 -60
- data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +85 -0
- data/lib/rubocop/cop/lint/void.rb +6 -8
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
- data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/style/access_control.rb +4 -6
- data/lib/rubocop/cop/style/alias.rb +1 -3
- data/lib/rubocop/cop/style/align_array.rb +47 -0
- data/lib/rubocop/cop/style/align_hash.rb +145 -0
- data/lib/rubocop/cop/style/align_parameters.rb +9 -3
- data/lib/rubocop/cop/style/and_or.rb +3 -4
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -3
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -4
- data/lib/rubocop/cop/style/begin_block.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_nesting.rb +2 -2
- data/lib/rubocop/cop/style/blocks.rb +14 -2
- data/lib/rubocop/cop/style/case_equality.rb +1 -3
- data/lib/rubocop/cop/style/case_indentation.rb +1 -1
- data/lib/rubocop/cop/style/character_literal.rb +1 -2
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/style/class_methods.rb +1 -3
- data/lib/rubocop/cop/style/{avoid_class_vars.rb → class_vars.rb} +6 -2
- data/lib/rubocop/cop/style/collection_methods.rb +7 -9
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
- data/lib/rubocop/cop/style/comment_annotation.rb +6 -6
- data/lib/rubocop/cop/style/constant_name.rb +1 -3
- data/lib/rubocop/cop/style/def_parentheses.rb +4 -12
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/dot_position.rb +2 -4
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +21 -6
- data/lib/rubocop/cop/style/empty_lines.rb +1 -1
- data/lib/rubocop/cop/style/empty_literal.rb +3 -12
- data/lib/rubocop/cop/style/encoding.rb +6 -6
- data/lib/rubocop/cop/style/end_block.rb +1 -1
- data/lib/rubocop/cop/style/end_of_line.rb +5 -5
- data/lib/rubocop/cop/style/even_odd.rb +2 -2
- data/lib/rubocop/cop/style/favor_join.rb +1 -3
- data/lib/rubocop/cop/style/favor_modifier.rb +7 -3
- data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
- data/lib/rubocop/cop/style/final_newline.rb +23 -0
- data/lib/rubocop/cop/style/{avoid_for.rb → for.rb} +2 -2
- data/lib/rubocop/cop/style/{avoid_global_vars.rb → global_vars.rb} +19 -6
- data/lib/rubocop/cop/style/hash_methods.rb +3 -5
- data/lib/rubocop/cop/style/hash_syntax.rb +4 -4
- data/lib/rubocop/cop/style/if_then_else.rb +1 -1
- data/lib/rubocop/cop/style/indentation_width.rb +4 -4
- data/lib/rubocop/cop/style/lambda.rb +2 -2
- data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/style/line_length.rb +7 -8
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +1 -1
- data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -4
- data/lib/rubocop/cop/style/method_length.rb +4 -4
- data/lib/rubocop/cop/style/module_function.rb +1 -3
- data/lib/rubocop/cop/style/multiline_block_chain.rb +44 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -3
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +26 -6
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +26 -0
- data/lib/rubocop/cop/style/proc.rb +1 -3
- data/lib/rubocop/cop/style/reduce_arguments.rb +7 -5
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +9 -2
- data/lib/rubocop/cop/style/redundant_self.rb +9 -2
- data/lib/rubocop/cop/style/regexp_literal.rb +7 -8
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +10 -10
- data/lib/rubocop/cop/style/signal_exception.rb +2 -4
- data/lib/rubocop/cop/style/single_line_methods.rb +2 -4
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -1
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
- data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +34 -0
- data/lib/rubocop/cop/style/{avoid_perlisms.rb → special_global_vars.rb} +17 -8
- data/lib/rubocop/cop/style/string_literals.rb +1 -2
- data/lib/rubocop/cop/style/surrounding_space.rb +9 -8
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/symbol_name.rb +9 -2
- data/lib/rubocop/cop/style/tab.rb +5 -5
- data/lib/rubocop/cop/style/ternary_operator.rb +2 -6
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +32 -0
- data/lib/rubocop/cop/style/trailing_whitespace.rb +5 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -3
- data/lib/rubocop/cop/style/when_then.rb +1 -4
- data/lib/rubocop/cop/style/while_until_do.rb +7 -5
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/team.rb +100 -0
- data/lib/rubocop/cop/variable_inspector.rb +323 -235
- data/lib/rubocop/cop/variable_inspector/assignment.rb +103 -0
- data/lib/rubocop/cop/variable_inspector/locatable.rb +162 -0
- data/lib/rubocop/cop/variable_inspector/reference.rb +31 -0
- data/lib/rubocop/cop/variable_inspector/scope.rb +70 -0
- data/lib/rubocop/cop/variable_inspector/variable.rb +87 -0
- data/lib/rubocop/cop/variable_inspector/variable_table.rb +129 -0
- data/lib/rubocop/formatter/json_formatter.rb +8 -8
- data/lib/rubocop/formatter/progress_formatter.rb +4 -4
- data/lib/rubocop/processed_source.rb +22 -1
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +1 -1
- data/spec/rubocop/cli_spec.rb +32 -30
- data/spec/rubocop/config_spec.rb +4 -6
- data/spec/rubocop/cop/commissioner_spec.rb +4 -5
- data/spec/rubocop/cop/cop_spec.rb +8 -26
- data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +5 -9
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +105 -57
- data/spec/rubocop/cop/lint/empty_ensure_spec.rb +1 -1
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +1 -1
- data/spec/rubocop/cop/lint/end_in_method_spec.rb +1 -1
- data/spec/rubocop/cop/lint/ensure_return_spec.rb +1 -1
- data/spec/rubocop/cop/lint/eval_spec.rb +3 -3
- data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +2 -2
- data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +1 -1
- data/spec/rubocop/cop/lint/loop_spec.rb +1 -1
- data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +1 -1
- data/spec/rubocop/cop/lint/rescue_exception_spec.rb +5 -5
- data/spec/rubocop/cop/lint/unreachable_code_spec.rb +1 -1
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1545 -108
- data/spec/rubocop/cop/lint/useless_comparison_spec.rb +1 -1
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +101 -0
- data/spec/rubocop/cop/lint/void_spec.rb +1 -1
- data/spec/rubocop/cop/offence_spec.rb +4 -4
- data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +1 -1
- data/spec/rubocop/cop/rails/read_attribute_spec.rb +1 -1
- data/spec/rubocop/cop/rails/validation_spec.rb +1 -1
- data/spec/rubocop/cop/style/access_control_spec.rb +20 -20
- data/spec/rubocop/cop/style/alias_spec.rb +3 -3
- data/spec/rubocop/cop/style/align_array_spec.rb +62 -0
- data/spec/rubocop/cop/style/align_hash_spec.rb +267 -0
- data/spec/rubocop/cop/style/align_parameters_spec.rb +2 -2
- data/spec/rubocop/cop/style/and_or_spec.rb +1 -1
- data/spec/rubocop/cop/style/ascii_comments_spec.rb +2 -2
- data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +2 -2
- data/spec/rubocop/cop/style/attr_spec.rb +1 -1
- data/spec/rubocop/cop/style/begin_block_spec.rb +1 -1
- data/spec/rubocop/cop/style/block_comments_spec.rb +1 -1
- data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
- data/spec/rubocop/cop/style/blocks_spec.rb +25 -1
- data/spec/rubocop/cop/style/case_equality_spec.rb +1 -1
- data/spec/rubocop/cop/style/case_indentation_spec.rb +5 -5
- data/spec/rubocop/cop/style/character_literal_spec.rb +1 -1
- data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +1 -1
- data/spec/rubocop/cop/style/class_methods_spec.rb +1 -1
- data/spec/rubocop/cop/style/class_vars_spec.rb +25 -0
- data/spec/rubocop/cop/style/collection_methods_spec.rb +5 -6
- data/spec/rubocop/cop/style/colon_method_call_spec.rb +0 -3
- data/spec/rubocop/cop/style/comment_annotation_spec.rb +20 -18
- data/spec/rubocop/cop/style/constant_name_spec.rb +1 -1
- data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +1 -1
- data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +1 -1
- data/spec/rubocop/cop/style/documentation_spec.rb +1 -1
- data/spec/rubocop/cop/style/dot_position_spec.rb +5 -5
- data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +35 -4
- data/spec/rubocop/cop/style/empty_lines_spec.rb +1 -1
- data/spec/rubocop/cop/style/empty_literal_spec.rb +7 -7
- data/spec/rubocop/cop/style/encoding_spec.rb +11 -5
- data/spec/rubocop/cop/style/end_block_spec.rb +1 -1
- data/spec/rubocop/cop/style/end_of_line_spec.rb +4 -3
- data/spec/rubocop/cop/style/even_odd_spec.rb +1 -1
- data/spec/rubocop/cop/style/favor_join_spec.rb +2 -2
- data/spec/rubocop/cop/style/favor_modifier_spec.rb +13 -10
- data/spec/rubocop/cop/style/favor_sprintf_spec.rb +4 -4
- data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +1 -1
- data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +4 -4
- data/spec/rubocop/cop/style/final_newline_spec.rb +25 -0
- data/spec/rubocop/cop/style/{avoid_for_spec.rb → for_spec.rb} +8 -12
- data/spec/rubocop/cop/style/{avoid_global_vars_spec.rb → global_vars_spec.rb} +13 -3
- data/spec/rubocop/cop/style/hash_methods_spec.rb +1 -1
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +20 -9
- data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cop/style/indentation_width_spec.rb +19 -19
- data/spec/rubocop/cop/style/lambda_spec.rb +6 -6
- data/spec/rubocop/cop/style/leading_comment_space_spec.rb +1 -1
- data/spec/rubocop/cop/style/line_length_spec.rb +3 -3
- data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +8 -9
- data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +1 -1
- data/spec/rubocop/cop/style/method_length_spec.rb +18 -17
- data/spec/rubocop/cop/style/module_function_spec.rb +1 -1
- data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +84 -0
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +2 -2
- data/spec/rubocop/cop/style/nil_comparison_spec.rb +1 -1
- data/spec/rubocop/cop/style/not_spec.rb +1 -1
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +15 -25
- data/spec/rubocop/cop/style/one_line_conditional_spec.rb +2 -2
- data/spec/rubocop/cop/style/op_method_spec.rb +3 -3
- data/spec/rubocop/cop/style/parameter_lists_spec.rb +5 -5
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +4 -8
- data/spec/rubocop/cop/style/perl_backrefs_spec.rb +23 -0
- data/spec/rubocop/cop/style/proc_spec.rb +1 -1
- data/spec/rubocop/cop/style/reduce_arguments_spec.rb +18 -11
- data/spec/rubocop/cop/style/redundant_begin_spec.rb +1 -1
- data/spec/rubocop/cop/style/redundant_return_spec.rb +16 -1
- data/spec/rubocop/cop/style/redundant_self_spec.rb +6 -1
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +19 -23
- data/spec/rubocop/cop/style/rescue_modifier_spec.rb +3 -3
- data/spec/rubocop/cop/style/semicolon_spec.rb +3 -3
- data/spec/rubocop/cop/style/signal_exception_spec.rb +1 -1
- data/spec/rubocop/cop/style/single_line_methods_spec.rb +22 -18
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +4 -4
- data/spec/rubocop/cop/style/space_after_comma_spec.rb +4 -4
- data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +1 -1
- data/spec/rubocop/cop/style/space_after_method_name_spec.rb +1 -1
- data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cop/style/space_around_braces_spec.rb +13 -12
- data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +3 -3
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +25 -25
- data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +53 -0
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +9 -9
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +47 -61
- data/spec/rubocop/cop/style/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +52 -0
- data/spec/rubocop/cop/style/string_literals_spec.rb +5 -5
- data/spec/rubocop/cop/style/symbol_array_spec.rb +1 -1
- data/spec/rubocop/cop/style/symbol_name_spec.rb +27 -18
- data/spec/rubocop/cop/style/tab_spec.rb +1 -1
- data/spec/rubocop/cop/style/ternary_operator_spec.rb +2 -2
- data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +24 -0
- data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +7 -7
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -14
- data/spec/rubocop/cop/style/unless_else_spec.rb +3 -3
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +5 -5
- data/spec/rubocop/cop/style/when_then_spec.rb +15 -15
- data/spec/rubocop/cop/style/while_until_do_spec.rb +3 -3
- data/spec/rubocop/cop/style/word_array_spec.rb +1 -1
- data/spec/rubocop/cop/team_spec.rb +158 -0
- data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +217 -0
- data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +740 -0
- data/spec/rubocop/cop/variable_inspector/scope_spec.rb +191 -0
- data/spec/rubocop/cop/variable_inspector/variable_spec.rb +79 -0
- data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +275 -0
- data/spec/rubocop/cop/variable_inspector_spec.rb +13 -533
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +4 -4
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +3 -3
- data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
- data/spec/spec_helper.rb +5 -1
- data/spec/support/ast_helper.rb +15 -0
- data/spec/support/shared_context.rb +18 -0
- data/spec/support/shared_examples.rb +1 -1
- metadata +95 -32
- checksums.yaml +0 -7
- data/lib/rubocop/cop/lint/unused_local_variable.rb +0 -32
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -19
- data/spec/rubocop/cop/lint/unused_local_variable_spec.rb +0 -588
- data/spec/rubocop/cop/style/avoid_class_vars_spec.rb +0 -27
- data/spec/rubocop/cop/style/avoid_perl_backrefs_spec.rb +0 -20
- data/spec/rubocop/cop/style/avoid_perlisms_spec.rb +0 -47
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for setter call to local variable as the final
|
7
|
+
# expression of a function definition.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# def something
|
12
|
+
# x = Something.new
|
13
|
+
# x.attr = 5
|
14
|
+
# end
|
15
|
+
class UselessSetterCall < Cop
|
16
|
+
MSG = 'Useless setter call to local variable %s.'
|
17
|
+
|
18
|
+
def on_def(node)
|
19
|
+
_name, args, body = *node
|
20
|
+
|
21
|
+
check_for_useless_assignment(body, args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def on_defs(node)
|
25
|
+
_target, _name, args, body = *node
|
26
|
+
|
27
|
+
check_for_useless_assignment(body, args)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def check_for_useless_assignment(body, args)
|
33
|
+
return unless body
|
34
|
+
|
35
|
+
if body.type == :begin
|
36
|
+
expression = body.children
|
37
|
+
else
|
38
|
+
expression = body
|
39
|
+
end
|
40
|
+
|
41
|
+
last_expr = expression.is_a?(Array) ? expression.last : expression
|
42
|
+
|
43
|
+
return unless setter_call_to_local_variable?(last_expr)
|
44
|
+
|
45
|
+
receiver, = *last_expr
|
46
|
+
var_name, = *receiver
|
47
|
+
return if contains_object_passed_as_argument?(var_name, body, args)
|
48
|
+
|
49
|
+
warning(receiver,
|
50
|
+
:name,
|
51
|
+
MSG.format(receiver.loc.name.source))
|
52
|
+
end
|
53
|
+
|
54
|
+
def setter_call_to_local_variable?(node)
|
55
|
+
return unless node && node.type == :send
|
56
|
+
receiver, method, _args = *node
|
57
|
+
return unless receiver && receiver.type == :lvar
|
58
|
+
method =~ /\w=$/
|
59
|
+
end
|
60
|
+
|
61
|
+
def contains_object_passed_as_argument?(lvar_name, body, args)
|
62
|
+
variable_table = {}
|
63
|
+
|
64
|
+
args.children.each do |arg_node|
|
65
|
+
arg_name, = *arg_node
|
66
|
+
variable_table[arg_name] = true
|
67
|
+
end
|
68
|
+
|
69
|
+
on_node([:lvasgn, :ivasgn, :cvasgn, :gvasgn], body) do |asgn_node|
|
70
|
+
lhs_var_name, rhs_node = *asgn_node
|
71
|
+
|
72
|
+
if [:lvar, :ivar, :cvar, :gvar].include?(rhs_node.type)
|
73
|
+
rhs_var_name, = *rhs_node
|
74
|
+
variable_table[lhs_var_name] = variable_table[rhs_var_name]
|
75
|
+
else
|
76
|
+
variable_table[lhs_var_name] = false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
variable_table[lvar_name]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -18,7 +18,7 @@ module Rubocop
|
|
18
18
|
def on_begin(node)
|
19
19
|
expressions = *node
|
20
20
|
|
21
|
-
expressions
|
21
|
+
expressions.drop_last(1).each do |expr|
|
22
22
|
check_for_void_op(expr)
|
23
23
|
check_for_literal(expr)
|
24
24
|
check_for_var(expr)
|
@@ -32,22 +32,20 @@ module Rubocop
|
|
32
32
|
|
33
33
|
op = node.loc.selector.source
|
34
34
|
|
35
|
-
if OPS.include?(op)
|
36
|
-
add_offence(:warning, node.loc.selector, sprintf(OP_MSG, op))
|
37
|
-
end
|
35
|
+
warning(node, :selector, sprintf(OP_MSG, op)) if OPS.include?(op)
|
38
36
|
end
|
39
37
|
|
40
38
|
def check_for_var(node)
|
41
39
|
if VARS.include?(node.type)
|
42
|
-
|
43
|
-
|
40
|
+
warning(node, :name,
|
41
|
+
sprintf(VAR_MSG, node.loc.name.source))
|
44
42
|
end
|
45
43
|
end
|
46
44
|
|
47
45
|
def check_for_literal(node)
|
48
46
|
if LITERALS.include?(node.type)
|
49
|
-
|
50
|
-
|
47
|
+
warning(node, :expression,
|
48
|
+
sprintf(LIT_MSG, node.loc.expression.source))
|
51
49
|
end
|
52
50
|
end
|
53
51
|
end
|
@@ -33,18 +33,16 @@ module Rubocop
|
|
33
33
|
selector = send_node.loc.selector.source
|
34
34
|
|
35
35
|
if send_start_col - 2 != class_start_col
|
36
|
-
|
37
|
-
|
38
|
-
format(INDENT_MSG, selector))
|
36
|
+
convention(send_node, :expression,
|
37
|
+
format(INDENT_MSG, selector))
|
39
38
|
end
|
40
39
|
|
41
40
|
send_line = send_node.loc.line
|
42
41
|
|
43
42
|
unless processed_source[send_line].chomp.empty? &&
|
44
43
|
processed_source[send_line - 2].chomp.empty?
|
45
|
-
|
46
|
-
|
47
|
-
format(BLANK_MSG, selector))
|
44
|
+
convention(send_node, :expression,
|
45
|
+
format(BLANK_MSG, selector))
|
48
46
|
end
|
49
47
|
end
|
50
48
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Here we check if the elements of a multi-line array literal are
|
7
|
+
# aligned.
|
8
|
+
class AlignArray < Cop
|
9
|
+
MSG = 'Align the elements of an array literal if they span more ' +
|
10
|
+
'than one line.'
|
11
|
+
|
12
|
+
def on_array(node)
|
13
|
+
first_element = node.children.first
|
14
|
+
|
15
|
+
node.children.each_cons(2) do |prev, current|
|
16
|
+
if current.loc.line != prev.loc.line
|
17
|
+
@column_delta = first_element.loc.column - current.loc.column
|
18
|
+
if current.loc.column != first_element.loc.column
|
19
|
+
convention(current, :expression)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def autocorrect_action(node)
|
26
|
+
# We can't use the instance variable inside the lambda. That would
|
27
|
+
# just give each lambda the same reference and they would all get
|
28
|
+
# the last value of @column_delta. A local variable fixes the
|
29
|
+
# problem.
|
30
|
+
column_delta = @column_delta
|
31
|
+
|
32
|
+
@corrections << lambda do |corrector|
|
33
|
+
expr = node.loc.expression
|
34
|
+
if column_delta > 0
|
35
|
+
corrector.replace(expr, ' ' * column_delta + expr.source)
|
36
|
+
else
|
37
|
+
range = Parser::Source::Range.new(expr.source_buffer,
|
38
|
+
expr.begin_pos + column_delta,
|
39
|
+
expr.end_pos)
|
40
|
+
corrector.replace(range, expr.source)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Here we check if the keys, separators, and values of a multi-line hash
|
7
|
+
# literal are aligned.
|
8
|
+
class AlignHash < Cop
|
9
|
+
MSG = 'Align the elements of a hash literal if they span more than ' +
|
10
|
+
'one line.'
|
11
|
+
|
12
|
+
def on_hash(node)
|
13
|
+
first_pair = node.children.first
|
14
|
+
|
15
|
+
if [cop_config['EnforcedHashRocketStyle'],
|
16
|
+
cop_config['EnforcedColonStyle']].include?('table')
|
17
|
+
key_widths = node.children.map do |pair|
|
18
|
+
key, _value = *pair
|
19
|
+
key.loc.expression.source.length
|
20
|
+
end
|
21
|
+
@max_key_width = key_widths.max
|
22
|
+
if first_pair && value_delta(nil, first_pair, @max_key_width) != 0
|
23
|
+
@column_deltas = {}
|
24
|
+
convention(first_pair, :expression)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
node.children.each_cons(2) do |prev, current|
|
29
|
+
@column_deltas = deltas(first_pair, prev, current, @max_key_width)
|
30
|
+
convention(current, :expression) unless good_alignment?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def autocorrect_action(node)
|
35
|
+
# We can't use the instance variable inside the lambda. That would
|
36
|
+
# just give each lambda the same reference and they would all get the
|
37
|
+
# last value of each. Some local variables fix the problem.
|
38
|
+
max_key_width = @max_key_width
|
39
|
+
key_delta = @column_deltas[:key] || 0
|
40
|
+
|
41
|
+
key, value = *node
|
42
|
+
|
43
|
+
@corrections << lambda do |corrector|
|
44
|
+
expr = node.loc.expression
|
45
|
+
b = expr.begin_pos
|
46
|
+
b -= key_delta.abs if key_delta < 0
|
47
|
+
range = Parser::Source::Range.new(expr.source_buffer, b,
|
48
|
+
expr.end_pos)
|
49
|
+
source = ' ' * [key_delta, 0].max +
|
50
|
+
if enforced_style(node) == 'key'
|
51
|
+
expr.source
|
52
|
+
else
|
53
|
+
key_source = key.loc.expression.source
|
54
|
+
padded_separator = case enforced_style(node)
|
55
|
+
when 'separator'
|
56
|
+
spaced_separator(node)
|
57
|
+
when 'table'
|
58
|
+
space = ' ' * (max_key_width -
|
59
|
+
key_source.length)
|
60
|
+
if node.loc.operator.is?('=>')
|
61
|
+
space + spaced_separator(node)
|
62
|
+
else
|
63
|
+
spaced_separator(node) + space
|
64
|
+
end
|
65
|
+
end
|
66
|
+
key_source + padded_separator + value.loc.expression.source
|
67
|
+
end
|
68
|
+
corrector.replace(range, source)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def good_alignment?
|
75
|
+
@column_deltas.values.compact.none? { |v| v != 0 }
|
76
|
+
end
|
77
|
+
|
78
|
+
def deltas(first_pair, prev_pair, current_pair, max_key_width)
|
79
|
+
enforced_style = enforced_style(current_pair)
|
80
|
+
unless %w(key separator table).include?(enforced_style)
|
81
|
+
fail "Unknown #{config_parameter(current_pair)}: #{enforced_style}"
|
82
|
+
end
|
83
|
+
|
84
|
+
return {} if current_pair.loc.line == prev_pair.loc.line
|
85
|
+
|
86
|
+
key_left_alignment_delta = (first_pair.loc.column -
|
87
|
+
current_pair.loc.column)
|
88
|
+
if enforced_style == 'key'
|
89
|
+
{ key: key_left_alignment_delta }
|
90
|
+
else
|
91
|
+
{
|
92
|
+
key: if enforced_style == 'table'
|
93
|
+
key_left_alignment_delta
|
94
|
+
else
|
95
|
+
key_end_column(first_pair) - key_end_column(current_pair)
|
96
|
+
end,
|
97
|
+
separator: if current_pair.loc.operator.is?(':') &&
|
98
|
+
enforced_style == 'table'
|
99
|
+
# Colon follows directly after key
|
100
|
+
(key_end_column(current_pair) -
|
101
|
+
current_pair.loc.operator.column)
|
102
|
+
else
|
103
|
+
# Aligned separator
|
104
|
+
(first_pair.loc.operator.column -
|
105
|
+
current_pair.loc.operator.column)
|
106
|
+
end,
|
107
|
+
value: value_delta(first_pair, current_pair, max_key_width)
|
108
|
+
}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def key_end_column(pair)
|
113
|
+
key, _value = *pair
|
114
|
+
key.loc.column + key.loc.expression.source.length
|
115
|
+
end
|
116
|
+
|
117
|
+
def value_delta(first_pair, current_pair, max_key_width)
|
118
|
+
key, value = *current_pair
|
119
|
+
correct_value_column = if enforced_style(current_pair) == 'table'
|
120
|
+
key.loc.column +
|
121
|
+
spaced_separator(current_pair).length +
|
122
|
+
max_key_width
|
123
|
+
else
|
124
|
+
_key1, value1 = *first_pair
|
125
|
+
value1.loc.column
|
126
|
+
end
|
127
|
+
correct_value_column - value.loc.column
|
128
|
+
end
|
129
|
+
|
130
|
+
def spaced_separator(node)
|
131
|
+
node.loc.operator.is?('=>') ? ' => ' : ': '
|
132
|
+
end
|
133
|
+
|
134
|
+
def enforced_style(node)
|
135
|
+
cop_config[config_parameter(node)]
|
136
|
+
end
|
137
|
+
|
138
|
+
def config_parameter(node)
|
139
|
+
separator = node.loc.operator.is?('=>') ? 'HashRocket' : 'Colon'
|
140
|
+
"Enforced#{separator}Style"
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -22,13 +22,19 @@ module Rubocop
|
|
22
22
|
|
23
23
|
if current_pos.line > prev.loc.expression.line &&
|
24
24
|
current_pos.column != first_arg_column
|
25
|
-
|
26
|
-
|
25
|
+
@column_delta = first_arg_column - current_pos.column
|
26
|
+
convention(current, current_pos)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
def autocorrect_action(node
|
31
|
+
def autocorrect_action(node)
|
32
|
+
# We can't use the instance variable inside the lambda. That would
|
33
|
+
# just give each lambda the same reference and they would all get
|
34
|
+
# the last value of @column_delta. A local variable fixes the
|
35
|
+
# problem.
|
36
|
+
column_delta = @column_delta
|
37
|
+
|
32
38
|
@corrections << lambda do |corrector|
|
33
39
|
expr = node.loc.expression
|
34
40
|
if column_delta > 0
|
@@ -24,10 +24,9 @@ module Rubocop
|
|
24
24
|
op_type = node.type.to_s
|
25
25
|
|
26
26
|
if op == op_type
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
do_autocorrect(node)
|
27
|
+
convention(node,
|
28
|
+
:operator,
|
29
|
+
sprintf(MSG, OPS[op], op))
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
@@ -10,9 +10,7 @@ module Rubocop
|
|
10
10
|
|
11
11
|
def investigate(processed_source)
|
12
12
|
processed_source.comments.each do |comment|
|
13
|
-
if comment.text =~ /[^\x00-\x7f]/
|
14
|
-
add_offence(:convention, comment.loc.expression, MSG)
|
15
|
-
end
|
13
|
+
convention(comment, :expression) if comment.text =~ /[^\x00-\x7f]/
|
16
14
|
end
|
17
15
|
end
|
18
16
|
end
|