rbs-trace 0.1.0 → 0.2.0

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: 84f76be342e771b3dc3ea9ae507fce861475310bc8c093e91999e802516114a7
4
- data.tar.gz: a8a78314b1ba4ab837318e0b4f555e7898a1d16f938f2d8bd58c0e57cc5b5076
3
+ metadata.gz: e581815794d8c52a9561fb7ff53b65c1a55e724f344755a62425e819c9f13026
4
+ data.tar.gz: f47c3e52cd245fda6baa8fa5f551e0522959e22cfb955f51eaaab80eb206250d
5
5
  SHA512:
6
- metadata.gz: ef51d01034f05ac60091ed78956bd01923735aa6692b32e74e973a4eb598b27f46ebeaf60a33bd842a9d9ae4c1f8310e85de75d60cff3859c9c8a20329ba7ec9
7
- data.tar.gz: b78f4dbb9aca347a74a5f2461a553870b11509405569a1bee5464147b417bd894f51cbbe70678cd3b5ed906af1cdcea4c0e9a756a18072e496492ce4c44a0077
6
+ metadata.gz: 0fc4e5b0ab08a35732431a76003dfe6b2d11ded93552a84da005f16304714cdaa18e69a94615118373b22885b60e54dc1aa056f5416c6804155fefa305f94eb7
7
+ data.tar.gz: 3ae37b7e9b5e99bfa44bbfad481db06bf5c567d9d7852a8b868b7003a752d8df03464e58f56d6c90bd2baba11b095b7ccc31c65168599a0182c70082e5f776d5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-09-11
4
+
5
+ - feat: Support Union type
6
+ - feat: Support Optional type
7
+ - fix: Fix a bug that generated invalid RBS
8
+ - fix: Fix errors to parse methods with anonymous arguments
9
+ - fix: Consider the case where the caller cannot be found
10
+ - fix: Ignore a method generated by `class_eval`
11
+
3
12
  ## [0.1.0] - 2024-09-07
4
13
 
5
14
  - Initial release
@@ -6,6 +6,7 @@ module RBS
6
6
  METHOD_KINDS = %i[req opt rest keyreq key keyrest].freeze
7
7
  private_constant :METHOD_KINDS
8
8
 
9
+ attr_reader :parameters, :void
9
10
  attr_accessor :return_type
10
11
 
11
12
  def initialize(parameters, void: false)
@@ -14,17 +15,24 @@ module RBS
14
15
  end
15
16
 
16
17
  def to_rbs
17
- ret = if @void
18
- "void"
19
- elsif @return_type == NilClass
20
- "nil"
21
- elsif @return_type == TrueClass || @return_type == FalseClass
22
- "bool"
23
- else
24
- @return_type
25
- end
18
+ return_rbs = void ? "void" : convert_type(return_type)
26
19
 
27
- "(#{parameters_rbs}) -> #{ret}"
20
+ "(#{parameters_rbs}) -> #{return_rbs}"
21
+ end
22
+
23
+ def merge(other) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
24
+ new_parameters = @parameters.map.with_index do |parameter, index|
25
+ kind = parameter[0]
26
+ name = parameter[1]
27
+ klass = parameter[2]
28
+ other_klass = other.parameters[index][2]
29
+
30
+ merged_klass = (klass + other_klass).uniq
31
+ [kind, name, merged_klass]
32
+ end
33
+ Declaration.new(new_parameters, void: void && other.void).tap do |decl|
34
+ decl.return_type = (return_type + other.return_type).uniq
35
+ end
28
36
  end
29
37
 
30
38
  private
@@ -45,19 +53,46 @@ module RBS
45
53
  end
46
54
 
47
55
  def convert(kind, name, klass) # rubocop:disable Metrics/MethodLength
56
+ type = convert_type(klass)
48
57
  case kind
49
58
  when :req
50
- klass
59
+ type
51
60
  when :opt
52
- "?#{klass}"
61
+ "?#{type}"
53
62
  when :rest
54
- "*#{klass.join("|")}"
63
+ "*#{type}"
55
64
  when :keyreq
56
- "#{name}: #{klass}"
65
+ "#{name}: #{type}"
57
66
  when :key
58
- "?#{name}: #{klass}"
67
+ "?#{name}: #{type}"
59
68
  when :keyrest
