sorbet_auto_typer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0da8912b32505d98bed3b009083eb99f6d6130dc3615cf02e511943fe4eb2923
4
+ data.tar.gz: c88e9338b3d3d2010acdfe09ccf4005a840f5ad3b6778d696f39a4c0ef1d9724
5
+ SHA512:
6
+ metadata.gz: 6c6f759476c9f0f7c21ebf7eaaba6c6f359a0a652692f10c1d1c296911c920287691e3a18016bb90abc0f77f68f4223d6f3b2619a8594158e52c5166a791899d
7
+ data.tar.gz: f07876569f6b62e4272af5fc3bf1cbc7d110698087ee10817e51783e54c07a21cccb798d3ff87ded1207738d1db4213992f7feb297ad6fb85e4c3d3f0980a17a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at bo@stendal-sorensen.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sorbet_auto_typer.gemspec
4
+ gemspec
5
+
6
+ gem 'pry-byebug'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Bo Stendal Sorensen
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.
@@ -0,0 +1,106 @@
1
+ # SorbetAutoTyper
2
+
3
+ SorbetAutoTyper is a tool that is able to add sorbet signatures to files by instrumenting code, for example by running the test suite of a project.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile (you probably want to add it in the development/test section):
8
+
9
+ ```ruby
10
+ gem 'sorbet_auto_typer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ SorbetAutoTyper works in two steps:
20
+
21
+ 1. Instrumentation
22
+ 2. Analysis
23
+
24
+ ### Instrumentation
25
+
26
+ The first step is to instrument the code. To be able to instrument even a large codebase, SorbetAutoTyper will stop instrumenting a method after a specific amount of calls. Underneath the hood the ruby `TracePoint` API is used to ensure maximum speed.
27
+
28
+ To instrument a codeblock, first configure the gem:
29
+
30
+ ```ruby
31
+ SorbetAutoTyper.configure do |c|
32
+ c.output_file = File.join(Dir.pwd, 'tmp', "#{SecureRandom.uuid}.sig")
33
+ c.filter_path = File.join(Dir.pwd, 'lib')
34
+ end
35
+ ```
36
+
37
+ This will set up the gem to output a signature file with a random name to the `tmp` directory and only inspect files in the `lib` directory.
38
+ This configuration only needs to be done once pr. process.
39
+
40
+ Next we can start and stop SorbetAutoTyper using `SorbetAutoTyper.start!` and `SorbetAutoTyper.stop!`.
41
+
42
+ Instrumenting a simple file could look like this:
43
+
44
+ ```ruby
45
+ SorbetAutoTyper.start!
46
+ a = MyModule::MyClass.new
47
+ a.hello
48
+ SorbetAutoTyper.stop!
49
+ ```
50
+
51
+ In the context of a test runner like RSpec, the following could be added to the spec helper of the project:
52
+
53
+ ```ruby
54
+ SorbetAutoTyper.configure do |c|
55
+ c.output_file = File.join(Dir.pwd, 'tmp', "#{SecureRandom.uuid}.sig")
56
+ c.filter_path = File.join(Dir.pwd, 'lib')
57
+ end
58
+
59
+ RSpec.configure do |config|
60
+ config.before(:suite) do
61
+ SorbetAutoTyper.start!
62
+ end
63
+
64
+ config.after(:suite) do
65
+ SorbetAutoTyper.stop!
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### Analysis
71
+
72
+ Running the analysis is as simple as running the `sorbet-auto-typer` tool within your project. If you run it without any arguments, it will print a bit of help:
73
+
74
+ ```bash
75
+ $ bundle exec sorbet-auto-typer
76
+ Error: Please provide a SIGNATURE_FILE.
77
+
78
+ sorbet-auto-typer [OPTIONS] ... SIGNATURE_FILE
79
+
80
+ -h, --help:
81
+ show help
82
+
83
+ --dry, -d:
84
+ do not overwrite files, only display desired changes
85
+
86
+ --verbose, -v:
87
+ more verbose output
88
+
89
+ SIGNATURE_FILE: Signature file to use for auto-generated types
90
+ ```
91
+
92
+ The `--dry` and `--verbose` flags will make sure none of the source files are overwritten and provide a diff of the files. Those are recommended options on the first run.
93
+
94
+ When you are ready to run `SorbetAutoTyper` on your project, simply run it without any options and it will add method signatures to the source files in the project.
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bss/sorbet_auto_typer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
103
+
104
+ ## Code of Conduct
105
+
106
+ Everyone interacting in the SorbetAutoTyper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bss/sorbet_auto_typer/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sorbet_auto_typer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env ruby
2
+ require 'getoptlong'
3
+ require 'bundler/setup'
4
+ require 'parser'
5
+ require 'parser/current'
6
+
7
+ require 'diff-lcs'
8
+
9
+ require 'sorbet_auto_typer'
10
+
11
+ # Load up everything where this file is being run.
12
+ Bundler.require(:default)
13
+
14
+ LOOK_BEFORE = 1
15
+ LOOK_AFTER = 1
16
+
17
+ def print_help
18
+ puts <<-EOF
19
+ #{File.basename($0)} [OPTIONS] ... SIGNATURE_FILE
20
+
21
+ -h, --help:
22
+ show help
23
+
24
+ --dry, -d:
25
+ do not overwrite files, only display desired changes
26
+
27
+ --verbose, -v:
28
+ more verbose output
29
+
30
+ SIGNATURE_FILE: Signature file to use for auto-generated types
31
+ EOF
32
+ end
33
+
34
+ opts = GetoptLong.new(
35
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
36
+ [ '--dry', '-d', GetoptLong::NO_ARGUMENT ],
37
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
38
+ )
39
+
40
+ dry_run = false
41
+ verbose = false
42
+ opts.each do |opt, arg|
43
+ case opt
44
+ when '--help'
45
+ print_help
46
+ Kernel.exit(0)
47
+ when '--dry'
48
+ dry_run = true
49
+ when '--verbose'
50
+ verbose = true
51
+ end
52
+ end
53
+
54
+ if ARGV.size < 1
55
+ puts "Error: Please provide a SIGNATURE_FILE."
56
+ puts
57
+ print_help
58
+ Kernel.exit(1)
59
+ end
60
+
61
+ trace_file = ARGV.first
62
+ uniq_trace_lines = Set.new()
63
+
64
+ File.foreach(trace_file) do |line|
65
+ uniq_trace_lines << line
66
+ end
67
+
68
+ traces = uniq_trace_lines.map do |l|
69
+ SorbetAutoTyper::MethodTrace.from_trace_line(l.strip)
70
+ end.compact
71
+
72
+ annotator = SorbetAutoTyper::Annotator.new(traces)
73
+
74
+ all_files = traces.map(&:method_file).uniq
75
+ all_files.each do |source_file|
76
+ puts "Annotating: #{source_file}"
77
+ new_source = annotator.annotate_file(source_file)
78
+
79
+ if verbose
80
+ puts "----"
81
+
82
+ original_source_lines = File.read(source_file).split("\n")
83
+ new_source_lines = new_source.split("\n")
84
+ diffs = Diff::LCS.sdiff(original_source_lines, new_source_lines)
85
+ changed_diffs = diffs.each_with_index.reject { |(d, idx)| d.unchanged? }
86
+ indexes_to_display = changed_diffs.flat_map { |(_, line)| (line-LOOK_BEFORE..line+LOOK_AFTER).to_a }.uniq
87
+ indexes_to_display.each do |idx|
88
+ dc = diffs[idx]
89
+ unless dc.nil?
90
+ if dc.unchanged?
91
+ puts " #{dc.old_element}"
92
+ else
93
+ if dc.deleting? || dc.changed?
94
+ puts "+ #{dc.old_element}"
95
+ end
96
+
97
+ if dc.adding? || dc.changed?
98
+ puts "+ #{dc.new_element}"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ File.write(source_file, new_source) unless dry_run
106
+ end
@@ -0,0 +1,57 @@
1
+ # typed: ignore
2
+ require 'sorbet-runtime'
3
+ require "sorbet_auto_typer/version"
4
+ require "sorbet_auto_typer/configuration"
5
+ require "sorbet_auto_typer/tracer"
6
+ require "sorbet_auto_typer/method_trace"
7
+ require "sorbet_auto_typer/source_rewriter"
8
+ require "sorbet_auto_typer/annotator"
9
+
10
+ module SorbetAutoTyper
11
+ class Error < StandardError; end
12
+ class MissingConfigurationError < Error; end
13
+ class InvalidConfigurationError < Error; end
14
+ class TracerAlreadyRunning < Error; end
15
+
16
+ class << self
17
+ extend T::Sig
18
+
19
+ sig { void }
20
+ def start!
21
+ raise MissingConfigurationError.new if !@config
22
+ raise InvalidConfigurationError.new unless @config.valid?
23
+ raise TracerAlreadyRunning.new unless @current_tracer.nil?
24
+
25
+ output_file = File.open(T.must(@config.output_file), 'w')
26
+
27
+ @current_tracer = Tracer.new(output_file, T.must(@config.filter_path))
28
+ @current_tracer.start!
29
+ end
30
+
31
+ sig { void }
32
+ def stop!
33
+ @current_tracer = T.let(@current_tracer, T.nilable(Tracer))
34
+ unless @current_tracer.nil?
35
+ @current_tracer.stop!
36
+ @current_tracer = nil
37
+ end
38
+ end
39
+
40
+ sig { params(blk: T.proc.params(arg0: Configuration).void).void }
41
+ def configure(&blk)
42
+ config = Configuration.new(output_file: nil)
43
+ yield config
44
+ @config = T.let(config, T.nilable(Configuration))
45
+ end
46
+
47
+ sig { void }
48
+ def reset!
49
+ stop!
50
+ @config = nil
51
+ end
52
+
53
+ private
54
+ sig { returns(T.nilable(Configuration)) }
55
+ attr_reader :config
56
+ end
57
+ end