rbs-trace 0.4.0 → 0.4.1

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: 2e7d9ae59c5c4d3427c486422dcda9158bb65b1d5054d0ab5713a9714de9f39c
4
- data.tar.gz: fc377a935ef972ac2e153d40f336154c03cb1e42b88dc732132f2e71bba83047
3
+ metadata.gz: 4040f1116ab68a5b072e7a847480080c778b1ff87c37a0a47d1ee3b923f61ffb
4
+ data.tar.gz: b262f219610e463cdccfa5d7e3442526963f118d2ea1386ff442919920b9053f
5
5
  SHA512:
6
- metadata.gz: e3ff05d28fbef44e66500377dd38e74a0a3890da197d00bbf61b461358d404fef1933d43e30655a5dffce3850f1a71b4d535b88db8e209d2d1100ba7c1f502e9
7
- data.tar.gz: 446d6c0d49e72fae943708bc146bf80365cd107bac3d9c509950a0184611f19982dc244f0398964ff50d4d462756a3004ce73e5d5d0488a1ae9e41257555a447
6
+ metadata.gz: e3e1eea5dc319b4eafd62e62ffc1dc10ccc3301ffa603ddd8696f9b0a9a8f16a7604037873595fd91291211264e79622e8f1eba05a22dd02e78133466ae35e7d
7
+ data.tar.gz: 30f08d4582741295df623ab8ddf1f87c97723eeea0a8217e39a6ae4bcd12dd6a1ee88623256d96e29a83a410687ec8e3fa89a3ce95a72973dc5ad1002846481d
data/.rubocop.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  require:
2
2
  - rubocop-rake
3
3
  - rubocop-rspec
4
+ - rubocop-performance
4
5
 
5
6
  AllCops:
6
7
  TargetRubyVersion: 3.1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.1] - 2025-02-24
4
+
5
+ - fix: Ensure class names are referenced
6
+ - fix: Prefer other types over void and untyped
7
+ - fix: Fix process to guess return value as void
8
+ - fix: Do not parse if the caller is not Ruby
9
+ - chore: Auto correct with rubocop-performance
10
+
3
11
  ## [0.4.0] - 2025-02-23
4
12
 
5
13
  - feat: Implement the function to save as RBS files
@@ -2,9 +2,13 @@
2
2
 
3
3
  module RBS
4
4
  class Trace
5
- class Builder
5
+ class Builder # rubocop:disable Metrics/ClassLength
6
6
  include Helpers
7
7
 
8
+ UNBOUND_CLASS_METHOD = Object.instance_method(:class)
9
+ UNBOUND_NAME_METHOD = Class.instance_method(:name)
10
+ private_constant :UNBOUND_CLASS_METHOD, :UNBOUND_NAME_METHOD
11
+
8
12
  GENERICS_SIZE = {
9
13
  Array => 1,
10
14
  Range => 1,
@@ -12,12 +16,14 @@ module RBS
12
16
  }.freeze
13
17
  private_constant :GENERICS_SIZE
14
18
 
15
- # @rbs (bind: Binding, parameters: Array[__todo__], void: bool) -> void
19
+ # @rbs (bind: Binding, parameters: Array[__todo__], void: bool) -> Array[__todo__]
16
20
  def method_call(bind:, parameters:, void:)
17
21
  method_type = parse_parameters(bind, parameters)
18
22
  return_type = type_void if void
19
23
 
20
- stack_traces << [method_type, return_type]
24
+ [method_type, return_type].tap do |types|
25
+ stack_traces << types
26
+ end
21
27
  end
22
28
 
23
29
  # @rbs (__todo__) -> AST::Members::MethodDefinition::Overload
@@ -101,16 +107,17 @@ module RBS
101
107
 
102
108
  # @rbs (untyped) -> Types::t
103
109
  def parse_class(klass) # rubocop:disable Metrics/MethodLength
110
+ class_name = UNBOUND_NAME_METHOD.bind_call(klass)
104
111
  if [TrueClass, FalseClass].include?(klass)
105
112
  type_bool
106
113
  elsif klass == NilClass
107
114
  type_nil
108
- elsif klass == Object || klass.name.nil?
115
+ elsif klass == Object || class_name.nil?
109
116
  type_untyped
110
117
  else
111
118
  size = GENERICS_SIZE[klass].to_i
