spoom 1.7.15 → 1.7.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0726847d39f3461a3ac2998e8a84c235633275b9f15549e7ecaa24730496e12a'
4
- data.tar.gz: a21f1fc2df2b4b9467d59c676aea601b733711709895daa47f80d8af5543f301
3
+ metadata.gz: 2110b75c9f1cfaf75e50c901f26af66d149f2a6353a22ee2154062f24f808472
4
+ data.tar.gz: 1f8daac5e4e94b7b135ed0b2d7dafb701dcf338302f337982ced3e6a32aa65fa
5
5
  SHA512:
6
- metadata.gz: 625897a0c781818fefa36d6bf2c49c9674300d162e54d939e53b3b2e4b128d9dd97674c5418364e018e1cc01513ac664048191a9fdeda0667b7c41218815688c
7
- data.tar.gz: 8b269cb7ec6f258e4403f83993609985078fba2512d7d96fc89ad8d63522cd2913e30019edb1ae2e7b7aeaf2f4476c6d5141cc4aa594ca317db04a7cb975d4b0
6
+ metadata.gz: bb98e214157092a61b830ce8e5836b1bc9c304d902b2886e10ab52c753febd6c7233142a509b461b26ee9c1c1150bd8346b02370c031bda68a6c555b2bc1c128
7
+ data.tar.gz: 6b3b0ec9bbc739161a4c642084aa1edcae82cf9293e20e3997656cfa2108eff1c32369c5d4225c6ebdd0716cb030eaec7c749cfa623273cdb59dce848f5ab039
data/Gemfile CHANGED
@@ -12,5 +12,6 @@ group :development do
12
12
  gem "debug"
13
13
  gem "rubocop-shopify", require: false
14
14
  gem "rubocop-sorbet", require: false
15
+ gem "rubocop-minitest", require: false
15
16
  gem "tapioca", require: false
16
17
  end
@@ -60,7 +60,7 @@ module Spoom
60
60
  .sort_by { |_key, value| -value }
61
61
  .each do |key, value|
62
62
  say(" * #{key}: `#{value}`")
63
- end
63
+ end
64
64
  end
65
65
  end
66
66
  end
data/lib/spoom/rbs.rb CHANGED
@@ -105,7 +105,7 @@ module Spoom
105
105
  location = location.join(continuation_comment.location)
106
106
  end
107
107
  continuation_comments.clear
108
- res.signatures << Signature.new(string, location)
108
+ res.signatures.prepend(Signature.new(string, location))
109
109
  elsif string.start_with?("#|")
110
110
  continuation_comments << comment
111
111
  end
@@ -20,25 +20,33 @@ module Spoom
20
20
  RBS_REWRITE_PATTERN = Regexp.union(["#:", "#|", *RBS_ANNOTATION_MARKERS]).freeze #: Regexp
21
21
  private_constant :RBS_ANNOTATION_MARKERS, :RBS_REWRITE_PATTERN
22
22
 
23
+ ALLOWED_OVERLOAD_STRATEGIES = [:translate_all, :translate_last, :raise].freeze #: Array[Symbol]
24
+
23
25
  class << self
24
26
  #: (String source) -> bool
25
27
  def contains_rbs_syntax?(source)
26
28
  Sigils.contains_valid_sigil?(source) && source.match?(RBS_REWRITE_PATTERN)
27
29
  end
28
30
 
29
- #: (String ruby_contents, file: String, ?max_line_length: Integer?) -> String
30
- def rewrite_if_needed(ruby_contents, file:, max_line_length: nil)
31
+ #: (String ruby_contents, file: String, ?max_line_length: Integer?, ?overloads_strategy: Symbol) -> String
32
+ def rewrite_if_needed(ruby_contents, file:, max_line_length: nil, overloads_strategy: :translate_all)
31
33
  return ruby_contents unless contains_rbs_syntax?(ruby_contents)
32
34
 
33
- new(ruby_contents, file:, max_line_length:).rewrite
35
+ new(ruby_contents, file:, max_line_length:, overloads_strategy:).rewrite
34
36
  end
35
37
  end
36
38
 
