rbs-trace 0.3.1 → 0.4.0

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +11 -1
  3. data/CHANGELOG.md +30 -0
  4. data/README.md +47 -24
  5. data/Rakefile +8 -6
  6. data/exe/rbs-trace +8 -0
  7. data/lib/rbs/trace/builder.rb +124 -0
  8. data/lib/rbs/trace/cli/inline.rb +62 -0
  9. data/lib/rbs/trace/cli/merge.rb +102 -0
  10. data/lib/rbs/trace/cli.rb +40 -0
  11. data/lib/rbs/trace/file.rb +69 -20
  12. data/lib/rbs/trace/helpers.rb +75 -0
  13. data/lib/rbs/trace/inline_comment_visitor.rb +63 -0
  14. data/lib/rbs/trace/overload_compact.rb +85 -0
  15. data/lib/rbs/trace/return_value_visitor.rb +63 -0
  16. data/lib/rbs/trace/version.rb +2 -2
  17. data/lib/rbs/trace.rb +153 -4
  18. data/rbs_collection.lock.yaml +27 -3
  19. data/sig/generated/rbs/trace/builder.rbs +33 -0
  20. data/sig/generated/rbs/trace/cli/inline.rbs +19 -0
  21. data/sig/generated/rbs/trace/cli/merge.rbs +24 -0
  22. data/sig/generated/rbs/trace/cli.rbs +17 -0
  23. data/sig/generated/rbs/trace/file.rbs +20 -9
  24. data/sig/generated/rbs/trace/helpers.rbs +33 -0
  25. data/sig/generated/rbs/trace/inline_comment_visitor.rbs +27 -0
  26. data/sig/generated/rbs/trace/overload_compact.rbs +21 -0
  27. data/sig/generated/rbs/trace/return_value_visitor.rbs +27 -0
  28. data/sig/generated/rbs/trace/version.rbs +1 -1
  29. data/sig/generated/rbs/trace.rbs +63 -1
  30. data/tmp/.keep +0 -0
  31. metadata +39 -17
  32. data/gemfiles/3.1.gemfile +0 -12
  33. data/gemfiles/3.1.gemfile.lock +0 -68
  34. data/lib/rbs/trace/declaration.rb +0 -107
  35. data/lib/rbs/trace/definition.rb +0 -33
  36. data/lib/rbs/trace/method_tracing.rb +0 -178
  37. data/sig/generated/rbs/trace/declaration.rbs +0 -36
  38. data/sig/generated/rbs/trace/definition.rbs +0 -25
  39. data/sig/generated/rbs/trace/method_tracing.rbs +0 -63