60
- "**#{klass.join("|")}"
69
+ "**#{type}"
70
+ end
71
+ end
72
+
73
+ def convert_type(klass) # rubocop:disable Metrics
74
+ optional = klass.any? { |k| k == NilClass }
75
+ types = klass.filter_map do |k|
76
+ if k == NilClass
77
+ nil
78
+ elsif [TrueClass, FalseClass].include?(k)
79
+ "bool"
80
+ elsif k == Object
81
+ "untyped"
82
+ else
83
+ k.name
84
+ end
85
+ end.uniq
86
+ type = types.join("|")
87
+
88
+ if types.size > 1 && optional
89
+ "(#{type})?"
90
+ elsif types.empty?
91
+ "nil"
92
+ elsif optional
93
+ "#{type}?"
94
+ else
95
+ type
61
96
  end
62
97
  end
63
98
  end
@@ -20,8 +20,7 @@ module RBS
20
20
  end
21
21
 
22
22
  def rbs
23
- # TODO: merge multiple decls
24
- @decls.first.to_rbs
23
+ @decls.inject { |result, decl| result.merge(decl) }.to_rbs
25
24
  end
26
25
  end
27
26
  end
@@ -72,18 +72,19 @@ module RBS
72
72
  logger.debug(e)
73
73
  end
74
74
 
75
- def call_event(tp) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
76
- parameters = tp.parameters.map do |kind, name|
77
- value = tp.binding.local_variable_get(name)
75
+ def call_event(tp) # rubocop:disable Metrics
76
+ parameters = tp.parameters.filter_map do |kind, name|
77
+ value = tp.binding.local_variable_get(name) unless %i[* ** &].include?(name)
78
78
  klass = case kind
79
79
  when :rest
80
- value.map(&:class).uniq
80
+ value ? value.map(&:class).uniq : [Object]
81
81
  when :keyrest
82
- value.map { |_, v| v.class }.uniq
82
+ value ? value.map { |_, v| v.class }.uniq : [Object]
83
83
  when :block
84
84
  # TODO: support block argument
85
+ next
85
86
  else
86
- value.class
87
+ [value.class]
87
88
  end
88
89
  [kind, name, klass]
89
90
  end
@@ -95,7 +96,7 @@ module RBS
95
96
  # TODO: check usecase where decl is nil
96
97
  return unless decl
97
98
 
98
- decl.return_type = tp.event == :return ? tp.return_value.class : nil
99
+ decl.return_type = tp.event == :return ? [tp.return_value.class] : [nil]
99
100
  definition.decls << decl
100
101
  end
101
102
 
@@ -104,17 +105,20 @@ module RBS
104
105
  ruby_lib_path = RbConfig::CONFIG["rubylibdir"]
105
106
 
106
107
  path.start_with?("<internal") ||
108
+ path.start_with?("(eval") ||
107
109
  path.start_with?(bundle_path) ||
108
110
  path.start_with?(ruby_lib_path) ||
109
111
  path.start_with?(__FILE__)
110
112
  end
111
113
 
112
- def assign_return_value?(path, method_id) # rubocop:disable Metrics/AbcSize
114
+ def assign_return_value?(path, method_id) # rubocop:disable Metrics
113
115
  is_initialize = method_id == :initialize
114
116
  return false if is_initialize
115
117
 
116
118
  i = caller_locations.index { |loc| loc.path == path && loc.label == method_id.to_s }
117
- loc = caller_locations[i + 1]
119
+ loc = caller_locations[i + 1] if i
120
+ # If the caller is not found, assume the return value is used.
121
+ return true unless loc
118
122
 
119
123
  node = parsed_nodes(loc.path)
120
124
  method_name = is_initialize ? :new : method_id
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RBS
4
4
  module Trace
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takumi Shotoku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-07 00:00:00.000000000 Z
11
+ date: 2024-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -54,8 +54,8 @@ licenses:
54
54
  - MIT
55
55
  metadata:
56
56
  homepage_uri: https://github.com/sinsoku/rbs-trace
57
- source_code_uri: https://github.com/sinsoku/rbs-trace/blob/v0.1.0/CHANGELOG.md
58
- changelog_uri: https://github.com/sinsoku/rbs-trace/tree/v0.1.0
57
+ source_code_uri: https://github.com/sinsoku/rbs-trace/blob/v0.2.0/CHANGELOG.md
58
+ changelog_uri: https://github.com/sinsoku/rbs-trace/tree/v0.2.0
59
59
  rubygems_mfa_required: 'true'
60
60
  post_install_message:
61
61
  rdoc_options: []