37
- #: (String, file: String, ?max_line_length: Integer?) -> void
38
- def initialize(ruby_contents, file:, max_line_length: nil)
39
+ #: (String, file: String, ?max_line_length: Integer?, ?overloads_strategy: Symbol) -> void
40
+ def initialize(ruby_contents, file:, max_line_length: nil, overloads_strategy: :translate_all)
39
41
  super(ruby_contents, file: file)
40
42
 
43
+ unless ALLOWED_OVERLOAD_STRATEGIES.include?(overloads_strategy)
44
+ raise ArgumentError, "Unknown overloads_strategy: #{overloads_strategy.inspect}. " \
45
+ "Must be one of: #{ALLOWED_OVERLOAD_STRATEGIES.map(&:inspect).join(", ")}"
46
+ end
47
+
41
48
  @max_line_length = max_line_length
49
+ @overloads_strategy = overloads_strategy
42
50
  end
43
51
 
44
52
  # @override
@@ -107,7 +115,13 @@ module Spoom
107
115
 
108
116
  return if comments.signatures.empty?
109
117
 
110
- comments.signatures.each do |signature|
118
+ signatures = apply_overloads_strategy(
119
+ comments.signatures,
120
+ method_name: node.message.to_s,
121
+ location: "#{@file}:#{node.location.start_line}",
122
+ )
123
+
124
+ signatures.each do |signature|
111
125
  attr_type = ::RBS::Parser.parse_type(signature.string)
112
126
  sig = RBI::Sig.new
113
127
 
@@ -143,11 +157,17 @@ module Spoom
143
157
  return if comments.empty?
144
158
  return if comments.signatures.empty?
145
159
 
160
+ signatures = apply_overloads_strategy(
161
+ comments.signatures,
162
+ method_name: def_node.name.to_s,
163
+ location: "#{@file}:#{def_node.location.start_line}",
164
+ )
165
+
146
166
  builder = RBI::Parser::TreeBuilder.new(@ruby_contents, comments: [], file: @file)
147
167
  builder.visit(def_node)
148
168
  rbi_node = builder.tree.nodes.first #: as RBI::Method
149
169
 
150
- comments.signatures.each do |signature|
170
+ signatures.each do |signature|
151
171
  begin
152
172
  method_type = ::RBS::Parser.parse_method_type(signature.string)
153
173
  rescue ::RBS::ParsingError
@@ -180,6 +200,29 @@ module Spoom
180
200
  end
181
201
  end
182
202
 
203
+ #: (Array[RBS::Signature], method_name: String, location: String) -> Array[RBS::Signature]
204
+ def apply_overloads_strategy(signatures, method_name:, location:)
205
+ return signatures if signatures.size <= 1
206
+
207
+ case @overloads_strategy
208
+ when :translate_all
209
+ signatures
210
+ when :translate_last
211
+ kept = signatures.last #: as RBS::Signature
212
+ others = signatures[0...-1] #: as !nil
213
+
214
+ # Delete all the signatures we didn't keep
215
+ others.each do |signature|
216
+ from = adjust_to_line_start(signature.location.start_offset)
217
+ to = adjust_to_line_end(signature.location.end_offset)
218
+ @rewriter << Source::Delete.new(from, to)
219
+ end
220
+ [kept]
221
+ else # :raise
222
+ raise Error, "Method `#{method_name}` at #{location} has multiple overloaded signatures"
223
+ end
224
+ end
225
+
183
226
  #: (Prism::ClassNode | Prism::ModuleNode | Prism::SingletonClassNode) -> void
184
227
  def apply_class_annotations(node)
185
228
  comments = node_rbs_comments(node)
@@ -53,9 +53,14 @@ module Spoom
53
53
 
54
54
  # Converts all the RBS comments in the given Ruby code to `sig` nodes.
55
55
  # It also handles type members and class annotations.
56
- #: (String ruby_contents, file: String, ?max_line_length: Integer?) -> String
57
- def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: nil)
58
- RBSCommentsToSorbetSigs.rewrite_if_needed(ruby_contents, file: file, max_line_length: max_line_length)
56
+ #: (String ruby_contents, file: String, ?max_line_length: Integer?, ?overloads_strategy: Symbol) -> String
57
+ def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: nil, overloads_strategy: :translate_all)
58
+ RBSCommentsToSorbetSigs.rewrite_if_needed(
59
+ ruby_contents,
60
+ file: file,
61
+ max_line_length: max_line_length,
62
+ overloads_strategy: overloads_strategy,
63
+ )
59
64
  end
