Sha256: 1c1c41c4969191ff612da1750ca6647cc58e8d5dab6572e057c5b66abd884854

Contents?: true

Size: 1.35 KB

Versions: 26

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module RubyNext
  module Language
    module Rewriters
      class EndlessRange < Base
        NAME = "endless-range"
        SYNTAX_PROBE = "[0, 1][1..]"
        MIN_SUPPORTED_VERSION = Gem::Version.new("2.6.0")

        def on_index(node)
          @current_index = node
          new_index = process(node.children.last)
          return unless new_index != node.children.last

          node.updated(
            nil,
            [
              node.children.first,
              new_index
            ]
          )
        end

        def on_erange(node)
          return unless node.children.last.nil?

          context.track! self

          new_end =
            if index_arg?(node)
              s(:int, -1)
            else
              s(:const,
                s(:const,
                  s(:cbase), :Float),
                :INFINITY)
            end

          replace(node.loc.expression, "#{node.children.first.loc.expression.source}..#{unparse(new_end)}")

          node.updated(
            :irange,
            [
              node.children.first,
              new_end
            ]
          )
        end

        alias_method :on_irange, :on_erange

        private

        attr_reader :current_index

        def index_arg?(node)
          current_index&.children&.include?(node)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
ruby-next-core-0.10.4 lib/ruby-next/language/rewriters/endless_range.rb
ruby-next-core-0.10.3 lib/ruby-next/language/rewriters/endless_range.rb
ruby-next-core-0.10.2 lib/ruby-next/language/rewriters/endless_range.rb
ruby-next-core-0.10.1 lib/ruby-next/language/rewriters/endless_range.rb
ruby-next-core-0.10.0 lib/ruby-next/language/rewriters/endless_range.rb
ruby-next-core-0.9.2 lib/ruby-next/language/rewriters/endless_range.rb