sorpryse 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 305ceab675cf12812fa21b1cf54bdf0cb2d2b6604f4abf34dc6180915af467ca
4
+ data.tar.gz: 2a63ecae6bb80027d10dd764e7d90d22d09a336efbfb0c5516ce2595a392c35d
5
+ SHA512:
6
+ metadata.gz: 4df36ed95026a4c79ce580a2711b3bbc0de201c0e1ebe2abc962b424e072c5f09d813bff3b89e5a2f250ca9cfe798a3f53ac01ba944ee9c038622ece3b926c56
7
+ data.tar.gz: a1901dc50f4d7016444a4b23f0022377eb45d247e2c9a5a3021e4dd52f66eff6d73246aca46a42b5a2e9a723ff51896fe31e4048c9785f45df0653d9ccc1709e
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sorpryse.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Aaron Christiansen
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,61 @@
1
+ # Sorpryse
2
+
3
+ This extension makes Pry work seamlessly with Sorbet projects.
4
+
5
+ **Before:** Incorrect method source
6
+
7
+ ```
8
+ From: /home/aaron/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.4.4929/lib/types/private/methods/_methods.rb @ line 208:
9
+ Owner: #<Class:Foo>
10
+ Visibility: public
11
+ Number of lines: 35
12
+
13
+ T::Private::ClassUtils.replace_method(mod, method_name) do |*args, &blk|
14
+ if !T::Private::Methods.has_sig_block_for_method(new_method)
15
+ # This should only happen if the user used alias_method to grab a handle
16
+ # to the original pre-unwound `sig` method. I guess we'll just proxy the
17
+ # call forever since we don't know who is holding onto this handle to
18
+ ```
19
+
20
+ **After:** Method source and a signature!
21
+
22
+ ```
23
+ From: playground/test.rb @ line 9:
24
+ Owner: #<Class:Foo>
25
+ Visibility: public
26
+ Sorbet: sig { returns(Integer) }
27
+ Number of lines: 3
28
+
29
+ def self.bar
30
+ 3
31
+ end
32
+ ```
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'sorpryse'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install sorpryse
49
+
50
+ ## Usage
51
+
52
+ Make sure you've required `sorpryse`. The `$` command in Pry will be
53
+ automatically overwritten to add the Sorbet-specific functionality.
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/AaronC81/sorpryse.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/lib/extension.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'sorbet-runtime'
2
+ require 'pry'
3
+
4
+ class Pry
5
+ class Command
6
+ class ShowInfo
7
+ old_method_sections = instance_method(:method_sections)
8
+
9
+ define_method(:method_sections) do |code_object|
10
+ sorbet_object = T::Private::Methods.signature_for_method(code_object)
11
+
12
+ if sorbet_object
13
+ call_chain = []
14
+
15
+ # Modifiers
16
+ call_chain << "generated" if sorbet_object.generated
17
+
18
+ if sorbet_object.mode != "standard"
19
+ # This is a string like "overridable_override"
20
+ call_chain += sorbet_object.mode.split("_")
21
+ end
22
+
23
+ # Parameters
24
+ all_parameters = []
25
+
26
+ # Positional
27
+ all_parameters += sorbet_object.arg_types.map do |(name, type)|
28
+ "#{name}: #{type}"
29
+ end
30
+
31
+ # Splat
32
+ if sorbet_object.rest_type
33
+ all_parameters << "#{sorbet_object.rest_name}: #{sorbet_object.rest_type}"
34
+ end
35
+
36
+ # Keyword
37
+ all_parameters += sorbet_object.kwarg_types.map do |(name, type)|
38
+ "#{name}: #{type}"
39
+ end
40
+
41
+ # Double-splat
42
+ if sorbet_object.rest_type
43
+ all_parameters << "#{sorbet_object.keyrest_name}: #{sorbet_object.keyrest_type}"
44
+ end
45
+
46
+ # Block
47
+ if sorbet_object.block_type
48
+ all_parameters << "#{sorbet_object.block_name}: #{sorbet_object.block_type}"
49
+ end
50
+
51
+ call_chain << "params(#{all_parameters.join(", ")})" if all_parameters.any?
52
+
53
+ # Returns
54
+ if sorbet_object.return_type.is_a?(T::Private::Types::Void)
55
+ call_chain << "void"
56
+ else
57
+ call_chain << "returns(#{sorbet_object.return_type})"
58
+ end
59
+
60
+ sorbet_string = "sig { #{call_chain.join(".")} }"
61
+ else
62
+ sorbet_string = "Unknown"
63
+ end
64
+
65
+ old_method_sections.bind(self).(code_object).merge({
66
+ sorbet: "\n\e[1mSorbet:\e[0m #{sorbet_string}"
67
+ })
68
+ end
69
+
70
+ old_method_header = instance_method(:method_header)
71
+
72
+ define_method(:method_header) do |code_object, line_num|
73
+ old_method_header.bind(self).(code_object, line_num) \
74
+ + method_sections(code_object)[:sorbet]
75
+ end
76
+ end
77
+ end
78
+
79
+ class Method
80
+ # Maybe use a dict or something, I think methods are suitable keys
81
+ define_method(:source_location) do
82
+ loc = super()
83
+
84
+ file_path, line = loc
85
+ return loc unless file_path && File.exists?(file_path)
86
+
87
+ first_source_line = IO.readlines(file_path)[line - 1]
88
+
89
+ # This is how Sorbet replaces methods.
90
+ # If Sorbet undergoes drastic refactorings, this may need to be updated!
91
+ initial_sorbet_line = "T::Private::ClassUtils.replace_method(mod, method_name) do |*args, &blk|"
92
+ replaced_sorbet_line = "mod.send(:define_method, method_sig.method_name) do |*args, &blk|"
93
+
94
+ if [initial_sorbet_line, replaced_sorbet_line].include?(first_source_line.strip)
95
+ T::Private::Methods.signature_for_method(@method).method.source_location
96
+ else
97
+ loc
98
+ end
99
+ end
100
+ end
101
+ end
data/lib/sorpryse.rb ADDED
@@ -0,0 +1,5 @@
1
+ require_relative 'extension.rb'
2
+
3
+ module Sorpryse
4
+ VERSION = '0.1.0'
5
+ end
data/sorpryse.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "sorpryse"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sorpryse"
7
+ spec.version = Sorpryse::VERSION
8
+ spec.authors = ["Aaron Christiansen"]
9
+ spec.email = ["aaronc20000@gmail.com"]
10
+
11
+ spec.summary = %q{Pry plugin for Sorbet}
12
+ spec.homepage = "https://github.com/AaronC81/sorpryse"
13
+ spec.license = "MIT"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sorpryse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Christiansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - aaronc20000@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/extension.rb
54
+ - lib/sorpryse.rb
55
+ - sorpryse.gemspec
56
+ homepage: https://github.com/AaronC81/sorpryse
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/AaronC81/sorpryse
61
+ source_code_uri: https://github.com/AaronC81/sorpryse
62
+ changelog_uri: https://github.com/AaronC81/sorpryse/CHANGELOG.md
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Pry plugin for Sorbet
82
+ test_files: []