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,11 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
|
-
describe TrivialAccessors do
|
9
|
-
subject(:cop) { described_class.new }
|
10
|
-
|
11
|
-
described_class.config = {}
|
12
|
-
end
|
8
|
+
describe TrivialAccessors, :config do
|
9
|
+
subject(:cop) { described_class.new(config) }
|
10
|
+
let(:cop_config) { {} }
|
13
11
|
|
14
12
|
it 'finds trivial reader' do
|
15
13
|
inspect_source(cop,
|
@@ -348,7 +346,7 @@ module Rubocop
|
|
348
346
|
end
|
349
347
|
|
350
348
|
context 'exact name match required' do
|
351
|
-
|
349
|
+
let(:cop_config) { { 'ExactNameMatch' => true } }
|
352
350
|
|
353
351
|
it 'finds only 1 trivial reader' do
|
354
352
|
inspect_source(cop,
|
@@ -380,9 +378,7 @@ module Rubocop
|
|
380
378
|
end
|
381
379
|
|
382
380
|
context 'with predicates allowed' do
|
383
|
-
|
384
|
-
described_class.config['AllowPredicates'] = true
|
385
|
-
end
|
381
|
+
let(:cop_config) { { 'AllowPredicates' => true } }
|
386
382
|
|
387
383
|
it 'ignores accessors ending with a question mark' do
|
388
384
|
inspect_source(cop,
|
@@ -394,11 +390,7 @@ module Rubocop
|
|
394
390
|
end
|
395
391
|
|
396
392
|
context 'with whitelist defined' do
|
397
|
-
|
398
|
-
described_class.config = {
|
399
|
-
'Whitelist' => ['to_foo', 'bar=']
|
400
|
-
}
|
401
|
-
end
|
393
|
+
let(:cop_config) { { 'Whitelist' => ['to_foo', 'bar='] } }
|
402
394
|
|
403
395
|
it 'ignores accessors in the whitelist' do
|
404
396
|
inspect_source(cop,
|
@@ -6,7 +6,7 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe UnlessElse do
|
9
|
-
|
9
|
+
subject(:ue) { UnlessElse.new }
|
10
10
|
|
11
11
|
it 'registers an offence for an unless with else' do
|
12
12
|
inspect_source(ue, ['unless x',
|
@@ -14,7 +14,7 @@ module Rubocop
|
|
14
14
|
'else',
|
15
15
|
' a = 0',
|
16
16
|
'end'])
|
17
|
-
expect(ue.
|
17
|
+
expect(ue.messages).to eq(
|
18
18
|
['Never use unless with else. Rewrite these with the ' +
|
19
19
|
'positive case first.'])
|
20
20
|
end
|
@@ -23,7 +23,7 @@ module Rubocop
|
|
23
23
|
inspect_source(ue, ['unless x',
|
24
24
|
' a = 1',
|
25
25
|
'end'])
|
26
|
-
expect(ue.
|
26
|
+
expect(ue.messages).to be_empty
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -6,13 +6,13 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe VariableInterpolation do
|
9
|
-
|
9
|
+
subject(:vi) { VariableInterpolation.new }
|
10
10
|
|
11
11
|
it 'registers an offence for interpolated global variables' do
|
12
12
|
inspect_source(vi,
|
13
13
|
['puts "this is a #$test"'])
|
14
14
|
expect(vi.offences.size).to eq(1)
|
15
|
-
expect(vi.
|
15
|
+
expect(vi.messages)
|
16
16
|
.to eq(['Replace interpolated var $test' +
|
17
17
|
' with expression #{$test}.'])
|
18
18
|
end
|
@@ -21,7 +21,7 @@ module Rubocop
|
|
21
21
|
inspect_source(vi,
|
22
22
|
['puts "this is a #$1"'])
|
23
23
|
expect(vi.offences.size).to eq(1)
|
24
|
-
expect(vi.
|
24
|
+
expect(vi.messages)
|
25
25
|
.to eq(['Replace interpolated var $1 with expression #{$1}.'])
|
26
26
|
end
|
27
27
|
|
@@ -29,7 +29,7 @@ module Rubocop
|
|
29
29
|
inspect_source(vi,
|
30
30
|
['puts "this is a #@test"'])
|
31
31
|
expect(vi.offences.size).to eq(1)
|
32
|
-
expect(vi.
|
32
|
+
expect(vi.messages)
|
33
33
|
.to eq(['Replace interpolated var @test' +
|
34
34
|
' with expression #{@test}.'])
|
35
35
|
end
|
@@ -38,7 +38,7 @@ module Rubocop
|
|
38
38
|
inspect_source(vi,
|
39
39
|
['puts "this is a #@@t"'])
|
40
40
|
expect(vi.offences.size).to eq(1)
|
41
|
-
expect(vi.
|
41
|
+
expect(vi.messages)
|
42
42
|
.to eq(['Replace interpolated var @@t with expression #{@@t}.'])
|
43
43
|
end
|
44
44
|
|
@@ -6,33 +6,33 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe WhenThen do
|
9
|
-
|
9
|
+
subject(:cop) { WhenThen.new }
|
10
10
|
|
11
11
|
it 'registers an offence for when x;' do
|
12
12
|
inspect_source(cop, ['case a',
|
13
|
-
|
14
|
-
|
15
|
-
expect(cop.
|
13
|
+
'when b; c',
|
14
|
+
'end'])
|
15
|
+
expect(cop.messages).to eq(
|
16
16
|
['Never use "when x;". Use "when x then" instead.'])
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'accepts when x then' do
|
20
20
|
inspect_source(cop, ['case a',
|
21
|
-
|
22
|
-
|
23
|
-
expect(cop.
|
21
|
+
'when b then c',
|
22
|
+
'end'])
|
23
|
+
expect(cop.messages).to be_empty
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'accepts ; separating statements in the body of when' do
|
27
27
|
inspect_source(cop, ['case a',
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
expect(cop.
|
28
|
+
'when b then c; d',
|
29
|
+
'end',
|
30
|
+
'',
|
31
|
+
'case e',
|
32
|
+
'when f',
|
33
|
+
' g; h',
|
34
|
+
'end'])
|
35
|
+
expect(cop.messages).to be_empty
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'auto-corrects "when x;" with "when x then"' do
|
@@ -6,7 +6,7 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe WhileUntilDo do
|
9
|
-
|
9
|
+
subject(:cop) { WhileUntilDo.new }
|
10
10
|
|
11
11
|
it 'registers an offence for do in multiline while' do
|
12
12
|
inspect_source(cop, ['while cond do',
|
@@ -45,13 +45,13 @@ module Rubocop
|
|
45
45
|
it 'auto-corrects the usage of "do" in multiline while' do
|
46
46
|
new_source = autocorrect_source(cop, ['while cond do',
|
47
47
|
'end'])
|
48
|
-
expect(new_source).to eq("while cond
|
48
|
+
expect(new_source).to eq("while cond\nend")
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'auto-corrects the usage of "do" in multiline until' do
|
52
52
|
new_source = autocorrect_source(cop, ['until cond do',
|
53
53
|
'end'])
|
54
|
-
expect(new_source).to eq("until cond
|
54
|
+
expect(new_source).to eq("until cond\nend")
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe Team do
|
8
|
+
subject(:team) { Team.new(cop_classes, config, options) }
|
9
|
+
let(:cop_classes) { Cop.all }
|
10
|
+
let(:config) { Config.default_configuration }
|
11
|
+
let(:options) { nil }
|
12
|
+
|
13
|
+
describe '#autocorrect?' do
|
14
|
+
subject { team.autocorrect? }
|
15
|
+
|
16
|
+
context 'when the option argument of .new is omitted' do
|
17
|
+
subject { Team.new(cop_classes, config).autocorrect? }
|
18
|
+
it { should be_false }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when { autocorrect: true } is passed to .new' do
|
22
|
+
let(:options) { { autocorrect: true } }
|
23
|
+
it { should be_true }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#debug?' do
|
28
|
+
subject { team.debug? }
|
29
|
+
|
30
|
+
context 'when the option argument of .new is omitted' do
|
31
|
+
subject { Team.new(cop_classes, config).debug? }
|
32
|
+
it { should be_false }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when { debug: true } is passed to .new' do
|
36
|
+
let(:options) { { debug: true } }
|
37
|
+
it { should be_true }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#inspect_file', :isolated_environment do
|
42
|
+
include FileHelper
|
43
|
+
|
44
|
+
let(:file_path) { 'example.rb' }
|
45
|
+
let(:offences) { team.inspect_file(file_path) }
|
46
|
+
|
47
|
+
before do
|
48
|
+
create_file(file_path, [
|
49
|
+
'#' * 90,
|
50
|
+
'puts test;'
|
51
|
+
])
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns offences' do
|
55
|
+
expect(offences).not_to be_empty
|
56
|
+
expect(offences.all? { |o| o.is_a?(Offence) }).to be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when Parser cannot parse the file' do
|
60
|
+
before do
|
61
|
+
create_file(file_path, [
|
62
|
+
'#' * 90,
|
63
|
+
'class Test'
|
64
|
+
])
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns only error offences' do
|
68
|
+
expect(offences.size).to eq(1)
|
69
|
+
offence = offences.first
|
70
|
+
expect(offence.cop_name).to eq('Syntax')
|
71
|
+
expect(offence.severity).to eq(:error)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when Parser reports non-fatal warning for the file' do
|
76
|
+
before do
|
77
|
+
create_file(file_path, [
|
78
|
+
'# encoding: utf-8',
|
79
|
+
'#' * 90,
|
80
|
+
'puts *test'
|
81
|
+
])
|
82
|
+
end
|
83
|
+
|
84
|
+
let(:cop_names) { offences.map(&:cop_name) }
|
85
|
+
|
86
|
+
it 'returns Parser warning offences' do
|
87
|
+
expect(cop_names).to include('Syntax')
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'returns offences from cops' do
|
91
|
+
expect(cop_names).to include('LineLength')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when autocorrection is enabled' do
|
96
|
+
let(:options) { { autocorrect: true } }
|
97
|
+
|
98
|
+
before do
|
99
|
+
create_file(file_path, [
|
100
|
+
'# encoding: utf-8',
|
101
|
+
'puts "string"'
|
102
|
+
])
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'does autocorrection' do
|
106
|
+
team.inspect_file(file_path)
|
107
|
+
corrected_source = File.read(file_path)
|
108
|
+
expect(corrected_source).to eq([
|
109
|
+
'# encoding: utf-8',
|
110
|
+
"puts 'string'",
|
111
|
+
''
|
112
|
+
].join("\n"))
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'still returns offences' do
|
116
|
+
expect(offences.first.cop_name).to eq('StringLiterals')
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#cops' do
|
122
|
+
subject(:cops) { team.cops }
|
123
|
+
|
124
|
+
it 'returns cop instances' do
|
125
|
+
expect(cops).not_to be_empty
|
126
|
+
expect(cops.all? { |c| c.is_a?(Cop) }).to be_true
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'when only some cop classes are passed to .new' do
|
130
|
+
let(:cop_classes) { [Lint::Void, Style::LineLength] }
|
131
|
+
|
132
|
+
it 'returns only intances of the classes' do
|
133
|
+
expect(cops.size).to eq(2)
|
134
|
+
cops.sort! { |a, b| a.name <=> b.name }
|
135
|
+
expect(cops[0].name).to eq('LineLength')
|
136
|
+
expect(cops[1].name).to eq('Void')
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when some classes are disabled with config' do
|
141
|
+
before do
|
142
|
+
%w(Void LineLength).each do |cop_name|
|
143
|
+
config.for_cop(cop_name)['Enabled'] = false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
let(:cop_names) { cops.map(&:name) }
|
148
|
+
|
149
|
+
it 'does not return intances of the classes' do
|
150
|
+
expect(cops).not_to be_empty
|
151
|
+
expect(cop_names).not_to include('Void')
|
152
|
+
expect(cop_names).not_to include('LineLength')
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,217 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module VariableInspector
|
8
|
+
describe Assignment do
|
9
|
+
include ASTHelper
|
10
|
+
include AST::Sexp
|
11
|
+
|
12
|
+
let(:ast) do
|
13
|
+
processed_source = Rubocop::SourceParser.parse(source)
|
14
|
+
processed_source.ast
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:source) do
|
18
|
+
<<-END
|
19
|
+
class SomeClass
|
20
|
+
def some_method(flag)
|
21
|
+
puts 'Hello World!'
|
22
|
+
|
23
|
+
if flag > 0
|
24
|
+
foo = 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
END
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:def_node) do
|
32
|
+
found_node = scan_node(ast, include_origin_node: true) do |node|
|
33
|
+
break node if node.type == :def
|
34
|
+
end
|
35
|
+
fail 'No def node found!' unless found_node
|
36
|
+
found_node
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:lvasgn_node) do
|
40
|
+
found_node = scan_node(ast) do |node|
|
41
|
+
break node if node.type == :lvasgn
|
42
|
+
end
|
43
|
+
fail 'No lvasgn node found!' unless found_node
|
44
|
+
found_node
|
45
|
+
end
|
46
|
+
|
47
|
+
let(:name) { lvasgn_node.children.first }
|
48
|
+
let(:scope) { Scope.new(def_node) }
|
49
|
+
let(:variable) { Variable.new(name, lvasgn_node, scope) }
|
50
|
+
let(:assignment) { Assignment.new(lvasgn_node, variable) }
|
51
|
+
|
52
|
+
describe '.new' do
|
53
|
+
let(:variable) { double('variable') }
|
54
|
+
|
55
|
+
context 'when an assignment node is passed' do
|
56
|
+
it 'does not raise error' do
|
57
|
+
node = s(:lvasgn, :foo)
|
58
|
+
expect { Assignment.new(node, variable) }.not_to raise_error
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when an argument declaration node is passed' do
|
63
|
+
it 'raises error' do
|
64
|
+
node = s(:arg, :foo)
|
65
|
+
expect { Assignment.new(node, variable) }
|
66
|
+
.to raise_error(ArgumentError)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when any other type node is passed' do
|
71
|
+
it 'raises error' do
|
72
|
+
node = s(:def)
|
73
|
+
expect { Assignment.new(node, variable) }
|
74
|
+
.to raise_error(ArgumentError)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#name' do
|
80
|
+
it 'returns the variable name' do
|
81
|
+
expect(assignment.name).to eq(:foo)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#meta_assignment_node' do
|
86
|
+
context 'when it is += operator assignment' do
|
87
|
+
let(:source) do
|
88
|
+
<<-END
|
89
|
+
def some_method
|
90
|
+
foo += 1
|
91
|
+
end
|
92
|
+
END
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'returns op_asgn node' do
|
96
|
+
expect(assignment.meta_assignment_node.type).to eq(:op_asgn)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when it is ||= operator assignment' do
|
101
|
+
let(:source) do
|
102
|
+
<<-END
|
103
|
+
def some_method
|
104
|
+
foo ||= 1
|
105
|
+
end
|
106
|
+
END
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'returns or_asgn node' do
|
110
|
+
expect(assignment.meta_assignment_node.type).to eq(:or_asgn)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'when it is &&= operator assignment' do
|
115
|
+
let(:source) do
|
116
|
+
<<-END
|
117
|
+
def some_method
|
118
|
+
foo &&= 1
|
119
|
+
end
|
120
|
+
END
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'returns and_asgn node' do
|
124
|
+
expect(assignment.meta_assignment_node.type).to eq(:and_asgn)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'when it is multiple assignment' do
|
129
|
+
let(:source) do
|
130
|
+
<<-END
|
131
|
+
def some_method
|
132
|
+
foo, bar = [1, 2]
|
133
|
+
end
|
134
|
+
END
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'returns masgn node' do
|
138
|
+
expect(assignment.meta_assignment_node.type).to eq(:masgn)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#operator' do
|
144
|
+
context 'when it is normal assignment' do
|
145
|
+
let(:source) do
|
146
|
+
<<-END
|
147
|
+
def some_method
|
148
|
+
foo = 1
|
149
|
+
end
|
150
|
+
END
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'returns =' do
|
154
|
+
expect(assignment.operator).to eq('=')
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'when it is += operator assignment' do
|
159
|
+
let(:source) do
|
160
|
+
<<-END
|
161
|
+
def some_method
|
162
|
+
foo += 1
|
163
|
+
end
|
164
|
+
END
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'returns +=' do
|
168
|
+
expect(assignment.operator).to eq('+=')
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context 'when it is ||= operator assignment' do
|
173
|
+
let(:source) do
|
174
|
+
<<-END
|
175
|
+
def some_method
|
176
|
+
foo ||= 1
|
177
|
+
end
|
178
|
+
END
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'returns ||=' do
|
182
|
+
expect(assignment.operator).to eq('||=')
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'when it is &&= operator assignment' do
|
187
|
+
let(:source) do
|
188
|
+
<<-END
|
189
|
+
def some_method
|
190
|
+
foo &&= 1
|
191
|
+
end
|
192
|
+
END
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'returns &&=' do
|
196
|
+
expect(assignment.operator).to eq('&&=')
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context 'when it is multiple assignment' do
|
201
|
+
let(:source) do
|
202
|
+
<<-END
|
203
|
+
def some_method
|
204
|
+
foo, bar = [1, 2]
|
205
|
+
end
|
206
|
+
END
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'returns =' do
|
210
|
+
expect(assignment.operator).to eq('=')
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|