rubocop 0.78.0 → 0.82.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +4 -4
- data/config/default.yml +158 -48
- data/lib/rubocop.rb +19 -4
- data/lib/rubocop/ast/builder.rb +45 -42
- data/lib/rubocop/ast/node.rb +12 -19
- data/lib/rubocop/ast/node/array_node.rb +13 -0
- data/lib/rubocop/ast/node/block_node.rb +5 -1
- data/lib/rubocop/ast/node/case_match_node.rb +56 -0
- data/lib/rubocop/ast/node/def_node.rb +11 -0
- data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
- data/lib/rubocop/ast/node/regexp_node.rb +2 -4
- data/lib/rubocop/ast/traversal.rb +29 -10
- data/lib/rubocop/cli.rb +10 -4
- data/lib/rubocop/cli/command/show_cops.rb +11 -4
- data/lib/rubocop/comment_config.rb +6 -1
- data/lib/rubocop/config.rb +36 -10
- data/lib/rubocop/config_loader.rb +42 -33
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/config_obsoletion.rb +4 -1
- data/lib/rubocop/config_validator.rb +66 -92
- data/lib/rubocop/cop/autocorrect_logic.rb +6 -3
- data/lib/rubocop/cop/badge.rb +5 -5
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
- data/lib/rubocop/cop/corrector.rb +48 -24
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/generator.rb +3 -4
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
- data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
- data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
- data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
- data/lib/rubocop/cop/layout/line_length.rb +32 -3
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +15 -6
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +19 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
- data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
- data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/loop.rb +6 -4
- data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
- data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
- data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
- data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
- data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
- data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
- data/lib/rubocop/cop/migration/department_name.rb +47 -6
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +2 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +30 -0
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
- data/lib/rubocop/cop/registry.rb +15 -3
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
- data/lib/rubocop/cop/style/alias.rb +4 -4
- data/lib/rubocop/cop/style/and_or.rb +5 -6
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
- data/lib/rubocop/cop/style/case_equality.rb +24 -1
- data/lib/rubocop/cop/style/character_literal.rb +2 -2
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
- data/lib/rubocop/cop/style/copyright.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
- data/lib/rubocop/cop/style/documentation.rb +43 -5
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +3 -3
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/end_block.rb +6 -0
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
- data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
- data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
- data/lib/rubocop/cop/style/lambda.rb +3 -2
- data/lib/rubocop/cop/style/lambda_call.rb +2 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/module_function.rb +58 -12
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +5 -4
- data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
- data/lib/rubocop/cop/style/or_assignment.rb +4 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
- data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_return.rb +5 -7
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/return_nil.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
- data/lib/rubocop/cop/style/self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
- data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +3 -3
- data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unpack_first.rb +0 -4
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
- data/lib/rubocop/cop/variable_force.rb +4 -1
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +74 -0
- data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/node_pattern.rb +96 -10
- data/lib/rubocop/options.rb +7 -1
- data/lib/rubocop/processed_source.rb +1 -4
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +1 -1
- data/lib/rubocop/rspec/shared_contexts.rb +5 -4
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/target_ruby.rb +151 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +39 -12
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -3,10 +3,11 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# This cop checks for missing top-level documentation of
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
6
|
+
# This cop checks for missing top-level documentation of classes and
|
7
|
+
# modules. Classes with no body are exempt from the check and so are
|
8
|
+
# namespace modules - modules that have nothing in their bodies except
|
9
|
+
# classes, other modules, constant definitions or constant visibility
|
10
|
+
# declarations.
|
10
11
|
#
|
11
12
|
# The documentation requirement is annulled if the class or module has
|
12
13
|
# a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the
|
@@ -18,12 +19,42 @@ module RuboCop
|
|
18
19
|
# # ...
|
19
20
|
# end
|
20
21
|
#
|
22
|
+
# module Math
|
23
|
+
# end
|
24
|
+
#
|
21
25
|
# # good
|
22
26
|
# # Description/Explanation of Person class
|
23
27
|
# class Person
|
24
28
|
# # ...
|
25
29
|
# end
|
26
30
|
#
|
31
|
+
# # allowed
|
32
|
+
# # Class without body
|
33
|
+
# class Person
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# # Namespace - A namespace can be a class or a module
|
37
|
+
# # Containing a class
|
38
|
+
# module Namespace
|
39
|
+
# # Description/Explanation of Person class
|
40
|
+
# class Person
|
41
|
+
# # ...
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# # Containing constant visibility declaration
|
46
|
+
# module Namespace
|
47
|
+
# class Private
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# private_constant :Private
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# # Containing constant definition
|
54
|
+
# module Namespace
|
55
|
+
# Public = Class.new
|
56
|
+
# end
|
57
|
+
#
|
27
58
|
class Documentation < Cop
|
28
59
|
include DocumentationComment
|
29
60
|
|
@@ -31,6 +62,9 @@ module RuboCop
|
|
31
62
|
|
32
63
|
def_node_matcher :constant_definition?, '{class module casgn}'
|
33
64
|
def_node_search :outer_module, '(const (const nil? _) _)'
|
65
|
+
def_node_matcher :constant_visibility_declaration?, <<~PATTERN
|
66
|
+
(send nil? {:public_constant :private_constant} ({sym str} _))
|
67
|
+
PATTERN
|
34
68
|
|
35
69
|
def on_class(node)
|
36
70
|
return unless node.body
|
@@ -59,12 +93,16 @@ module RuboCop
|
|
59
93
|
return false unless node
|
60
94
|
|
61
95
|
if node.begin_type?
|
62
|
-
node.children.all?
|
96
|
+
node.children.all?(&method(:constant_declaration?))
|
63
97
|
else
|
64
98
|
constant_definition?(node)
|
65
99
|
end
|
66
100
|
end
|
67
101
|
|
102
|
+
def constant_declaration?(node)
|
103
|
+
constant_definition?(node) || constant_visibility_declaration?(node)
|
104
|
+
end
|
105
|
+
|
68
106
|
def compact_namespace?(node)
|
69
107
|
node.loc.name.source =~ /::/
|
70
108
|
end
|
@@ -48,15 +48,15 @@ module RuboCop
|
|
48
48
|
|
49
49
|
first_arg, second_arg = *node.arguments
|
50
50
|
|
51
|
-
corrector.replace(first_arg
|
52
|
-
corrector.replace(second_arg
|
51
|
+
corrector.replace(first_arg, second_arg.source)
|
52
|
+
corrector.replace(second_arg, first_arg.source)
|
53
53
|
|
54
54
|
return_value = return_value(node.body)
|
55
55
|
|
56
56
|
if return_value_occupies_whole_line?(return_value)
|
57
57
|
corrector.remove(whole_line_expression(return_value))
|
58
58
|
else
|
59
|
-
corrector.remove(return_value
|
59
|
+
corrector.remove(return_value)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -38,7 +38,7 @@ module RuboCop
|
|
38
38
|
replacement_method = replacement_method(arg, method)
|
39
39
|
|
40
40
|
correction = "#{base_number.source}.#{replacement_method}?"
|
41
|
-
->(corrector) { corrector.replace(node
|
41
|
+
->(corrector) { corrector.replace(node, correction) }
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -97,7 +97,7 @@ module RuboCop
|
|
97
97
|
autocorrect_expand_path(corrector, current_path, default_dir)
|
98
98
|
elsif (default_dir = pathname_parent_expand_path(node)) ||
|
99
99
|
(default_dir = pathname_new_parent_expand_path(node))
|
100
|
-
corrector.replace(default_dir
|
100
|
+
corrector.replace(default_dir, '__dir__')
|
101
101
|
remove_parent_method(corrector, default_dir)
|
102
102
|
end
|
103
103
|
end
|
@@ -145,8 +145,8 @@ module RuboCop
|
|
145
145
|
else
|
146
146
|
new_path = "'#{parent_path(stripped_current_path)}'"
|
147
147
|
|
148
|
-
corrector.replace(current_path
|
149
|
-
corrector.replace(default_dir
|
148
|
+
corrector.replace(current_path, new_path)
|
149
|
+
corrector.replace(default_dir, '__dir__')
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop enforces consistency when using exponential notation
|
7
|
+
# for numbers in the code (eg 1.2e4). Different styles are supported:
|
8
|
+
# - `scientific` which enforces a mantissa between 1 (inclusive)
|
9
|
+
# and 10 (exclusive).
|
10
|
+
# - `engineering` which enforces the exponent to be a multiple of 3
|
11
|
+
# and the mantissa to be between 0.1 (inclusive)
|
12
|
+
# and 10 (exclusive).
|
13
|
+
# - `integral` which enforces the mantissa to always be a whole number
|
14
|
+
# without trailing zeroes.
|
15
|
+
#
|
16
|
+
# @example EnforcedStyle: scientific (default)
|
17
|
+
# # Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# 10e6
|
21
|
+
# 0.3e4
|
22
|
+
# 11.7e5
|
23
|
+
# 3.14e0
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# 1e7
|
27
|
+
# 3e3
|
28
|
+
# 1.17e6
|
29
|
+
# 3.14
|
30
|
+
#
|
31
|
+
# @example EnforcedStyle: engineering
|
32
|
+
# # Enforces using multiple of 3 exponents,
|
33
|
+
# # mantissa should be between 0.1 (inclusive) and 1000 (exclusive)
|
34
|
+
#
|
35
|
+
# # bad
|
36
|
+
# 3.2e7
|
37
|
+
# 0.1e5
|
38
|
+
# 12e5
|
39
|
+
# 1232e6
|
40
|
+
#
|
41
|
+
# # good
|
42
|
+
# 32e6
|
43
|
+
# 10e3
|
44
|
+
# 1.2e6
|
45
|
+
# 1.232e9
|
46
|
+
#
|
47
|
+
# @example EnforcedStyle: integral
|
48
|
+
# # Enforces the mantissa to have no decimal part and no
|
49
|
+
# # trailing zeroes.
|
50
|
+
#
|
51
|
+
# # bad
|
52
|
+
# 3.2e7
|
53
|
+
# 0.1e5
|
54
|
+
# 120e4
|
55
|
+
#
|
56
|
+
# # good
|
57
|
+
# 32e6
|
58
|
+
# 1e4
|
59
|
+
# 12e5
|
60
|
+
#
|
61
|
+
class ExponentialNotation < Cop
|
62
|
+
include ConfigurableEnforcedStyle
|
63
|
+
|
64
|
+
def on_float(node)
|
65
|
+
add_offense(node) if offense?(node)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def scientific?(node)
|
71
|
+
mantissa, = node.source.split('e')
|
72
|
+
mantissa =~ /^-?[1-9](\.\d*[0-9])?$/
|
73
|
+
end
|
74
|
+
|
75
|
+
def engineering?(node)
|
76
|
+
mantissa, exponent = node.source.split('e')
|
77
|
+
return false unless /^-?\d+$/.match?(exponent)
|
78
|
+
return false unless (exponent.to_i % 3).zero?
|
79
|
+
return false if /^-?\d{4}/.match?(mantissa)
|
80
|
+
return false if /^-?0\d/.match?(mantissa)
|
81
|
+
return false if /^-?0.0/.match?(mantissa)
|
82
|
+
|
83
|
+
true
|
84
|
+
end
|
85
|
+
|
86
|
+
def integral(node)
|
87
|
+
mantissa, = node.source.split('e')
|
88
|
+
mantissa =~ /^-?[1-9](\d*[1-9])?$/
|
89
|
+
end
|
90
|
+
|
91
|
+
def offense?(node)
|
92
|
+
return false unless node.source['e']
|
93
|
+
|
94
|
+
case style
|
95
|
+
when :scientific
|
96
|
+
!scientific?(node)
|
97
|
+
when :engineering
|
98
|
+
!engineering?(node)
|
99
|
+
when :integral
|
100
|
+
!integral(node)
|
101
|
+
else
|
102
|
+
false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def message(_node)
|
107
|
+
case style
|
108
|
+
when :scientific
|
109
|
+
'Use a mantissa in [1, 10[.'
|
110
|
+
when :engineering
|
111
|
+
'Use an exponent divisible by 3 and a mantissa in [0.1, 1000[.'
|
112
|
+
when :integral
|
113
|
+
'Use an integer as mantissa, without trailing zero.'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -104,7 +104,7 @@ module RuboCop
|
|
104
104
|
|
105
105
|
corrected = "#{style}(#{node.receiver.source}, #{args})"
|
106
106
|
|
107
|
-
corrector.replace(node
|
107
|
+
corrector.replace(node, corrected)
|
108
108
|
end
|
109
109
|
|
110
110
|
def autocorrect_to_percent(corrector, node)
|
@@ -119,7 +119,7 @@ module RuboCop
|
|
119
119
|
"[#{param_args.map(&:source).join(', ')}]"
|
120
120
|
end
|
121
121
|
|
122
|
-
corrector.replace(node
|
122
|
+
corrector.replace(node, "#{format} % #{args}")
|
123
123
|
end
|
124
124
|
end
|
125
125
|
end
|
@@ -51,29 +51,65 @@ module RuboCop
|
|
51
51
|
# module Baz
|
52
52
|
# # ...
|
53
53
|
# end
|
54
|
+
#
|
55
|
+
# @example EnforcedStyle: always_true
|
56
|
+
# # The `always_true` style enforces that the frozen string literal
|
57
|
+
# # comment is set to `true`. This is a stricter option than `always`
|
58
|
+
# # and forces projects to use frozen string literals.
|
59
|
+
# # bad
|
60
|
+
# # frozen_string_literal: false
|
61
|
+
#
|
62
|
+
# module Baz
|
63
|
+
# # ...
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# # bad
|
67
|
+
# module Baz
|
68
|
+
# # ...
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# # good
|
72
|
+
# # frozen_string_literal: true
|
73
|
+
#
|
74
|
+
# module Bar
|
75
|
+
# # ...
|
76
|
+
# end
|
54
77
|
class FrozenStringLiteralComment < Cop
|
55
78
|
include ConfigurableEnforcedStyle
|
56
79
|
include FrozenStringLiteral
|
57
80
|
include RangeHelp
|
58
81
|
|
59
|
-
|
82
|
+
MSG_MISSING_TRUE = 'Missing magic comment `# frozen_string_literal: '\
|
83
|
+
'true`.'
|
84
|
+
MSG_MISSING = 'Missing frozen string literal comment.'
|
60
85
|
MSG_UNNECESSARY = 'Unnecessary frozen string literal comment.'
|
86
|
+
MSG_DISABLED = 'Frozen string literal comment must be set to `true`.'
|
61
87
|
SHEBANG = '#!'
|
62
88
|
|
63
89
|
def investigate(processed_source)
|
64
90
|
return if processed_source.tokens.empty?
|
65
91
|
|
66
|
-
|
67
|
-
|
92
|
+
case style
|
93
|
+
when :never
|
94
|
+
ensure_no_comment(processed_source)
|
95
|
+
when :always_true
|
96
|
+
ensure_enabled_comment(processed_source)
|
68
97
|
else
|
69
|
-
|
98
|
+
ensure_comment(processed_source)
|
70
99
|
end
|
71
100
|
end
|
72
101
|
|
73
102
|
def autocorrect(node)
|
74
103
|
lambda do |corrector|
|
75
|
-
|
104
|
+
case style
|
105
|
+
when :never
|
76
106
|
remove_comment(corrector, node)
|
107
|
+
when :always_true
|
108
|
+
if frozen_string_literal_specified?
|
109
|
+
enable_comment(corrector)
|
110
|
+
else
|
111
|
+
insert_comment(corrector)
|
112
|
+
end
|
77
113
|
else
|
78
114
|
insert_comment(corrector)
|
79
115
|
end
|
@@ -82,12 +118,27 @@ module RuboCop
|
|
82
118
|
|
83
119
|
private
|
84
120
|
|
85
|
-
def
|
86
|
-
|
121
|
+
def ensure_no_comment(processed_source)
|
122
|
+
return unless frozen_string_literal_comment_exists?
|
123
|
+
|
124
|
+
unnecessary_comment_offense(processed_source)
|
87
125
|
end
|
88
126
|
|
89
|
-
def
|
90
|
-
|
127
|
+
def ensure_comment(processed_source)
|
128
|
+
return if frozen_string_literal_comment_exists?
|
129
|
+
|
130
|
+
missing_offense(processed_source)
|
131
|
+
end
|
132
|
+
|
133
|
+
def ensure_enabled_comment(processed_source)
|
134
|
+
if frozen_string_literal_specified?
|
135
|
+
return if frozen_string_literals_enabled?
|
136
|
+
|
137
|
+
# The comment exists, but is not enabled.
|
138
|
+
disabled_offense(processed_source)
|
139
|
+
else # The comment doesn't exist at all.
|
140
|
+
missing_true_offense(processed_source)
|
141
|
+
end
|
91
142
|
end
|
92
143
|
|
93
144
|
def last_special_comment(processed_source)
|
@@ -111,11 +162,22 @@ module RuboCop
|
|
111
162
|
end
|
112
163
|
end
|
113
164
|
|
114
|
-
def
|
165
|
+
def missing_offense(processed_source)
|
166
|
+
last_special_comment = last_special_comment(processed_source)
|
167
|
+
range = source_range(processed_source.buffer, 0, 0)
|
168
|
+
|
169
|
+
add_offense(last_special_comment,
|
170
|
+
location: range,
|
171
|
+
message: MSG_MISSING)
|
172
|
+
end
|
173
|
+
|
174
|
+
def missing_true_offense(processed_source)
|
115
175
|
last_special_comment = last_special_comment(processed_source)
|
116
176
|
range = source_range(processed_source.buffer, 0, 0)
|
117
177
|
|
118
|
-
add_offense(last_special_comment,
|
178
|
+
add_offense(last_special_comment,
|
179
|
+
location: range,
|
180
|
+
message: MSG_MISSING_TRUE)
|
119
181
|
end
|
120
182
|
|
121
183
|
def unnecessary_comment_offense(processed_source)
|
@@ -127,11 +189,27 @@ module RuboCop
|
|
127
189
|
message: MSG_UNNECESSARY)
|
128
190
|
end
|
129
191
|
|
192
|
+
def disabled_offense(processed_source)
|
193
|
+
frozen_string_literal_comment =
|
194
|
+
frozen_string_literal_comment(processed_source)
|
195
|
+
|
196
|
+
add_offense(frozen_string_literal_comment,
|
197
|
+
location: frozen_string_literal_comment.pos,
|
198
|
+
message: MSG_DISABLED)
|
199
|
+
end
|
200
|
+
|
130
201
|
def remove_comment(corrector, node)
|
131
202
|
corrector.remove(range_with_surrounding_space(range: node.pos,
|
132
203
|
side: :right))
|
133
204
|
end
|
134
205
|
|
206
|
+
def enable_comment(corrector)
|
207
|
+
comment = frozen_string_literal_comment(processed_source)
|
208
|
+
|
209
|
+
corrector.replace(line_range(comment.line),
|
210
|
+
FROZEN_STRING_LITERAL_ENABLED)
|
211
|
+
end
|
212
|
+
|
135
213
|
def insert_comment(corrector)
|
136
214
|
comment = last_special_comment(processed_source)
|
137
215
|
|