sabat-rubocop 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +50 -0
- data/.rspec +1 -0
- data/.rubocop.yml +7 -0
- data/.travis.yml +7 -0
- data/.yardopts +2 -0
- data/CHANGELOG.md +268 -0
- data/CONTRIBUTING.md +16 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +324 -0
- data/Rakefile +29 -0
- data/bin/rubocop +22 -0
- data/config/default.yml +58 -0
- data/config/disabled.yml +5 -0
- data/config/enabled.yml +403 -0
- data/lib/rubocop.rb +116 -0
- data/lib/rubocop/cli.rb +407 -0
- data/lib/rubocop/config.rb +250 -0
- data/lib/rubocop/config_store.rb +39 -0
- data/lib/rubocop/cop/cop.rb +138 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +54 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +189 -0
- data/lib/rubocop/cop/lint/end_in_method.rb +30 -0
- data/lib/rubocop/cop/lint/ensure_return.rb +22 -0
- data/lib/rubocop/cop/lint/eval.rb +22 -0
- data/lib/rubocop/cop/lint/handle_exceptions.rb +20 -0
- data/lib/rubocop/cop/lint/literal_in_condition.rb +81 -0
- data/lib/rubocop/cop/lint/loop.rb +29 -0
- data/lib/rubocop/cop/lint/rescue_exception.rb +29 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +34 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +35 -0
- data/lib/rubocop/cop/lint/unused_local_variable.rb +32 -0
- data/lib/rubocop/cop/lint/void.rb +58 -0
- data/lib/rubocop/cop/offence.rb +136 -0
- data/lib/rubocop/cop/rails/validation.rb +30 -0
- data/lib/rubocop/cop/style/access_control.rb +58 -0
- data/lib/rubocop/cop/style/alias.rb +28 -0
- data/lib/rubocop/cop/style/align_parameters.rb +39 -0
- data/lib/rubocop/cop/style/and_or.rb +45 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +21 -0
- data/lib/rubocop/cop/style/ascii_identifiers.rb +22 -0
- data/lib/rubocop/cop/style/attr.rb +20 -0
- data/lib/rubocop/cop/style/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/style/avoid_for.rb +18 -0
- data/lib/rubocop/cop/style/avoid_global_vars.rb +65 -0
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +21 -0
- data/lib/rubocop/cop/style/avoid_perlisms.rb +50 -0
- data/lib/rubocop/cop/style/begin_block.rb +18 -0
- data/lib/rubocop/cop/style/block_comments.rb +20 -0
- data/lib/rubocop/cop/style/block_nesting.rb +47 -0
- data/lib/rubocop/cop/style/blocks.rb +27 -0
- data/lib/rubocop/cop/style/case_equality.rb +22 -0
- data/lib/rubocop/cop/style/case_indentation.rb +28 -0
- data/lib/rubocop/cop/style/character_literal.rb +37 -0
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +33 -0
- data/lib/rubocop/cop/style/class_methods.rb +22 -0
- data/lib/rubocop/cop/style/collection_methods.rb +56 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +29 -0
- data/lib/rubocop/cop/style/constant_name.rb +31 -0
- data/lib/rubocop/cop/style/def_parentheses.rb +70 -0
- data/lib/rubocop/cop/style/documentation.rb +58 -0
- data/lib/rubocop/cop/style/dot_position.rb +25 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +26 -0
- data/lib/rubocop/cop/style/empty_lines.rb +40 -0
- data/lib/rubocop/cop/style/empty_literal.rb +53 -0
- data/lib/rubocop/cop/style/encoding.rb +29 -0
- data/lib/rubocop/cop/style/end_block.rb +18 -0
- data/lib/rubocop/cop/style/end_of_line.rb +23 -0
- data/lib/rubocop/cop/style/favor_join.rb +29 -0
- data/lib/rubocop/cop/style/favor_modifier.rb +118 -0
- data/lib/rubocop/cop/style/favor_sprintf.rb +28 -0
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +54 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +47 -0
- data/lib/rubocop/cop/style/if_then_else.rb +29 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -0
- data/lib/rubocop/cop/style/lambda.rb +47 -0
- data/lib/rubocop/cop/style/leading_comment_space.rb +25 -0
- data/lib/rubocop/cop/style/line_continuation.rb +26 -0
- data/lib/rubocop/cop/style/line_length.rb +30 -0
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +61 -0
- data/lib/rubocop/cop/style/method_call_parentheses.rb +22 -0
- data/lib/rubocop/cop/style/method_length.rb +57 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +47 -0
- data/lib/rubocop/cop/style/not.rb +24 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +25 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +20 -0
- data/lib/rubocop/cop/style/op_method.rb +29 -0
- data/lib/rubocop/cop/style/parameter_lists.rb +42 -0
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +42 -0
- data/lib/rubocop/cop/style/proc.rb +30 -0
- data/lib/rubocop/cop/style/reduce_arguments.rb +34 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +39 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +55 -0
- data/lib/rubocop/cop/style/semicolon.rb +51 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +48 -0
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +69 -0
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +32 -0
- data/lib/rubocop/cop/style/string_literals.rb +36 -0
- data/lib/rubocop/cop/style/surrounding_space.rb +314 -0
- data/lib/rubocop/cop/style/symbol_array.rb +31 -0
- data/lib/rubocop/cop/style/symbol_name.rb +27 -0
- data/lib/rubocop/cop/style/tab.rb +25 -0
- data/lib/rubocop/cop/style/ternary_operator.rb +49 -0
- data/lib/rubocop/cop/style/trailing_whitespace.rb +24 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +32 -0
- data/lib/rubocop/cop/style/unless_else.rb +26 -0
- data/lib/rubocop/cop/style/variable_interpolation.rb +32 -0
- data/lib/rubocop/cop/style/when_then.rb +25 -0
- data/lib/rubocop/cop/style/while_until_do.rb +45 -0
- data/lib/rubocop/cop/style/word_array.rb +44 -0
- data/lib/rubocop/cop/util.rb +27 -0
- data/lib/rubocop/cop/variable_inspector.rb +280 -0
- data/lib/rubocop/formatter/base_formatter.rb +119 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +21 -0
- data/lib/rubocop/formatter/emacs_style_formatter.rb +17 -0
- data/lib/rubocop/formatter/formatter_set.rb +77 -0
- data/lib/rubocop/formatter/json_formatter.rb +76 -0
- data/lib/rubocop/formatter/progress_formatter.rb +63 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +62 -0
- data/lib/rubocop/version.rb +21 -0
- data/rubocop.gemspec +36 -0
- data/spec/.rubocop.yml +5 -0
- data/spec/project_spec.rb +24 -0
- data/spec/rubocop/cli_spec.rb +906 -0
- data/spec/rubocop/config_spec.rb +470 -0
- data/spec/rubocop/config_store_spec.rb +66 -0
- data/spec/rubocop/cops/cop_spec.rb +38 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +111 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +333 -0
- data/spec/rubocop/cops/lint/end_in_method_spec.rb +35 -0
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/lint/eval_spec.rb +41 -0
- data/spec/rubocop/cops/lint/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/lint/literal_in_condition_spec.rb +42 -0
- data/spec/rubocop/cops/lint/loop_spec.rb +33 -0
- data/spec/rubocop/cops/lint/rescue_exception_spec.rb +127 -0
- data/spec/rubocop/cops/lint/shadowing_outer_local_variable_spec.rb +243 -0
- data/spec/rubocop/cops/lint/unreachable_code_spec.rb +69 -0
- data/spec/rubocop/cops/lint/unused_local_variable_spec.rb +497 -0
- data/spec/rubocop/cops/lint/void_spec.rb +63 -0
- data/spec/rubocop/cops/offence_spec.rb +133 -0
- data/spec/rubocop/cops/rails/validation_spec.rb +27 -0
- data/spec/rubocop/cops/style/access_control_spec.rb +142 -0
- data/spec/rubocop/cops/style/alias_spec.rb +47 -0
- data/spec/rubocop/cops/style/align_parameters_spec.rb +199 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +39 -0
- data/spec/rubocop/cops/style/ascii_comments_spec.rb +28 -0
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +28 -0
- data/spec/rubocop/cops/style/attr_spec.rb +20 -0
- data/spec/rubocop/cops/style/avoid_class_vars_spec.rb +27 -0
- data/spec/rubocop/cops/style/avoid_for_spec.rb +37 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +34 -0
- data/spec/rubocop/cops/style/avoid_perl_backrefs_spec.rb +20 -0
- data/spec/rubocop/cops/style/avoid_perlisms_spec.rb +47 -0
- data/spec/rubocop/cops/style/begin_block_spec.rb +19 -0
- data/spec/rubocop/cops/style/block_comments_spec.rb +27 -0
- data/spec/rubocop/cops/style/block_nesting_spec.rb +159 -0
- data/spec/rubocop/cops/style/blocks_spec.rb +35 -0
- data/spec/rubocop/cops/style/case_equality_spec.rb +18 -0
- data/spec/rubocop/cops/style/case_indentation_spec.rb +88 -0
- data/spec/rubocop/cops/style/character_literal_spec.rb +28 -0
- data/spec/rubocop/cops/style/class_and_module_camel_case_spec.rb +46 -0
- data/spec/rubocop/cops/style/class_methods_spec.rb +51 -0
- data/spec/rubocop/cops/style/collection_methods_spec.rb +41 -0
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +55 -0
- data/spec/rubocop/cops/style/constant_name_spec.rb +56 -0
- data/spec/rubocop/cops/style/def_with_parentheses_spec.rb +40 -0
- data/spec/rubocop/cops/style/def_without_parentheses_spec.rb +34 -0
- data/spec/rubocop/cops/style/documentation_spec.rb +79 -0
- data/spec/rubocop/cops/style/dot_position_spec.rb +30 -0
- data/spec/rubocop/cops/style/empty_line_between_defs_spec.rb +85 -0
- data/spec/rubocop/cops/style/empty_lines_spec.rb +40 -0
- data/spec/rubocop/cops/style/empty_literal_spec.rb +91 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +49 -0
- data/spec/rubocop/cops/style/end_block_spec.rb +19 -0
- data/spec/rubocop/cops/style/end_of_line_spec.rb +25 -0
- data/spec/rubocop/cops/style/favor_join_spec.rb +37 -0
- data/spec/rubocop/cops/style/favor_modifier_spec.rb +160 -0
- data/spec/rubocop/cops/style/favor_sprintf_spec.rb +53 -0
- data/spec/rubocop/cops/style/favor_unless_over_negated_if_spec.rb +64 -0
- data/spec/rubocop/cops/style/favor_until_over_negated_while_spec.rb +47 -0
- data/spec/rubocop/cops/style/hash_syntax_spec.rb +51 -0
- data/spec/rubocop/cops/style/if_with_semicolon_spec.rb +25 -0
- data/spec/rubocop/cops/style/lambda_spec.rb +45 -0
- data/spec/rubocop/cops/style/leading_comment_space_spec.rb +65 -0
- data/spec/rubocop/cops/style/line_continuation_spec.rb +26 -0
- data/spec/rubocop/cops/style/line_length_spec.rb +25 -0
- data/spec/rubocop/cops/style/method_and_variable_snake_case_spec.rb +95 -0
- data/spec/rubocop/cops/style/method_call_parentheses_spec.rb +25 -0
- data/spec/rubocop/cops/style/method_length_spec.rb +151 -0
- data/spec/rubocop/cops/style/multiline_if_then_spec.rb +97 -0
- data/spec/rubocop/cops/style/not_spec.rb +28 -0
- data/spec/rubocop/cops/style/numeric_literals_spec.rb +51 -0
- data/spec/rubocop/cops/style/one_line_conditional_spec.rb +18 -0
- data/spec/rubocop/cops/style/op_method_spec.rb +80 -0
- data/spec/rubocop/cops/style/parameter_lists_spec.rb +49 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +59 -0
- data/spec/rubocop/cops/style/proc_spec.rb +28 -0
- data/spec/rubocop/cops/style/reduce_arguments_spec.rb +59 -0
- data/spec/rubocop/cops/style/regexp_literal_spec.rb +83 -0
- data/spec/rubocop/cops/style/rescue_modifier_spec.rb +122 -0
- data/spec/rubocop/cops/style/semicolon_spec.rb +95 -0
- data/spec/rubocop/cops/style/single_line_methods_spec.rb +54 -0
- data/spec/rubocop/cops/style/space_after_colon_spec.rb +29 -0
- data/spec/rubocop/cops/style/space_after_comma_spec.rb +31 -0
- data/spec/rubocop/cops/style/space_after_control_keyword_spec.rb +69 -0
- data/spec/rubocop/cops/style/space_after_semicolon_spec.rb +24 -0
- data/spec/rubocop/cops/style/space_around_braces_spec.rb +49 -0
- data/spec/rubocop/cops/style/space_around_equals_in_default_parameter_spec.rb +34 -0
- data/spec/rubocop/cops/style/space_around_operators_spec.rb +216 -0
- data/spec/rubocop/cops/style/space_inside_brackets_spec.rb +51 -0
- data/spec/rubocop/cops/style/space_inside_hash_literal_braces_spec.rb +99 -0
- data/spec/rubocop/cops/style/space_inside_parens_spec.rb +33 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +62 -0
- data/spec/rubocop/cops/style/symbol_array_spec.rb +45 -0
- data/spec/rubocop/cops/style/symbol_name_spec.rb +122 -0
- data/spec/rubocop/cops/style/tab_spec.rb +23 -0
- data/spec/rubocop/cops/style/ternary_operator_spec.rb +42 -0
- data/spec/rubocop/cops/style/trailing_whitespace_spec.rb +29 -0
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +338 -0
- data/spec/rubocop/cops/style/unless_else_spec.rb +31 -0
- data/spec/rubocop/cops/style/variable_interpolation_spec.rb +53 -0
- data/spec/rubocop/cops/style/when_then_spec.rb +40 -0
- data/spec/rubocop/cops/style/while_until_do_spec.rb +47 -0
- data/spec/rubocop/cops/style/word_array_spec.rb +61 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +374 -0
- data/spec/rubocop/formatter/base_formatter_spec.rb +190 -0
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +70 -0
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +32 -0
- data/spec/rubocop/formatter/formatter_set_spec.rb +132 -0
- data/spec/rubocop/formatter/json_formatter_spec.rb +142 -0
- data/spec/rubocop/formatter/progress_formatter_spec.rb +196 -0
- data/spec/rubocop/formatter/simple_text_formatter_spec.rb +74 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +27 -0
- data/spec/support/mri_syntax_checker.rb +69 -0
- data/spec/support/shared_examples.rb +33 -0
- metadata +517 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks whether comments have a leading space
|
7
|
+
# after the # denoting the start of the comment. The
|
8
|
+
# leading space is not required for some RDoc special syntax,
|
9
|
+
# like #++, #--, #:nodoc, etc.
|
10
|
+
class LeadingCommentSpace < Cop
|
11
|
+
MSG = 'Missing space after #.'
|
12
|
+
|
13
|
+
def inspect(source_buffer, source, tokens, ast, comments)
|
14
|
+
comments.each do |comment|
|
15
|
+
if comment.text =~ /^#+[^#\s:+-]/
|
16
|
+
unless comment.text.start_with?('#!') && comment.loc.line == 1
|
17
|
+
add_offence(:convention, comment.loc, MSG)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for uses of the line continuation character.
|
7
|
+
#
|
8
|
+
# This check has to be refined or retired, since it doesn't make a lot
|
9
|
+
# of sense without inspection of its context.
|
10
|
+
class LineContinuation < Cop
|
11
|
+
MSG = 'Avoid the use of the line continuation character(\).'
|
12
|
+
|
13
|
+
def inspect(source_buffer, source, tokens, ast, comments)
|
14
|
+
source.each_with_index do |line, index|
|
15
|
+
if line =~ /.*\\\z/
|
16
|
+
add_offence(:convention,
|
17
|
+
source_range(source_buffer, source[0...index],
|
18
|
+
line.length - 1, 1),
|
19
|
+
MSG)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks the length of lines in the source code.
|
7
|
+
# The maximum length is configurable.
|
8
|
+
class LineLength < Cop
|
9
|
+
MSG = 'Line is too long. [%d/%d]'
|
10
|
+
|
11
|
+
def inspect(source_buffer, source, tokens, ast, comments)
|
12
|
+
source.each_with_index do |line, index|
|
13
|
+
max = LineLength.max
|
14
|
+
if line.length > max
|
15
|
+
message = sprintf(MSG, line.length, max)
|
16
|
+
add_offence(:convention,
|
17
|
+
source_range(source_buffer, source[0...index], max,
|
18
|
+
line.length - max),
|
19
|
+
message)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.max
|
25
|
+
LineLength.config['Max']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop makes sure that all methods and variables use
|
7
|
+
# snake_case for their names. Some special arrangements have to be
|
8
|
+
# made for operator methods.
|
9
|
+
class MethodAndVariableSnakeCase < Cop
|
10
|
+
MSG = 'Use snake_case for methods and variables.'
|
11
|
+
SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
|
12
|
+
|
13
|
+
# http://phrogz.net/programmingruby/language.html#table_18.4
|
14
|
+
OPERATOR_METHODS = %w(
|
15
|
+
| ^ & <=> == === =~ > >= < <= << >>
|
16
|
+
+ - * / % ** ~ +@ -@ [] []= ` ! != !~
|
17
|
+
).map(&:to_sym)
|
18
|
+
|
19
|
+
def inspect(source_buffer, source, tokens, node, comments)
|
20
|
+
return unless node
|
21
|
+
on_node([:def, :defs, :lvasgn, :ivasgn, :send], node) do |n|
|
22
|
+
name = case n.type
|
23
|
+
when :def
|
24
|
+
name_of_instance_method(n)
|
25
|
+
when :defs
|
26
|
+
name_of_singleton_method(n)
|
27
|
+
when :lvasgn, :ivasgn
|
28
|
+
name_of_variable(n)
|
29
|
+
when :send
|
30
|
+
name_of_setter(n)
|
31
|
+
end
|
32
|
+
|
33
|
+
next unless name
|
34
|
+
next if name =~ SNAKE_CASE || OPERATOR_METHODS.include?(name)
|
35
|
+
|
36
|
+
add_offence(:convention, n.location.expression, MSG)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def name_of_instance_method(def_node)
|
41
|
+
def_node.children.first
|
42
|
+
end
|
43
|
+
|
44
|
+
def name_of_singleton_method(defs_node)
|
45
|
+
defs_node.children[1]
|
46
|
+
end
|
47
|
+
|
48
|
+
def name_of_variable(vasgn_node)
|
49
|
+
vasgn_node.children.first
|
50
|
+
end
|
51
|
+
|
52
|
+
def name_of_setter(send_node)
|
53
|
+
receiver, method_name = *send_node
|
54
|
+
return nil unless receiver && receiver.type == :self
|
55
|
+
return nil unless method_name.to_s.end_with?('=')
|
56
|
+
method_name
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for unwanted parentheses in parameterless method calls.
|
7
|
+
class MethodCallParentheses < Cop
|
8
|
+
MSG = 'Do not use parentheses for method calls with no arguments.'
|
9
|
+
|
10
|
+
def on_send(node)
|
11
|
+
_receiver, _method_name, *args = *node
|
12
|
+
|
13
|
+
if args.empty? && node.loc.begin
|
14
|
+
add_offence(:convention, node.loc.begin, MSG)
|
15
|
+
end
|
16
|
+
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks if the length a method exceeds some maximum value.
|
7
|
+
# Comment lines can optionally be ignored.
|
8
|
+
# The maximum allowed length is configurable.
|
9
|
+
class MethodLength < Cop
|
10
|
+
MSG = 'Method has too many lines. [%d/%d]'
|
11
|
+
|
12
|
+
def on_def(node)
|
13
|
+
check(node)
|
14
|
+
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_defs(node)
|
19
|
+
check(node)
|
20
|
+
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
def max_length
|
25
|
+
MethodLength.config['Max']
|
26
|
+
end
|
27
|
+
|
28
|
+
def count_comments?
|
29
|
+
MethodLength.config['CountComments']
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def check(node)
|
35
|
+
method_length = calculate_length(node.loc.expression.source)
|
36
|
+
|
37
|
+
if method_length > max_length
|
38
|
+
message = sprintf(MSG, method_length, max_length)
|
39
|
+
add_offence(:convention, node.loc.keyword, message)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def calculate_length(source)
|
44
|
+
lines = source.lines.to_a[1...-1]
|
45
|
+
|
46
|
+
return 0 unless lines
|
47
|
+
|
48
|
+
lines.map!(&:strip).reject!(&:empty?)
|
49
|
+
|
50
|
+
lines.reject! { |line| line =~ /^\s*#/ } unless count_comments?
|
51
|
+
|
52
|
+
lines.size
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for uses of the `then` keyword in multi-line if statements.
|
7
|
+
#
|
8
|
+
# This is considered bad practice:
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# if cond then
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# While if statements can contain `then` on the same line:
|
15
|
+
# @example
|
16
|
+
#
|
17
|
+
# if cond then a
|
18
|
+
# elsif cond then b
|
19
|
+
# end
|
20
|
+
class MultilineIfThen < Cop
|
21
|
+
include IfThenElse
|
22
|
+
|
23
|
+
def offending_line(node)
|
24
|
+
condition, body, else_clause = *node
|
25
|
+
next_thing = if body && body.loc.expression
|
26
|
+
body.loc.expression.begin
|
27
|
+
elsif else_clause && else_clause.loc.expression
|
28
|
+
else_clause.loc.expression.begin
|
29
|
+
else
|
30
|
+
node.loc.end # No body, use "end".
|
31
|
+
end
|
32
|
+
right_after_cond =
|
33
|
+
Parser::Source::Range.new(next_thing.source_buffer,
|
34
|
+
condition.loc.expression.end.end_pos,
|
35
|
+
next_thing.begin_pos)
|
36
|
+
if right_after_cond.source =~ /\A\s*then\s*(#.*)?\s*\n/
|
37
|
+
node.loc.expression.begin.line
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def error_message
|
42
|
+
'Never use then for multi-line if/unless.'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for uses if the keyword *not* instead of !.
|
7
|
+
class Not < Cop
|
8
|
+
MSG = 'Use ! instead of not.'
|
9
|
+
|
10
|
+
def on_send(node)
|
11
|
+
_receiver, method_name, *args = *node
|
12
|
+
|
13
|
+
# not does not take any arguments
|
14
|
+
if args.empty? && method_name == :! &&
|
15
|
+
node.loc.selector.is?('not')
|
16
|
+
add_offence(:convention, node.loc.selector, MSG)
|
17
|
+
end
|
18
|
+
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for big numeric literals without _ between groups
|
7
|
+
# of digits in them.
|
8
|
+
class NumericLiterals < Cop
|
9
|
+
MSG = 'Add underscores to large numeric literals to ' +
|
10
|
+
'improve their readability.'
|
11
|
+
|
12
|
+
def on_int(node)
|
13
|
+
value, = *node
|
14
|
+
|
15
|
+
if value > 10000 &&
|
16
|
+
node.loc.expression.source.split('.').grep(/\d{6}/).any?
|
17
|
+
add_offence(:convention, node.loc.expression, MSG)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_method :on_float, :on_int
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for uses of if/then/else/end on a single line.
|
7
|
+
class OneLineConditional < Cop
|
8
|
+
include IfThenElse
|
9
|
+
|
10
|
+
def offending_line(node)
|
11
|
+
node.loc.expression.line unless node.loc.expression.source =~ /\n/
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_message
|
15
|
+
'Favor the ternary operator (?:) over if/then/else/end constructs.'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop makes sure that certain operator methods have their sole
|
7
|
+
# parameter named *other*.
|
8
|
+
class OpMethod < Cop
|
9
|
+
MSG = 'When defining the %s operator, name its argument *other*.'
|
10
|
+
|
11
|
+
BLACKLISTED = [:+@, :-@, :[], :[]=, :<<]
|
12
|
+
|
13
|
+
TARGET_ARGS = s(:args, s(:arg, :other))
|
14
|
+
|
15
|
+
def on_def(node)
|
16
|
+
name, args, _body = *node
|
17
|
+
|
18
|
+
if name !~ /\A\w/ && !BLACKLISTED.include?(name) &&
|
19
|
+
args.children.size == 1 && args != TARGET_ARGS
|
20
|
+
add_offence(:convention, args.children[0].loc.expression,
|
21
|
+
sprintf(MSG, name))
|
22
|
+
end
|
23
|
+
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for methods with too many parameters.
|
7
|
+
# The maximum number of parameters in configurable.
|
8
|
+
# On Ruby 2.0+ keyword arguments can optionally
|
9
|
+
# be excluded from the total count.
|
10
|
+
class ParameterLists < Cop
|
11
|
+
MSG = 'Avoid parameter lists longer than %d parameters.'
|
12
|
+
|
13
|
+
def on_args(node)
|
14
|
+
if args_count(node) > max_params
|
15
|
+
add_offence(:convention, node.loc.expression,
|
16
|
+
sprintf(MSG, max_params))
|
17
|
+
end
|
18
|
+
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def args_count(node)
|
25
|
+
if count_keyword_args?
|
26
|
+
node.children.size
|
27
|
+
else
|
28
|
+
node.children.reject { |a| a.type == :kwoptarg }.size
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def max_params
|
33
|
+
ParameterLists.config['Max']
|
34
|
+
end
|
35
|
+
|
36
|
+
def count_keyword_args?
|
37
|
+
ParameterLists.config['CountKeywordArgs']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for the presence of superfluous parentheses around the
|
7
|
+
# condition of if/while/until.
|
8
|
+
class ParenthesesAroundCondition < Cop
|
9
|
+
MSG = "Don't use parentheses around the condition of an " +
|
10
|
+
'if/unless/while/until'
|
11
|
+
|
12
|
+
def on_if(node)
|
13
|
+
process_control_op(node)
|
14
|
+
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_while(node)
|
19
|
+
process_control_op(node)
|
20
|
+
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
def on_until(node)
|
25
|
+
process_control_op(node)
|
26
|
+
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def process_control_op(node)
|
33
|
+
cond, _body = *node
|
34
|
+
|
35
|
+
if cond.type == :begin
|
36
|
+
add_offence(:convention, cond.loc.expression, MSG)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|