@@ -0,0 +1,33 @@
1
+ # Generated from lib/rbs/trace/helpers.rb with RBS::Inline
2
+
3
+ module RBS
4
+ class Trace
5
+ module Helpers
6
+ UNBOUND_CLASS_METHOD: untyped
7
+
8
+ # @rbs (name: TypeName) -> AST::Declarations::Module
9
+ def new_module_decl: (name: TypeName) -> AST::Declarations::Module
10
+
11
+ # @rbs (name: TypeName) -> AST::Declarations::Class
12
+ def new_class_decl: (name: TypeName) -> AST::Declarations::Class
13
+
14
+ # @rbs (name: Symbol, kind: (:singleton | :instance)) -> AST::Members::MethodDefinition
15
+ def new_method_definition: (name: Symbol, kind: :singleton | :instance) -> AST::Members::MethodDefinition
16
+
17
+ # @rbs (BasicObject) -> Class
18
+ def obj_to_class: (BasicObject) -> Class
19
+
20
+ # @rbs () -> Types::Bases::Void
21
+ def type_void: () -> Types::Bases::Void
22
+
23
+ # @rbs () -> Types::Bases::Nil
24
+ def type_nil: () -> Types::Bases::Nil
25
+
26
+ # @rbs () -> Types::Bases::Bool
27
+ def type_bool: () -> Types::Bases::Bool
28
+
29
+ # @rbs () -> Types::Bases::Any
30
+ def type_untyped: () -> Types::Bases::Any
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ # Generated from lib/rbs/trace/inline_comment_visitor.rb with RBS::Inline
2
+
3
+ module RBS
4
+ class Trace
5
+ class InlineCommentVisitor < Prism::Visitor
6
+ # @rbs (Hash[TypeName, AST::Declarations::t], Hash[Integer, String]) -> void
7
+ def initialize: (Hash[TypeName, AST::Declarations::t], Hash[Integer, String]) -> void
8
+
9
+ # @rbs override
10
+ # @rbs (Prism::Node) -> void
11
+ def visit_class_node: ...
12
+
13
+ # @rbs override
14
+ # @rbs (Prism::Node) -> void
15
+ def visit_module_node: ...
16
+
17
+ # @rbs override
18
+ # @rbs (Prism::Node) -> void
19
+ def visit_def_node: ...
20
+
21
+ private
22
+
23
+ # @rbs (Symbol) -> AST::Members::MethodDefinition?
24
+ def find_method_definition: (Symbol) -> AST::Members::MethodDefinition?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ # Generated from lib/rbs/trace/overload_compact.rb with RBS::Inline
2
+
3
+ module RBS
4
+ class Trace
5
+ class OverloadCompact
6
+ # @rbs (Array[AST::Members::MethodDefinition::Overload]) -> void
7
+ def initialize: (Array[AST::Members::MethodDefinition::Overload]) -> void
8
+
9
+ # @rbs () -> Array[AST::Members::MethodDefinition::Overload]
10
+ def call: () -> Array[AST::Members::MethodDefinition::Overload]
11
+
12
+ private
13
+
14
+ # @rbs (Array[MethodType]) -> MethodType
15
+ def merge_method_types: (Array[MethodType]) -> MethodType
16
+
17
+ # @rbs (Array[Types::t]) -> Types::t
18
+ def merge_types: (Array[Types::t]) -> Types::t
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ # Generated from lib/rbs/trace/return_value_visitor.rb with RBS::Inline
2
+
3
+ module RBS
4
+ class Trace
5
+ class ReturnValueVisitor < Prism::BasicVisitor
6
+ # @rbs (String) -> ReturnValueVisitor
7
+ def self.parse_file: (String) -> ReturnValueVisitor
8
+
9
+ # @rbs () -> void
10
+ def initialize: () -> void
11
+
12
+ # @rbs (Integer, Symbol) -> bool
13
+ def void_type?: (Integer, Symbol) -> bool
14
+
15
+ # @rbs (Prism::Node) -> void
16
+ def visit_child_nodes: (Prism::Node) -> void
17
+
18
+ # @rbs (Prism::CallNode) -> void
19
+ def visit_call_node: (Prism::CallNode) -> void
20
+
21
+ private
22
+
23
+ # @rbs (Prism::CallNode) -> bool
24
+ def use_return_value?: (Prism::CallNode) -> bool
25
+ end
26
+ end
27
+ end
@@ -1,7 +1,7 @@
1
1
  # Generated from lib/rbs/trace/version.rb with RBS::Inline
2
2
 
3
3
  module RBS
4
- module Trace
4
+ class Trace
5
5
  VERSION: ::String
6
6
  end
7
7
  end
@@ -1,8 +1,70 @@
1
1
  # Generated from lib/rbs/trace.rb with RBS::Inline
2
2
 
3
3
  module RBS
4
- module Trace
4
+ class Trace
5
5
  class Error < StandardError
6
6
  end
