rubocop 0.83.0 → 0.87.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -18
- data/config/default.yml +128 -23
- data/lib/rubocop.rb +21 -59
- data/lib/rubocop/ast_aliases.rb +8 -0
- data/lib/rubocop/cli.rb +2 -4
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +2 -6
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader.rb +24 -70
- data/lib/rubocop/config_loader_resolver.rb +21 -9
- data/lib/rubocop/config_obsoletion.rb +0 -1
- data/lib/rubocop/config_store.rb +12 -2
- data/lib/rubocop/cop/autocorrect_logic.rb +14 -25
- data/lib/rubocop/cop/base.rb +399 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
- data/lib/rubocop/cop/commissioner.rb +48 -71
- data/lib/rubocop/cop/cop.rb +91 -227
- data/lib/rubocop/cop/corrector.rb +38 -115
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -6
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +2 -2
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/ignored_node.rb +1 -3
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
- data/lib/rubocop/cop/layout/case_indentation.rb +21 -22
- data/lib/rubocop/cop/layout/class_structure.rb +19 -16
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +57 -4
- data/lib/rubocop/cop/layout/end_of_line.rb +3 -3
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -4
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -3
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +8 -9
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +3 -7
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +20 -103
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
- data/lib/rubocop/cop/layout/line_length.rb +17 -17
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -2
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +19 -25
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -3
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -3
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
- data/lib/rubocop/cop/legacy/corrector.rb +29 -0
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +5 -2
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +137 -0
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -3
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
- data/lib/rubocop/cop/lint/loop.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +69 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +20 -5
- data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
- data/lib/rubocop/cop/lint/rand_one.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +15 -2
- data/lib/rubocop/cop/lint/syntax.rb +11 -28
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -4
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +22 -0
- data/lib/rubocop/cop/metrics/class_length.rb +25 -2
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/method_length.rb +23 -0
- data/lib/rubocop/cop/metrics/module_length.rb +25 -2
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/migration/department_name.rb +7 -7
- data/lib/rubocop/cop/mixin/alignment.rb +1 -3
- data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/array_min_size.rb +1 -3
- data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -9
- data/lib/rubocop/cop/mixin/code_length.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +3 -5
- data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
- data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -1
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +43 -0
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
- data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
- data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +7 -5
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
- data/lib/rubocop/cop/naming/file_name.rb +28 -17
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +4 -6
- data/lib/rubocop/cop/naming/variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/offense.rb +16 -2
- data/lib/rubocop/cop/registry.rb +63 -10
- data/lib/rubocop/cop/severity.rb +1 -3
- data/lib/rubocop/cop/style/accessor_grouping.rb +136 -0
- data/lib/rubocop/cop/style/and_or.rb +2 -2
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -3
- data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -2
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +121 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +4 -12
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +21 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -4
- data/lib/rubocop/cop/style/copyright.rb +5 -5
- data/lib/rubocop/cop/style/date_time.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +2 -2
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/double_negation.rb +41 -4
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
- data/lib/rubocop/cop/style/empty_literal.rb +6 -8
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
- data/lib/rubocop/cop/style/exponential_notation.rb +5 -5
- data/lib/rubocop/cop/style/format_string_token.rb +2 -3
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -6
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +16 -7
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +1 -3
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -3
- data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
- data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
- data/lib/rubocop/cop/style/negated_if.rb +3 -3
- data/lib/rubocop/cop/style/negated_unless.rb +3 -3
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -6
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
- data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
- data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +10 -8
- data/lib/rubocop/cop/style/redundant_percent_q.rb +3 -3
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +90 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +121 -0
- data/lib/rubocop/cop/style/redundant_self.rb +6 -9
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -6
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +2 -6
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
- data/lib/rubocop/cop/team.rb +141 -81
- data/lib/rubocop/cop/util.rb +3 -3
- data/lib/rubocop/cop/utils/format_string.rb +19 -2
- data/lib/rubocop/cop/variable_force.rb +3 -9
- data/lib/rubocop/cop/variable_force/branch.rb +1 -3
- data/lib/rubocop/cop/variable_force/variable.rb +2 -6
- data/lib/rubocop/ext/processed_source.rb +18 -0
- data/lib/rubocop/formatter/base_formatter.rb +0 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -13
- data/lib/rubocop/formatter/formatter_set.rb +2 -4
- data/lib/rubocop/formatter/junit_formatter.rb +14 -4
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/name_similarity.rb +7 -3
- data/lib/rubocop/options.rb +30 -15
- data/lib/rubocop/path_util.rb +2 -2
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rake_task.rb +6 -9
- data/lib/rubocop/remote_config.rb +1 -3
- data/lib/rubocop/result_cache.rb +5 -7
- data/lib/rubocop/rspec/cop_helper.rb +5 -28
- data/lib/rubocop/rspec/expect_offense.rb +60 -23
- data/lib/rubocop/rspec/shared_contexts.rb +55 -17
- data/lib/rubocop/runner.rb +42 -38
- data/lib/rubocop/target_finder.rb +2 -4
- data/lib/rubocop/target_ruby.rb +5 -2
- data/lib/rubocop/version.rb +5 -3
- metadata +60 -62
- data/lib/rubocop/ast/builder.rb +0 -85
- data/lib/rubocop/ast/node.rb +0 -637
- data/lib/rubocop/ast/node/alias_node.rb +0 -24
- data/lib/rubocop/ast/node/and_node.rb +0 -29
- data/lib/rubocop/ast/node/args_node.rb +0 -29
- data/lib/rubocop/ast/node/array_node.rb +0 -70
- data/lib/rubocop/ast/node/block_node.rb +0 -121
- data/lib/rubocop/ast/node/break_node.rb +0 -17
- data/lib/rubocop/ast/node/case_match_node.rb +0 -56
- data/lib/rubocop/ast/node/case_node.rb +0 -56
- data/lib/rubocop/ast/node/class_node.rb +0 -31
- data/lib/rubocop/ast/node/def_node.rb +0 -82
- data/lib/rubocop/ast/node/defined_node.rb +0 -17
- data/lib/rubocop/ast/node/ensure_node.rb +0 -17
- data/lib/rubocop/ast/node/float_node.rb +0 -12
- data/lib/rubocop/ast/node/for_node.rb +0 -53
- data/lib/rubocop/ast/node/forward_args_node.rb +0 -18
- data/lib/rubocop/ast/node/hash_node.rb +0 -109
- data/lib/rubocop/ast/node/if_node.rb +0 -175
- data/lib/rubocop/ast/node/int_node.rb +0 -12
- data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
- data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +0 -16
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -43
- data/lib/rubocop/ast/node/mixin/collection_node.rb +0 -15
- data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
- data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -269
- data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +0 -114
- data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
- data/lib/rubocop/ast/node/mixin/numeric_node.rb +0 -21
- data/lib/rubocop/ast/node/mixin/parameterized_node.rb +0 -61
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
- data/lib/rubocop/ast/node/module_node.rb +0 -24
- data/lib/rubocop/ast/node/or_node.rb +0 -29
- data/lib/rubocop/ast/node/pair_node.rb +0 -63
- data/lib/rubocop/ast/node/range_node.rb +0 -18
- data/lib/rubocop/ast/node/regexp_node.rb +0 -33
- data/lib/rubocop/ast/node/resbody_node.rb +0 -24
- data/lib/rubocop/ast/node/retry_node.rb +0 -17
- data/lib/rubocop/ast/node/return_node.rb +0 -24
- data/lib/rubocop/ast/node/self_class_node.rb +0 -24
- data/lib/rubocop/ast/node/send_node.rb +0 -17
- data/lib/rubocop/ast/node/str_node.rb +0 -16
- data/lib/rubocop/ast/node/super_node.rb +0 -21
- data/lib/rubocop/ast/node/symbol_node.rb +0 -12
- data/lib/rubocop/ast/node/until_node.rb +0 -35
- data/lib/rubocop/ast/node/when_node.rb +0 -53
- data/lib/rubocop/ast/node/while_node.rb +0 -35
- data/lib/rubocop/ast/node/yield_node.rb +0 -21
- data/lib/rubocop/ast/sexp.rb +0 -16
- data/lib/rubocop/ast/traversal.rb +0 -202
- data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
- data/lib/rubocop/node_pattern.rb +0 -887
- data/lib/rubocop/processed_source.rb +0 -213
- data/lib/rubocop/token.rb +0 -114
@@ -54,9 +54,7 @@ module RuboCop
|
|
54
54
|
# @return [Array] Array of filenames
|
55
55
|
def target_files_in_dir(base_dir = Dir.pwd)
|
56
56
|
# Support Windows: Backslashes from command-line -> forward slashes
|
57
|
-
if File::ALT_SEPARATOR
|
58
|
-
base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
|
59
|
-
end
|
57
|
+
base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
|
60
58
|
all_files = find_files(base_dir, File::FNM_DOTMATCH)
|
61
59
|
hidden_files = Set.new(all_files - find_files(base_dir, 0))
|
62
60
|
base_dir_config = @config_store.for(base_dir)
|
@@ -141,7 +139,7 @@ module RuboCop
|
|
141
139
|
return false unless File.extname(file).empty? && File.exist?(file)
|
142
140
|
|
143
141
|
first_line = File.open(file, &:readline)
|
144
|
-
|
142
|
+
/#!.*(#{ruby_interpreters(file).join('|')})/.match?(first_line)
|
145
143
|
rescue EOFError, ArgumentError => e
|
146
144
|
warn("Unprocessable file #{file}: #{e.class}, #{e.message}") if debug?
|
147
145
|
|
data/lib/rubocop/target_ruby.rb
CHANGED
@@ -3,11 +3,11 @@
|
|
3
3
|
module RuboCop
|
4
4
|
# The kind of Ruby that code inspected by RuboCop is written in.
|
5
5
|
class TargetRuby
|
6
|
-
KNOWN_RUBIES = [2.4, 2.5, 2.6, 2.7].freeze
|
6
|
+
KNOWN_RUBIES = [2.4, 2.5, 2.6, 2.7, 2.8].freeze
|
7
7
|
DEFAULT_VERSION = KNOWN_RUBIES.first
|
8
8
|
|
9
9
|
OBSOLETE_RUBIES = {
|
10
|
-
1.9 => '0.
|
10
|
+
1.9 => '0.41', 2.0 => '0.50', 2.1 => '0.57', 2.2 => '0.68', 2.3 => '0.81'
|
11
11
|
}.freeze
|
12
12
|
private_constant :KNOWN_RUBIES, :OBSOLETE_RUBIES
|
13
13
|
|
@@ -52,7 +52,10 @@ module RuboCop
|
|
52
52
|
file = ruby_version_file
|
53
53
|
return unless file && File.file?(file)
|
54
54
|
|
55
|
+
# rubocop:disable Lint/MixedRegexpCaptureTypes
|
56
|
+
# `(ruby-)` is not a capture type.
|
55
57
|
File.read(file).match(/\A(ruby-)?(?<version>\d+\.\d+)/) do |md|
|
58
|
+
# rubocop:enable Lint/MixedRegexpCaptureTypes
|
56
59
|
md[:version].to_f
|
57
60
|
end
|
58
61
|
end
|
data/lib/rubocop/version.rb
CHANGED
@@ -3,14 +3,16 @@
|
|
3
3
|
module RuboCop
|
4
4
|
# This module holds the RuboCop version information.
|
5
5
|
module Version
|
6
|
-
STRING = '0.
|
6
|
+
STRING = '0.87.0'
|
7
7
|
|
8
|
-
MSG = '%<version>s (using Parser %<parser_version>s,
|
9
|
-
'
|
8
|
+
MSG = '%<version>s (using Parser %<parser_version>s, '\
|
9
|
+
'rubocop-ast %<rubocop_ast_version>s, ' \
|
10
|
+
'running on %<ruby_engine>s %<ruby_version>s %<ruby_platform>s)'
|
10
11
|
|
11
12
|
def self.version(debug = false)
|
12
13
|
if debug
|
13
14
|
format(MSG, version: STRING, parser_version: Parser::VERSION,
|
15
|
+
rubocop_ast_version: RuboCop::AST::Version::STRING,
|
14
16
|
ruby_engine: RUBY_ENGINE, ruby_version: RUBY_VERSION,
|
15
17
|
ruby_platform: RUBY_PLATFORM)
|
16
18
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.87.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-
|
13
|
+
date: 2020-07-06 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: parallel
|
@@ -32,14 +32,14 @@ dependencies:
|
|
32
32
|
requirements:
|
33
33
|
- - ">="
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: 2.7.
|
35
|
+
version: 2.7.1.1
|
36
36
|
type: :runtime
|
37
37
|
prerelease: false
|
38
38
|
version_requirements: !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
40
|
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: 2.7.
|
42
|
+
version: 2.7.1.1
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
44
|
name: rainbow
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,6 +60,20 @@ dependencies:
|
|
60
60
|
- - "<"
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '4.0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: regexp_parser
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.7'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '1.7'
|
63
77
|
- !ruby/object:Gem::Dependency
|
64
78
|
name: rexml
|
65
79
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,6 +88,26 @@ dependencies:
|
|
74
88
|
- - ">="
|
75
89
|
- !ruby/object:Gem::Version
|
76
90
|
version: '0'
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rubocop-ast
|
93
|
+
requirement: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 0.1.0
|
98
|
+
- - "<"
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '1.0'
|
101
|
+
type: :runtime
|
102
|
+
prerelease: false
|
103
|
+
version_requirements: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 0.1.0
|
108
|
+
- - "<"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.0'
|
77
111
|
- !ruby/object:Gem::Dependency
|
78
112
|
name: ruby-progressbar
|
79
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,8 +163,8 @@ dependencies:
|
|
129
163
|
- !ruby/object:Gem::Version
|
130
164
|
version: '3.0'
|
131
165
|
description: |2
|
132
|
-
|
133
|
-
|
166
|
+
RuboCop is a Ruby code style checking and code formatting tool.
|
167
|
+
It aims to enforce the community-driven Ruby Style Guide.
|
134
168
|
email: rubocop@googlegroups.com
|
135
169
|
executables:
|
136
170
|
- rubocop
|
@@ -148,57 +182,7 @@ files:
|
|
148
182
|
- config/default.yml
|
149
183
|
- exe/rubocop
|
150
184
|
- lib/rubocop.rb
|
151
|
-
- lib/rubocop/
|
152
|
-
- lib/rubocop/ast/node.rb
|
153
|
-
- lib/rubocop/ast/node/alias_node.rb
|
154
|
-
- lib/rubocop/ast/node/and_node.rb
|
155
|
-
- lib/rubocop/ast/node/args_node.rb
|
156
|
-
- lib/rubocop/ast/node/array_node.rb
|
157
|
-
- lib/rubocop/ast/node/block_node.rb
|
158
|
-
- lib/rubocop/ast/node/break_node.rb
|
159
|
-
- lib/rubocop/ast/node/case_match_node.rb
|
160
|
-
- lib/rubocop/ast/node/case_node.rb
|
161
|
-
- lib/rubocop/ast/node/class_node.rb
|
162
|
-
- lib/rubocop/ast/node/def_node.rb
|
163
|
-
- lib/rubocop/ast/node/defined_node.rb
|
164
|
-
- lib/rubocop/ast/node/ensure_node.rb
|
165
|
-
- lib/rubocop/ast/node/float_node.rb
|
166
|
-
- lib/rubocop/ast/node/for_node.rb
|
167
|
-
- lib/rubocop/ast/node/forward_args_node.rb
|
168
|
-
- lib/rubocop/ast/node/hash_node.rb
|
169
|
-
- lib/rubocop/ast/node/if_node.rb
|
170
|
-
- lib/rubocop/ast/node/int_node.rb
|
171
|
-
- lib/rubocop/ast/node/keyword_splat_node.rb
|
172
|
-
- lib/rubocop/ast/node/mixin/basic_literal_node.rb
|
173
|
-
- lib/rubocop/ast/node/mixin/binary_operator_node.rb
|
174
|
-
- lib/rubocop/ast/node/mixin/collection_node.rb
|
175
|
-
- lib/rubocop/ast/node/mixin/conditional_node.rb
|
176
|
-
- lib/rubocop/ast/node/mixin/hash_element_node.rb
|
177
|
-
- lib/rubocop/ast/node/mixin/method_dispatch_node.rb
|
178
|
-
- lib/rubocop/ast/node/mixin/method_identifier_predicates.rb
|
179
|
-
- lib/rubocop/ast/node/mixin/modifier_node.rb
|
180
|
-
- lib/rubocop/ast/node/mixin/numeric_node.rb
|
181
|
-
- lib/rubocop/ast/node/mixin/parameterized_node.rb
|
182
|
-
- lib/rubocop/ast/node/mixin/predicate_operator_node.rb
|
183
|
-
- lib/rubocop/ast/node/module_node.rb
|
184
|
-
- lib/rubocop/ast/node/or_node.rb
|
185
|
-
- lib/rubocop/ast/node/pair_node.rb
|
186
|
-
- lib/rubocop/ast/node/range_node.rb
|
187
|
-
- lib/rubocop/ast/node/regexp_node.rb
|
188
|
-
- lib/rubocop/ast/node/resbody_node.rb
|
189
|
-
- lib/rubocop/ast/node/retry_node.rb
|
190
|
-
- lib/rubocop/ast/node/return_node.rb
|
191
|
-
- lib/rubocop/ast/node/self_class_node.rb
|
192
|
-
- lib/rubocop/ast/node/send_node.rb
|
193
|
-
- lib/rubocop/ast/node/str_node.rb
|
194
|
-
- lib/rubocop/ast/node/super_node.rb
|
195
|
-
- lib/rubocop/ast/node/symbol_node.rb
|
196
|
-
- lib/rubocop/ast/node/until_node.rb
|
197
|
-
- lib/rubocop/ast/node/when_node.rb
|
198
|
-
- lib/rubocop/ast/node/while_node.rb
|
199
|
-
- lib/rubocop/ast/node/yield_node.rb
|
200
|
-
- lib/rubocop/ast/sexp.rb
|
201
|
-
- lib/rubocop/ast/traversal.rb
|
185
|
+
- lib/rubocop/ast_aliases.rb
|
202
186
|
- lib/rubocop/cached_data.rb
|
203
187
|
- lib/rubocop/cli.rb
|
204
188
|
- lib/rubocop/cli/command.rb
|
@@ -218,6 +202,7 @@ files:
|
|
218
202
|
- lib/rubocop/config_validator.rb
|
219
203
|
- lib/rubocop/cop/autocorrect_logic.rb
|
220
204
|
- lib/rubocop/cop/badge.rb
|
205
|
+
- lib/rubocop/cop/base.rb
|
221
206
|
- lib/rubocop/cop/bundler/duplicated_gem.rb
|
222
207
|
- lib/rubocop/cop/bundler/gem_comment.rb
|
223
208
|
- lib/rubocop/cop/bundler/insecure_protocol_source.rb
|
@@ -347,6 +332,8 @@ files:
|
|
347
332
|
- lib/rubocop/cop/layout/space_inside_string_interpolation.rb
|
348
333
|
- lib/rubocop/cop/layout/trailing_empty_lines.rb
|
349
334
|
- lib/rubocop/cop/layout/trailing_whitespace.rb
|
335
|
+
- lib/rubocop/cop/legacy/corrections_proxy.rb
|
336
|
+
- lib/rubocop/cop/legacy/corrector.rb
|
350
337
|
- lib/rubocop/cop/lint/ambiguous_block_association.rb
|
351
338
|
- lib/rubocop/cop/lint/ambiguous_operator.rb
|
352
339
|
- lib/rubocop/cop/lint/ambiguous_regexp_literal.rb
|
@@ -354,8 +341,10 @@ files:
|
|
354
341
|
- lib/rubocop/cop/lint/big_decimal_new.rb
|
355
342
|
- lib/rubocop/cop/lint/boolean_symbol.rb
|
356
343
|
- lib/rubocop/cop/lint/circular_argument_reference.rb
|
344
|
+
- lib/rubocop/cop/lint/constant_resolution.rb
|
357
345
|
- lib/rubocop/cop/lint/debugger.rb
|
358
346
|
- lib/rubocop/cop/lint/deprecated_class_methods.rb
|
347
|
+
- lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb
|
359
348
|
- lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb
|
360
349
|
- lib/rubocop/cop/lint/duplicate_case_condition.rb
|
361
350
|
- lib/rubocop/cop/lint/duplicate_hash_key.rb
|
@@ -380,6 +369,7 @@ files:
|
|
380
369
|
- lib/rubocop/cop/lint/literal_in_interpolation.rb
|
381
370
|
- lib/rubocop/cop/lint/loop.rb
|
382
371
|
- lib/rubocop/cop/lint/missing_cop_enable_directive.rb
|
372
|
+
- lib/rubocop/cop/lint/mixed_regexp_capture_types.rb
|
383
373
|
- lib/rubocop/cop/lint/multiple_comparison.rb
|
384
374
|
- lib/rubocop/cop/lint/nested_method_definition.rb
|
385
375
|
- lib/rubocop/cop/lint/nested_percent_literal.rb
|
@@ -441,14 +431,17 @@ files:
|
|
441
431
|
- lib/rubocop/cop/metrics/parameter_lists.rb
|
442
432
|
- lib/rubocop/cop/metrics/perceived_complexity.rb
|
443
433
|
- lib/rubocop/cop/metrics/utils/abc_size_calculator.rb
|
434
|
+
- lib/rubocop/cop/metrics/utils/code_length_calculator.rb
|
435
|
+
- lib/rubocop/cop/metrics/utils/iterating_block.rb
|
444
436
|
- lib/rubocop/cop/migration/department_name.rb
|
445
437
|
- lib/rubocop/cop/mixin/alignment.rb
|
438
|
+
- lib/rubocop/cop/mixin/allowed_methods.rb
|
446
439
|
- lib/rubocop/cop/mixin/annotation_comment.rb
|
447
440
|
- lib/rubocop/cop/mixin/array_min_size.rb
|
448
441
|
- lib/rubocop/cop/mixin/array_syntax.rb
|
442
|
+
- lib/rubocop/cop/mixin/auto_corrector.rb
|
449
443
|
- lib/rubocop/cop/mixin/check_assignment.rb
|
450
444
|
- lib/rubocop/cop/mixin/check_line_breakable.rb
|
451
|
-
- lib/rubocop/cop/mixin/classish_length.rb
|
452
445
|
- lib/rubocop/cop/mixin/code_length.rb
|
453
446
|
- lib/rubocop/cop/mixin/configurable_enforced_style.rb
|
454
447
|
- lib/rubocop/cop/mixin/configurable_formatting.rb
|
@@ -492,6 +485,7 @@ files:
|
|
492
485
|
- lib/rubocop/cop/mixin/preferred_delimiters.rb
|
493
486
|
- lib/rubocop/cop/mixin/range_help.rb
|
494
487
|
- lib/rubocop/cop/mixin/rational_literal.rb
|
488
|
+
- lib/rubocop/cop/mixin/regexp_literal_help.rb
|
495
489
|
- lib/rubocop/cop/mixin/rescue_node.rb
|
496
490
|
- lib/rubocop/cop/mixin/safe_assignment.rb
|
497
491
|
- lib/rubocop/cop/mixin/space_after_punctuation.rb
|
@@ -531,6 +525,7 @@ files:
|
|
531
525
|
- lib/rubocop/cop/security/yaml_load.rb
|
532
526
|
- lib/rubocop/cop/severity.rb
|
533
527
|
- lib/rubocop/cop/style/access_modifier_declarations.rb
|
528
|
+
- lib/rubocop/cop/style/accessor_grouping.rb
|
534
529
|
- lib/rubocop/cop/style/alias.rb
|
535
530
|
- lib/rubocop/cop/style/and_or.rb
|
536
531
|
- lib/rubocop/cop/style/array_join.rb
|
@@ -539,6 +534,7 @@ files:
|
|
539
534
|
- lib/rubocop/cop/style/auto_resource_cleanup.rb
|
540
535
|
- lib/rubocop/cop/style/bare_percent_literals.rb
|
541
536
|
- lib/rubocop/cop/style/begin_block.rb
|
537
|
+
- lib/rubocop/cop/style/bisected_attr_accessor.rb
|
542
538
|
- lib/rubocop/cop/style/block_comments.rb
|
543
539
|
- lib/rubocop/cop/style/block_delimiters.rb
|
544
540
|
- lib/rubocop/cop/style/case_equality.rb
|
@@ -650,15 +646,19 @@ files:
|
|
650
646
|
- lib/rubocop/cop/style/proc.rb
|
651
647
|
- lib/rubocop/cop/style/raise_args.rb
|
652
648
|
- lib/rubocop/cop/style/random_with_offset.rb
|
649
|
+
- lib/rubocop/cop/style/redundant_assignment.rb
|
653
650
|
- lib/rubocop/cop/style/redundant_begin.rb
|
654
651
|
- lib/rubocop/cop/style/redundant_capital_w.rb
|
655
652
|
- lib/rubocop/cop/style/redundant_condition.rb
|
656
653
|
- lib/rubocop/cop/style/redundant_conditional.rb
|
657
654
|
- lib/rubocop/cop/style/redundant_exception.rb
|
655
|
+
- lib/rubocop/cop/style/redundant_fetch_block.rb
|
658
656
|
- lib/rubocop/cop/style/redundant_freeze.rb
|
659
657
|
- lib/rubocop/cop/style/redundant_interpolation.rb
|
660
658
|
- lib/rubocop/cop/style/redundant_parentheses.rb
|
661
659
|
- lib/rubocop/cop/style/redundant_percent_q.rb
|
660
|
+
- lib/rubocop/cop/style/redundant_regexp_character_class.rb
|
661
|
+
- lib/rubocop/cop/style/redundant_regexp_escape.rb
|
662
662
|
- lib/rubocop/cop/style/redundant_return.rb
|
663
663
|
- lib/rubocop/cop/style/redundant_self.rb
|
664
664
|
- lib/rubocop/cop/style/redundant_sort.rb
|
@@ -721,6 +721,7 @@ files:
|
|
721
721
|
- lib/rubocop/cop/variable_force/variable_table.rb
|
722
722
|
- lib/rubocop/core_ext/string.rb
|
723
723
|
- lib/rubocop/error.rb
|
724
|
+
- lib/rubocop/ext/processed_source.rb
|
724
725
|
- lib/rubocop/file_finder.rb
|
725
726
|
- lib/rubocop/formatter/auto_gen_config_formatter.rb
|
726
727
|
- lib/rubocop/formatter/base_formatter.rb
|
@@ -744,11 +745,9 @@ files:
|
|
744
745
|
- lib/rubocop/formatter/worst_offenders_formatter.rb
|
745
746
|
- lib/rubocop/magic_comment.rb
|
746
747
|
- lib/rubocop/name_similarity.rb
|
747
|
-
- lib/rubocop/node_pattern.rb
|
748
748
|
- lib/rubocop/options.rb
|
749
749
|
- lib/rubocop/path_util.rb
|
750
750
|
- lib/rubocop/platform.rb
|
751
|
-
- lib/rubocop/processed_source.rb
|
752
751
|
- lib/rubocop/rake_task.rb
|
753
752
|
- lib/rubocop/remote_config.rb
|
754
753
|
- lib/rubocop/result_cache.rb
|
@@ -761,7 +760,6 @@ files:
|
|
761
760
|
- lib/rubocop/string_interpreter.rb
|
762
761
|
- lib/rubocop/target_finder.rb
|
763
762
|
- lib/rubocop/target_ruby.rb
|
764
|
-
- lib/rubocop/token.rb
|
765
763
|
- lib/rubocop/version.rb
|
766
764
|
- lib/rubocop/warning.rb
|
767
765
|
- lib/rubocop/yaml_duplication_checker.rb
|
@@ -769,7 +767,7 @@ homepage: https://github.com/rubocop-hq/rubocop
|
|
769
767
|
licenses:
|
770
768
|
- MIT
|
771
769
|
metadata:
|
772
|
-
homepage_uri: https://
|
770
|
+
homepage_uri: https://rubocop.org/
|
773
771
|
changelog_uri: https://github.com/rubocop-hq/rubocop/blob/master/CHANGELOG.md
|
774
772
|
source_code_uri: https://github.com/rubocop-hq/rubocop/
|
775
773
|
documentation_uri: https://docs.rubocop.org/
|
data/lib/rubocop/ast/builder.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module AST
|
5
|
-
# `RuboCop::AST::Builder` is an AST builder that is utilized to let `Parser`
|
6
|
-
# generate ASTs with {RuboCop::AST::Node}.
|
7
|
-
#
|
8
|
-
# @example
|
9
|
-
# buffer = Parser::Source::Buffer.new('(string)')
|
10
|
-
# buffer.source = 'puts :foo'
|
11
|
-
#
|
12
|
-
# builder = RuboCop::AST::Builder.new
|
13
|
-
# require 'parser/ruby25'
|
14
|
-
# parser = Parser::Ruby25.new(builder)
|
15
|
-
# root_node = parser.parse(buffer)
|
16
|
-
class Builder < Parser::Builders::Default
|
17
|
-
NODE_MAP = {
|
18
|
-
and: AndNode,
|
19
|
-
alias: AliasNode,
|
20
|
-
args: ArgsNode,
|
21
|
-
array: ArrayNode,
|
22
|
-
block: BlockNode,
|
23
|
-
numblock: BlockNode,
|
24
|
-
break: BreakNode,
|
25
|
-
case_match: CaseMatchNode,
|
26
|
-
case: CaseNode,
|
27
|
-
class: ClassNode,
|
28
|
-
def: DefNode,
|
29
|
-
defined?: DefinedNode,
|
30
|
-
defs: DefNode,
|
31
|
-
ensure: EnsureNode,
|
32
|
-
for: ForNode,
|
33
|
-
forward_args: ForwardArgsNode,
|
34
|
-
float: FloatNode,
|
35
|
-
hash: HashNode,
|
36
|
-
if: IfNode,
|
37
|
-
int: IntNode,
|
38
|
-
irange: RangeNode,
|
39
|
-
erange: RangeNode,
|
40
|
-
kwsplat: KeywordSplatNode,
|
41
|
-
module: ModuleNode,
|
42
|
-
or: OrNode,
|
43
|
-
pair: PairNode,
|
44
|
-
regexp: RegexpNode,
|
45
|
-
resbody: ResbodyNode,
|
46
|
-
retry: RetryNode,
|
47
|
-
return: ReturnNode,
|
48
|
-
csend: SendNode,
|
49
|
-
send: SendNode,
|
50
|
-
str: StrNode,
|
51
|
-
dstr: StrNode,
|
52
|
-
xstr: StrNode,
|
53
|
-
sclass: SelfClassNode,
|
54
|
-
super: SuperNode,
|
55
|
-
zsuper: SuperNode,
|
56
|
-
sym: SymbolNode,
|
57
|
-
until: UntilNode,
|
58
|
-
until_post: UntilNode,
|
59
|
-
when: WhenNode,
|
60
|
-
while: WhileNode,
|
61
|
-
while_post: WhileNode,
|
62
|
-
yield: YieldNode
|
63
|
-
}.freeze
|
64
|
-
|
65
|
-
# Generates {Node} from the given information.
|
66
|
-
#
|
67
|
-
# @return [Node] the generated node
|
68
|
-
def n(type, children, source_map)
|
69
|
-
node_klass(type).new(type, children, location: source_map)
|
70
|
-
end
|
71
|
-
|
72
|
-
# TODO: Figure out what to do about literal encoding handling...
|
73
|
-
# More details here https://github.com/whitequark/parser/issues/283
|
74
|
-
def string_value(token)
|
75
|
-
value(token)
|
76
|
-
end
|
77
|
-
|
78
|
-
private
|
79
|
-
|
80
|
-
def node_klass(type)
|
81
|
-
NODE_MAP[type] || Node
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
data/lib/rubocop/ast/node.rb
DELETED
@@ -1,637 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module AST
|
5
|
-
# `RuboCop::AST::Node` is a subclass of `Parser::AST::Node`. It provides
|
6
|
-
# access to parent nodes and an object-oriented way to traverse an AST with
|
7
|
-
# the power of `Enumerable`.
|
8
|
-
#
|
9
|
-
# It has predicate methods for every node type, like this:
|
10
|
-
#
|
11
|
-
# @example
|
12
|
-
# node.send_type? # Equivalent to: `node.type == :send`
|
13
|
-
# node.op_asgn_type? # Equivalent to: `node.type == :op_asgn`
|
14
|
-
#
|
15
|
-
# # Non-word characters (other than a-zA-Z0-9_) in type names are omitted.
|
16
|
-
# node.defined_type? # Equivalent to: `node.type == :defined?`
|
17
|
-
#
|
18
|
-
# # Find the first lvar node under the receiver node.
|
19
|
-
# lvar_node = node.each_descendant.find(&:lvar_type?)
|
20
|
-
#
|
21
|
-
class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
|
22
|
-
include RuboCop::AST::Sexp
|
23
|
-
extend NodePattern::Macros
|
24
|
-
|
25
|
-
# <=> isn't included here, because it doesn't return a boolean.
|
26
|
-
COMPARISON_OPERATORS = %i[== === != <= >= > <].freeze
|
27
|
-
|
28
|
-
TRUTHY_LITERALS = %i[str dstr xstr int float sym dsym array
|
29
|
-
hash regexp true irange erange complex
|
30
|
-
rational regopt].freeze
|
31
|
-
FALSEY_LITERALS = %i[false nil].freeze
|
32
|
-
LITERALS = (TRUTHY_LITERALS + FALSEY_LITERALS).freeze
|
33
|
-
COMPOSITE_LITERALS = %i[dstr xstr dsym array hash irange
|
34
|
-
erange regexp].freeze
|
35
|
-
BASIC_LITERALS = (LITERALS - COMPOSITE_LITERALS).freeze
|
36
|
-
MUTABLE_LITERALS = %i[str dstr xstr array hash
|
37
|
-
regexp irange erange].freeze
|
38
|
-
IMMUTABLE_LITERALS = (LITERALS - MUTABLE_LITERALS).freeze
|
39
|
-
|
40
|
-
EQUALS_ASSIGNMENTS = %i[lvasgn ivasgn cvasgn gvasgn
|
41
|
-
casgn masgn].freeze
|
42
|
-
SHORTHAND_ASSIGNMENTS = %i[op_asgn or_asgn and_asgn].freeze
|
43
|
-
ASSIGNMENTS = (EQUALS_ASSIGNMENTS + SHORTHAND_ASSIGNMENTS).freeze
|
44
|
-
|
45
|
-
BASIC_CONDITIONALS = %i[if while until].freeze
|
46
|
-
CONDITIONALS = [*BASIC_CONDITIONALS, :case].freeze
|
47
|
-
VARIABLES = %i[ivar gvar cvar lvar].freeze
|
48
|
-
REFERENCES = %i[nth_ref back_ref].freeze
|
49
|
-
KEYWORDS = %i[alias and break case class def defs defined?
|
50
|
-
kwbegin do else ensure for if module next
|
51
|
-
not or postexe redo rescue retry return self
|
52
|
-
super zsuper then undef until when while
|
53
|
-
yield].freeze
|
54
|
-
OPERATOR_KEYWORDS = %i[and or].freeze
|
55
|
-
SPECIAL_KEYWORDS = %w[__FILE__ __LINE__ __ENCODING__].freeze
|
56
|
-
|
57
|
-
# @see https://www.rubydoc.info/gems/ast/AST/Node:initialize
|
58
|
-
def initialize(type, children = [], properties = {})
|
59
|
-
@mutable_attributes = {}
|
60
|
-
|
61
|
-
# ::AST::Node#initialize freezes itself.
|
62
|
-
super
|
63
|
-
|
64
|
-
# #parent= may be invoked multiple times for a node because there are
|
65
|
-
# pending nodes while constructing AST and they are replaced later.
|
66
|
-
# For example, `lvar` and `send` type nodes are initially created as an
|
67
|
-
# `ident` type node and fixed to the appropriate type later.
|
68
|
-
# So, the #parent attribute needs to be mutable.
|
69
|
-
each_child_node do |child_node|
|
70
|
-
child_node.parent = self unless child_node.complete?
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
Parser::Meta::NODE_TYPES.each do |node_type|
|
75
|
-
method_name = "#{node_type.to_s.gsub(/\W/, '')}_type?"
|
76
|
-
define_method(method_name) do
|
77
|
-
type == node_type
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# Returns the parent node, or `nil` if the receiver is a root node.
|
82
|
-
#
|
83
|
-
# @return [Node, nil] the parent node or `nil`
|
84
|
-
def parent
|
85
|
-
@mutable_attributes[:parent]
|
86
|
-
end
|
87
|
-
|
88
|
-
def parent=(node)
|
89
|
-
@mutable_attributes[:parent] = node
|
90
|
-
end
|
91
|
-
|
92
|
-
def complete!
|
93
|
-
@mutable_attributes.freeze
|
94
|
-
each_child_node(&:complete!)
|
95
|
-
end
|
96
|
-
|
97
|
-
def complete?
|
98
|
-
@mutable_attributes.frozen?
|
99
|
-
end
|
100
|
-
|
101
|
-
protected :parent=
|
102
|
-
|
103
|
-
# Override `AST::Node#updated` so that `AST::Processor` does not try to
|
104
|
-
# mutate our ASTs. Since we keep references from children to parents and
|
105
|
-
# not just the other way around, we cannot update an AST and share
|
106
|
-
# identical subtrees. Rather, the entire AST must be copied any time any
|
107
|
-
# part of it is changed.
|
108
|
-
def updated(type = nil, children = nil, properties = {})
|
109
|
-
properties[:location] ||= @location
|
110
|
-
klass = RuboCop::AST::Builder::NODE_MAP[type || @type] || Node
|
111
|
-
klass.new(type || @type, children || @children, properties)
|
112
|
-
end
|
113
|
-
|
114
|
-
# Returns the index of the receiver node in its siblings. (Sibling index
|
115
|
-
# uses zero based numbering.)
|
116
|
-
#
|
117
|
-
# @return [Integer] the index of the receiver node in its siblings
|
118
|
-
def sibling_index
|
119
|
-
parent&.children&.index { |sibling| sibling.equal?(self) }
|
120
|
-
end
|
121
|
-
|
122
|
-
# Common destructuring method. This can be used to normalize
|
123
|
-
# destructuring for different variations of the node.
|
124
|
-
# Some node types override this with their own custom
|
125
|
-
# destructuring method.
|
126
|
-
#
|
127
|
-
# @return [Array<Node>] the different parts of the ndde
|
128
|
-
def node_parts
|
129
|
-
to_a
|
130
|
-
end
|
131
|
-
|
132
|
-
# Calls the given block for each ancestor node from parent to root.
|
133
|
-
# If no block is given, an `Enumerator` is returned.
|
134
|
-
#
|
135
|
-
# @overload each_ancestor
|
136
|
-
# Yield all nodes.
|
137
|
-
# @overload each_ancestor(type)
|
138
|
-
# Yield only nodes matching the type.
|
139
|
-
# @param [Symbol] type a node type
|
140
|
-
# @overload each_ancestor(type_a, type_b, ...)
|
141
|
-
# Yield only nodes matching any of the types.
|
142
|
-
# @param [Symbol] type_a a node type
|
143
|
-
# @param [Symbol] type_b a node type
|
144
|
-
# @yieldparam [Node] node each ancestor node
|
145
|
-
# @return [self] if a block is given
|
146
|
-
# @return [Enumerator] if no block is given
|
147
|
-
def each_ancestor(*types, &block)
|
148
|
-
return to_enum(__method__, *types) unless block_given?
|
149
|
-
|
150
|
-
visit_ancestors(types, &block)
|
151
|
-
|
152
|
-
self
|
153
|
-
end
|
154
|
-
|
155
|
-
# Returns an array of ancestor nodes.
|
156
|
-
# This is a shorthand for `node.each_ancestor.to_a`.
|
157
|
-
#
|
158
|
-
# @return [Array<Node>] an array of ancestor nodes
|
159
|
-
def ancestors
|
160
|
-
each_ancestor.to_a
|
161
|
-
end
|
162
|
-
|
163
|
-
# Calls the given block for each child node.
|
164
|
-
# If no block is given, an `Enumerator` is returned.
|
165
|
-
#
|
166
|
-
# Note that this is different from `node.children.each { |child| ... }`
|
167
|
-
# which yields all children including non-node elements.
|
168
|
-
#
|
169
|
-
# @overload each_child_node
|
170
|
-
# Yield all nodes.
|
171
|
-
# @overload each_child_node(type)
|
172
|
-
# Yield only nodes matching the type.
|
173
|
-
# @param [Symbol] type a node type
|
174
|
-
# @overload each_child_node(type_a, type_b, ...)
|
175
|
-
# Yield only nodes matching any of the types.
|
176
|
-
# @param [Symbol] type_a a node type
|
177
|
-
# @param [Symbol] type_b a node type
|
178
|
-
# @yieldparam [Node] node each child node
|
179
|
-
# @return [self] if a block is given
|
180
|
-
# @return [Enumerator] if no block is given
|
181
|
-
def each_child_node(*types)
|
182
|
-
return to_enum(__method__, *types) unless block_given?
|
183
|
-
|
184
|
-
children.each do |child|
|
185
|
-
next unless child.is_a?(Node)
|
186
|
-
|
187
|
-
yield child if types.empty? || types.include?(child.type)
|
188
|
-
end
|
189
|
-
|
190
|
-
self
|
191
|
-
end
|
192
|
-
|
193
|
-
# Returns an array of child nodes.
|
194
|
-
# This is a shorthand for `node.each_child_node.to_a`.
|
195
|
-
#
|
196
|
-
# @return [Array<Node>] an array of child nodes
|
197
|
-
def child_nodes
|
198
|
-
each_child_node.to_a
|
199
|
-
end
|
200
|
-
|
201
|
-
# Calls the given block for each descendant node with depth first order.
|
202
|
-
# If no block is given, an `Enumerator` is returned.
|
203
|
-
#
|
204
|
-
# @overload each_descendant
|
205
|
-
# Yield all nodes.
|
206
|
-
# @overload each_descendant(type)
|
207
|
-
# Yield only nodes matching the type.
|
208
|
-
# @param [Symbol] type a node type
|
209
|
-
# @overload each_descendant(type_a, type_b, ...)
|
210
|
-
# Yield only nodes matching any of the types.
|
211
|
-
# @param [Symbol] type_a a node type
|
212
|
-
# @param [Symbol] type_b a node type
|
213
|
-
# @yieldparam [Node] node each descendant node
|
214
|
-
# @return [self] if a block is given
|
215
|
-
# @return [Enumerator] if no block is given
|
216
|
-
def each_descendant(*types, &block)
|
217
|
-
return to_enum(__method__, *types) unless block_given?
|
218
|
-
|
219
|
-
visit_descendants(types, &block)
|
220
|
-
|
221
|
-
self
|
222
|
-
end
|
223
|
-
|
224
|
-
# Returns an array of descendant nodes.
|
225
|
-
# This is a shorthand for `node.each_descendant.to_a`.
|
226
|
-
#
|
227
|
-
# @return [Array<Node>] an array of descendant nodes
|
228
|
-
def descendants
|
229
|
-
each_descendant.to_a
|
230
|
-
end
|
231
|
-
|
232
|
-
# Calls the given block for the receiver and each descendant node in
|
233
|
-
# depth-first order.
|
234
|
-
# If no block is given, an `Enumerator` is returned.
|
235
|
-
#
|
236
|
-
# This method would be useful when you treat the receiver node as the root
|
237
|
-
# of a tree and want to iterate over all nodes in the tree.
|
238
|
-
#
|
239
|
-
# @overload each_node
|
240
|
-
# Yield all nodes.
|
241
|
-
# @overload each_node(type)
|
242
|
-
# Yield only nodes matching the type.
|
243
|
-
# @param [Symbol] type a node type
|
244
|
-
# @overload each_node(type_a, type_b, ...)
|
245
|
-
# Yield only nodes matching any of the types.
|
246
|
-
# @param [Symbol] type_a a node type
|
247
|
-
# @param [Symbol] type_b a node type
|
248
|
-
# @yieldparam [Node] node each node
|
249
|
-
# @return [self] if a block is given
|
250
|
-
# @return [Enumerator] if no block is given
|
251
|
-
def each_node(*types, &block)
|
252
|
-
return to_enum(__method__, *types) unless block_given?
|
253
|
-
|
254
|
-
yield self if types.empty? || types.include?(type)
|
255
|
-
|
256
|
-
visit_descendants(types, &block)
|
257
|
-
|
258
|
-
self
|
259
|
-
end
|
260
|
-
|
261
|
-
def source
|
262
|
-
loc.expression.source
|
263
|
-
end
|
264
|
-
|
265
|
-
def source_range
|
266
|
-
loc.expression
|
267
|
-
end
|
268
|
-
|
269
|
-
def first_line
|
270
|
-
loc.line
|
271
|
-
end
|
272
|
-
|
273
|
-
def last_line
|
274
|
-
loc.last_line
|
275
|
-
end
|
276
|
-
|
277
|
-
def line_count
|
278
|
-
return 0 unless source_range
|
279
|
-
|
280
|
-
source_range.last_line - source_range.first_line + 1
|
281
|
-
end
|
282
|
-
|
283
|
-
def nonempty_line_count
|
284
|
-
source.lines.grep(/\S/).size
|
285
|
-
end
|
286
|
-
|
287
|
-
def source_length
|
288
|
-
source_range ? source_range.size : 0
|
289
|
-
end
|
290
|
-
|
291
|
-
## Destructuring
|
292
|
-
|
293
|
-
def_node_matcher :receiver, <<~PATTERN
|
294
|
-
{(send $_ ...) ({block numblock} (send $_ ...) ...)}
|
295
|
-
PATTERN
|
296
|
-
|
297
|
-
def_node_matcher :str_content, '(str $_)'
|
298
|
-
|
299
|
-
def const_name
|
300
|
-
return unless const_type?
|
301
|
-
|
302
|
-
namespace, name = *self
|
303
|
-
if namespace && !namespace.cbase_type?
|
304
|
-
"#{namespace.const_name}::#{name}"
|
305
|
-
else
|
306
|
-
name.to_s
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
def_node_matcher :defined_module0, <<~PATTERN
|
311
|
-
{(class (const $_ $_) ...)
|
312
|
-
(module (const $_ $_) ...)
|
313
|
-
(casgn $_ $_ (send (const nil? {:Class :Module}) :new ...))
|
314
|
-
(casgn $_ $_ (block (send (const nil? {:Class :Module}) :new ...) ...))}
|
315
|
-
PATTERN
|
316
|
-
|
317
|
-
private :defined_module0
|
318
|
-
|
319
|
-
def defined_module
|
320
|
-
namespace, name = *defined_module0
|
321
|
-
s(:const, namespace, name) if name
|
322
|
-
end
|
323
|
-
|
324
|
-
def defined_module_name
|
325
|
-
(const = defined_module) && const.const_name
|
326
|
-
end
|
327
|
-
|
328
|
-
## Searching the AST
|
329
|
-
|
330
|
-
def parent_module_name
|
331
|
-
# what class or module is this method/constant/etc definition in?
|
332
|
-
# returns nil if answer cannot be determined
|
333
|
-
ancestors = each_ancestor(:class, :module, :sclass, :casgn, :block)
|
334
|
-
result = ancestors.map do |ancestor|
|
335
|
-
parent_module_name_part(ancestor) { |full_name| return full_name }
|
336
|
-
end.compact.reverse.join('::')
|
337
|
-
result.empty? ? 'Object' : result
|
338
|
-
end
|
339
|
-
|
340
|
-
## Predicates
|
341
|
-
|
342
|
-
def multiline?
|
343
|
-
line_count > 1
|
344
|
-
end
|
345
|
-
|
346
|
-
def single_line?
|
347
|
-
line_count == 1
|
348
|
-
end
|
349
|
-
|
350
|
-
def empty_source?
|
351
|
-
source_length.zero?
|
352
|
-
end
|
353
|
-
|
354
|
-
# Some cops treat the shovel operator as a kind of assignment.
|
355
|
-
def_node_matcher :assignment_or_similar?, <<~PATTERN
|
356
|
-
{assignment? (send _recv :<< ...)}
|
357
|
-
PATTERN
|
358
|
-
|
359
|
-
def literal?
|
360
|
-
LITERALS.include?(type)
|
361
|
-
end
|
362
|
-
|
363
|
-
def basic_literal?
|
364
|
-
BASIC_LITERALS.include?(type)
|
365
|
-
end
|
366
|
-
|
367
|
-
def truthy_literal?
|
368
|
-
TRUTHY_LITERALS.include?(type)
|
369
|
-
end
|
370
|
-
|
371
|
-
def falsey_literal?
|
372
|
-
FALSEY_LITERALS.include?(type)
|
373
|
-
end
|
374
|
-
|
375
|
-
def mutable_literal?
|
376
|
-
MUTABLE_LITERALS.include?(type)
|
377
|
-
end
|
378
|
-
|
379
|
-
def immutable_literal?
|
380
|
-
IMMUTABLE_LITERALS.include?(type)
|
381
|
-
end
|
382
|
-
|
383
|
-
%i[literal basic_literal].each do |kind|
|
384
|
-
recursive_kind = :"recursive_#{kind}?"
|
385
|
-
kind_filter = :"#{kind}?"
|
386
|
-
define_method(recursive_kind) do
|
387
|
-
case type
|
388
|
-
when :send
|
389
|
-
[*COMPARISON_OPERATORS, :!, :<=>].include?(method_name) &&
|
390
|
-
receiver.send(recursive_kind) &&
|
391
|
-
arguments.all?(&recursive_kind)
|
392
|
-
when :begin, :pair, *OPERATOR_KEYWORDS, *COMPOSITE_LITERALS
|
393
|
-
children.compact.all?(&recursive_kind)
|
394
|
-
else
|
395
|
-
send(kind_filter)
|
396
|
-
end
|
397
|
-
end
|
398
|
-
end
|
399
|
-
|
400
|
-
def variable?
|
401
|
-
VARIABLES.include?(type)
|
402
|
-
end
|
403
|
-
|
404
|
-
def reference?
|
405
|
-
REFERENCES.include?(type)
|
406
|
-
end
|
407
|
-
|
408
|
-
def equals_asgn?
|
409
|
-
EQUALS_ASSIGNMENTS.include?(type)
|
410
|
-
end
|
411
|
-
|
412
|
-
def shorthand_asgn?
|
413
|
-
SHORTHAND_ASSIGNMENTS.include?(type)
|
414
|
-
end
|
415
|
-
|
416
|
-
def assignment?
|
417
|
-
ASSIGNMENTS.include?(type)
|
418
|
-
end
|
419
|
-
|
420
|
-
def basic_conditional?
|
421
|
-
BASIC_CONDITIONALS.include?(type)
|
422
|
-
end
|
423
|
-
|
424
|
-
def conditional?
|
425
|
-
CONDITIONALS.include?(type)
|
426
|
-
end
|
427
|
-
|
428
|
-
def keyword?
|
429
|
-
return true if special_keyword? || send_type? && prefix_not?
|
430
|
-
return false unless KEYWORDS.include?(type)
|
431
|
-
|
432
|
-
!OPERATOR_KEYWORDS.include?(type) || loc.operator.is?(type.to_s)
|
433
|
-
end
|
434
|
-
|
435
|
-
def special_keyword?
|
436
|
-
SPECIAL_KEYWORDS.include?(source)
|
437
|
-
end
|
438
|
-
|
439
|
-
def operator_keyword?
|
440
|
-
OPERATOR_KEYWORDS.include?(type)
|
441
|
-
end
|
442
|
-
|
443
|
-
def parenthesized_call?
|
444
|
-
loc.respond_to?(:begin) && loc.begin && loc.begin.is?('(')
|
445
|
-
end
|
446
|
-
|
447
|
-
def call_type?
|
448
|
-
send_type? || csend_type?
|
449
|
-
end
|
450
|
-
|
451
|
-
def chained?
|
452
|
-
parent&.call_type? && eql?(parent.receiver)
|
453
|
-
end
|
454
|
-
|
455
|
-
def argument?
|
456
|
-
parent&.send_type? && parent.arguments.include?(self)
|
457
|
-
end
|
458
|
-
|
459
|
-
def boolean_type?
|
460
|
-
true_type? || false_type?
|
461
|
-
end
|
462
|
-
|
463
|
-
def numeric_type?
|
464
|
-
int_type? || float_type?
|
465
|
-
end
|
466
|
-
|
467
|
-
def range_type?
|
468
|
-
irange_type? || erange_type?
|
469
|
-
end
|
470
|
-
|
471
|
-
def guard_clause?
|
472
|
-
node = and_type? || or_type? ? rhs : self
|
473
|
-
|
474
|
-
node.match_guard_clause?
|
475
|
-
end
|
476
|
-
|
477
|
-
def_node_matcher :match_guard_clause?, <<~PATTERN
|
478
|
-
[${(send nil? {:raise :fail} ...) return break next} single_line?]
|
479
|
-
PATTERN
|
480
|
-
|
481
|
-
def_node_matcher :proc?, <<~PATTERN
|
482
|
-
{(block (send nil? :proc) ...)
|
483
|
-
(block (send (const nil? :Proc) :new) ...)
|
484
|
-
(send (const nil? :Proc) :new)}
|
485
|
-
PATTERN
|
486
|
-
|
487
|
-
def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
|
488
|
-
def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
|
489
|
-
|
490
|
-
def_node_matcher :class_constructor?, <<~PATTERN
|
491
|
-
{ (send (const nil? {:Class :Module}) :new ...)
|
492
|
-
(block (send (const nil? {:Class :Module}) :new ...) ...)}
|
493
|
-
PATTERN
|
494
|
-
|
495
|
-
# Some expressions are evaluated for their value, some for their side
|
496
|
-
# effects, and some for both
|
497
|
-
# If we know that an expression is useful only for its side effects, that
|
498
|
-
# means we can transform it in ways which preserve the side effects, but
|
499
|
-
# change the return value
|
500
|
-
# So, does the return value of this node matter? If we changed it to
|
501
|
-
# `(...; nil)`, might that affect anything?
|
502
|
-
#
|
503
|
-
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
|
504
|
-
def value_used?
|
505
|
-
# Be conservative and return true if we're not sure.
|
506
|
-
return false if parent.nil?
|
507
|
-
|
508
|
-
case parent.type
|
509
|
-
when :array, :defined?, :dstr, :dsym, :eflipflop, :erange, :float,
|
510
|
-
:hash, :iflipflop, :irange, :not, :pair, :regexp, :str, :sym,
|
511
|
-
:when, :xstr
|
512
|
-
parent.value_used?
|
513
|
-
when :begin, :kwbegin
|
514
|
-
begin_value_used?
|
515
|
-
when :for
|
516
|
-
for_value_used?
|
517
|
-
when :case, :if
|
518
|
-
case_if_value_used?
|
519
|
-
when :while, :until, :while_post, :until_post
|
520
|
-
while_until_value_used?
|
521
|
-
else
|
522
|
-
true
|
523
|
-
end
|
524
|
-
end
|
525
|
-
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity
|
526
|
-
|
527
|
-
# Some expressions are evaluated for their value, some for their side
|
528
|
-
# effects, and some for both.
|
529
|
-
# If we know that expressions are useful only for their return values,
|
530
|
-
# and have no side effects, that means we can reorder them, change the
|
531
|
-
# number of times they are evaluated, or replace them with other
|
532
|
-
# expressions which are equivalent in value.
|
533
|
-
# So, is evaluation of this node free of side effects?
|
534
|
-
#
|
535
|
-
def pure?
|
536
|
-
# Be conservative and return false if we're not sure
|
537
|
-
case type
|
538
|
-
when :__FILE__, :__LINE__, :const, :cvar, :defined?, :false, :float,
|
539
|
-
:gvar, :int, :ivar, :lvar, :nil, :str, :sym, :true, :regopt
|
540
|
-
true
|
541
|
-
when :and, :array, :begin, :case, :dstr, :dsym, :eflipflop, :ensure,
|
542
|
-
:erange, :for, :hash, :if, :iflipflop, :irange, :kwbegin, :not,
|
543
|
-
:or, :pair, :regexp, :until, :until_post, :when, :while,
|
544
|
-
:while_post
|
545
|
-
child_nodes.all?(&:pure?)
|
546
|
-
else
|
547
|
-
false
|
548
|
-
end
|
549
|
-
end
|
550
|
-
|
551
|
-
protected
|
552
|
-
|
553
|
-
def visit_descendants(types, &block)
|
554
|
-
each_child_node do |child|
|
555
|
-
yield child if types.empty? || types.include?(child.type)
|
556
|
-
child.visit_descendants(types, &block)
|
557
|
-
end
|
558
|
-
end
|
559
|
-
|
560
|
-
private
|
561
|
-
|
562
|
-
def visit_ancestors(types)
|
563
|
-
last_node = self
|
564
|
-
|
565
|
-
while (current_node = last_node.parent)
|
566
|
-
yield current_node if types.empty? ||
|
567
|
-
types.include?(current_node.type)
|
568
|
-
last_node = current_node
|
569
|
-
end
|
570
|
-
end
|
571
|
-
|
572
|
-
def begin_value_used?
|
573
|
-
# the last child node determines the value of the parent
|
574
|
-
sibling_index == parent.children.size - 1 ? parent.value_used? : false
|
575
|
-
end
|
576
|
-
|
577
|
-
def for_value_used?
|
578
|
-
# `for var in enum; body; end`
|
579
|
-
# (for <var> <enum> <body>)
|
580
|
-
sibling_index == 2 ? parent.value_used? : true
|
581
|
-
end
|
582
|
-
|
583
|
-
def case_if_value_used?
|
584
|
-
# (case <condition> <when...>)
|
585
|
-
# (if <condition> <truebranch> <falsebranch>)
|
586
|
-
sibling_index.zero? ? true : parent.value_used?
|
587
|
-
end
|
588
|
-
|
589
|
-
def while_until_value_used?
|
590
|
-
# (while <condition> <body>) -> always evaluates to `nil`
|
591
|
-
sibling_index.zero?
|
592
|
-
end
|
593
|
-
|
594
|
-
def parent_module_name_part(node)
|
595
|
-
case node.type
|
596
|
-
when :class, :module, :casgn
|
597
|
-
# TODO: if constant name has cbase (leading ::), then we don't need
|
598
|
-
# to keep traversing up through nested classes/modules
|
599
|
-
node.defined_module_name
|
600
|
-
when :sclass
|
601
|
-
yield parent_module_name_for_sclass(node)
|
602
|
-
else # block
|
603
|
-
parent_module_name_for_block(node) { yield nil }
|
604
|
-
end
|
605
|
-
end
|
606
|
-
|
607
|
-
def parent_module_name_for_sclass(sclass_node)
|
608
|
-
# TODO: look for constant definition and see if it is nested
|
609
|
-
# inside a class or module
|
610
|
-
subject = sclass_node.children[0]
|
611
|
-
|
612
|
-
if subject.const_type?
|
613
|
-
"#<Class:#{subject.const_name}>"
|
614
|
-
elsif subject.self_type?
|
615
|
-
"#<Class:#{sclass_node.parent_module_name}>"
|
616
|
-
end
|
617
|
-
end
|
618
|
-
|
619
|
-
def parent_module_name_for_block(ancestor)
|
620
|
-
if ancestor.method?(:class_eval)
|
621
|
-
# `class_eval` with no receiver applies to whatever module or class
|
622
|
-
# we are currently in
|
623
|
-
return unless (receiver = ancestor.receiver)
|
624
|
-
|
625
|
-
yield unless receiver.const_type?
|
626
|
-
receiver.const_name
|
627
|
-
elsif !new_class_or_module_block?(ancestor)
|
628
|
-
yield
|
629
|
-
end
|
630
|
-
end
|
631
|
-
|
632
|
-
def_node_matcher :new_class_or_module_block?, <<~PATTERN
|
633
|
-
^(casgn _ _ (block (send (const _ {:Class :Module}) :new) ...))
|
634
|
-
PATTERN
|
635
|
-
end
|
636
|
-
end
|
637
|
-
end
|