Class: RBI::Rewriters::FilterVersions

Inherits:
Visitor
  • Object
show all
Defined in:
lib/rbi/rewriters/filter_versions.rb

Overview

Take a gem version and filter out all RBI that is not relevant to that version based on @version annotations in comments. As an example:

tree = Parser.parse_string(<<~RBI)
  class Foo
    # @version > 0.3.0
    def bar
    end

    # @version <= 0.3.0
    def bar(arg1)
    end
  end
RBI

Rewriters::FilterVersions.filter(tree, Gem::Version.new("0.3.1"))

assert_equal(<<~RBI, tree.string)
  class Foo
    # @version > 0.3.0
    def bar
    end
  end
RBI

Supported operators:

  • equals =
  • not equals !=
  • greater than >
  • greater than or equal to >=
  • less than <
  • less than or equal to <=
  • pessimistic or twiddle-wakka~>

And/or logic:

  • "And" logic: put multiple versions on the same line
    • e.g. @version > 0.3.0, <1.0.0 means version must be greater than 0.3.0 AND less than 1.0.0
  • "Or" logic: put multiple versions on subsequent lines
    • e.g. the following means version must be less than 0.3.0 OR greater than 1.0.0

      # @version < 0.3.0
      # @version > 1.0.0
      

      Prerelease versions:

  • Prerelease versions are considered less than their non-prerelease counterparts
    • e.g. 0.4.0-prerelease is less than 0.4.0

RBI with no versions:

  • RBI with no version annotations are automatically counted towards ALL versions

Constant Summary collapse

VERSION_PREFIX =
"version "

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit_all, #visit_file

Constructor Details

#initialize(version) ⇒ FilterVersions

: (Gem::Version version) -> void



69
70
71
72
# File 'lib/rbi/rewriters/filter_versions.rb', line 69

def initialize(version)
  super()
  @version = version
end

Class Method Details

.filter(tree, version) ⇒ Object

: (Tree tree, Gem::Version version) -> void



62
63
64
65
# File 'lib/rbi/rewriters/filter_versions.rb', line 62

def filter(tree, version)
  v = new(version)
  v.visit(tree)
end

Instance Method Details

#visit(node) ⇒ Object

: (Node? node) -> void



76
77
78
79
80
81
82
83
84
85
# File 'lib/rbi/rewriters/filter_versions.rb', line 76

def visit(node)
  return unless node

  unless node.satisfies_version?(@version)
    node.detach
    return
  end

  visit_all(node.nodes.dup) if node.is_a?(Tree)
end