rbi 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 502c584fe293212d2f14f1a6b8d76457645afe9a4bc6355cd32c2179bb9a4e68
4
- data.tar.gz: 4ed5b4fd62249dec73277ab31fc07595f1cdcfacdec5d6f13de9951ccffecc0c
3
+ metadata.gz: 88fce4a776d172e793f815ac8eee6c6958a140f0657532eb2d792f4caf6d18e1
4
+ data.tar.gz: ea01e4a38874e2e9045e8ea528ca595fef2c1da4178e527642cc81078cc31de0
5
5
  SHA512:
6
- metadata.gz: 84333e72029b10d6a9ee7fc2a2392e08d6e9fc2d0871deec2a35e6e1042fde559efba0de2df008b91fcecfff2ba1b1b414275fb6a29c0a2cc52db62b08ae9915
7
- data.tar.gz: 1162d80c92024e93af674629594c726bd2b5b965bebc63150f6384f488d0a24dcf7c52f2c56552aecec2010d26352b43fc88e3191cbfc2a042ea8ea64eca7e00
6
+ metadata.gz: 948f130aa780426a010b991355d2ea3c45bb175b714b97b19094ac52a369cf45be637589e590fa72a3fbebcfe96e58f77979f40e05c90702d60da9ffa90cc8f9
7
+ data.tar.gz: bdae9d96ba12457444d6f1073c006b242ca2e683a6414698eee0bfa1ae9da164cb2b5b25337d637d46c55bc989de0a00b4b990d40bdd3fb5d5a79053be677621
@@ -0,0 +1,123 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module RBI
5
+ module Rewriters
6
+ # Take a gem version and filter out all RBI that is not relevant to that version based on @version annotations
7
+ # in comments. As an example:
8
+ #
9
+ # ~~~rb
10
+ # tree = Parser.parse_string(<<~RBI)
11
+ # class Foo
12
+ # # @version > 0.3.0
13
+ # def bar
14
+ # end
15
+ #
16
+ # # @version <= 0.3.0
17
+ # def bar(arg1)
18
+ # end
19
+ # end
20
+ # RBI
21
+ #
22
+ # Rewriters::FilterVersions.filter(tree, Gem::Version.new("0.3.1"))
23
+ #
24
+ # assert_equal(<<~RBI, tree.string)
25
+ # class Foo
26
+ # # @version > 0.3.0
27
+ # def bar
28
+ # end
29
+ # end
30
+ # RBI
31
+ # ~~~
32
+ #
33
+ # Supported operators:
34
+ # - equals `=`
35
+ # - not equals `!=`
36
+ # - greater than `>`
37
+ # - greater than or equal to `>=`
38
+ # - less than `<`
39
+ # - less than or equal to `<=`
40
+ # - pessimistic or twiddle-wakka`~>`
41
+ #
42
+ # And/or logic:
43
+ # - "And" logic: put multiple versions on the same line
44
+ # - e.g. `@version > 0.3.0, <1.0.0` means version must be greater than 0.3.0 AND less than 1.0.0
45
+ # - "Or" logic: put multiple versions on subsequent lines
46
+ # - e.g. the following means version must be less than 0.3.0 OR greater than 1.0.0
47
+ # ```
48
+ # # @version < 0.3.0
49
+ # # @version > 1.0.0
50
+ # ```
51
+ # Prerelease versions:
52
+ # - Prerelease versions are considered less than their non-prerelease counterparts
53
+ # - e.g. `0.4.0-prerelease` is less than `0.4.0`
54
+ #
55
+ # RBI with no versions:
56
+ # - RBI with no version annotations are automatically counted towards ALL versions
57
+ class FilterVersions < Visitor
58
+ extend T::Sig
59
+
60
+ VERSION_PREFIX = "version "
61
+
62
+ class << self
63
+ extend T::Sig
64
+
65
+ sig { params(tree: Tree, version: Gem::Version).void }
66
+ def filter(tree, version)
67
+ v = new(version)
68
+ v.visit(tree)
69
+ end
70
+ end
71
+
72
+ sig { params(version: Gem::Version).void }
73
+ def initialize(version)
74
+ super()
75
+ @version = version
76
+ end
77
+
78
+ sig { override.params(node: T.nilable(Node)).void }
79
+ def visit(node)
80
+ return unless node
81
+
82
+ unless node.satisfies_version?(@version)
83
+ node.detach
84
+ return
85
+ end
86
+
87
+ visit_all(node.nodes.dup) if node.is_a?(Tree)
88
+ end
89
+ end
90
+ end
91
+
92
+ class Node
93
+ sig { params(version: Gem::Version).returns(T::Boolean) }
94
+ def satisfies_version?(version)
95
+ return true unless is_a?(NodeWithComments)
96
+
97
+ requirements = version_requirements
98
+ requirements.empty? || requirements.any? { |req| req.satisfied_by?(version) }
99
+ end
100
+ end
101
+
102
+ class NodeWithComments
103
+ sig { returns(T::Array[Gem::Requirement]) }
104
+ def version_requirements
105
+ annotations.select do |annotation|
106
+ annotation.start_with?(Rewriters::FilterVersions::VERSION_PREFIX)
107
+ end.map do |annotation|
108
+ versions = annotation.delete_prefix(Rewriters::FilterVersions::VERSION_PREFIX).split(/, */)
109
+ Gem::Requirement.new(versions)
110
+ end
111
+ end
112
+ end
113
+
114
+ class Tree
115
+ extend T::Sig
116
+
117
+ sig { params(version: Gem::Version).void }
118
+ def filter_versions!(version)
119
+ visitor = Rewriters::FilterVersions.new(version)
120
+ visitor.visit(self)
121
+ end
122
+ end
123
+ end
data/lib/rbi/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RBI
5
- VERSION = "0.1.8"
5
+ VERSION = "0.1.9"
6
6
  end
data/lib/rbi.rb CHANGED
@@ -10,6 +10,7 @@ require "rbi/index"
10
10
  require "rbi/rewriters/add_sig_templates"
11
11
  require "rbi/rewriters/annotate"
12
12
  require "rbi/rewriters/deannotate"
13
+ require "rbi/rewriters/filter_versions"
13
14
  require "rbi/rewriters/merge_trees"
14
15
  require "rbi/rewriters/nest_singleton_methods"
15
16
  require "rbi/rewriters/nest_non_public_methods"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Terrasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-06 00:00:00.000000000 Z
11
+ date: 2024-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 0.18.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '0.22'
22
+ version: '0.25'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 0.18.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '0.22'
32
+ version: '0.25'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: sorbet-runtime
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -64,6 +64,7 @@ files:
64
64
  - lib/rbi/rewriters/add_sig_templates.rb
65
65
  - lib/rbi/rewriters/annotate.rb
66
66
  - lib/rbi/rewriters/deannotate.rb
67
+ - lib/rbi/rewriters/filter_versions.rb
67
68
  - lib/rbi/rewriters/group_nodes.rb
68
69
  - lib/rbi/rewriters/merge_trees.rb
69
70
  - lib/rbi/rewriters/nest_non_public_methods.rb
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  - !ruby/object:Gem::Version
93
94
  version: '0'
94
95
  requirements: []
95
- rubygems_version: 3.5.5
96
+ rubygems_version: 3.5.6
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: RBI generation framework