rubocop 0.47.1 → 0.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +28 -16
- data/config/default.yml +203 -115
- data/config/disabled.yml +0 -5
- data/config/enabled.yml +92 -8
- data/lib/rubocop.rb +24 -1
- data/lib/rubocop/ast/builder.rb +7 -2
- data/lib/rubocop/ast/node.rb +23 -20
- data/lib/rubocop/ast/node/and_node.rb +37 -0
- data/lib/rubocop/ast/node/array_node.rb +4 -1
- data/lib/rubocop/ast/node/case_node.rb +1 -0
- data/lib/rubocop/ast/node/ensure_node.rb +25 -0
- data/lib/rubocop/ast/node/hash_node.rb +10 -3
- data/lib/rubocop/ast/node/if_node.rb +2 -0
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
- data/lib/rubocop/ast/node/or_node.rb +37 -0
- data/lib/rubocop/ast/node/resbody_node.rb +25 -0
- data/lib/rubocop/ast/node/send_node.rb +190 -0
- data/lib/rubocop/ast/node/when_node.rb +1 -1
- data/lib/rubocop/ast/traversal.rb +15 -15
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +39 -15
- data/lib/rubocop/config_loader.rb +34 -13
- data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
- data/lib/rubocop/cop/commissioner.rb +4 -0
- data/lib/rubocop/cop/cop.rb +5 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
- data/lib/rubocop/cop/lint/debugger.rb +8 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
- data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
- data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
- data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
- data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
- data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
- data/lib/rubocop/cop/metrics/line_length.rb +2 -9
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
- data/lib/rubocop/cop/mixin/duplication.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
- data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
- data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
- data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
- data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
- data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/performance/casecmp.rb +1 -1
- data/lib/rubocop/cop/performance/detect.rb +2 -1
- data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
- data/lib/rubocop/cop/performance/end_with.rb +3 -1
- data/lib/rubocop/cop/performance/flat_map.rb +6 -6
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +3 -1
- data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
- data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
- data/lib/rubocop/cop/performance/size.rb +6 -11
- data/lib/rubocop/cop/performance/start_with.rb +3 -1
- data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
- data/lib/rubocop/cop/performance/times_map.rb +4 -4
- data/lib/rubocop/cop/rails/action_filter.rb +42 -42
- data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
- data/lib/rubocop/cop/rails/blank.rb +131 -0
- data/lib/rubocop/cop/rails/date.rb +25 -28
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
- data/lib/rubocop/cop/rails/exit.rb +9 -9
- data/lib/rubocop/cop/rails/file_path.rb +5 -14
- data/lib/rubocop/cop/rails/find_by.rb +8 -10
- data/lib/rubocop/cop/rails/find_each.rb +6 -9
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
- data/lib/rubocop/cop/rails/output.rb +3 -5
- data/lib/rubocop/cop/rails/output_safety.rb +4 -8
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
- data/lib/rubocop/cop/rails/present.rb +137 -0
- data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
- data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
- data/lib/rubocop/cop/rails/request_referer.rb +7 -4
- data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
- data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
- data/lib/rubocop/cop/rails/save_bang.rb +10 -10
- data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
- data/lib/rubocop/cop/rails/time_zone.rb +20 -18
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
- data/lib/rubocop/cop/rails/validation.rb +8 -11
- data/lib/rubocop/cop/registry.rb +3 -3
- data/lib/rubocop/cop/security/json_load.rb +1 -1
- data/lib/rubocop/cop/security/marshal_load.rb +5 -1
- data/lib/rubocop/cop/security/yaml_load.rb +3 -3
- data/lib/rubocop/cop/severity.rb +1 -1
- data/lib/rubocop/cop/style/alias.rb +5 -5
- data/lib/rubocop/cop/style/align_hash.rb +1 -1
- data/lib/rubocop/cop/style/align_parameters.rb +5 -5
- data/lib/rubocop/cop/style/and_or.rb +16 -31
- data/lib/rubocop/cop/style/attr.rb +14 -8
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
- data/lib/rubocop/cop/style/case_indentation.rb +2 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_check.rb +5 -7
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
- data/lib/rubocop/cop/style/collection_methods.rb +8 -8
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
- data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
- data/lib/rubocop/cop/style/constant_name.rb +1 -1
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/dot_position.rb +3 -7
- data/lib/rubocop/cop/style/double_negation.rb +2 -1
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +2 -2
- data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
- data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
- data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
- data/lib/rubocop/cop/style/empty_literal.rb +17 -9
- data/lib/rubocop/cop/style/end_of_line.rb +25 -3
- data/lib/rubocop/cop/style/file_name.rb +1 -1
- data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
- data/lib/rubocop/cop/style/for.rb +2 -4
- data/lib/rubocop/cop/style/format_string.rb +5 -4
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
- data/lib/rubocop/cop/style/indent_hash.rb +2 -1
- data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
- data/lib/rubocop/cop/style/indentation_width.rb +61 -29
- data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
- data/lib/rubocop/cop/style/lambda_call.rb +15 -11
- data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/missing_else.rb +4 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
- data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
- data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
- data/lib/rubocop/cop/style/negated_if.rb +73 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
- data/lib/rubocop/cop/style/not.rb +3 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
- data/lib/rubocop/cop/style/raise_args.rb +28 -24
- data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
- data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self.rb +17 -35
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/send.rb +4 -5
- data/lib/rubocop/cop/style/space_after_not.rb +7 -8
- data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
- data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
- data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
- data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
- data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +10 -5
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
- data/lib/rubocop/cop/style/symbol_array.rb +31 -35
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +12 -34
- data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
- data/lib/rubocop/cop/team.rb +4 -1
- data/lib/rubocop/cop/util.rb +33 -26
- data/lib/rubocop/cop/variable_force.rb +13 -13
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
- data/lib/rubocop/cop/variable_force/branch.rb +318 -0
- data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
- data/lib/rubocop/cop/variable_force/reference.rb +1 -3
- data/lib/rubocop/cop/variable_force/scope.rb +36 -20
- data/lib/rubocop/cop/variable_force/variable.rb +9 -8
- data/lib/rubocop/formatter/colorizable.rb +10 -10
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +2 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
- data/lib/rubocop/magic_comment.rb +20 -6
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/platform.rb +11 -0
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/remote_config.rb +18 -6
- data/lib/rubocop/result_cache.rb +8 -8
- data/lib/rubocop/rspec/cop_helper.rb +2 -0
- data/lib/rubocop/rspec/shared_contexts.rb +20 -0
- data/lib/rubocop/rspec/shared_examples.rb +1 -1
- data/lib/rubocop/runner.rb +2 -2
- data/lib/rubocop/target_finder.rb +64 -6
- data/lib/rubocop/version.rb +2 -4
- metadata +27 -4
- data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -13,21 +13,21 @@ module RuboCop
|
|
13
13
|
nil
|
14
14
|
end
|
15
15
|
|
16
|
-
NO_CHILD_NODES =
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
ONE_CHILD_NODE =
|
22
|
-
|
23
|
-
|
24
|
-
MANY_CHILD_NODES =
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
SECOND_CHILD_ONLY =
|
30
|
-
|
16
|
+
NO_CHILD_NODES = %i(true false nil int float complex
|
17
|
+
rational str sym regopt self lvar
|
18
|
+
ivar cvar gvar nth_ref back_ref cbase
|
19
|
+
arg restarg blockarg shadowarg
|
20
|
+
kwrestarg zsuper lambda redo retry).freeze
|
21
|
+
ONE_CHILD_NODE = %i(splat kwsplat block_pass not break next
|
22
|
+
preexe postexe match_current_line defined?
|
23
|
+
arg_expr).freeze
|
24
|
+
MANY_CHILD_NODES = %i(dstr dsym xstr regexp array hash pair
|
25
|
+
irange erange mlhs masgn or_asgn and_asgn
|
26
|
+
undef alias args super yield or and
|
27
|
+
while_post until_post iflipflop eflipflop
|
28
|
+
match_with_lvasgn begin kwbegin return).freeze
|
29
|
+
SECOND_CHILD_ONLY = %i(lvasgn ivasgn cvasgn gvasgn optarg kwarg
|
30
|
+
kwoptarg).freeze
|
31
31
|
|
32
32
|
NO_CHILD_NODES.each do |type|
|
33
33
|
module_eval("def on_#{type}(node); end")
|
@@ -11,7 +11,7 @@ module RuboCop
|
|
11
11
|
COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})".freeze
|
12
12
|
|
13
13
|
COMMENT_DIRECTIVE_REGEXP = Regexp.new(
|
14
|
-
('
|
14
|
+
('# rubocop : ((?:dis|en)able)\b ' + COPS_PATTERN).gsub(' ', '\s*')
|
15
15
|
)
|
16
16
|
|
17
17
|
CopAnalysis = Struct.new(:line_ranges, :start_line_number)
|
data/lib/rubocop/config.rb
CHANGED
@@ -18,6 +18,7 @@ module RuboCop
|
|
18
18
|
# 2.1 is the oldest officially supported Ruby version.
|
19
19
|
DEFAULT_RUBY_VERSION = 2.1
|
20
20
|
KNOWN_RUBIES = [1.9, 2.0, 2.1, 2.2, 2.3, 2.4].freeze
|
21
|
+
DEFAULT_RAILS_VERSION = 5.0
|
21
22
|
OBSOLETE_COPS = {
|
22
23
|
'Style/TrailingComma' =>
|
23
24
|
'The `Style/TrailingComma` cop no longer exists. Please use ' \
|
@@ -27,7 +28,7 @@ module RuboCop
|
|
27
28
|
'The `Rails/DefaultScope` cop no longer exists.',
|
28
29
|
'Style/SingleSpaceBeforeFirstArg' =>
|
29
30
|
'The `Style/SingleSpaceBeforeFirstArg` cop has been renamed to ' \
|
30
|
-
'`Style/SpaceBeforeFirstArg
|
31
|
+
'`Style/SpaceBeforeFirstArg`.',
|
31
32
|
'Lint/SpaceBeforeFirstArg' =>
|
32
33
|
'The `Lint/SpaceBeforeFirstArg` cop has been removed, since it was a ' \
|
33
34
|
'duplicate of `Style/SpaceBeforeFirstArg`. Please use ' \
|
@@ -96,7 +97,10 @@ module RuboCop
|
|
96
97
|
def initialize(hash = {}, loaded_path = nil)
|
97
98
|
@loaded_path = loaded_path
|
98
99
|
@for_cop = Hash.new do |h, cop|
|
99
|
-
|
100
|
+
qualified_cop_name = Cop::Cop.qualified_cop_name(cop, loaded_path)
|
101
|
+
cop_options = self[qualified_cop_name] || {}
|
102
|
+
cop_options['Enabled'] = enable_cop?(qualified_cop_name, cop_options)
|
103
|
+
h[cop] = cop_options
|
100
104
|
end
|
101
105
|
@hash = hash
|
102
106
|
end
|
@@ -192,14 +196,6 @@ module RuboCop
|
|
192
196
|
@for_all_cops ||= self['AllCops'] || {}
|
193
197
|
end
|
194
198
|
|
195
|
-
def cop_enabled?(cop)
|
196
|
-
if (dept_config = self[cop.department.to_s])
|
197
|
-
return false if dept_config['Enabled'] == false
|
198
|
-
end
|
199
|
-
|
200
|
-
for_cop(cop).empty? || for_cop(cop).fetch('Enabled', true)
|
201
|
-
end
|
202
|
-
|
203
199
|
def validate
|
204
200
|
# Don't validate RuboCop's own files. Avoids infinite recursion.
|
205
201
|
base_config_path = File.expand_path(File.join(ConfigLoader::RUBOCOP_HOME,
|
@@ -215,6 +211,7 @@ module RuboCop
|
|
215
211
|
check_target_ruby
|
216
212
|
validate_parameter_names(valid_cop_names)
|
217
213
|
validate_enforced_styles(valid_cop_names)
|
214
|
+
reject_mutually_exclusive_defaults
|
218
215
|
end
|
219
216
|
|
220
217
|
def file_to_include?(file)
|
@@ -263,15 +260,14 @@ module RuboCop
|
|
263
260
|
relative_path(path, base_dir_for_path_parameters)
|
264
261
|
end
|
265
262
|
|
266
|
-
# Paths specified in
|
267
|
-
# to the directory where that file is. Paths in other config files
|
268
|
-
# relative to the current directory. This is so that paths in
|
263
|
+
# Paths specified in configuration files starting with .rubocop are
|
264
|
+
# relative to the directory where that file is. Paths in other config files
|
265
|
+
# are relative to the current directory. This is so that paths in
|
269
266
|
# config/default.yml, for example, are not relative to RuboCop's config
|
270
267
|
# directory since that wouldn't work.
|
271
268
|
def base_dir_for_path_parameters
|
272
|
-
config_files = [ConfigLoader::DOTFILE, ConfigLoader::AUTO_GENERATED_FILE]
|
273
269
|
@base_dir_for_path_parameters ||=
|
274
|
-
if
|
270
|
+
if File.basename(loaded_path).start_with?('.rubocop') &&
|
275
271
|
loaded_path != File.join(Dir.home, ConfigLoader::DOTFILE)
|
276
272
|
File.expand_path(File.dirname(loaded_path))
|
277
273
|
else
|
@@ -296,6 +292,11 @@ module RuboCop
|
|
296
292
|
end
|
297
293
|
end
|
298
294
|
|
295
|
+
def target_rails_version
|
296
|
+
@target_rails_version ||=
|
297
|
+
for_all_cops.fetch('TargetRailsVersion', DEFAULT_RAILS_VERSION)
|
298
|
+
end
|
299
|
+
|
299
300
|
private
|
300
301
|
|
301
302
|
def warn_about_unrecognized_cops(invalid_cop_names)
|
@@ -400,5 +401,28 @@ module RuboCop
|
|
400
401
|
|
401
402
|
raise ValidationError, msg
|
402
403
|
end
|
404
|
+
|
405
|
+
def reject_mutually_exclusive_defaults
|
406
|
+
disabled_by_default = for_all_cops['DisabledByDefault']
|
407
|
+
enabled_by_default = for_all_cops['EnabledByDefault']
|
408
|
+
return unless disabled_by_default && enabled_by_default
|
409
|
+
|
410
|
+
msg = 'Cops cannot be both enabled by default and disabled by default'
|
411
|
+
raise ValidationError, msg
|
412
|
+
end
|
413
|
+
|
414
|
+
def enable_cop?(qualified_cop_name, cop_options)
|
415
|
+
cop_department, cop_name = qualified_cop_name.split('/')
|
416
|
+
department = cop_name.nil?
|
417
|
+
|
418
|
+
unless department
|
419
|
+
department_options = self[cop_department]
|
420
|
+
if department_options && department_options.fetch('Enabled') == false
|
421
|
+
return false
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
cop_options.fetch('Enabled', true)
|
426
|
+
end
|
403
427
|
end
|
404
428
|
end
|
@@ -37,11 +37,17 @@ module RuboCop
|
|
37
37
|
resolve_requires(path, hash)
|
38
38
|
|
39
39
|
add_missing_namespaces(path, hash)
|
40
|
+
target_ruby_version_to_f!(hash)
|
40
41
|
|
41
42
|
resolve_inheritance_from_gems(hash, hash.delete('inherit_gem'))
|
42
43
|
resolve_inheritance(path, hash)
|
43
44
|
|
44
45
|
hash.delete('inherit_from')
|
46
|
+
|
47
|
+
create_config(hash, path)
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_config(hash, path)
|
45
51
|
config = Config.new(hash, path)
|
46
52
|
|
47
53
|
config.deprecation_check do |deprecation_message|
|
@@ -125,20 +131,35 @@ module RuboCop
|
|
125
131
|
# Merges the given configuration with the default one. If
|
126
132
|
# AllCops:DisabledByDefault is true, it changes the Enabled params so
|
127
133
|
# that only cops from user configuration are enabled.
|
134
|
+
# If AllCops::EnabledByDefault is true, it changes the Enabled params
|
135
|
+
# so that only cops explicitly disabled in user configuration are
|
136
|
+
# disabled.
|
128
137
|
def merge_with_default(config, config_file)
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
[disabled_default, enabled_user_config]
|
138
|
-
else
|
139
|
-
[default_configuration, config]
|
138
|
+
default_configuration = self.default_configuration
|
139
|
+
|
140
|
+
disabled_by_default = config.for_all_cops['DisabledByDefault']
|
141
|
+
enabled_by_default = config.for_all_cops['EnabledByDefault']
|
142
|
+
|
143
|
+
if disabled_by_default || enabled_by_default
|
144
|
+
default_configuration = transform(default_configuration) do |params|
|
145
|
+
params.merge('Enabled' => !disabled_by_default)
|
140
146
|
end
|
141
|
-
|
147
|
+
end
|
148
|
+
|
149
|
+
if disabled_by_default
|
150
|
+
config = transform(config) do |params|
|
151
|
+
{ 'Enabled' => true }.merge(params) # Set true if not set.
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
Config.new(merge(default_configuration, config), config_file)
|
156
|
+
end
|
157
|
+
|
158
|
+
def target_ruby_version_to_f!(hash)
|
159
|
+
version = 'TargetRubyVersion'
|
160
|
+
return unless hash['AllCops'] && hash['AllCops'][version]
|
161
|
+
|
162
|
+
hash['AllCops'][version] = hash['AllCops'][version].to_f
|
142
163
|
end
|
143
164
|
|
144
165
|
private
|
@@ -168,7 +189,7 @@ module RuboCop
|
|
168
189
|
SafeYAML.load(yaml_code, filename,
|
169
190
|
whitelisted_tags: %w(!ruby/regexp))
|
170
191
|
else
|
171
|
-
YAML.safe_load(yaml_code, [Regexp], [], false, filename)
|
192
|
+
YAML.safe_load(yaml_code, [Regexp, Symbol], [], false, filename)
|
172
193
|
end
|
173
194
|
else
|
174
195
|
YAML.load(yaml_code, filename) # rubocop:disable Security/YAMLLoad
|
@@ -1,8 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module RuboCop
|
3
4
|
module Cop
|
4
5
|
module Bundler
|
5
|
-
# Gems
|
6
|
+
# Gems should be alphabetically sorted within groups.
|
7
|
+
#
|
6
8
|
# @example
|
7
9
|
# # bad
|
8
10
|
# gem 'rubocop'
|
@@ -16,7 +18,15 @@ module RuboCop
|
|
16
18
|
# gem 'rubocop'
|
17
19
|
#
|
18
20
|
# gem 'rspec'
|
21
|
+
#
|
22
|
+
# # good only if TreatCommentsAsGroupSeparators is true
|
23
|
+
# # For code quality
|
24
|
+
# gem 'rubocop'
|
25
|
+
# # For tests
|
26
|
+
# gem 'rspec'
|
19
27
|
class OrderedGems < Cop
|
28
|
+
include ConfigurableEnforcedStyle
|
29
|
+
|
20
30
|
MSG = 'Gems should be sorted in an alphabetical order within their '\
|
21
31
|
'section of the Gemfile. '\
|
22
32
|
'Gem `%s` should appear before `%s`.'.freeze
|
@@ -34,11 +44,12 @@ module RuboCop
|
|
34
44
|
end
|
35
45
|
|
36
46
|
def case_insensitive_out_of_order?(string_a, string_b)
|
37
|
-
|
47
|
+
string_a.downcase < string_b.downcase
|
38
48
|
end
|
39
49
|
|
40
50
|
def consecutive_lines(previous, current)
|
41
|
-
|
51
|
+
first_line = get_source_range(current).first_line
|
52
|
+
previous.source_range.last_line == first_line - 1
|
42
53
|
end
|
43
54
|
|
44
55
|
def register_offense(previous, current)
|
@@ -66,7 +77,7 @@ module RuboCop
|
|
66
77
|
|
67
78
|
def declaration_with_comment(node)
|
68
79
|
buffer = processed_source.buffer
|
69
|
-
begin_pos = node.
|
80
|
+
begin_pos = get_source_range(node).begin_pos
|
70
81
|
end_line = buffer.line_for_position(node.loc.expression.end_pos)
|
71
82
|
end_pos = buffer.line_range(end_line).end_pos
|
72
83
|
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
@@ -85,6 +96,14 @@ module RuboCop
|
|
85
96
|
declarations.to_a[node_index - 1]
|
86
97
|
end
|
87
98
|
|
99
|
+
def get_source_range(node)
|
100
|
+
unless cop_config['TreatCommentsAsGroupSeparators']
|
101
|
+
first_comment = processed_source.ast_with_comments[node].first
|
102
|
+
return first_comment.loc.expression unless first_comment.nil?
|
103
|
+
end
|
104
|
+
node.source_range
|
105
|
+
end
|
106
|
+
|
88
107
|
def_node_search :gem_declarations, <<-PATTERN
|
89
108
|
(:send, nil, :gem, ...)
|
90
109
|
PATTERN
|
@@ -73,6 +73,10 @@ module RuboCop
|
|
73
73
|
cop.class.respond_to?(:support_target_ruby_version?) &&
|
74
74
|
!cop.class.support_target_ruby_version?(cop.target_ruby_version)
|
75
75
|
end
|
76
|
+
@cops.reject! do |cop|
|
77
|
+
cop.class.respond_to?(:support_target_rails_version?) &&
|
78
|
+
!cop.class.support_target_rails_version?(cop.target_rails_version)
|
79
|
+
end
|
76
80
|
end
|
77
81
|
|
78
82
|
def reset_callbacks
|
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'uri'
|
3
4
|
|
4
5
|
module RuboCop
|
@@ -150,6 +151,10 @@ module RuboCop
|
|
150
151
|
@config.target_ruby_version
|
151
152
|
end
|
152
153
|
|
154
|
+
def target_rails_version
|
155
|
+
@config.target_rails_version
|
156
|
+
end
|
157
|
+
|
153
158
|
def parse(source, path = nil)
|
154
159
|
ProcessedSource.new(source, target_ruby_version, path)
|
155
160
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for ambiguous block association with method
|
7
|
+
# when param passed without parentheses.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# some_method a { |val| puts val }
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# # With parentheses, there's no ambiguity.
|
18
|
+
# some_method(a) { |val| puts val }
|
19
|
+
class AmbiguousBlockAssociation < Cop
|
20
|
+
MSG = 'Parenthesize the param `%s` to make sure that the block will be'\
|
21
|
+
' associated with the `%s` method call.'.freeze
|
22
|
+
|
23
|
+
def on_send(node)
|
24
|
+
return if node.parenthesized? || node.assignment? || node.method?(:[])
|
25
|
+
|
26
|
+
return unless method_with_block?(node.first_argument)
|
27
|
+
first_param = node.first_argument.children.first
|
28
|
+
return unless method_as_param?(first_param)
|
29
|
+
|
30
|
+
add_offense(node, :expression, message(first_param, node.method_name))
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def method_with_block?(param)
|
36
|
+
return false unless param
|
37
|
+
|
38
|
+
param.block_type?
|
39
|
+
end
|
40
|
+
|
41
|
+
def method_as_param?(param)
|
42
|
+
return false unless param
|
43
|
+
|
44
|
+
param.send_type? && !param.arguments?
|
45
|
+
end
|
46
|
+
|
47
|
+
def message(param, method_name)
|
48
|
+
format(MSG, param.children[1], method_name)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -53,7 +53,14 @@ module RuboCop
|
|
53
53
|
|
54
54
|
def on_send(node)
|
55
55
|
return unless debugger_call?(node) || binding_irb?(node)
|
56
|
-
|
56
|
+
|
57
|
+
add_offense(node, :expression)
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def message(node)
|
63
|
+
format(MSG, node.source)
|
57
64
|
end
|
58
65
|
|
59
66
|
def autocorrect(node)
|
@@ -66,11 +66,9 @@ module RuboCop
|
|
66
66
|
private
|
67
67
|
|
68
68
|
def check(node)
|
69
|
-
receiver, method_name, *_args = *node
|
70
|
-
|
71
69
|
DEPRECATED_METHODS_OBJECT.each do |data|
|
72
|
-
next unless data.class_nodes.include?(receiver)
|
73
|
-
next unless
|
70
|
+
next unless data.class_nodes.include?(node.receiver)
|
71
|
+
next unless node.method?(data.deprecated_method)
|
74
72
|
yield data
|
75
73
|
end
|
76
74
|
end
|