deprewriter 0.1.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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +172 -0
- data/Rakefile +10 -0
- data/examples/change_arg.rb +6 -0
- data/examples/legacy.rb +30 -0
- data/examples/rename.rb +6 -0
- data/lib/deprewriter/call_site_finder.rb +43 -0
- data/lib/deprewriter/configuration.rb +31 -0
- data/lib/deprewriter/diff.rb +40 -0
- data/lib/deprewriter/rewriter.rb +19 -0
- data/lib/deprewriter/transformer.rb +37 -0
- data/lib/deprewriter/version.rb +5 -0
- data/lib/deprewriter.rb +74 -0
- data/sig/deprewriter.rbs +7 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6a1731461f0d81a70da7f392caa676648aecb1d431fb47da19853a9561a777cd
|
4
|
+
data.tar.gz: ed04323ce3750d048bdb9e771c3c8aafd9d6e3bfad502ed6286be4f9073cf4a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d9ad5c9d04d5de76ed5c098cd847e4ee48a048603e7e812562283f5ea1d740d13773fe0a1605f08f8773f3bf73147e513cbf98c6cc6f2d0d83258f5425692a4
|
7
|
+
data.tar.gz: 1ad1ef83e1bce07723eb8e9bf2f476f9e78f71e4dfce5611d2f752b2bf8e264989225db0a4989d0ebd3e65584b051cfb982c784a66848dfd9efa792c829cb0f7
|
data/.rspec
ADDED
data/.standard.yml
ADDED
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) 2025 ohbarye
|
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,172 @@
|
|
1
|
+
# Deprewriter
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/deprewriter)
|
4
|
+
[](https://github.com/ohbarye/deprewriter/actions/workflows/main.yml)
|
5
|
+
[](https://www.rubydoc.info/gems/deprewriter)
|
6
|
+
|
7
|
+
Deprewriter is an experimental gem that helps you rewrite deprecated code automatically.
|
8
|
+
|
9
|
+
When library authors deprecate methods, they typically notify users through documentation and deprecation warnings at runtime. Users then need to manually update their code to stop using deprecated methods and switch to recommended alternatives. Deprewriter automates this process.
|
10
|
+
|
11
|
+
This project is inspired by Deprewriter in the Pharo programming language. For more details, see "[Deprewriter: On the fly rewriting method deprecations.](https://inria.hal.science/hal-03563605/document#page=2.15&gsr=0)" (2022).
|
12
|
+
|
13
|
+
For more details about this gem and its implementation, see the presentation at RubyKaigi 2025: [On-the-fly Suggestions of Rewriting Method Deprecations](https://rubykaigi.org/2025/presentations/ohbarye.html).
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Install the gem and add to the application's Gemfile by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle add deprewriter
|
21
|
+
```
|
22
|
+
|
23
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
gem install deprewriter
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Let's say a library code has a class `Legacy` with a deprecated method `old_method` and a new method `new_method`. The code owner can declare `deprewrite` with transformation rules.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# Library code
|
35
|
+
require "deprewriter"
|
36
|
+
|
37
|
+
class Legacy
|
38
|
+
def old_method(arg)
|
39
|
+
# ...
|
40
|
+
end
|
41
|
+
|
42
|
+
def new_method(arg)
|
43
|
+
# ...
|
44
|
+
end
|
45
|
+
|
46
|
+
extend Deprewriter
|
47
|
+
deprewrite :old_method, to: "new_method({{arguments}})"
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
And then, when a user program calls `old_method` with the following code:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# User code
|
55
|
+
legacy = Legacy.new
|
56
|
+
legacy.old_method(arg) # This line will trigger Deprewriter
|
57
|
+
```
|
58
|
+
|
59
|
+
If a user program runs with `DEPREWRITER=log` environment variable...
|
60
|
+
|
61
|
+
```bash
|
62
|
+
DEPREWRITER=1 ruby your_script.rb
|
63
|
+
```
|
64
|
+
|
65
|
+
...it will log a warning with the suggested changes to rewrite the code:
|
66
|
+
|
67
|
+
```diff
|
68
|
+
legacy = Legacy.new
|
69
|
+
-legacy.old_method(arg)
|
70
|
+
+legacy.new_method(arg)
|
71
|
+
```
|
72
|
+
|
73
|
+
### For code owners
|
74
|
+
|
75
|
+
You can also specify a pattern to match for transformation using the `from` option:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
deprewrite :old_method,
|
79
|
+
from: '.call_node[name=old_method]', # Rewrite calls only when matched
|
80
|
+
to: 'new_method({{arguments}})'
|
81
|
+
```
|
82
|
+
|
83
|
+
The syntax of transformation rules is taken from [Synvert](https://synvert.net/) and it uses [Prism](https://ruby.github.io/prism/) to parse and rewrite the source code. Variables in a transformation rule string like `{{arguments}}` and `{{block}}` etc. correspond to methods provided by Prism node.
|
84
|
+
|
85
|
+
For more details, see:
|
86
|
+
|
87
|
+
- [Synvert::Core::Rewriter::Instance#replace_with](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter/Instance.html#replace_with-instance_method).
|
88
|
+
- [Prism::CallNode](https://docs.ruby-lang.org/en/master/Prism/CallNode.html)
|
89
|
+
|
90
|
+
### For client users
|
91
|
+
|
92
|
+
Deprewriter can be configured either through environment variables or programmatically. There are three modes of operation:
|
93
|
+
|
94
|
+
### Environment Variable Configuration
|
95
|
+
|
96
|
+
Set the `DEPREWRITER` environment variable to one of these values:
|
97
|
+
|
98
|
+
- `log` - For logging mode. Logs deprecation warnings and shows diffs (default)
|
99
|
+
- `diff` - For diff mode. Creates patch files for each deprecation
|
100
|
+
- `dangerously_rewrite` - For rewrite mode. Automatically rewrites files (use with caution!)
|
101
|
+
- If not set, Deprewriter will be disabled
|
102
|
+
|
103
|
+
Example:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# Deprewriter is disabled
|
107
|
+
ruby your_script.rb
|
108
|
+
|
109
|
+
# Deprewriter is enabled as log mode.
|
110
|
+
DEPREWRITER=1 ruby your_script.rb
|
111
|
+
DEPREWRITER=log ruby your_script.rb
|
112
|
+
|
113
|
+
# Deprewriter is enabled as diff mode.
|
114
|
+
DEPREWRITER=diff ruby your_script.rb
|
115
|
+
|
116
|
+
# Deprewriter is enabled as rewrite mode.
|
117
|
+
DEPREWRITER=dangerously_rewrite ruby your_script.rb
|
118
|
+
```
|
119
|
+
|
120
|
+
### Programmatic Configuration
|
121
|
+
|
122
|
+
You can configure Deprewriter programmatically using the configuration block:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
Deprewriter.configure do |config|
|
126
|
+
# Set the mode (:log, :diff, :rewrite, or :disabled)
|
127
|
+
config.mode = :log
|
128
|
+
|
129
|
+
# Configure custom logger (defaults to STDOUT)
|
130
|
+
config.logger = Logger.new('deprewriter.log')
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
## Mechanism
|
135
|
+
|
136
|
+
Deprewriter determines caller code at runtime and attempts to rewrite it according to predefined transformation rules, though the actual rewriting depends on the configured mode and file permissions.
|
137
|
+
|
138
|
+
1. Library authors define deprecation rules for methods they want to deprecate, along with transformation rules specifying how the code should be rewritten.
|
139
|
+
2. When the program loads, Deprewriter renames the deprecated method to `_deprecated_foo` and dynamically defines a new method `foo`. This is similar to how [`Gem::Deprecate.deprecate`](https://github.com/ruby/ruby/blob/v3_4_1/lib/rubygems/deprecate.rb#L103-L121) works.
|
140
|
+
3. When the deprecated method `foo` is called by user code, Deprewriter analyzes the caller's code and generates a transformation according to the rules. Depending on the configured mode. In all cases, it calls the original `_deprecated_foo` to preserve the original behavior.
|
141
|
+
4. If the file was successfully rewritten, subsequent executions will use the transformed code. Otherwise, the deprecated method will continue to be called and generate warnings/diffs on each invocation.
|
142
|
+
|
143
|
+
## TODO
|
144
|
+
|
145
|
+
- [x] Complete major deprecation scenarios
|
146
|
+
- Rename method
|
147
|
+
- Remove argument(-s)
|
148
|
+
- Add argument(-s)
|
149
|
+
- [x] Decide DSL for transformation rules
|
150
|
+
- [x] Use Synvert for now
|
151
|
+
- [x] Testable code structure and write tests
|
152
|
+
- [x] Skip redundant rewrite, when the same code is rewritten multiple times
|
153
|
+
- [ ] Reduce other gem dependencies to be a standalone gem
|
154
|
+
- [ ] Handle more complex cases, e.g. metaprogramming, DSLs, etc.
|
155
|
+
|
156
|
+
## Development
|
157
|
+
|
158
|
+
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.
|
159
|
+
|
160
|
+
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).
|
161
|
+
|
162
|
+
## Contributing
|
163
|
+
|
164
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ohbarye/deprewriter. 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/[USERNAME]/deprewriter/blob/master/CODE_OF_CONDUCT.md).
|
165
|
+
|
166
|
+
## License
|
167
|
+
|
168
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
169
|
+
|
170
|
+
## Code of Conduct
|
171
|
+
|
172
|
+
Everyone interacting in the Deprewriter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/deprewriter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/examples/legacy.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative "../lib/deprewriter"
|
2
|
+
|
3
|
+
class Legacy
|
4
|
+
extend Deprewriter
|
5
|
+
|
6
|
+
def old_method
|
7
|
+
# This method is deprecated
|
8
|
+
# Use new_method instead
|
9
|
+
end
|
10
|
+
|
11
|
+
def new_method
|
12
|
+
end
|
13
|
+
|
14
|
+
# Just rename method
|
15
|
+
deprewrite :old_method,
|
16
|
+
to: "new_method"
|
17
|
+
|
18
|
+
def change_arg(arg1, arg2, message: "", to: "")
|
19
|
+
# Passing arg1 and arg2 is deprecated
|
20
|
+
# Use keyword args instead
|
21
|
+
end
|
22
|
+
|
23
|
+
# Rewrite method to use keyword args
|
24
|
+
deprewrite :change_arg, to: <<~RULE
|
25
|
+
change_arg(
|
26
|
+
message: {{arguments.arguments.0}},
|
27
|
+
to: {{arguments.arguments.1}}
|
28
|
+
)
|
29
|
+
RULE
|
30
|
+
end
|
data/examples/rename.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "node_query"
|
4
|
+
|
5
|
+
module Deprewriter
|
6
|
+
# Finds method call sites in source code
|
7
|
+
class CallSiteFinder < Prism::Visitor
|
8
|
+
# @return [Prism::CallNode, nil] The found method call node
|
9
|
+
# @param method_name [Symbol] The name of the method to find
|
10
|
+
# @param line [Integer] Line number where the method is called
|
11
|
+
# @param from [String, nil] Pattern to match for transformation
|
12
|
+
def initialize(method_name, line, from: nil)
|
13
|
+
@method_name = method_name
|
14
|
+
@line = line
|
15
|
+
@node_query = from ? NodeQuery.new(from, adapter: :prism) : nil
|
16
|
+
super()
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param source [String] The source code to search in
|
20
|
+
# @return [Prism::CallNode, nil] The found method call node
|
21
|
+
def find(source)
|
22
|
+
parsed_result = Prism.parse(source)
|
23
|
+
parsed_result.value.statements.accept(self)
|
24
|
+
@node
|
25
|
+
end
|
26
|
+
|
27
|
+
# Visits a call node in the AST
|
28
|
+
# @param node [Prism::CallNode] The call node to visit
|
29
|
+
# @return [void]
|
30
|
+
def visit_call_node(node)
|
31
|
+
if node.name == @method_name && node.start_line == @line
|
32
|
+
if @node_query
|
33
|
+
matched = @node_query.query_nodes(node, {including_self: true, stop_at_first_match: true, recursive: true})
|
34
|
+
@node = matched.first if matched.any?
|
35
|
+
else
|
36
|
+
@node = node
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "logger"
|
4
|
+
|
5
|
+
module Deprewriter
|
6
|
+
class Configuration
|
7
|
+
attr_accessor :mode, :logger, :skip_redundant_rewrite
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@mode = case ENV["DEPREWRITER"]
|
11
|
+
when nil
|
12
|
+
:disabled
|
13
|
+
when "log"
|
14
|
+
:log
|
15
|
+
when "diff"
|
16
|
+
:diff
|
17
|
+
when "dangerously_rewrite"
|
18
|
+
:rewrite
|
19
|
+
else
|
20
|
+
:log # default
|
21
|
+
end
|
22
|
+
|
23
|
+
@logger = Logger.new($stdout)
|
24
|
+
@skip_redundant_rewrite = true # Test purpose
|
25
|
+
end
|
26
|
+
|
27
|
+
def enabled?
|
28
|
+
@mode != :disabled
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "diffy"
|
4
|
+
require "digest"
|
5
|
+
|
6
|
+
module Deprewriter
|
7
|
+
class Diff
|
8
|
+
attr_reader :rewritten_source
|
9
|
+
|
10
|
+
def initialize(source, rewritten_source, filepath, timestamp: Time.now)
|
11
|
+
@source = source
|
12
|
+
@rewritten_source = rewritten_source
|
13
|
+
@filepath = filepath
|
14
|
+
@timestamp = timestamp
|
15
|
+
end
|
16
|
+
|
17
|
+
def different?
|
18
|
+
@source != @rewritten_source
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
@output ||= begin
|
23
|
+
original_diff = Diffy::Diff.new(@source, @rewritten_source, include_diff_info: true, diff: "-u").to_s
|
24
|
+
|
25
|
+
# Create a header to indicate a filepath since original_diff doesn't contain a correct filepath
|
26
|
+
formatted_timestamp = @timestamp.strftime("%Y-%m-%d %H:%M:%S.%N %z")
|
27
|
+
header = [
|
28
|
+
"--- #{@filepath}\t#{formatted_timestamp}",
|
29
|
+
"+++ #{@filepath}\t#{formatted_timestamp}"
|
30
|
+
].join("\n")
|
31
|
+
|
32
|
+
[header, original_diff.lines[2..].join].join("\n")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def id
|
37
|
+
"deprewriter_#{Digest::MD5.hexdigest(to_s)}.patch"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Deprewriter
|
4
|
+
# Handles source code rewriting for deprecated methods
|
5
|
+
module Rewriter
|
6
|
+
module_function
|
7
|
+
|
8
|
+
# @param source [String] The source code
|
9
|
+
# @param method_name [Symbol] The name of the method to rewrite
|
10
|
+
# @param line [Integer] Line number where the method is called
|
11
|
+
# @param from [String, nil] Pattern to match for transformation
|
12
|
+
# @param to [String] Pattern to transform to
|
13
|
+
# @return [String] The rewritten source code
|
14
|
+
def transform_source(source, method_name, line, to:, from: nil)
|
15
|
+
node = CallSiteFinder.new(method_name, line, from: from).find(source)
|
16
|
+
Transformer.new(source, node, to).transform
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "node_mutation"
|
4
|
+
|
5
|
+
module Deprewriter
|
6
|
+
# Handles source code transformation for deprecated methods
|
7
|
+
class Transformer
|
8
|
+
# @param source [String] The source code
|
9
|
+
# @param node [Prism::CallNode] The node to transform
|
10
|
+
# @param transform_with [String] The transformation pattern
|
11
|
+
def initialize(source, node, transform_with)
|
12
|
+
@source = source
|
13
|
+
@node = node
|
14
|
+
@transform_with = transform_with
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [String] The transformed source code
|
18
|
+
def transform
|
19
|
+
return @source if @node.nil?
|
20
|
+
|
21
|
+
before_node = @source[0...@node.location.start_offset]
|
22
|
+
new_code = transform_call_node
|
23
|
+
after_node = @source[@node.location.end_offset..]
|
24
|
+
before_node + new_code + after_node
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def transform_call_node
|
30
|
+
adapter = NodeMutation::PrismAdapter.new
|
31
|
+
receiver_code = @node.receiver ? "#{@node.receiver.slice}." : ""
|
32
|
+
|
33
|
+
call_node = Prism.parse(@node.slice.gsub(receiver_code, "")).value.statements.body.first
|
34
|
+
receiver_code + adapter.rewritten_source(call_node, @transform_with).each_line.map(&:rstrip).join("\n")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/deprewriter.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "prism"
|
4
|
+
require_relative "deprewriter/version"
|
5
|
+
|
6
|
+
module Deprewriter
|
7
|
+
autoload :Configuration, "deprewriter/configuration"
|
8
|
+
autoload :CallSiteFinder, "deprewriter/call_site_finder"
|
9
|
+
autoload :Diff, "deprewriter/diff"
|
10
|
+
autoload :Rewriter, "deprewriter/rewriter"
|
11
|
+
autoload :Transformer, "deprewriter/transformer"
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def config
|
15
|
+
@config ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def configure
|
19
|
+
yield config if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def processed_location_of_callers
|
23
|
+
@processed_location_of_callers ||= Set.new
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Marks a method as deprecated and sets up automatic code rewriting
|
28
|
+
# @param method_name [Symbol] The name of the method to deprecate
|
29
|
+
# @param from [String, nil] Pattern to match for transformation
|
30
|
+
# @param to [String] Pattern to transform to
|
31
|
+
def deprewrite(method_name, to:, from: nil)
|
32
|
+
return if !Deprewriter.config.enabled?
|
33
|
+
|
34
|
+
class_eval do
|
35
|
+
old = "_deprecated_#{method_name}"
|
36
|
+
alias_method old, method_name
|
37
|
+
|
38
|
+
define_method method_name do |*args, &block|
|
39
|
+
filepath, line = Gem.location_of_caller
|
40
|
+
location_of_caller = "#{filepath}:#{line}"
|
41
|
+
skippable = Deprewriter.config.skip_redundant_rewrite && Deprewriter.processed_location_of_callers.include?(location_of_caller)
|
42
|
+
|
43
|
+
if File.exist?(filepath) && !skippable
|
44
|
+
source = File.read(filepath)
|
45
|
+
rewritten_source = Rewriter.transform_source(source, method_name, line, to: to, from: from)
|
46
|
+
diff = Diff.new(source, rewritten_source, File.join(".", filepath))
|
47
|
+
|
48
|
+
if diff.different?
|
49
|
+
target = is_a?(Module) ? "#{self}." : "#{self.class}#"
|
50
|
+
|
51
|
+
case Deprewriter.config.mode
|
52
|
+
in :rewrite
|
53
|
+
if File.writable?(filepath)
|
54
|
+
Deprewriter.config.logger.warn "DEPREWRITER: Dangerously trying to rewrite. It will rewrite a file to apply the deprecation but won't load the file"
|
55
|
+
File.write(filepath, diff.rewritten_source)
|
56
|
+
else
|
57
|
+
Deprewriter.config.logger.error "DEPREWRITER: Failed to rewrite #{filepath} because it is not writable."
|
58
|
+
end
|
59
|
+
in :diff
|
60
|
+
File.write(File.join(Dir.pwd, diff.id), diff.to_s)
|
61
|
+
in :log
|
62
|
+
Deprewriter.config.logger.warn "DEPREWRITER: #{target}#{method_name} usage at #{filepath}:#{line} is deprecated." \
|
63
|
+
" You can apply the diff below to resolve the deprecation.\n#{diff}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Deprewriter.processed_location_of_callers << location_of_caller if Deprewriter.config.skip_redundant_rewrite
|
68
|
+
end
|
69
|
+
|
70
|
+
send old, *args, &block
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/sig/deprewriter.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deprewriter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ohbarye
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: diffy
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: logger
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: node_query
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: node_mutation
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
type: :runtime
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: prism
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
type: :runtime
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: prism_ext
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
type: :runtime
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
email:
|
97
|
+
- over.rye@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- ".rspec"
|
103
|
+
- ".standard.yml"
|
104
|
+
- CHANGELOG.md
|
105
|
+
- CODE_OF_CONDUCT.md
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- examples/change_arg.rb
|
110
|
+
- examples/legacy.rb
|
111
|
+
- examples/rename.rb
|
112
|
+
- lib/deprewriter.rb
|
113
|
+
- lib/deprewriter/call_site_finder.rb
|
114
|
+
- lib/deprewriter/configuration.rb
|
115
|
+
- lib/deprewriter/diff.rb
|
116
|
+
- lib/deprewriter/rewriter.rb
|
117
|
+
- lib/deprewriter/transformer.rb
|
118
|
+
- lib/deprewriter/version.rb
|
119
|
+
- sig/deprewriter.rbs
|
120
|
+
homepage: https://github.com/ohbarye/deprewriter
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata:
|
124
|
+
homepage_uri: https://github.com/ohbarye/deprewriter
|
125
|
+
source_code_uri: https://github.com/ohbarye/deprewriter
|
126
|
+
changelog_uri: https://github.com/ohbarye/deprewriter/blob/main/CHANGELOG.md
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '3.2'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubygems_version: 3.6.7
|
142
|
+
specification_version: 4
|
143
|
+
summary: An experimental gem that helps you rewrite deprecated code automatically.
|
144
|
+
test_files: []
|