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
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
7
|
describe Cop do
|
8
|
-
|
8
|
+
subject(:cop) { Cop.new }
|
9
9
|
let(:location) do
|
10
10
|
source_buffer = Parser::Source::Buffer.new('test', 1)
|
11
11
|
source_buffer.source = "a\n"
|
@@ -17,39 +17,21 @@ module Rubocop
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'keeps track of offences' do
|
20
|
-
cop.
|
20
|
+
cop.convention(nil, location, 'message')
|
21
21
|
|
22
22
|
expect(cop.offences.size).to eq(1)
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'will report registered offences' do
|
26
|
-
cop.
|
26
|
+
cop.convention(nil, location, 'message')
|
27
27
|
|
28
28
|
expect(cop.offences).not_to be_empty
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'registers offence with its name' do
|
32
|
-
cop = Style::
|
33
|
-
cop.
|
34
|
-
expect(cop.offences.first.cop_name).to eq('
|
35
|
-
end
|
36
|
-
|
37
|
-
describe 'description' do
|
38
|
-
let(:short_desc) { 'abc' }
|
39
|
-
let(:long_desc) { short_desc + "\n" + short_desc + 'def' }
|
40
|
-
before { Cop.config['Description'] = long_desc }
|
41
|
-
context '#full_description' do
|
42
|
-
it 'contains whole text' do
|
43
|
-
expect(Cop.full_description).to eq(long_desc)
|
44
|
-
expect(Cop.full_description.lines.to_a.size).to be > 1
|
45
|
-
end
|
46
|
-
end
|
47
|
-
context '#short_description' do
|
48
|
-
it 'contains first line' do
|
49
|
-
expect(Cop.short_description).to eq(short_desc)
|
50
|
-
expect(Cop.short_description.lines.to_a.size).to eq(1)
|
51
|
-
end
|
52
|
-
end
|
32
|
+
cop = Style::For.new
|
33
|
+
cop.convention(nil, location, 'message')
|
34
|
+
expect(cop.offences.first.cop_name).to eq('For')
|
53
35
|
end
|
54
36
|
|
55
37
|
context 'with no submodule' do
|
@@ -59,8 +41,8 @@ module Rubocop
|
|
59
41
|
end
|
60
42
|
|
61
43
|
context 'with style cops' do
|
62
|
-
subject(:cop) { Style::
|
63
|
-
it('has right name') { expect(cop.cop_name).to eq('
|
44
|
+
subject(:cop) { Style::For }
|
45
|
+
it('has right name') { expect(cop.cop_name).to eq('For') }
|
64
46
|
it('has right type') { expect(cop.cop_type).to eq(:style) }
|
65
47
|
end
|
66
48
|
|
@@ -5,11 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
7
|
module Lint
|
8
|
-
describe AssignmentInCondition do
|
9
|
-
|
10
|
-
|
11
|
-
AssignmentInCondition.config = { 'AllowSafeAssignment' => true }
|
12
|
-
end
|
8
|
+
describe AssignmentInCondition, :config do
|
9
|
+
subject(:cop) { AssignmentInCondition.new(config) }
|
10
|
+
let(:cop_config) { { 'AllowSafeAssignment' => true } }
|
13
11
|
|
14
12
|
it 'registers an offence for lvar assignment in condition' do
|
15
13
|
inspect_source(cop,
|
@@ -99,14 +97,12 @@ module Rubocop
|
|
99
97
|
end
|
100
98
|
|
101
99
|
context 'safe assignment is not allowed' do
|
102
|
-
|
103
|
-
AssignmentInCondition.config['AllowSafeAssignment'] = false
|
104
|
-
end
|
100
|
+
let(:cop_config) { { 'AllowSafeAssignment' => false } }
|
105
101
|
|
106
102
|
it 'does not accepts = in condition surrounded with braces' do
|
107
103
|
inspect_source(cop,
|
108
104
|
['if (test = 10)',
|
109
|
-
|
105
|
+
'end'
|
110
106
|
])
|
111
107
|
expect(cop.offences.size).to eq(1)
|
112
108
|
end
|
@@ -7,14 +7,15 @@ module Rubocop
|
|
7
7
|
module Cop
|
8
8
|
module Lint
|
9
9
|
describe BlockAlignment do
|
10
|
-
|
10
|
+
subject(:cop) { BlockAlignment.new }
|
11
11
|
|
12
12
|
it 'registers an offence for mismatched block end' do
|
13
13
|
inspect_source(cop,
|
14
14
|
['test do |ala|',
|
15
15
|
' end'
|
16
16
|
])
|
17
|
-
expect(cop.
|
17
|
+
expect(cop.messages)
|
18
|
+
.to eq(['end at 2, 2 is not aligned with test do |ala| at 1, 0'])
|
18
19
|
end
|
19
20
|
|
20
21
|
context 'when the block is a logical operand' do
|
@@ -33,7 +34,7 @@ module Rubocop
|
|
33
34
|
it 'accepts end aligned with a variable' do
|
34
35
|
inspect_source(cop,
|
35
36
|
['variable = test do |ala|',
|
36
|
-
|
37
|
+
'end'
|
37
38
|
])
|
38
39
|
expect(cop.offences).to be_empty
|
39
40
|
end
|
@@ -44,7 +45,8 @@ module Rubocop
|
|
44
45
|
['a = b = c = test do |ala|',
|
45
46
|
' end'
|
46
47
|
])
|
47
|
-
expect(cop.
|
48
|
+
expect(cop.messages)
|
49
|
+
.to eq(['end at 2, 4 is not aligned with a = b = c = test do |ala| at 1, 0'])
|
48
50
|
end
|
49
51
|
|
50
52
|
it 'accepts end aligned with the first variable' do
|
@@ -60,8 +62,8 @@ module Rubocop
|
|
60
62
|
it 'accepts end aligned with a variable' do
|
61
63
|
inspect_source(cop,
|
62
64
|
['b = 1 + preceding_line.reduce(0) do |a, e|',
|
63
|
-
|
64
|
-
|
65
|
+
' a + e.length + newline_length',
|
66
|
+
'end + 1'
|
65
67
|
])
|
66
68
|
expect(cop.offences).to be_empty
|
67
69
|
end
|
@@ -70,9 +72,10 @@ module Rubocop
|
|
70
72
|
it 'registers an offence for mismatched block end with a variable' do
|
71
73
|
inspect_source(cop,
|
72
74
|
['variable = test do |ala|',
|
73
|
-
|
75
|
+
' end'
|
74
76
|
])
|
75
|
-
expect(cop.
|
77
|
+
expect(cop.messages)
|
78
|
+
.to eq(['end at 2, 2 is not aligned with variable = test do |ala| at 1, 0'])
|
76
79
|
end
|
77
80
|
|
78
81
|
context 'when the block is defined on the next line' do
|
@@ -93,12 +96,15 @@ module Rubocop
|
|
93
96
|
' v.foo',
|
94
97
|
'end'
|
95
98
|
])
|
96
|
-
expect(cop.
|
99
|
+
expect(cop.messages)
|
100
|
+
.to eq(['end at 4, 0 is not aligned with a_long_method_that_dont_fit_on_the_line ' +
|
101
|
+
'do |v| at 2, 2'])
|
97
102
|
end
|
98
103
|
end
|
99
104
|
|
100
105
|
context 'when the method part is a call chain that spans several lines' do
|
101
|
-
|
106
|
+
# Example from issue 346 of bbatsov/rubocop on github:
|
107
|
+
it 'accepts pretty alignment style' do
|
102
108
|
src = ['def foo(bar)',
|
103
109
|
' bar.get_stuffs',
|
104
110
|
' .reject do |stuff| ',
|
@@ -111,16 +117,13 @@ module Rubocop
|
|
111
117
|
' end',
|
112
118
|
'end']
|
113
119
|
inspect_source(cop, src)
|
114
|
-
expect(cop.offences.
|
115
|
-
.to eq(['end at 10, 6 is not aligned with bar.get_stuffs at 2, 2',
|
116
|
-
'end at 7, 6 is not aligned with bar.get_stuffs at 2, 2',
|
117
|
-
'end at 5, 6 is not aligned with bar.get_stuffs at 2, 2'])
|
120
|
+
expect(cop.offences).to be_empty
|
118
121
|
end
|
119
122
|
|
120
123
|
it 'registers offences for misaligned ends' do
|
121
124
|
src = ['def foo(bar)',
|
122
125
|
' bar.get_stuffs',
|
123
|
-
' .reject do |stuff|
|
126
|
+
' .reject do |stuff|',
|
124
127
|
' stuff.with_a_very_long_expression_that_doesnt_fit_the_line',
|
125
128
|
' end.select do |stuff|',
|
126
129
|
' stuff.another_very_long_expression_that_doesnt_fit_the_line',
|
@@ -130,17 +133,49 @@ module Rubocop
|
|
130
133
|
' end',
|
131
134
|
'end']
|
132
135
|
inspect_source(cop, src)
|
133
|
-
expect(cop.
|
136
|
+
expect(cop.messages)
|
137
|
+
.to eq(['end at 10, 8 is not aligned with bar.get_stuffs at 2, 2 or' +
|
138
|
+
' .select do |stuff| at 8, 6',
|
139
|
+
'end at 7, 4 is not aligned with bar.get_stuffs at 2, 2 or' +
|
140
|
+
' end.select do |stuff| at 5, 8',
|
141
|
+
'end at 5, 8 is not aligned with bar.get_stuffs at 2, 2 or' +
|
142
|
+
' .reject do |stuff| at 3, 6'])
|
134
143
|
end
|
135
144
|
|
145
|
+
# Example from issue 393 of bbatsov/rubocop on github:
|
136
146
|
it 'accepts end indented as the start of the block' do
|
137
147
|
src = ['my_object.chaining_this_very_long_method(with_a_parameter)',
|
138
148
|
' .and_one_with_a_block do',
|
139
149
|
' do_something',
|
150
|
+
'end',
|
151
|
+
'', # Other variant:
|
152
|
+
'my_object.chaining_this_very_long_method(',
|
153
|
+
' with_a_parameter).and_one_with_a_block do',
|
154
|
+
' do_something',
|
140
155
|
'end']
|
141
156
|
inspect_source(cop, src)
|
142
157
|
expect(cop.offences).to be_empty
|
143
158
|
end
|
159
|
+
|
160
|
+
# Example from issue 447 of bbatsov/rubocop on github:
|
161
|
+
it 'accepts two kinds of end alignment' do
|
162
|
+
src = [
|
163
|
+
# Aligned with start of line where do is:
|
164
|
+
'params = default_options.merge(options)',
|
165
|
+
' .delete_if { |k, v| v.nil? }',
|
166
|
+
' .each_with_object({}) do |(k, v), new_hash|',
|
167
|
+
' new_hash[k.to_s] = v.to_s',
|
168
|
+
' end',
|
169
|
+
# Aligned with start of the whole expression:
|
170
|
+
'params = default_options.merge(options)',
|
171
|
+
' .delete_if { |k, v| v.nil? }',
|
172
|
+
' .each_with_object({}) do |(k, v), new_hash|',
|
173
|
+
' new_hash[k.to_s] = v.to_s',
|
174
|
+
'end'
|
175
|
+
]
|
176
|
+
inspect_source(cop, src)
|
177
|
+
expect(cop.offences).to be_empty
|
178
|
+
end
|
144
179
|
end
|
145
180
|
|
146
181
|
context 'when variables of a mass assignment spans several lines' do
|
@@ -150,7 +185,7 @@ module Rubocop
|
|
150
185
|
' i - 5',
|
151
186
|
'end']
|
152
187
|
inspect_source(cop, src)
|
153
|
-
expect(cop.offences
|
188
|
+
expect(cop.offences).to be_empty
|
154
189
|
end
|
155
190
|
|
156
191
|
it 'registers an offence for end aligned with the block' do
|
@@ -159,14 +194,15 @@ module Rubocop
|
|
159
194
|
' i - 5',
|
160
195
|
' end']
|
161
196
|
inspect_source(cop, src)
|
162
|
-
expect(cop.
|
197
|
+
expect(cop.messages)
|
198
|
+
.to eq(['end at 4, 4 is not aligned with e, at 1, 0 or f = [5, 6].map do |i| at 2, 0'])
|
163
199
|
end
|
164
200
|
end
|
165
201
|
|
166
202
|
it 'accepts end aligned with an instance variable' do
|
167
203
|
inspect_source(cop,
|
168
204
|
['@variable = test do |ala|',
|
169
|
-
|
205
|
+
'end'
|
170
206
|
])
|
171
207
|
expect(cop.offences).to be_empty
|
172
208
|
end
|
@@ -174,15 +210,16 @@ module Rubocop
|
|
174
210
|
it 'registers an offence for mismatched block end with an instance variable' do
|
175
211
|
inspect_source(cop,
|
176
212
|
['@variable = test do |ala|',
|
177
|
-
|
213
|
+
' end'
|
178
214
|
])
|
179
|
-
expect(cop.
|
215
|
+
expect(cop.messages)
|
216
|
+
.to eq(['end at 2, 2 is not aligned with @variable = test do |ala| at 1, 0'])
|
180
217
|
end
|
181
218
|
|
182
219
|
it 'accepts end aligned with a class variable' do
|
183
220
|
inspect_source(cop,
|
184
221
|
['@@variable = test do |ala|',
|
185
|
-
|
222
|
+
'end'
|
186
223
|
])
|
187
224
|
expect(cop.offences).to be_empty
|
188
225
|
end
|
@@ -190,15 +227,16 @@ module Rubocop
|
|
190
227
|
it 'registers an offence for mismatched block end with a class variable' do
|
191
228
|
inspect_source(cop,
|
192
229
|
['@@variable = test do |ala|',
|
193
|
-
|
230
|
+
' end'
|
194
231
|
])
|
195
|
-
expect(cop.
|
232
|
+
expect(cop.messages)
|
233
|
+
.to eq(['end at 2, 2 is not aligned with @@variable = test do |ala| at 1, 0'])
|
196
234
|
end
|
197
235
|
|
198
236
|
it 'accepts end aligned with a global variable' do
|
199
237
|
inspect_source(cop,
|
200
238
|
['$variable = test do |ala|',
|
201
|
-
|
239
|
+
'end'
|
202
240
|
])
|
203
241
|
expect(cop.offences).to be_empty
|
204
242
|
end
|
@@ -206,15 +244,16 @@ module Rubocop
|
|
206
244
|
it 'registers an offence for mismatched block end with a global variable' do
|
207
245
|
inspect_source(cop,
|
208
246
|
['$variable = test do |ala|',
|
209
|
-
|
247
|
+
' end'
|
210
248
|
])
|
211
|
-
expect(cop.
|
249
|
+
expect(cop.messages)
|
250
|
+
.to eq(['end at 2, 2 is not aligned with $variable = test do |ala| at 1, 0'])
|
212
251
|
end
|
213
252
|
|
214
253
|
it 'accepts end aligned with a constant' do
|
215
254
|
inspect_source(cop,
|
216
255
|
['CONSTANT = test do |ala|',
|
217
|
-
|
256
|
+
'end'
|
218
257
|
])
|
219
258
|
expect(cop.offences).to be_empty
|
220
259
|
end
|
@@ -222,16 +261,17 @@ module Rubocop
|
|
222
261
|
it 'registers an offence for mismatched block end with a constant' do
|
223
262
|
inspect_source(cop,
|
224
263
|
['Module::CONSTANT = test do |ala|',
|
225
|
-
|
264
|
+
' end'
|
226
265
|
])
|
227
|
-
expect(cop.
|
266
|
+
expect(cop.messages)
|
267
|
+
.to eq(['end at 2, 2 is not aligned with Module::CONSTANT = test do |ala| at 1, 0'])
|
228
268
|
end
|
229
269
|
|
230
270
|
it 'accepts end aligned with a method call' do
|
231
271
|
inspect_source(cop,
|
232
272
|
['parser.childs << lambda do |token|',
|
233
|
-
|
234
|
-
|
273
|
+
' token << 1',
|
274
|
+
'end'
|
235
275
|
])
|
236
276
|
expect(cop.offences).to be_empty
|
237
277
|
end
|
@@ -239,17 +279,18 @@ module Rubocop
|
|
239
279
|
it 'registers an offence for mismatched block end with a method call' do
|
240
280
|
inspect_source(cop,
|
241
281
|
['parser.childs << lambda do |token|',
|
242
|
-
|
243
|
-
|
282
|
+
' token << 1',
|
283
|
+
' end'
|
244
284
|
])
|
245
|
-
expect(cop.
|
285
|
+
expect(cop.messages)
|
286
|
+
.to eq(['end at 3, 2 is not aligned with parser.childs << lambda do |token| at 1, 0'])
|
246
287
|
end
|
247
288
|
|
248
289
|
it 'accepts end aligned with a method call with arguments' do
|
249
290
|
inspect_source(cop,
|
250
291
|
['@h[:f] = f.each_pair.map do |f, v|',
|
251
|
-
|
252
|
-
|
292
|
+
' v = 1',
|
293
|
+
'end'
|
253
294
|
])
|
254
295
|
expect(cop.offences).to be_empty
|
255
296
|
end
|
@@ -257,10 +298,11 @@ module Rubocop
|
|
257
298
|
it 'registers an offence for mismatched end with a method call with arguments' do
|
258
299
|
inspect_source(cop,
|
259
300
|
['@h[:f] = f.each_pair.map do |f, v|',
|
260
|
-
|
261
|
-
|
301
|
+
' v = 1',
|
302
|
+
' end'
|
262
303
|
])
|
263
|
-
expect(cop.
|
304
|
+
expect(cop.messages)
|
305
|
+
.to eq(['end at 3, 2 is not aligned with @h[:f] = f.each_pair.map do |f, v| at 1, 0'])
|
264
306
|
end
|
265
307
|
|
266
308
|
it 'does not raise an error for nested block in a method call' do
|
@@ -285,14 +327,16 @@ module Rubocop
|
|
285
327
|
' o.valid?',
|
286
328
|
' end)'
|
287
329
|
])
|
288
|
-
expect(cop.
|
330
|
+
expect(cop.messages)
|
331
|
+
.to eq(['end at 3, 2 is not aligned with arr.all? do |o| at 1, 7 or ' +
|
332
|
+
'expect(arr.all? do |o| at 1, 0'])
|
289
333
|
end
|
290
334
|
|
291
335
|
it 'accepts end aligned with an op-asgn (+=, -=)' do
|
292
336
|
inspect_source(cop,
|
293
337
|
['rb += files.select do |file|',
|
294
|
-
|
295
|
-
|
338
|
+
' file << something',
|
339
|
+
'end'
|
296
340
|
])
|
297
341
|
expect(cop.offences).to be_empty
|
298
342
|
end
|
@@ -300,16 +344,17 @@ module Rubocop
|
|
300
344
|
it 'registers an offence for mismatched block end with an op-asgn (+=, -=)' do
|
301
345
|
inspect_source(cop,
|
302
346
|
['rb += files.select do |file|',
|
303
|
-
|
304
|
-
|
347
|
+
' file << something',
|
348
|
+
' end'
|
305
349
|
])
|
306
|
-
expect(cop.
|
350
|
+
expect(cop.messages)
|
351
|
+
.to eq(['end at 3, 2 is not aligned with rb at 1, 0'])
|
307
352
|
end
|
308
353
|
|
309
354
|
it 'accepts end aligned with an and-asgn (&&=)' do
|
310
355
|
inspect_source(cop,
|
311
356
|
['variable &&= test do |ala|',
|
312
|
-
|
357
|
+
'end'
|
313
358
|
])
|
314
359
|
expect(cop.offences).to be_empty
|
315
360
|
end
|
@@ -317,15 +362,16 @@ module Rubocop
|
|
317
362
|
it 'registers an offence for mismatched block end with an and-asgn (&&=)' do
|
318
363
|
inspect_source(cop,
|
319
364
|
['variable &&= test do |ala|',
|
320
|
-
|
365
|
+
' end'
|
321
366
|
])
|
322
|
-
expect(cop.
|
367
|
+
expect(cop.messages)
|
368
|
+
.to eq(['end at 2, 2 is not aligned with variable &&= test do |ala| at 1, 0'])
|
323
369
|
end
|
324
370
|
|
325
371
|
it 'accepts end aligned with an or-asgn (||=)' do
|
326
372
|
inspect_source(cop,
|
327
373
|
['variable ||= test do |ala|',
|
328
|
-
|
374
|
+
'end'
|
329
375
|
])
|
330
376
|
expect(cop.offences).to be_empty
|
331
377
|
end
|
@@ -333,16 +379,17 @@ module Rubocop
|
|
333
379
|
it 'registers an offence for mismatched block end with an or-asgn (||=)' do
|
334
380
|
inspect_source(cop,
|
335
381
|
['variable ||= test do |ala|',
|
336
|
-
|
382
|
+
' end'
|
337
383
|
])
|
338
|
-
expect(cop.
|
384
|
+
expect(cop.messages)
|
385
|
+
.to eq(['end at 2, 2 is not aligned with variable ||= test do |ala| at 1, 0'])
|
339
386
|
end
|
340
387
|
|
341
388
|
it 'accepts end aligned with a mass assignment' do
|
342
389
|
inspect_source(cop,
|
343
390
|
['var1, var2 = lambda do |test|',
|
344
|
-
|
345
|
-
|
391
|
+
' [1, 2]',
|
392
|
+
'end'
|
346
393
|
])
|
347
394
|
expect(cop.offences).to be_empty
|
348
395
|
end
|
@@ -358,10 +405,11 @@ module Rubocop
|
|
358
405
|
it 'registers an offence for mismatched block end with a mass assignment' do
|
359
406
|
inspect_source(cop,
|
360
407
|
['var1, var2 = lambda do |test|',
|
361
|
-
|
362
|
-
|
408
|
+
' [1, 2]',
|
409
|
+
' end'
|
363
410
|
])
|
364
|
-
expect(cop.
|
411
|
+
expect(cop.messages)
|
412
|
+
.to eq(['end at 3, 2 is not aligned with var1, var2 at 1, 0'])
|
365
413
|
end
|
366
414
|
end
|
367
415
|
end
|