112
119
  args = Array.new(size) { type_untyped }
113
- Types::ClassInstance.new(name: TypeName.parse(klass.name), args:, location: nil)
120
+ Types::ClassInstance.new(name: TypeName.parse(class_name), args:, location: nil)
114
121
  end
115
122
  end
116
123
 
@@ -119,6 +126,11 @@ module RBS
119
126
  klass = obj_to_class(object)
120
127
  parse_class(klass)
121
128
  end
129
+
130
+ # @rbs (BasicObject) -> Class
131
+ def obj_to_class(obj)
132
+ UNBOUND_CLASS_METHOD.bind_call(obj)
133
+ end
122
134
  end
123
135
  end
124
136
  end
@@ -3,9 +3,6 @@
3
3
  module RBS
4
4
  class Trace
5
5
  module Helpers
6
- UNBOUND_CLASS_METHOD = Object.instance_method(:class)
7
- private_constant :UNBOUND_CLASS_METHOD
8
-
9
6
  # @rbs (name: TypeName) -> AST::Declarations::Module
10
7
  def new_module_decl(name:)
11
8
  AST::Declarations::Module.new(
@@ -46,11 +43,6 @@ module RBS
46
43
  )
47
44
  end
48
45
 
49
- # @rbs (BasicObject) -> Class
50
- def obj_to_class(obj)
51
- UNBOUND_CLASS_METHOD.bind_call(obj)
52
- end
53
-
54
46
  # @rbs () -> Types::Bases::Void
55
47
  def type_void
56
48
  @type_void = Types::Bases::Void.new(location: nil)
@@ -72,14 +72,22 @@ module RBS
72
72
 
73
73
  # @rbs (Array[Types::t]) -> Types::t
74
74
  def merge_types(types)
75
+ types = compact_types(types)
75
76
  return types.first if types.one?
76
77
 
77
- optional = types.any? { |type| type.is_a?(Types::Bases::Nil) }
78
+ optional = types.any?(Types::Bases::Nil)
78
79
  types = types.reject { |type| type.is_a?(Types::Bases::Nil) }
79
80
  type = types.one? ? types.first : Types::Union.new(types:, location: nil) #: Types::t
80
81
 
81
82
  optional ? Types::Optional.new(type:, location: nil) : type
82
83
  end
84
+
85
+ # @rbs (Array[Types::t]) -> Array[Types::t]
86
+ def compact_types(types)
87
+ types = types.reject { |t| t.is_a?(Types::Bases::Void) } if types.any? { |t| !t.is_a?(Types::Bases::Void) }
88
+ types.reject! { |t| t.is_a?(Types::Bases::Any) } if types.any? { |t| !t.is_a?(Types::Bases::Any) }
89
+ types
90
+ end
83
91
  end
84
92
  end
85
93
  end
@@ -38,7 +38,7 @@ module RBS
38
38
  # @rbs (Prism::CallNode) -> void
39
39
  def visit_call_node(node)
40
40
  key = [node.location.start_line, node.name]
41
- @void_types[key] = !use_return_value?(node)
41
+ @void_types[key] = void?(node)
42
42
 
43
43
  visit_child_nodes(node)
44
44
  end
@@ -50,13 +50,16 @@ module RBS
50
50
  private
51
51
 
52
52
  # @rbs (Prism::CallNode) -> bool
53
- def use_return_value?(_node)
54
- parent_type = @parents[-1]&.type
55
- next_parent_type = @parents[-2]&.type
53
+ def void?(node)
54
+ parent_node = @parents[-1]
55
+ next_parent_node = @parents[-2]
56
+ return true if parent_node.nil? || next_parent_node.nil?
56
57
 
57
- parent_type.end_with?("write_node") ||
58
- parent_type == :call_node ||
59
- (parent_type == :statements_node && next_parent_type == :embedded_statements_node)
58
+ if next_parent_node.type == :program_node
59
+ parent_node.type == :statements_node
60
+ else
61
+ parent_node.type == :statements_node && parent_node.child_nodes[-1] != node
62
+ end
60
63
  end
61
64
  end
62
65
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RBS
4
4
  class Trace
5
- VERSION = "0.4.0"
5
+ VERSION = "0.4.1"
6
6
  end
7
7
  end
data/lib/rbs/trace.rb CHANGED
@@ -151,6 +151,9 @@ module RBS
151
151
  # Returns true if the file does not exist (eval, etc.)