7
+
8
+ # steep:ignore:start
9
+ BUNDLE_PATH: String
10
+
11
+ # steep:ignore:end
12
+ RUBY_LIB_PATH: String
13
+
14
+ PATH_INTERNAL: String
15
+
16
+ PATH_EVAL: String
17
+
18
+ PATH_INLINE_TEMPLATE: String
19
+
20
+ # @rbs (?log_level: Symbol, ?raises: bool) -> void
21
+ def initialize: (?log_level: Symbol, ?raises: bool) -> void
22
+
23
+ # @rbs [T] () { () -> T } -> T
24
+ def enable: [T] () { () -> T } -> T
25
+
26
+ # @rbs () -> void
27
+ def disable: () -> void
28
+
29
+ # @rbs () -> Hash[String, File]
30
+ def files: () -> Hash[String, File]
31
+
32
+ # @rbs () -> void
33
+ def save_comments: () -> void
34
+
35
+ # @rbs (out_dir: String) -> void
36
+ def save_files: (out_dir: String) -> void
37
+
38
+ private
39
+
40
+ # @rbs () -> TracePoint
41
+ def trace: () -> TracePoint
42
+
43
+ # @rbs () -> Logger
44
+ def logger: () -> Logger
45
+
46
+ # @rbs () -> Builder
47
+ def builder: () -> Builder
48
+
49
+ # @rbs (String) -> File
50
+ def find_or_new_file: (String) -> File
51
+
52
+ # @rbs (TracePoint) -> void
53
+ def record: (TracePoint) -> void
54
+
55
+ # @rbs (TracePoint, AST::Members::MethodDefinition) -> void
56
+ def call_event: (TracePoint, AST::Members::MethodDefinition) -> void
57
+
58
+ # @rbs (TracePoint, AST::Members::MethodDefinition) -> void
59
+ def return_event: (TracePoint, AST::Members::MethodDefinition) -> void
60
+
61
+ # @rbs (String) -> bool
62
+ def ignore_path?: (String) -> bool
63
+
64
+ # @rbs (String, Symbol) -> bool
65
+ def void_return_type?: (String, Symbol) -> bool
66
+
67
+ # @rbs (String, String) -> Thread::Backtrace::Location?
68
+ def find_caller_location: (String, String) -> Thread::Backtrace::Location?
7
69
  end
8
70
  end
data/tmp/.keep ADDED
File without changes
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takumi Shotoku
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-10-19 00:00:00.000000000 Z
10
+ date: 2025-02-23 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: prism
@@ -24,12 +23,27 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: 0.3.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: rbs
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.5.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.5.0
27
40
  description: |
28
41
  RBS::Trace collects argument types and return value types using TracePoint, and inserts
29
42
  inline RBS type declarations into files.
30
43
  email:
31
44
  - sinsoku.listy@gmail.com
32
- executables: []
45
+ executables:
46
+ - rbs-trace
33
47
  extensions: []
34
48
  extra_rdoc_files: []
35
49
  files:
@@ -41,33 +55,42 @@ files:
41
55
  - README.md
42
56
  - Rakefile
43
57
  - Steepfile
44
- - gemfiles/3.1.gemfile
45
- - gemfiles/3.1.gemfile.lock
58
+ - exe/rbs-trace
46
59
  - lib/rbs-trace.rb
47
60
  - lib/rbs/trace.rb
48
- - lib/rbs/trace/declaration.rb
49
- - lib/rbs/trace/definition.rb
61
+ - lib/rbs/trace/builder.rb
62
+ - lib/rbs/trace/cli.rb
63
+ - lib/rbs/trace/cli/inline.rb
64
+ - lib/rbs/trace/cli/merge.rb
50
65
  - lib/rbs/trace/file.rb
51
- - lib/rbs/trace/method_tracing.rb
66
+ - lib/rbs/trace/helpers.rb
67
+ - lib/rbs/trace/inline_comment_visitor.rb
68
+ - lib/rbs/trace/overload_compact.rb
69
+ - lib/rbs/trace/return_value_visitor.rb
52
70
  - lib/rbs/trace/version.rb
53
71
  - rbs_collection.lock.yaml
54
72
  - rbs_collection.yaml
55
73
  - sig/generated/rbs-trace.rbs
56
74
  - sig/generated/rbs/trace.rbs
57
- - sig/generated/rbs/trace/declaration.rbs
58
- - sig/generated/rbs/trace/definition.rbs
75
+ - sig/generated/rbs/trace/builder.rbs
76
+ - sig/generated/rbs/trace/cli.rbs
77
+ - sig/generated/rbs/trace/cli/inline.rbs
78
+ - sig/generated/rbs/trace/cli/merge.rbs
59
79
  - sig/generated/rbs/trace/file.rbs
60
- - sig/generated/rbs/trace/method_tracing.rbs
80
+ - sig/generated/rbs/trace/helpers.rbs
81
+ - sig/generated/rbs/trace/inline_comment_visitor.rbs
82
+ - sig/generated/rbs/trace/overload_compact.rbs
83
+ - sig/generated/rbs/trace/return_value_visitor.rbs
61
84
  - sig/generated/rbs/trace/version.rbs
85
+ - tmp/.keep
62
86
  homepage: https://github.com/sinsoku/rbs-trace
63
87
  licenses:
64
88
  - MIT
65
89
  metadata:
66
90
  homepage_uri: https://github.com/sinsoku/rbs-trace
67
- source_code_uri: https://github.com/sinsoku/rbs-trace/tree/v0.3.1
68
- changelog_uri: https://github.com/sinsoku/rbs-trace/blob/v0.3.1/CHANGELOG.md
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
69
93
  rubygems_mfa_required: 'true'
70
- post_install_message:
71
94
  rdoc_options: []
72
95
  require_paths:
73
96
  - lib
@@ -82,8 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
105
  - !ruby/object:Gem::Version
83
106
  version: '0'
84
107
  requirements: []
85
- rubygems_version: 3.5.17
86
- signing_key:
108
+ rubygems_version: 3.6.2
87
109
  specification_version: 4
88
110
  summary: Automatically Insert inline RBS type declarations using runtime information.
89
111
  test_files: []
data/gemfiles/3.1.gemfile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in rbs-trace.gemspec
6
- gemspec(path: "..")
7
-
8
- gem "rake"
9
- gem "rspec"
10
- gem "rubocop", require: false
11
- gem "rubocop-rake", require: false
12
- gem "rubocop-rspec", require: false
@@ -1,68 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- rbs-trace (0.3.1)
5
- prism (>= 0.3.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- ast (2.4.2)
11
- diff-lcs (1.5.1)
12
- json (2.7.2)
13
- language_server-protocol (3.17.0.3)
14
- parallel (1.26.3)
15
- parser (3.3.5.0)
16
- ast (~> 2.4.1)
17
- racc
18
- prism (1.0.0)
19
- racc (1.8.1)
20
- rainbow (3.1.1)
21
- rake (13.2.1)
22
- regexp_parser (2.9.2)
23
- rspec (3.13.0)
24
- rspec-core (~> 3.13.0)
25
- rspec-expectations (~> 3.13.0)
26
- rspec-mocks (~> 3.13.0)
27
- rspec-core (3.13.1)
28
- rspec-support (~> 3.13.0)
29
- rspec-expectations (3.13.3)
30
- diff-lcs (>= 1.2.0, < 2.0)
31
- rspec-support (~> 3.13.0)
32
- rspec-mocks (3.13.1)
33
- diff-lcs (>= 1.2.0, < 2.0)
34
- rspec-support (~> 3.13.0)
35
- rspec-support (3.13.1)
36
- rubocop (1.66.1)
37
- json (~> 2.3)
38
- language_server-protocol (>= 3.17.0)
39
- parallel (~> 1.10)
40
- parser (>= 3.3.0.2)
41
- rainbow (>= 2.2.2, < 4.0)
42
- regexp_parser (>= 2.4, < 3.0)
43
- rubocop-ast (>= 1.32.2, < 2.0)
44
- ruby-progressbar (~> 1.7)
45
- unicode-display_width (>= 2.4.0, < 3.0)
46
- rubocop-ast (1.32.3)
47
- parser (>= 3.3.1.0)
48
- rubocop-rake (0.6.0)
49
- rubocop (~> 1.0)
50
- rubocop-rspec (3.0.5)
51
- rubocop (~> 1.61)
52
- ruby-progressbar (1.13.0)
53
- unicode-display_width (2.5.0)
54
-
55
- PLATFORMS
56
- arm64-darwin-23
57
- ruby
58
-
59
- DEPENDENCIES
60
- rake
61
- rbs-trace!
62
- rspec
63
- rubocop
64
- rubocop-rake
65
- rubocop-rspec
66
-
67
- BUNDLED WITH
68
- 2.5.17
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RBS
4
- module Trace
5
- class Declaration
6
- METHOD_KINDS = %i[req opt rest keyreq key keyrest].freeze #: Array[Symbol]
7
- private_constant :METHOD_KINDS
8
-
9
- attr_reader :parameters #: Array[untyped]
10
- attr_reader :void #: bool
11
- attr_accessor :return_type #: Array[Object]
12
-
13
- # @rbs (Array[untyped], ?void: bool) -> void
14
- def initialize(parameters, void: false)
15
- @parameters = parameters
16
- @void = void
17
- end
18
-
19
- # @rbs () -> String
20
- def to_rbs
21
- return_rbs = void ? "void" : convert_type(return_type)
22
-
23
- "(#{parameters_rbs}) -> #{return_rbs}"
24
- end
25
-
26
- # @rbs (Declaration) -> Declaration
27
- def merge(other) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
28
- new_parameters = @parameters.map.with_index do |parameter, index|
29
- kind = parameter[0]
30
- name = parameter[1]
31
- klass = parameter[2]
32
- other_klass = other.parameters[index][2]
33
-
34
- merged_klass = (klass + other_klass).uniq
35
- [kind, name, merged_klass]
36
- end
37
- Declaration.new(new_parameters, void: void && other.void).tap do |decl|
38
- decl.return_type = (return_type + other.return_type).uniq
39
- end
40
- end
41
-
42
- private
43
-
44
- # TODO: support block argument
45
- # @rbs () -> String
46
- def parameters_rbs
47
- converted = {}
48
- @parameters.each do |kind, name, klass|
49
- converted[kind] ||= []
50
- converted[kind] << convert(kind, name, klass)
51
- end
52
-
53
- METHOD_KINDS.flat_map { |kind| converted[kind] }.compact.join(", ")
54
- end
55
-
56
- # @rbs (Symbol, Symbol, Array[Object]) -> String?
57
- def convert(kind, name, klass) # rubocop:disable Metrics/MethodLength
58
- type = convert_type(klass)
59
- case kind
60
- when :req
61
- type
62
- when :opt
63
- "?#{type}"
64
- when :rest
65
- "*#{type}"
66
- when :keyreq
67
- "#{name}: #{type}"
68
- when :key
69
- "?#{name}: #{type}"
70
- when :keyrest
71
- "**#{type}"
72
- end
73
- end
74
-
75
- # @rbs (Array[Object]) -> String
76
- def convert_type(klass) # rubocop:disable Metrics
77
- optional = klass.any? { |k| k == NilClass }
78
- types = klass.filter_map do |k|
79
- if k == NilClass
80
- nil
81
- elsif [TrueClass, FalseClass].include?(k) # steep:ignore ArgumentTypeMismatch
82
- "bool"
83
- elsif [Array, Range].include?(k) # steep:ignore ArgumentTypeMismatch
84
- "#{k}[untyped]"
85
- elsif k == Hash
86
- "#{k}[untyped, untyped]"
87
- elsif k == Object
88
- "untyped"
89
- else
90
- k.name # steep:ignore NoMethod
91
- end
92
- end.uniq
93
- type = types.join("|")
94
-
95
- if types.size > 1 && optional
96
- "(#{type})?"
97
- elsif types.empty?
98
- "nil"
99
- elsif optional
100
- "#{type}?"
101
- else
102
- type
103
- end
104
- end
105
- end
106
- end
107
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RBS
4
- module Trace
5
- class Definition
6
- attr_reader :klass #: Class
7
- attr_reader :name #: Symbol
8
- attr_reader :lineno #: Integer
9
-
10
- # @rbs (klass: Object, name: Symbol, lineno: Integer) -> void
11
- def initialize(klass:, name:, lineno:)
12
- @klass = klass
13
- @name = name
14
- @lineno = lineno
15
- end
16
-
17
- # @rbs () -> Array[Declaration]
18
- def decls
19
- @decls ||= []
20
- end
21
-
22
- # @rbs (?Integer) -> String
23
- def rbs_comment(indent = 0)
24
- "#{" " * indent}# @rbs #{rbs}\n"
25
- end
26
-
27
- # @rbs () -> String
28
- def rbs
29
- @decls.inject { |result, decl| result.merge(decl) }.to_rbs
30
- end
31
- end
32
- end
33
- end