rbs-trace 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +12 -0
- data/lib/rbs/trace/declaration.rb +65 -0
- data/lib/rbs/trace/definition.rb +28 -0
- data/lib/rbs/trace/file.rb +45 -0
- data/lib/rbs/trace/method_tracing.rb +146 -0
- data/lib/rbs/trace/version.rb +7 -0
- data/lib/rbs/trace.rb +17 -0
- data/lib/rbs-trace.rb +3 -0
- data/sig/rbs/trace.rbs +6 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 84f76be342e771b3dc3ea9ae507fce861475310bc8c093e91999e802516114a7
|
4
|
+
data.tar.gz: a8a78314b1ba4ab837318e0b4f555e7898a1d16f938f2d8bd58c0e57cc5b5076
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef51d01034f05ac60091ed78956bd01923735aa6692b32e74e973a4eb598b27f46ebeaf60a33bd842a9d9ae4c1f8310e85de75d60cff3859c9c8a20329ba7ec9
|
7
|
+
data.tar.gz: b78f4dbb9aca347a74a5f2461a553870b11509405569a1bee5464147b417bd894f51cbbe70678cd3b5ed906af1cdcea4c0e9a756a18072e496492ce4c44a0077
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
NewCops: enable
|
7
|
+
|
8
|
+
Style/StringLiterals:
|
9
|
+
EnforcedStyle: double_quotes
|
10
|
+
|
11
|
+
Style/StringLiteralsInInterpolation:
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
RSpec/ExampleLength:
|
18
|
+
Max: 15
|
19
|
+
|
20
|
+
Naming/MethodParameterName:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Naming/FileName:
|
24
|
+
Exclude:
|
25
|
+
- "lib/rbs-trace.rb"
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.3
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Takumi Shotoku
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# RBS::Trace
|
2
|
+
|
3
|
+
RBS::Trace collects argument types and return value types using TracePoint, and inserts inline RBS type declarations into files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add rbs-trace
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install rbs-trace
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
For example, suppose you have the following class:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User
|
21
|
+
def initialize(first_name, last_name)
|
22
|
+
@first_name = first_name
|
23
|
+
@last_name = last_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def full_name
|
27
|
+
"#{@first_name} #{@last_name}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def say_hello
|
31
|
+
puts "hi, #{full_name}."
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Call target methods within the `enable` method block, and call the `insert_rbs` method.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
tracing = RBS::Trace::MethodTracing.new
|
40
|
+
|
41
|
+
# Collects the types of methods called in the block.
|
42
|
+
tracing.enable do
|
43
|
+
user = User.new("Nanoha", "Takamachi")
|
44
|
+
user.say_hello
|
45
|
+
end
|
46
|
+
|
47
|
+
tracing.insert_rbs
|
48
|
+
```
|
49
|
+
|
50
|
+
Automatically inserts inline RBS definitions into the file.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class User
|
54
|
+
# @rbs (String, String) -> void
|
55
|
+
def initialize(first_name, last_name)
|
56
|
+
@first_name = first_name
|
57
|
+
@last_name = last_name
|
58
|
+
end
|
59
|
+
|
60
|
+
# @rbs () -> String
|
61
|
+
def full_name
|
62
|
+
"#{@first_name} #{@last_name}"
|
63
|
+
end
|
64
|
+
|
65
|
+
# @rbs () -> void
|
66
|
+
def say_hello
|
67
|
+
puts "hi, #{full_name}."
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### RSpec
|
73
|
+
|
74
|
+
Add the following code to `spec/support/rbs_trace.rb`.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
return unless ENV["RBS_TRACE"]
|
78
|
+
|
79
|
+
RSpec.configure do |config|
|
80
|
+
tracing = RBS::Trace::MethodTracing.new
|
81
|
+
|
82
|
+
config.before(:suite) { tracing.enable }
|
83
|
+
config.after(:suite) do
|
84
|
+
tracing.disable
|
85
|
+
tracing.insert_rbs
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
Then run RSpec with the environment variables.
|
91
|
+
|
92
|
+
```console
|
93
|
+
$ RBS_TRACE=1 bundle exec rspec
|
94
|
+
```
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sinsoku/rbs-trace. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sinsoku/rbs-trace/blob/main/CODE_OF_CONDUCT.md).
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
109
|
+
|
110
|
+
## Code of Conduct
|
111
|
+
|
112
|
+
Everyone interacting in the Rbs::Trace project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sinsoku/rbs-trace/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
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
|
7
|
+
private_constant :METHOD_KINDS
|
8
|
+
|
9
|
+
attr_accessor :return_type
|
10
|
+
|
11
|
+
def initialize(parameters, void: false)
|
12
|
+
@parameters = parameters
|
13
|
+
@void = void
|
14
|
+
end
|
15
|
+
|
16
|
+
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
|
26
|
+
|
27
|
+
"(#{parameters_rbs}) -> #{ret}"
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# TODO: support block argument
|
33
|
+
def parameters_rbs
|
34
|
+
converted = {}
|
35
|
+
@parameters.each do |kind, name, klass|
|
36
|
+
converted[kind] ||= []
|
37
|
+
converted[kind] << convert(kind, name, klass)
|
38
|
+
end
|
39
|
+
|
40
|
+
METHOD_KINDS.flat_map { |kind| converted[kind] }.compact.join(", ")
|
41
|
+
end
|
42
|
+
|
43
|
+
def select_parameters(selected)
|
44
|
+
@parameters.select { |kind, _name, _klass| kind == selected }
|
45
|
+
end
|
46
|
+
|
47
|
+
def convert(kind, name, klass) # rubocop:disable Metrics/MethodLength
|
48
|
+
case kind
|
49
|
+
when :req
|
50
|
+
klass
|
51
|
+
when :opt
|
52
|
+
"?#{klass}"
|
53
|
+
when :rest
|
54
|
+
"*#{klass.join("|")}"
|
55
|
+
when :keyreq
|
56
|
+
"#{name}: #{klass}"
|
57
|
+
when :key
|
58
|
+
"?#{name}: #{klass}"
|
59
|
+
when :keyrest
|
60
|
+
"**#{klass.join("|")}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RBS
|
4
|
+
module Trace
|
5
|
+
class Definition
|
6
|
+
attr_reader :klass, :name, :lineno
|
7
|
+
|
8
|
+
def initialize(klass:, name:, lineno:)
|
9
|
+
@klass = klass
|
10
|
+
@name = name
|
11
|
+
@lineno = lineno
|
12
|
+
end
|
13
|
+
|
14
|
+
def decls
|
15
|
+
@decls ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def rbs_comment(indent = 0)
|
19
|
+
"#{" " * indent}# @rbs #{rbs}\n"
|
20
|
+
end
|
21
|
+
|
22
|
+
def rbs
|
23
|
+
# TODO: merge multiple decls
|
24
|
+
@decls.first.to_rbs
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RBS
|
4
|
+
module Trace
|
5
|
+
class File
|
6
|
+
def initialize(path)
|
7
|
+
@path = path
|
8
|
+
end
|
9
|
+
|
10
|
+
def definitions
|
11
|
+
@definitions ||= {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_rbs
|
15
|
+
lines = ::File.readlines(@path)
|
16
|
+
reverse_definitions.each do |d|
|
17
|
+
next if skip_insert?(lines, d)
|
18
|
+
|
19
|
+
current = d.lineno - 1
|
20
|
+
indent = lines[current]&.index("def")
|
21
|
+
next unless indent
|
22
|
+
|
23
|
+
lines.insert(current, d.rbs_comment(indent))
|
24
|
+
end
|
25
|
+
lines.join
|
26
|
+
end
|
27
|
+
|
28
|
+
def rewrite
|
29
|
+
::File.write(@path, with_rbs)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def skip_insert?(lines, definition)
|
35
|
+
prev = definition.lineno - 2
|
36
|
+
|
37
|
+
definition.decls.empty? || lines[prev]&.include?("# @rbs")
|
38
|
+
end
|
39
|
+
|
40
|
+
def reverse_definitions
|
41
|
+
@definitions.values.sort_by { |d| -d.lineno }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RBS
|
4
|
+
module Trace
|
5
|
+
class MethodTracing # rubocop:disable Metrics/ClassLength
|
6
|
+
ASSIGNED_NODE_TYPES = %i[statements_node local_variable_write_node instance_variable_write_node
|
7
|
+
class_variable_write_node constant_write_node call_node embedded_statements_node].freeze
|
8
|
+
private_constant :ASSIGNED_NODE_TYPES
|
9
|
+
|
10
|
+
def enable(&)
|
11
|
+
trace.enable(&)
|
12
|
+
end
|
13
|
+
|
14
|
+
def disable
|
15
|
+
trace.disable
|
16
|
+
end
|
17
|
+
|
18
|
+
def files
|
19
|
+
@files ||= {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def insert_rbs
|
23
|
+
files.each_value(&:rewrite)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def trace
|
29
|
+
@trace ||= TracePoint.new(:call, :return, :raise) { |tp| record(tp) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def logger
|
33
|
+
return @logger if defined?(@logger)
|
34
|
+
|
35
|
+
level = ENV["RBS_TRACE_DEBUG"] ? :debug : :info
|
36
|
+
@logger = Logger.new($stdout, level:)
|
37
|
+
end
|
38
|
+
|
39
|
+
def stack_traces
|
40
|
+
@stack_traces ||= []
|
41
|
+
end
|
42
|
+
|
43
|
+
def find_or_new_file(path)
|
44
|
+
files[path] ||= File.new(path)
|
45
|
+
files[path]
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_or_new_definition(file, tp)
|
49
|
+
name = tp.method_id
|
50
|
+
is_singleton = tp.defined_class.singleton_class?
|
51
|
+
klass = is_singleton ? tp.self : tp.defined_class
|
52
|
+
mark = is_singleton ? "." : "#"
|
53
|
+
signature = "#{klass}#{mark}#{name}"
|
54
|
+
|
55
|
+
_filename, lineno = tp.self.method(name).source_location
|
56
|
+
file.definitions[signature] ||= Definition.new(klass:, name:, lineno:)
|
57
|
+
end
|
58
|
+
|
59
|
+
def record(tp) # rubocop:disable Metrics/MethodLength
|
60
|
+
return if ignore_path?(tp.path)
|
61
|
+
|
62
|
+
file = find_or_new_file(tp.path)
|
63
|
+
definition = find_or_new_definition(file, tp)
|
64
|
+
|
65
|
+
case tp.event
|
66
|
+
when :call
|
67
|
+
call_event(tp)
|
68
|
+
when :return, :raise
|
69
|
+
return_event(tp, definition)
|
70
|
+
end
|
71
|
+
rescue StandardError => e
|
72
|
+
logger.debug(e)
|
73
|
+
end
|
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)
|
78
|
+
klass = case kind
|
79
|
+
when :rest
|
80
|
+
value.map(&:class).uniq
|
81
|
+
when :keyrest
|
82
|
+
value.map { |_, v| v.class }.uniq
|
83
|
+
when :block
|
84
|
+
# TODO: support block argument
|
85
|
+
else
|
86
|
+
value.class
|
87
|
+
end
|
88
|
+
[kind, name, klass]
|
89
|
+
end
|
90
|
+
stack_traces << Declaration.new(parameters, void: !assign_return_value?(tp.path, tp.method_id))
|
91
|
+
end
|
92
|
+
|
93
|
+
def return_event(tp, definition)
|
94
|
+
decl = stack_traces.pop
|
95
|
+
# TODO: check usecase where decl is nil
|
96
|
+
return unless decl
|
97
|
+
|
98
|
+
decl.return_type = tp.event == :return ? tp.return_value.class : nil
|
99
|
+
definition.decls << decl
|
100
|
+
end
|
101
|
+
|
102
|
+
def ignore_path?(path)
|
103
|
+
bundle_path = Bundler.bundle_path.to_s
|
104
|
+
ruby_lib_path = RbConfig::CONFIG["rubylibdir"]
|
105
|
+
|
106
|
+
path.start_with?("<internal") ||
|
107
|
+
path.start_with?(bundle_path) ||
|
108
|
+
path.start_with?(ruby_lib_path) ||
|
109
|
+
path.start_with?(__FILE__)
|
110
|
+
end
|
111
|
+
|
112
|
+
def assign_return_value?(path, method_id) # rubocop:disable Metrics/AbcSize
|
113
|
+
is_initialize = method_id == :initialize
|
114
|
+
return false if is_initialize
|
115
|
+
|
116
|
+
i = caller_locations.index { |loc| loc.path == path && loc.label == method_id.to_s }
|
117
|
+
loc = caller_locations[i + 1]
|
118
|
+
|
119
|
+
node = parsed_nodes(loc.path)
|
120
|
+
method_name = is_initialize ? :new : method_id
|
121
|
+
parents = find_parents(node, method_name:, lineno: loc.lineno)
|
122
|
+
return false unless parents
|
123
|
+
|
124
|
+
parent = parents[1]
|
125
|
+
ASSIGNED_NODE_TYPES.include?(parent.type)
|
126
|
+
end
|
127
|
+
|
128
|
+
def find_parents(node, method_name:, lineno:, parents: [])
|
129
|
+
result = nil
|
130
|
+
node.compact_child_nodes.each do |child|
|
131
|
+
break if result
|
132
|
+
|
133
|
+
found = child.type == :call_node && child.name == method_name && child.location.start_line == lineno
|
134
|
+
result = found ? [child, *parents] : find_parents(child, method_name:, lineno:, parents: [node, *parents])
|
135
|
+
end
|
136
|
+
result
|
137
|
+
end
|
138
|
+
|
139
|
+
def parsed_nodes(path)
|
140
|
+
@parsed_nodes ||= {}
|
141
|
+
@parsed_nodes[path] ||= Prism.parse_file(path)
|
142
|
+
@parsed_nodes[path].value
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
data/lib/rbs/trace.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler"
|
4
|
+
require "logger"
|
5
|
+
require "prism"
|
6
|
+
|
7
|
+
require_relative "trace/declaration"
|
8
|
+
require_relative "trace/definition"
|
9
|
+
require_relative "trace/file"
|
10
|
+
require_relative "trace/method_tracing"
|
11
|
+
require_relative "trace/version"
|
12
|
+
|
13
|
+
module RBS
|
14
|
+
module Trace
|
15
|
+
class Error < StandardError; end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rbs-trace.rb
ADDED
data/sig/rbs/trace.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbs-trace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takumi Shotoku
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: prism
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.0
|
27
|
+
description: |
|
28
|
+
RBS::Trace collects argument types and return value types using TracePoint, and inserts
|
29
|
+
inline RBS type declarations into files."
|
30
|
+
email:
|
31
|
+
- sinsoku.listy@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- ".ruby-version"
|
39
|
+
- CHANGELOG.md
|
40
|
+
- CODE_OF_CONDUCT.md
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- lib/rbs-trace.rb
|
45
|
+
- lib/rbs/trace.rb
|
46
|
+
- lib/rbs/trace/declaration.rb
|
47
|
+
- lib/rbs/trace/definition.rb
|
48
|
+
- lib/rbs/trace/file.rb
|
49
|
+
- lib/rbs/trace/method_tracing.rb
|
50
|
+
- lib/rbs/trace/version.rb
|
51
|
+
- sig/rbs/trace.rbs
|
52
|
+
homepage: https://github.com/sinsoku/rbs-trace
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
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
|
59
|
+
rubygems_mfa_required: 'true'
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.2.0
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.5.17
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Automatically Insert inline RBS type declarations using runtime information.
|
79
|
+
test_files: []
|