152
152
  return true unless ::File.exist?(caller_path)
153
153
 
154
+ # If the file is not Ruby, assume the return value is used. (erb, haml, etc.)
155
+ return false if ::File.extname(caller_path) != ".rb"
156
+
154
157
  @return_value_visitors ||= {} #: Hash[String, ReturnValueVisitor]
155
158
  v = @return_value_visitors.fetch(caller_path) { ReturnValueVisitor.parse_file(caller_path) }
156
159
  v.void_type?(loc.lineno, method_id)
@@ -5,10 +5,14 @@ module RBS
5
5
  class Builder
6
6
  include Helpers
7
7
 
8
+ UNBOUND_CLASS_METHOD: untyped
9
+
10
+ UNBOUND_NAME_METHOD: untyped
11
+
8
12
  GENERICS_SIZE: untyped
9
13
 
10
- # @rbs (bind: Binding, parameters: Array[__todo__], void: bool) -> void
11
- def method_call: (bind: Binding, parameters: Array[__todo__], void: bool) -> void
14
+ # @rbs (bind: Binding, parameters: Array[__todo__], void: bool) -> Array[__todo__]
15
+ def method_call: (bind: Binding, parameters: Array[__todo__], void: bool) -> Array[__todo__]
12
16
 
13
17
  # @rbs (__todo__) -> AST::Members::MethodDefinition::Overload
14
18
  def method_return: (__todo__) -> AST::Members::MethodDefinition::Overload
@@ -28,6 +32,9 @@ module RBS
28
32
 
29
33
  # @rbs (BasicObject) -> Types::t
30
34
  def parse_object: (BasicObject) -> Types::t
35
+
36
+ # @rbs (BasicObject) -> Class
37
+ def obj_to_class: (BasicObject) -> Class
31
38
  end
32
39
  end
33
40
  end
@@ -3,8 +3,6 @@
3
3
  module RBS
4
4
  class Trace
5
5
  module Helpers
6
- UNBOUND_CLASS_METHOD: untyped
7
-
8
6
  # @rbs (name: TypeName) -> AST::Declarations::Module
9
7
  def new_module_decl: (name: TypeName) -> AST::Declarations::Module
10
8
 
@@ -14,9 +12,6 @@ module RBS
14
12
  # @rbs (name: Symbol, kind: (:singleton | :instance)) -> AST::Members::MethodDefinition
15
13
  def new_method_definition: (name: Symbol, kind: :singleton | :instance) -> AST::Members::MethodDefinition
16
14
 
17
- # @rbs (BasicObject) -> Class
18
- def obj_to_class: (BasicObject) -> Class
19
-
20
15
  # @rbs () -> Types::Bases::Void
21
16
  def type_void: () -> Types::Bases::Void
22
17
 
@@ -16,6 +16,9 @@ module RBS
16
16
 
17
17
  # @rbs (Array[Types::t]) -> Types::t
18
18
  def merge_types: (Array[Types::t]) -> Types::t
19
+
20
+ # @rbs (Array[Types::t]) -> Array[Types::t]
21
+ def compact_types: (Array[Types::t]) -> Array[Types::t]
19
22
  end
20
23
  end
21
24
  end
@@ -21,7 +21,7 @@ module RBS
21
21
  private
22
22
 
23
23
  # @rbs (Prism::CallNode) -> bool
24
- def use_return_value?: (Prism::CallNode) -> bool
24
+ def void?: (Prism::CallNode) -> bool
25
25
  end
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takumi Shotoku
@@ -88,8 +88,8 @@ licenses:
88
88
  - MIT
89
89
  metadata:
90
90
  homepage_uri: https://github.com/sinsoku/rbs-trace
91
- source_code_uri: https://github.com/sinsoku/rbs-trace/tree/v0.4.0
92
- changelog_uri: https://github.com/sinsoku/rbs-trace/blob/v0.4.0/CHANGELOG.md
91
+ source_code_uri: https://github.com/sinsoku/rbs-trace/tree/v0.4.1
92
+ changelog_uri: https://github.com/sinsoku/rbs-trace/blob/v0.4.1/CHANGELOG.md
93
93
  rubygems_mfa_required: 'true'
94
94
  rdoc_options: []
95
95
  require_paths: