From d22907d948ca32fc2aad9b34e41f5681b4573fbc Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 6 May 2019 03:14:45 -0500 Subject: [PATCH] [Fix #6945] Drop support for Ruby 2.2 Fixes #6945. By convention, a year after Ruby became an EOL, it seems that the old Ruby version support has been dropped. I'm not sure if it's good to drop Ruby 2.3 support, but it's good time to drop Ruby 2.2 support. --- lib/rubocop/ast/node.rb | 4 ++-- lib/rubocop/ast/node/array_node.rb | 4 ++-- lib/rubocop/ast/node/block_node.rb | 4 ++-- lib/rubocop/ast/node/for_node.rb | 2 +- lib/rubocop/ast/node/hash_node.rb | 2 +- lib/rubocop/ast/node/if_node.rb | 2 +- lib/rubocop/ast/node/keyword_splat_node.rb | 2 +- .../ast/node/mixin/method_dispatch_node.rb | 6 +++--- .../node/mixin/method_identifier_predicates.rb | 4 ++-- lib/rubocop/ast/node/mixin/parameterized_node.rb | 2 +- .../ast/node/mixin/predicate_operator_node.rb | 8 ++++---- lib/rubocop/ast/node/pair_node.rb | 8 ++++---- lib/rubocop/ast/node/until_node.rb | 2 +- lib/rubocop/ast/node/when_node.rb | 2 +- lib/rubocop/ast/node/while_node.rb | 2 +- lib/rubocop/node_pattern.rb | 16 ++++++++-------- lib/rubocop/processed_source.rb | 8 +++----- spec/rubocop/node_pattern_spec.rb | 2 +- spec/rubocop/processed_source_spec.rb | 2 +- 19 files changed, 40 insertions(+), 42 deletions(-) diff --git a/lib/rubocop/ast/node.rb b/lib/rubocop/ast/node.rb index b361ae655..b6d13f78b 100644 --- a/lib/rubocop/ast/node.rb +++ b/lib/rubocop/ast/node.rb @@ -464,11 +464,11 @@ def call_type? end def chained? - parent && parent.call_type? && eql?(parent.receiver) + parent&.call_type? && eql?(parent.receiver) end def argument? - parent && parent.send_type? && parent.arguments.include?(self) + parent&.send_type? && parent.arguments.include?(self) end def numeric_type? diff --git a/lib/rubocop/ast/node/array_node.rb b/lib/rubocop/ast/node/array_node.rb index d4f8835e0..8731440e7 100644 --- a/lib/rubocop/ast/node/array_node.rb +++ b/lib/rubocop/ast/node/array_node.rb @@ -22,7 +22,7 @@ def values # # @return [Boolean] whether the array is enclosed in square brackets def square_brackets? - loc.begin && loc.begin.is?('[') + loc.begin&.is?('[') end # Checks whether the `array` literal is delimited by percent brackets. @@ -40,7 +40,7 @@ def percent_literal?(type = nil) if type loc.begin && loc.begin.source =~ PERCENT_LITERAL_TYPES[type] else - loc.begin && loc.begin.source.start_with?('%') + loc.begin&.source&.start_with?('%') end end diff --git a/lib/rubocop/ast/node/block_node.rb b/lib/rubocop/ast/node/block_node.rb index f37d67650..d049eaa03 100644 --- a/lib/rubocop/ast/node/block_node.rb +++ b/lib/rubocop/ast/node/block_node.rb @@ -50,14 +50,14 @@ def arguments? # # @return [Boolean] whether the `block` literal is enclosed in braces def braces? - loc.end && loc.end.is?('}') + loc.end&.is?('}') end # Checks whether the `block` literal is delimited by `do`-`end` keywords. # # @return [Boolean] whether the `block` literal is enclosed in `do`-`end` def keywords? - loc.end && loc.end.is?('end') + loc.end&.is?('end') end # The delimiters for this `block` literal. diff --git a/lib/rubocop/ast/node/for_node.rb b/lib/rubocop/ast/node/for_node.rb index b6c10aef6..18c64530d 100644 --- a/lib/rubocop/ast/node/for_node.rb +++ b/lib/rubocop/ast/node/for_node.rb @@ -17,7 +17,7 @@ def keyword # # @return [Boolean] whether the `for` node has a `do` keyword def do? - loc.begin && loc.begin.is?('do') + loc.begin&.is?('do') end # Checks whether this node body is a void context. diff --git a/lib/rubocop/ast/node/hash_node.rb b/lib/rubocop/ast/node/hash_node.rb index 40afbcc03..661cb7355 100644 --- a/lib/rubocop/ast/node/hash_node.rb +++ b/lib/rubocop/ast/node/hash_node.rb @@ -102,7 +102,7 @@ def mixed_delimiters? # # @return [Boolean] whether the `hash` literal is enclosed in braces def braces? - loc.end && loc.end.is?('}') + loc.end&.is?('}') end end end diff --git a/lib/rubocop/ast/node/if_node.rb b/lib/rubocop/ast/node/if_node.rb index c8bb187ed..45c79657a 100644 --- a/lib/rubocop/ast/node/if_node.rb +++ b/lib/rubocop/ast/node/if_node.rb @@ -103,7 +103,7 @@ def nested_conditional? # # @return [Boolean] whether the `if` node has at least one `elsif` branch def elsif_conditional? - else_branch && else_branch.if_type? && else_branch.elsif? + else_branch&.if_type? && else_branch&.elsif? end # Returns the branch of the `if` node that gets evaluated when its diff --git a/lib/rubocop/ast/node/keyword_splat_node.rb b/lib/rubocop/ast/node/keyword_splat_node.rb index 263a4791c..2c7c66304 100644 --- a/lib/rubocop/ast/node/keyword_splat_node.rb +++ b/lib/rubocop/ast/node/keyword_splat_node.rb @@ -8,7 +8,7 @@ module AST class KeywordSplatNode < Node include HashElementNode - DOUBLE_SPLAT = '**'.freeze + DOUBLE_SPLAT = '**' # This is used for duck typing with `pair` nodes which also appear as # `hash` elements. diff --git a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb index f9a503b7e..0fd26765e 100644 --- a/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +++ b/lib/rubocop/ast/node/mixin/method_dispatch_node.rb @@ -116,7 +116,7 @@ def double_colon? # # @return [Boolean] whether the receiver of this method dispatch is `self` def self_receiver? - receiver && receiver.self_type? + receiver&.self_type? end # Checks whether the *explicit* receiver of this method dispatch is a @@ -125,7 +125,7 @@ def self_receiver? # @return [Boolean] whether the receiver of this method dispatch # is a `const` node def const_receiver? - receiver && receiver.const_type? + receiver&.const_type? end # Checks whether the method dispatch is the implicit form of `#call`, @@ -140,7 +140,7 @@ def implicit_call? # # @return [Boolean] whether the dispatched method has a block def block_literal? - parent && parent.block_type? && eql?(parent.send_node) + parent&.block_type? && eql?(parent.send_node) end # Checks whether this node is an arithmetic operation diff --git a/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb b/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb index a0c25fe7b..7ddca670a 100644 --- a/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +++ b/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb @@ -79,14 +79,14 @@ def camel_case_method? # # @return [Boolean] whether the receiver of this node is `self` def self_receiver? - receiver && receiver.self_type? + receiver&.self_type? end # Checks whether the *explicit* receiver of node is a `const` node. # # @return [Boolean] whether the receiver of this node is a `const` node def const_receiver? - receiver && receiver.const_type? + receiver&.const_type? end # Checks whether this is a negation method, i.e. `!` or keyword `not`. diff --git a/lib/rubocop/ast/node/mixin/parameterized_node.rb b/lib/rubocop/ast/node/mixin/parameterized_node.rb index 0ecb3afbc..9b26aca7e 100644 --- a/lib/rubocop/ast/node/mixin/parameterized_node.rb +++ b/lib/rubocop/ast/node/mixin/parameterized_node.rb @@ -10,7 +10,7 @@ module ParameterizedNode # @return [Boolean] whether this node's arguments are # wrapped in parentheses def parenthesized? - loc.end && loc.end.is?(')') + loc.end&.is?(')') end # A shorthand for getting the first argument of the node. diff --git a/lib/rubocop/ast/node/mixin/predicate_operator_node.rb b/lib/rubocop/ast/node/mixin/predicate_operator_node.rb index 1813976cd..43d635c34 100644 --- a/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +++ b/lib/rubocop/ast/node/mixin/predicate_operator_node.rb @@ -5,10 +5,10 @@ module AST # Common functionality for nodes that are predicates: # `or`, `and` ... module PredicateOperatorNode - LOGICAL_AND = '&&'.freeze - SEMANTIC_AND = 'and'.freeze - LOGICAL_OR = '||'.freeze - SEMANTIC_OR = 'or'.freeze + LOGICAL_AND = '&&' + SEMANTIC_AND = 'and' + LOGICAL_OR = '||' + SEMANTIC_OR = 'or' # Returns the operator as a string. # diff --git a/lib/rubocop/ast/node/pair_node.rb b/lib/rubocop/ast/node/pair_node.rb index 47e6fa4ed..da2e34bea 100644 --- a/lib/rubocop/ast/node/pair_node.rb +++ b/lib/rubocop/ast/node/pair_node.rb @@ -8,10 +8,10 @@ module AST class PairNode < Node include HashElementNode - HASH_ROCKET = '=>'.freeze - SPACED_HASH_ROCKET = ' => '.freeze - COLON = ':'.freeze - SPACED_COLON = ': '.freeze + HASH_ROCKET = '=>' + SPACED_HASH_ROCKET = ' => ' + COLON = ':' + SPACED_COLON = ': ' # Checks whether the `pair` uses a hash rocket delimiter. # diff --git a/lib/rubocop/ast/node/until_node.rb b/lib/rubocop/ast/node/until_node.rb index 18f8af898..54f1783c8 100644 --- a/lib/rubocop/ast/node/until_node.rb +++ b/lib/rubocop/ast/node/until_node.rb @@ -28,7 +28,7 @@ def inverse_keyword # # @return [Boolean] whether the `until` node has a `do` keyword def do? - loc.begin && loc.begin.is?('do') + loc.begin&.is?('do') end end end diff --git a/lib/rubocop/ast/node/when_node.rb b/lib/rubocop/ast/node/when_node.rb index 1fd1c15e2..f94ab4402 100644 --- a/lib/rubocop/ast/node/when_node.rb +++ b/lib/rubocop/ast/node/when_node.rb @@ -39,7 +39,7 @@ def branch_index # # @return [Boolean] whether the `when` node has a `then` keyword def then? - loc.begin && loc.begin.is?('then') + loc.begin&.is?('then') end # Returns the body of the `when` node. diff --git a/lib/rubocop/ast/node/while_node.rb b/lib/rubocop/ast/node/while_node.rb index 154862042..61f46e95e 100644 --- a/lib/rubocop/ast/node/while_node.rb +++ b/lib/rubocop/ast/node/while_node.rb @@ -28,7 +28,7 @@ def inverse_keyword # # @return [Boolean] whether the `until` node has a `do` keyword def do? - loc.begin && loc.begin.is?('do') + loc.begin&.is?('do') end end end diff --git a/lib/rubocop/node_pattern.rb b/lib/rubocop/node_pattern.rb index 6f4f8d473..28bdd65ff 100644 --- a/lib/rubocop/node_pattern.rb +++ b/lib/rubocop/node_pattern.rb @@ -127,18 +127,18 @@ class Compiler PARAM = /\A#{PARAM_NUMBER}\Z/.freeze CLOSING = /\A(?:\)|\}|\])\Z/.freeze - REST = '...'.freeze - CAPTURED_REST = '$...'.freeze + REST = '...' + CAPTURED_REST = '$...' attr_reader :match_code, :tokens, :captures SEQ_HEAD_INDEX = -1 # Placeholders while compiling, see with_..._context methods - CUR_PLACEHOLDER = '@@@cur'.freeze - CUR_NODE = "#{CUR_PLACEHOLDER} node@@@".freeze - CUR_ELEMENT = "#{CUR_PLACEHOLDER} element@@@".freeze - SEQ_HEAD_GUARD = '@@@seq guard head@@@'.freeze + CUR_PLACEHOLDER = '@@@cur' + CUR_NODE = "#{CUR_PLACEHOLDER} node@@@" + CUR_ELEMENT = "#{CUR_PLACEHOLDER} element@@@" + SEQ_HEAD_GUARD = '@@@seq guard head@@@' line = __LINE__ ANY_ORDER_TEMPLATE = ERB.new <<-RUBY.strip_indent.gsub("-%>\n", '%>') @@ -176,7 +176,7 @@ def run(node_var) @match_code = with_context(compile_expr, node_var, use_temp_node: false) @match_code.prepend("(captures = Array.new(#{@captures})) && ") \ - if @captures > 0 + if @captures.positive? fail_due_to('unbalanced pattern') unless tokens.empty? end @@ -334,7 +334,7 @@ def compile_variadic_term def variadic_arity return unless @variadic_index - first = @variadic_index > 0 ? first_terms_arity : SEQ_HEAD_INDEX + first = @variadic_index.positive? ? first_terms_arity : SEQ_HEAD_INDEX yield first..-last_terms_arity - 1 end end diff --git a/lib/rubocop/processed_source.rb b/lib/rubocop/processed_source.rb index 4b1f9a236..2741b7d87 100644 --- a/lib/rubocop/processed_source.rb +++ b/lib/rubocop/processed_source.rb @@ -7,7 +7,7 @@ module RuboCop # and other information such as disabled lines for cops. # It also provides a convenient way to access source lines. class ProcessedSource - STRING_SOURCE_NAME = '(string)'.freeze + STRING_SOURCE_NAME = '(string)' attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics, :parser_error, :raw_source, :ruby_version @@ -157,7 +157,8 @@ def parse(source, ruby_version) def tokenize(parser) begin ast, comments, tokens = parser.tokenize(@buffer) - ast.complete! if ast + + ast.respond_to?(:complete!) && ast.complete! rescue Parser::SyntaxError # rubocop:disable Lint/HandleExceptions # All errors are in diagnostics. No need to handle exception. end @@ -170,9 +171,6 @@ def tokenize(parser) # rubocop:disable Metrics/MethodLength def parser_class(ruby_version) case ruby_version - when 2.2 - require 'parser/ruby22' - Parser::Ruby22 when 2.3 require 'parser/ruby23' Parser::Ruby23 diff --git a/spec/rubocop/node_pattern_spec.rb b/spec/rubocop/node_pattern_spec.rb index 937bd0e5d..a66fcf5f1 100644 --- a/spec/rubocop/node_pattern_spec.rb +++ b/spec/rubocop/node_pattern_spec.rb @@ -512,7 +512,7 @@ end context 'on a node which meets all requirements of the second []' do - let(:ruby) { '2.2' } + let(:ruby) { '2.3' } it_behaves_like 'matching' end diff --git a/spec/rubocop/processed_source_spec.rb b/spec/rubocop/processed_source_spec.rb index e86332a18..e6144c191 100644 --- a/spec/rubocop/processed_source_spec.rb +++ b/spec/rubocop/processed_source_spec.rb @@ -98,7 +98,7 @@ def some_method # lacking an encoding comment will default to the external encoding, # which could for example be US-ASCII if the LC_ALL environment # variable is set to "C". - '号码 = 3'.dup.force_encoding('US-ASCII') + (+'号码 = 3').force_encoding('US-ASCII') end it 'is nil' do