60
65
 
61
66
  # Converts all `T.let` and `T.cast` nodes to RBS comments in the given Ruby code.
data/lib/spoom/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Spoom
5
- VERSION = "1.7.15"
5
+ VERSION = "1.7.16"
6
6
  end
data/rbi/spoom.rbi CHANGED
@@ -2854,8 +2854,15 @@ Spoom::Sorbet::Sigils::VALID_STRICTNESS = T.let(T.unsafe(nil), Array)
2854
2854
 
2855
2855
  module Spoom::Sorbet::Translate
2856
2856
  class << self
2857
- sig { params(ruby_contents: ::String, file: ::String, max_line_length: T.nilable(::Integer)).returns(::String) }
2858
- def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: T.unsafe(nil)); end
2857
+ sig do
2858
+ params(
2859
+ ruby_contents: ::String,
2860
+ file: ::String,
2861
+ max_line_length: T.nilable(::Integer),
2862
+ overloads_strategy: ::Symbol
2863
+ ).returns(::String)
2864
+ end
2865
+ def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil)); end
2859
2866
 
2860
2867
  sig do
2861
2868
  params(
@@ -2893,8 +2900,15 @@ class Spoom::Sorbet::Translate::Error < ::Spoom::Error; end
2893
2900
  class Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs < ::Spoom::Sorbet::Translate::Translator
2894
2901
  include ::Spoom::RBS::ExtractRBSComments
2895
2902
 
2896
- sig { params(ruby_contents: ::String, file: ::String, max_line_length: T.nilable(::Integer)).void }
2897
- def initialize(ruby_contents, file:, max_line_length: T.unsafe(nil)); end
2903
+ sig do
2904
+ params(
2905
+ ruby_contents: ::String,
2906
+ file: ::String,
2907
+ max_line_length: T.nilable(::Integer),
2908
+ overloads_strategy: ::Symbol
2909
+ ).void
2910
+ end
2911
+ def initialize(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil)); end
2898
2912
 
2899
2913
  sig { override.params(node: ::Prism::CallNode).void }
2900
2914
  def visit_call_node(node); end
@@ -2930,6 +2944,15 @@ class Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs < ::Spoom::Sorbet::Trans
2930
2944
  sig { params(annotations: T::Array[::Spoom::RBS::Annotation], sig: ::RBI::Sig).void }
2931
2945
  def apply_member_annotations(annotations, sig); end
2932
2946
 
2947
+ sig do
2948
+ params(
2949
+ signatures: T::Array[::Spoom::RBS::Signature],
2950
+ method_name: ::String,
2951
+ location: ::String
2952
+ ).returns(T::Array[::Spoom::RBS::Signature])
2953
+ end
2954
+ def apply_overloads_strategy(signatures, method_name:, location:); end
2955
+
2933
2956
  sig { params(comments: T::Array[::Prism::Comment]).void }
2934
2957
  def apply_type_aliases(comments); end
2935
2958
 
@@ -2946,11 +2969,19 @@ class Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs < ::Spoom::Sorbet::Trans
2946
2969
  sig { params(source: ::String).returns(T::Boolean) }
2947
2970
  def contains_rbs_syntax?(source); end
2948
2971
 
2949
- sig { params(ruby_contents: ::String, file: ::String, max_line_length: T.nilable(::Integer)).returns(::String) }
2950
- def rewrite_if_needed(ruby_contents, file:, max_line_length: T.unsafe(nil)); end
2972
+ sig do
2973
+ params(
2974
+ ruby_contents: ::String,
2975
+ file: ::String,
2976
+ max_line_length: T.nilable(::Integer),
2977
+ overloads_strategy: ::Symbol
2978
+ ).returns(::String)
2979
+ end
2980
+ def rewrite_if_needed(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil)); end
2951
2981
  end
2952
2982
  end
2953
2983
 
2984
+ Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::ALLOWED_OVERLOAD_STRATEGIES = T.let(T.unsafe(nil), Array)
2954
2985
  Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::RBS_ANNOTATION_MARKERS = T.let(T.unsafe(nil), Array)
2955
2986
  Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::RBS_REWRITE_PATTERN = T.let(T.unsafe(nil), Regexp)
2956
2987
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spoom
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.15
4
+ version: 1.7.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Terrasa