Class: RBI::Rewriters::FilterVersions

Inherits:
Visitor
  • Object
show all
Extended by:
T::Sig
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:

~~~rb 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

Returns a new instance of FilterVersions.



73
74
75
76
# File 'lib/rbi/rewriters/filter_versions.rb', line 73

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

Class Method Details

.filter(tree, version) ⇒ Object



66
67
68
69
# File 'lib/rbi/rewriters/filter_versions.rb', line 66

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

Instance Method Details

#visit(node) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/rbi/rewriters/filter_versions.rb', line 79

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