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,101 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Lint
|
8
|
+
describe UselessSetterCall do
|
9
|
+
subject(:cop) { UselessSetterCall.new }
|
10
|
+
|
11
|
+
it 'registers an offence for def ending with lvar attr assignment' do
|
12
|
+
inspect_source(cop,
|
13
|
+
['def test',
|
14
|
+
' top = Top.new',
|
15
|
+
' top.attr = 5',
|
16
|
+
'end'
|
17
|
+
])
|
18
|
+
expect(cop.offences.size).to eq(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'registers an offence for defs ending with lvar attr assignment' do
|
22
|
+
inspect_source(cop,
|
23
|
+
['def Top.test',
|
24
|
+
' top = Top.new',
|
25
|
+
' top.attr = 5',
|
26
|
+
'end'
|
27
|
+
])
|
28
|
+
expect(cop.offences.size).to eq(1)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'accepts def ending with ivar assignment' do
|
32
|
+
inspect_source(cop,
|
33
|
+
['def test',
|
34
|
+
' something',
|
35
|
+
' @top = 5',
|
36
|
+
'end'
|
37
|
+
])
|
38
|
+
expect(cop.offences).to be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'accepts def ending ivar attr assignment' do
|
42
|
+
inspect_source(cop,
|
43
|
+
['def test',
|
44
|
+
' something',
|
45
|
+
' @top.attr = 5',
|
46
|
+
'end'
|
47
|
+
])
|
48
|
+
expect(cop.offences).to be_empty
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'accepts def ending with argument attr assignment' do
|
52
|
+
inspect_source(cop,
|
53
|
+
['def test(some_arg)',
|
54
|
+
' unrelated_local_variable = Top.new',
|
55
|
+
' some_arg.attr = 5',
|
56
|
+
'end'
|
57
|
+
])
|
58
|
+
expect(cop.offences).to be_empty
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when a lvar has an object passed as argument ' +
|
62
|
+
'at the end of the method' do
|
63
|
+
it 'accepts the lvar attr assignment' do
|
64
|
+
inspect_source(cop,
|
65
|
+
['def test(some_arg)',
|
66
|
+
' @some_ivar = some_arg',
|
67
|
+
' @some_ivar.do_something',
|
68
|
+
' some_lvar = @some_ivar',
|
69
|
+
' some_lvar.do_something',
|
70
|
+
' some_lvar.attr = 5',
|
71
|
+
'end'
|
72
|
+
])
|
73
|
+
expect(cop.offences).to be_empty
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'when a lvar declared as an argument ' +
|
78
|
+
'is no longer the passed object at the end of the method' do
|
79
|
+
it 'registers an offence for the lvar attr assignment' do
|
80
|
+
inspect_source(cop,
|
81
|
+
['def test(some_arg)',
|
82
|
+
' some_arg = Top.new',
|
83
|
+
' some_arg.attr = 5',
|
84
|
+
'end'
|
85
|
+
])
|
86
|
+
expect(cop.offences.size).to eq(1)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'is not confused by operators ending with =' do
|
91
|
+
inspect_source(cop,
|
92
|
+
['def test',
|
93
|
+
' top.attr == 5',
|
94
|
+
'end'
|
95
|
+
])
|
96
|
+
expect(cop.offences).to be_empty
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -110,11 +110,11 @@ module Rubocop
|
|
110
110
|
describe '#<=>' do
|
111
111
|
def offence(hash = {})
|
112
112
|
attrs = {
|
113
|
-
|
113
|
+
sev: :convention,
|
114
114
|
line: 5,
|
115
|
-
|
116
|
-
|
117
|
-
|
115
|
+
col: 5,
|
116
|
+
mes: 'message',
|
117
|
+
cop: 'CopName'
|
118
118
|
}.merge(hash)
|
119
119
|
|
120
120
|
Offence.new(
|
@@ -6,18 +6,18 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe AccessControl do
|
9
|
-
|
9
|
+
subject(:a) { AccessControl.new }
|
10
10
|
|
11
11
|
it 'registers an offence for misaligned private' do
|
12
12
|
inspect_source(a,
|
13
13
|
['class Test',
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
'',
|
15
|
+
'private',
|
16
|
+
'',
|
17
|
+
' def test; end',
|
18
|
+
'end'])
|
19
19
|
expect(a.offences.size).to eq(1)
|
20
|
-
expect(a.
|
20
|
+
expect(a.messages)
|
21
21
|
.to eq([format(AccessControl::INDENT_MSG, 'private')])
|
22
22
|
end
|
23
23
|
|
@@ -30,20 +30,20 @@ module Rubocop
|
|
30
30
|
' def test; end',
|
31
31
|
'end'])
|
32
32
|
expect(a.offences.size).to eq(1)
|
33
|
-
expect(a.
|
33
|
+
expect(a.messages)
|
34
34
|
.to eq([format(AccessControl::INDENT_MSG, 'private')])
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'registers an offence for misaligned private in singleton class' do
|
38
38
|
inspect_source(a,
|
39
39
|
['class << self',
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
'',
|
41
|
+
'private',
|
42
|
+
'',
|
43
|
+
' def test; end',
|
44
|
+
'end'])
|
45
45
|
expect(a.offences.size).to eq(1)
|
46
|
-
expect(a.
|
46
|
+
expect(a.messages)
|
47
47
|
.to eq([format(AccessControl::INDENT_MSG, 'private')])
|
48
48
|
end
|
49
49
|
|
@@ -57,7 +57,7 @@ module Rubocop
|
|
57
57
|
' def test; end',
|
58
58
|
'end'])
|
59
59
|
expect(a.offences.size).to eq(1)
|
60
|
-
expect(a.
|
60
|
+
expect(a.messages)
|
61
61
|
.to eq([format(AccessControl::INDENT_MSG, 'private')])
|
62
62
|
end
|
63
63
|
|
@@ -71,7 +71,7 @@ module Rubocop
|
|
71
71
|
' def test; end',
|
72
72
|
'end'])
|
73
73
|
expect(a.offences.size).to eq(1)
|
74
|
-
expect(a.
|
74
|
+
expect(a.messages)
|
75
75
|
.to eq([format(AccessControl::INDENT_MSG, 'private')])
|
76
76
|
end
|
77
77
|
|
@@ -84,7 +84,7 @@ module Rubocop
|
|
84
84
|
' def test; end',
|
85
85
|
'end'])
|
86
86
|
expect(a.offences.size).to eq(1)
|
87
|
-
expect(a.
|
87
|
+
expect(a.messages)
|
88
88
|
.to eq([format(AccessControl::INDENT_MSG, 'protected')])
|
89
89
|
end
|
90
90
|
|
@@ -126,7 +126,7 @@ module Rubocop
|
|
126
126
|
' def test; end',
|
127
127
|
'end'])
|
128
128
|
expect(a.offences.size).to eq(1)
|
129
|
-
expect(a.
|
129
|
+
expect(a.messages)
|
130
130
|
.to eq([format(AccessControl::INDENT_MSG, 'private')])
|
131
131
|
end
|
132
132
|
|
@@ -138,7 +138,7 @@ module Rubocop
|
|
138
138
|
' def test; end',
|
139
139
|
'end'])
|
140
140
|
expect(a.offences.size).to eq(1)
|
141
|
-
expect(a.
|
141
|
+
expect(a.messages)
|
142
142
|
.to eq([format(AccessControl::BLANK_MSG, 'protected')])
|
143
143
|
end
|
144
144
|
|
@@ -150,7 +150,7 @@ module Rubocop
|
|
150
150
|
' def test; end',
|
151
151
|
'end'])
|
152
152
|
expect(a.offences.size).to eq(1)
|
153
|
-
expect(a.
|
153
|
+
expect(a.messages)
|
154
154
|
.to eq([format(AccessControl::BLANK_MSG, 'protected')])
|
155
155
|
end
|
156
156
|
|
@@ -6,13 +6,13 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe Alias do
|
9
|
-
|
9
|
+
subject(:a) { Alias.new }
|
10
10
|
|
11
11
|
it 'registers an offence for alias with symbol args' do
|
12
12
|
inspect_source(a,
|
13
13
|
['alias :ala :bala'])
|
14
14
|
expect(a.offences.size).to eq(1)
|
15
|
-
expect(a.
|
15
|
+
expect(a.messages)
|
16
16
|
.to eq([Alias::MSG])
|
17
17
|
end
|
18
18
|
|
@@ -20,7 +20,7 @@ module Rubocop
|
|
20
20
|
inspect_source(a,
|
21
21
|
['alias ala bala'])
|
22
22
|
expect(a.offences.size).to eq(1)
|
23
|
-
expect(a.
|
23
|
+
expect(a.messages)
|
24
24
|
.to eq([Alias::MSG])
|
25
25
|
end
|
26
26
|
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Style
|
8
|
+
describe AlignArray do
|
9
|
+
subject(:cop) { described_class.new }
|
10
|
+
|
11
|
+
it 'registers an offence for misaligned array elements' do
|
12
|
+
inspect_source(cop, ['array = [',
|
13
|
+
' a,',
|
14
|
+
' b,',
|
15
|
+
' c,',
|
16
|
+
' d',
|
17
|
+
']'])
|
18
|
+
expect(cop.messages).to eq(['Align the elements of an array ' +
|
19
|
+
'literal if they span more than ' +
|
20
|
+
'one line.'] * 2)
|
21
|
+
expect(cop.highlights).to eq(%w(b d))
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'accepts aligned array keys' do
|
25
|
+
inspect_source(cop, ['array = [',
|
26
|
+
' a,',
|
27
|
+
' b,',
|
28
|
+
' c,',
|
29
|
+
' d',
|
30
|
+
']'])
|
31
|
+
expect(cop.offences).to be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'accepts single line array' do
|
35
|
+
inspect_source(cop, 'array = [ a, b ]')
|
36
|
+
expect(cop.offences).to be_empty
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'accepts several elements per line' do
|
40
|
+
inspect_source(cop, ['array = [ a, b,',
|
41
|
+
' c, d ]'])
|
42
|
+
expect(cop.offences).to be_empty
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'auto-corrects alignment' do
|
46
|
+
new_source = autocorrect_source(cop, ['array = [',
|
47
|
+
' a,',
|
48
|
+
' b,',
|
49
|
+
' c,',
|
50
|
+
' d',
|
51
|
+
']'])
|
52
|
+
expect(new_source).to eq(['array = [',
|
53
|
+
' a,',
|
54
|
+
' b,',
|
55
|
+
' c,',
|
56
|
+
' d',
|
57
|
+
']'].join("\n"))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,267 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Style
|
8
|
+
describe AlignHash, :config do
|
9
|
+
subject(:cop) { described_class.new(config) }
|
10
|
+
let(:cop_config) do
|
11
|
+
{
|
12
|
+
'EnforcedHashRocketStyle' => 'key',
|
13
|
+
'EnforcedColonStyle' => 'key'
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with default configuration' do
|
18
|
+
it 'registers an offence for misaligned hash keys' do
|
19
|
+
inspect_source(cop, ['hash1 = {',
|
20
|
+
' a: 0,',
|
21
|
+
' bb: 1',
|
22
|
+
'}',
|
23
|
+
'hash2 = {',
|
24
|
+
" 'ccc' => 2,",
|
25
|
+
" 'dddd' => 2",
|
26
|
+
'}'])
|
27
|
+
expect(cop.messages).to eq(['Align the elements of a hash ' +
|
28
|
+
'literal if they span more than ' +
|
29
|
+
'one line.'] * 2)
|
30
|
+
expect(cop.highlights).to eq(['bb: 1',
|
31
|
+
"'dddd' => 2"])
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'accepts aligned hash keys' do
|
35
|
+
inspect_source(cop, ['hash1 = {',
|
36
|
+
' a: 0,',
|
37
|
+
' bb: 1,',
|
38
|
+
'}',
|
39
|
+
'hash2 = {',
|
40
|
+
" 'ccc' => 2,",
|
41
|
+
" 'dddd' => 2",
|
42
|
+
'}'])
|
43
|
+
expect(cop.offences).to be_empty
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'registers an offence for separator alignment' do
|
47
|
+
inspect_source(cop, ['hash = {',
|
48
|
+
" 'a' => 0,",
|
49
|
+
" 'bbb' => 1",
|
50
|
+
'}'])
|
51
|
+
expect(cop.offences).to have(1).item
|
52
|
+
expect(cop.highlights).to eq(["'bbb' => 1"])
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'with braceless hash as last argument' do
|
56
|
+
it 'registers an offence for misaligned hash keys' do
|
57
|
+
inspect_source(cop, ['func(a: 0,',
|
58
|
+
' b: 1)'])
|
59
|
+
expect(cop.offences).to have(1).item
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'registers an offence for right alignment of keys' do
|
63
|
+
inspect_source(cop, ['func(a: 0,',
|
64
|
+
' bbb: 1)'])
|
65
|
+
expect(cop.offences).to have(1).item
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'accepts aligned hash keys' do
|
69
|
+
inspect_source(cop, ['func(a: 0,',
|
70
|
+
' b: 1)'])
|
71
|
+
expect(cop.offences).to be_empty
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'auto-corrects alignment' do
|
76
|
+
new_source = autocorrect_source(cop, ['hash1 = { a: 0,',
|
77
|
+
' bb: 1,',
|
78
|
+
' ccc: 2 }',
|
79
|
+
'hash2 = { :a => 0,',
|
80
|
+
' :bb => 1,',
|
81
|
+
' :ccc =>2 }',
|
82
|
+
])
|
83
|
+
expect(new_source).to eq(['hash1 = { a: 0,',
|
84
|
+
' bb: 1,',
|
85
|
+
' ccc: 2 }',
|
86
|
+
'hash2 = { :a => 0,',
|
87
|
+
' :bb => 1,',
|
88
|
+
# Separator and value are not corrected
|
89
|
+
# in 'key' mode.
|
90
|
+
' :ccc =>2 }'].join("\n"))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'accepts single line hash' do
|
95
|
+
inspect_source(cop, 'hash = { a: 0, b: 1 }')
|
96
|
+
expect(cop.offences).to be_empty
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'accepts several pairs per line' do
|
100
|
+
inspect_source(cop, ['hash = { a: 0, b: 1,',
|
101
|
+
' c: 2, d: 3 }'])
|
102
|
+
expect(cop.offences).to be_empty
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'with table alignment configuration' do
|
106
|
+
let(:cop_config) do
|
107
|
+
{
|
108
|
+
'EnforcedHashRocketStyle' => 'table',
|
109
|
+
'EnforcedColonStyle' => 'table'
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'accepts aligned hash keys' do
|
114
|
+
inspect_source(cop, ['hash1 = {',
|
115
|
+
" 'a' => 0,",
|
116
|
+
" 'bbb' => 1",
|
117
|
+
'}',
|
118
|
+
'hash2 = {',
|
119
|
+
' a: 0,',
|
120
|
+
' bbb: 1',
|
121
|
+
'}',
|
122
|
+
])
|
123
|
+
expect(cop.offences).to be_empty
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'registers an offence for misaligned hash values' do
|
127
|
+
inspect_source(cop, ['hash1 = {',
|
128
|
+
" 'a' => 0,",
|
129
|
+
" 'bbb' => 1",
|
130
|
+
'}',
|
131
|
+
'hash2 = {',
|
132
|
+
' a: 0,',
|
133
|
+
' bbb:1',
|
134
|
+
'}',
|
135
|
+
])
|
136
|
+
expect(cop.highlights).to eq(["'a' => 0",
|
137
|
+
'bbb:1'])
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'registers an offence for misaligned hash rockets' do
|
141
|
+
inspect_source(cop, ['hash = {',
|
142
|
+
" 'a' => 0,",
|
143
|
+
" 'bbb' => 1",
|
144
|
+
'}'])
|
145
|
+
expect(cop.offences).to have(1).item
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'auto-corrects alignment' do
|
149
|
+
new_source = autocorrect_source(cop, ['hash1 = { a: 0,',
|
150
|
+
' bb: 1,',
|
151
|
+
' ccc: 2 }',
|
152
|
+
"hash2 = { 'a' => 0,",
|
153
|
+
" 'bb' => 1,",
|
154
|
+
" 'ccc' =>2 }"])
|
155
|
+
expect(new_source).to eq(['hash1 = { a: 0,',
|
156
|
+
' bb: 1,',
|
157
|
+
' ccc: 2 }',
|
158
|
+
"hash2 = { 'a' => 0,",
|
159
|
+
" 'bb' => 1,",
|
160
|
+
" 'ccc' => 2 }"].join("\n"))
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context 'with invalid configuration' do
|
165
|
+
let(:cop_config) do
|
166
|
+
{
|
167
|
+
'EnforcedHashRocketStyle' => 'junk',
|
168
|
+
'EnforcedColonStyle' => 'junk'
|
169
|
+
}
|
170
|
+
end
|
171
|
+
it 'fails' do
|
172
|
+
src = ['hash = {',
|
173
|
+
' a: 0,',
|
174
|
+
' bb: 1',
|
175
|
+
'}']
|
176
|
+
expect { inspect_source(cop, src) }.to raise_error(RuntimeError)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'with separator alignment configuration' do
|
181
|
+
let(:cop_config) do
|
182
|
+
{
|
183
|
+
'EnforcedHashRocketStyle' => 'separator',
|
184
|
+
'EnforcedColonStyle' => 'separator'
|
185
|
+
}
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'accepts aligned hash keys' do
|
189
|
+
inspect_source(cop, ['hash1 = {',
|
190
|
+
' a: 0,',
|
191
|
+
' bbb: 1',
|
192
|
+
'}',
|
193
|
+
'hash2 = {',
|
194
|
+
" 'a' => 0,",
|
195
|
+
" 'bbb' => 1",
|
196
|
+
'}'])
|
197
|
+
expect(cop.offences).to be_empty
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'registers an offence for misaligned hash values' do
|
201
|
+
inspect_source(cop, ['hash = {',
|
202
|
+
" 'a' => 0,",
|
203
|
+
" 'bbb' => 1",
|
204
|
+
'}'])
|
205
|
+
expect(cop.offences).to have(1).item
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'registers an offence for misaligned hash rockets' do
|
209
|
+
inspect_source(cop, ['hash = {',
|
210
|
+
" 'a' => 0,",
|
211
|
+
" 'bbb' => 1",
|
212
|
+
'}'])
|
213
|
+
expect(cop.offences).to have(1).item
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'auto-corrects alignment' do
|
217
|
+
new_source = autocorrect_source(cop, ['hash1 = { a: 0,',
|
218
|
+
' bb: 1,',
|
219
|
+
' ccc: 2 }',
|
220
|
+
'hash2 = { a => 0,',
|
221
|
+
' bb => 1,',
|
222
|
+
' ccc =>2 }'])
|
223
|
+
expect(new_source).to eq(['hash1 = { a: 0,',
|
224
|
+
' bb: 1,',
|
225
|
+
' ccc: 2 }',
|
226
|
+
'hash2 = { a => 0,',
|
227
|
+
' bb => 1,',
|
228
|
+
' ccc => 2 }'].join("\n"))
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
context 'with different settings for => and :' do
|
233
|
+
let(:cop_config) do
|
234
|
+
{
|
235
|
+
'EnforcedHashRocketStyle' => 'key',
|
236
|
+
'EnforcedColonStyle' => 'separator'
|
237
|
+
}
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'registers offences for misaligned entries' do
|
241
|
+
inspect_source(cop, ['hash1 = {',
|
242
|
+
' a: 0,',
|
243
|
+
' bbb: 1',
|
244
|
+
'}',
|
245
|
+
'hash2 = {',
|
246
|
+
" 'a' => 0,",
|
247
|
+
" 'bbb' => 1",
|
248
|
+
'}'])
|
249
|
+
expect(cop.highlights).to eq(['bbb: 1', "'bbb' => 1"])
|
250
|
+
end
|
251
|
+
|
252
|
+
it 'accepts aligned entries' do
|
253
|
+
inspect_source(cop, ['hash1 = {',
|
254
|
+
' a: 0,',
|
255
|
+
' bbb: 1',
|
256
|
+
'}',
|
257
|
+
'hash2 = {',
|
258
|
+
" 'a' => 0,",
|
259
|
+
" 'bbb' => 1",
|
260
|
+
'}'])
|
261
|
+
expect(cop.offences).to be_empty
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|