From 743ca4587fad4ca68010175eb4c407b565cc6fc2 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Thu, 3 Jan 2019 15:56:14 +0800 Subject: [PATCH] Add new RangeNode extension --- .rubocop_todo.yml | 16 +++++++-------- lib/rubocop.rb | 1 + lib/rubocop/ast/builder.rb | 2 ++ lib/rubocop/ast/node/range_node.rb | 11 ++++++++++ spec/rubocop/ast/range_node_spec.rb | 32 +++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 lib/rubocop/ast/node/range_node.rb create mode 100644 spec/rubocop/ast/range_node_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7489f825b86..b2f056c283f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,12 +1,12 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-12-27 11:29:54 +0800 using RuboCop version 0.61.1. +# on 2019-01-03 17:29:56 +0800 using RuboCop version 0.62.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 75 +# Offense count: 77 Metrics/AbcSize: Max: 17 @@ -15,7 +15,7 @@ Metrics/AbcSize: Metrics/ClassLength: Max: 181 -# Offense count: 220 +# Offense count: 222 # Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: Max: 14 @@ -23,9 +23,9 @@ Metrics/MethodLength: # Offense count: 5 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 139 + Max: 141 -# Offense count: 10 +# Offense count: 12 RSpec/AnyInstance: Exclude: - 'spec/rubocop/cli_spec.rb' @@ -33,13 +33,13 @@ RSpec/AnyInstance: - 'spec/rubocop/runner_spec.rb' - 'spec/rubocop/target_finder_spec.rb' -# Offense count: 1067 +# Offense count: 1071 # Configuration parameters: Prefixes. # Prefixes: when, with, without RSpec/ContextWording: Enabled: false -# Offense count: 3304 +# Offense count: 3313 # Configuration parameters: Max. RSpec/ExampleLength: Enabled: false @@ -60,7 +60,7 @@ RSpec/ExpectOutput: - 'spec/rubocop/target_finder_spec.rb' - 'spec/support/cli_spec_behavior.rb' -# Offense count: 525 +# Offense count: 528 # Configuration parameters: AggregateFailuresByDefault. RSpec/MultipleExpectations: Max: 25 diff --git a/lib/rubocop.rb b/lib/rubocop.rb index e8b89451de5..efcfe4d5d4f 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -48,6 +48,7 @@ require_relative 'rubocop/ast/node/keyword_splat_node' require_relative 'rubocop/ast/node/or_node' require_relative 'rubocop/ast/node/pair_node' +require_relative 'rubocop/ast/node/range_node' require_relative 'rubocop/ast/node/regexp_node' require_relative 'rubocop/ast/node/resbody_node' require_relative 'rubocop/ast/node/send_node' diff --git a/lib/rubocop/ast/builder.rb b/lib/rubocop/ast/builder.rb index 2dd806e3689..3cd4554cf58 100644 --- a/lib/rubocop/ast/builder.rb +++ b/lib/rubocop/ast/builder.rb @@ -27,6 +27,8 @@ class Builder < Parser::Builders::Default for: ForNode, hash: HashNode, if: IfNode, + irange: RangeNode, + erange: RangeNode, kwsplat: KeywordSplatNode, or: OrNode, pair: PairNode, diff --git a/lib/rubocop/ast/node/range_node.rb b/lib/rubocop/ast/node/range_node.rb new file mode 100644 index 00000000000..b7c5e74840a --- /dev/null +++ b/lib/rubocop/ast/node/range_node.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module RuboCop + module AST + # A node extension for `irange` and `erange` nodes. This will be used in + # place of a plain node when the builder constructs the AST, making its + # methods available to all `irange` and `erange` nodes within RuboCop. + class RangeNode < Node + end + end +end diff --git a/spec/rubocop/ast/range_node_spec.rb b/spec/rubocop/ast/range_node_spec.rb new file mode 100644 index 00000000000..0b10bd5a428 --- /dev/null +++ b/spec/rubocop/ast/range_node_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::AST::RangeNode do + let(:range_node) { parse_source(source).ast } + + describe '.new' do + context 'with an inclusive range' do + let(:source) do + '1..2' + end + + it { expect(range_node.is_a?(described_class)).to be(true) } + end + + context 'with an exclusive range' do + let(:source) do + '1...2' + end + + it { expect(range_node.is_a?(described_class)).to be(true) } + end + + context 'with an infinite range' do + let(:ruby_version) { 2.6 } + let(:source) do + '1..' + end + + it { expect(range_node.is_a?(described_class)).to be(true) } + end + end +end