gelauto 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8bc0cbf96338d28a4c1551f2fc2cf7e7cbea122ccbf1ecbcbe3904713d77293
4
- data.tar.gz: 42e169f43e1d41de1ebd1d426dcd23358271aa3a658dc3d8d37288767223df13
3
+ metadata.gz: a9a90485262d6867a89f54f088f6cd71cd7490b41d22622edd4376387d97555a
4
+ data.tar.gz: 808a3f83fd11126ccf4d41ff65e770e28ba9e4d4912bfd9ddca50655471369b9
5
5
  SHA512:
6
- metadata.gz: c74d87844d920d0abc555b55f395d61ca0235fd34a4bbcc6eb449a76ee5b304f36a37cf40d8b74817b380162def7aa1224d813fbc0fc7e8f209644428423fbc4
7
- data.tar.gz: 850a310b37004110ddbe9b78906c1524fd262cd8ae1c67c861172818231c930bcfd83ac57e9b394ef800ee255330e6b7f2a900bd5018026f78bc588ad68bb236
6
+ metadata.gz: 861d47eebcf4b9709d3cdafa5119a39a30bcecdb0a5fe201accf46464861676a23392322e962afbdd356332537a5fca1bf50e8578baafa7e728564b5d2f9ce6c
7
+ data.tar.gz: 19011ece03802f5bd64991382d5cf0b7eae12cc8e431c13ff72e4d807d16a29e949e2a1df90b8505ac1c9f145f731d45af5265686fe3cf27f66699431ce803f8
data/README.md CHANGED
@@ -51,7 +51,15 @@ Like most RSpec test suites, let's assume ours is stored in the `spec/` director
51
51
  gelauto run --annotate $(find . -name '*.rb') -- bundle exec rspec spec/
52
52
  ```
53
53
 
54
- You can also choose to run Gelauto with the `--rbi` flag, which will cause Gelauto to print results to standard output in [RBI format](https://sorbet.org/docs/rbi).
54
+ You can also choose to run Gelauto with the `--rbi` flag, which will cause Gelauto to print results to standard output or to a file in [RBI format](https://sorbet.org/docs/rbi):
55
+
56
+ ```bash
57
+ # print RBI output to STDOUT
58
+ gelauto run --annotate --rbi - $(find . -name '*.rb') -- bundle exec rspec spec/
59
+
60
+ # write RBI output to a file
61
+ gelauto run --annotate --rbi ./rbi/mylib.rbi $(find . -name '*.rb') -- bundle exec rspec spec/
62
+ ```
55
63
 
56
64
  In this second example, we're going to be running a minitest test suite. Like most minitest suites, let's assume ours is stored in the `test/` directory (that's the Rails default too). To run the test suite in `test/`, we might run the following command:
57
65
 
@@ -121,12 +129,14 @@ to your spec_helper.rb, Rakefile, or wherever RSpec is configured. You'll also n
121
129
  GELAUTO_FILES=$(find ./lib -name *.rb) bundle exec rspec
122
130
  ```
123
131
 
124
- Files can be separated by spaces, newlines, or commas. Finally, if you want Gelauto to annotate them, set `GELAUTO_ANNOTATE` to `true`, eg:
132
+ Files can be separated by spaces, newlines, or commas. If you want Gelauto to annotate them, set `GELAUTO_ANNOTATE` to `true`, eg:
125
133
 
126
134
  ```bash
127
135
  GELAUTO_FILES=$(find ./lib -name *.rb) GELAUTO_ANNOTATE=true bundle exec rspec
128
136
  ```
129
137
 
138
+ Finally, set `GELAUTO_RBI=/path/to/output.rbi` to have Gelauto emit an RBI file when the test suite finishes.
139
+
130
140
  ## How does it Work?
131
141
 
132
142
  Gelauto makes use of Ruby's [TracePoint API](https://ruby-doc.org/core-2.6/TracePoint.html). TracePoint effectively allows Gelauto to receive a notification whenever a Ruby method is called and whenever a method returns. That info combined with method location information gathered from parsing your Ruby files ahead of time allows Gelauto to know a) where methods are located, 2) what arguments they take, 3) the types of those arguments, and 4) the type of the return value.
@@ -1,5 +1,7 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
+ $:.push(File.expand_path(File.join(%w(.. lib)), __dir__))
4
+
3
5
  require 'gelauto'
4
6
  require 'gelauto/version'
5
7
  require 'gli'
@@ -31,10 +33,25 @@ module Gelauto
31
33
  c.switch [:a, :annotate]
32
34
 
33
35
  c.desc 'Print type signatures to STDOUT in RBI format. Ignores --silent.'
34
- c.default_value false
35
- c.switch [:r, :rbi]
36
+ c.flag [:r, :rbi], type: String
37
+
38
+ c.desc 'Change into the given directory before running the command.'
39
+ c.default_value '.'
40
+ c.flag [:d, :dir], type: String
36
41
 
37
42
  c.action do |global_options, options, args|
43
+ if options[:rbi] && options[:rbi] != '-'
44
+ dir = File.dirname(options[:rbi])
45
+
46
+ unless File.exist?(dir)
47
+ puts "--rbi: #{dir} does not exist :("
48
+ end
49
+ end
50
+
51
+ unless File.exist?(options[:dir])
52
+ puts "--dir: #{options[:dir]} does not exist :("
53
+ end
54
+
38
55
  paths, _, cmd = args.chunk_while { |arg1, arg2| arg1 != '--' && arg2 != '--' }.to_a
39
56
  Gelauto.paths += paths
40
57
 
@@ -47,6 +64,7 @@ module Gelauto
47
64
  begin
48
65
  Gelauto.setup
49
66
  ARGV.replace(cmd)
67
+ Dir.chdir(options[:dir])
50
68
  load exe
51
69
  ensure
52
70
  Gelauto.teardown
@@ -61,7 +79,13 @@ module Gelauto
61
79
  end
62
80
 
63
81
  if options[:rbi]
64
- puts Gelauto::Rbi.new(Gelauto.method_index).to_s
82
+ rbi_str = Gelauto::Rbi.new(Gelauto.method_index).to_s
83
+
84
+ if options[:rbi] == '-'
85
+ puts rbi_str
86
+ else
87
+ File.write(options[:rbi], rbi_str)
88
+ end
65
89
  end
66
90
  end
67
91
  end
@@ -12,7 +12,7 @@ module Gelauto
12
12
 
13
13
  def to_sig
14
14
  if self[:elem].empty?
15
- 'T::Array'
15
+ 'T::Array[T.untyped]'
16
16
  else
17
17
  "T::Array[#{self[:elem].to_sig}]"
18
18
  end
@@ -15,7 +15,7 @@ module Gelauto
15
15
 
16
16
  def to_sig
17
17
  if self[:key].empty? && self[:value].empty?
18
- 'T::Hash'
18
+ 'T::Hash[T.untyped, T.untyped]'
19
19
  else
20
20
  "T::Hash[#{self[:key].to_sig}, #{self[:value].to_sig}]"
21
21
  end
@@ -15,5 +15,15 @@ RSpec.configure do |config|
15
15
  Gelauto::Logger.info("Annotated #{path}")
16
16
  end
17
17
  end
18
+
19
+ if ENV['GELAUTO_RBI']
20
+ rbi_str = Gelauto::Rbi.new(Gelauto.method_index).to_s
21
+
22
+ if ENV['GELAUTO_RBI'] == '-'
23
+ puts rbi_str
24
+ else
25
+ File.write(ENV['GELAUTO_RBI'], rbi_str)
26
+ end
27
+ end
18
28
  end
19
29
  end
@@ -53,8 +53,10 @@ module Gelauto
53
53
  'T.untyped'
54
54
  elsif sigs.size == 1
55
55
  sigs.first
56
+ elsif nilable
57
+ "T.nilable(T.any(#{sigs.join(', ')}))"
56
58
  else
57
- "T.#{nilable ? 'nilable' : 'any'}(#{sigs.join(', ')})"
59
+ "T.any(#{sigs.join(', ')})"
58
60
  end
59
61
  end
60
62
  end
@@ -1,3 +1,3 @@
1
1
  module Gelauto
2
- VERSION = '1.3.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Gelauto do
@@ -23,6 +25,28 @@ describe Gelauto do
23
25
  end
24
26
  end
25
27
 
28
+ context 'with Sorbet generic types' do
29
+ it 'uses T.nilable with T.any correctly' do
30
+ Gelauto.discover do
31
+ GelautoSpecs::Utility.safe_to_string(nil)
32
+ GelautoSpecs::Utility.safe_to_string(1.0)
33
+ GelautoSpecs::Utility.safe_to_string(2)
34
+ end
35
+
36
+ safe_to_string = get_indexed_method(GelautoSpecs::Utility, :safe_to_string)
37
+ expect(safe_to_string.to_sig).to eq('sig { params(input: T.nilable(T.any(Float, Integer))).returns(String) }')
38
+ end
39
+
40
+ it 'uses T.Hash and T::Array with T.untyped correctly' do
41
+ Gelauto.discover do
42
+ GelautoSpecs::Utility.safe_get_keys({})
43
+ end
44
+
45
+ safe_get_keys = get_indexed_method(GelautoSpecs::Utility, :safe_get_keys)
46
+ expect(safe_get_keys.to_sig).to eq('sig { params(input: T::Hash[T.untyped, T.untyped]).returns(T::Array[T.untyped]) }')
47
+ end
48
+ end
49
+
26
50
  context 'with generic types' do
27
51
  before do
28
52
  Gelauto.discover do
@@ -24,6 +24,24 @@ module GelautoSpecs
24
24
  Response.new(200, 'it worked!')
25
25
  end
26
26
  end
27
+
28
+ class Utility
29
+ def self.safe_to_string(input)
30
+ if input.nil?
31
+ ''
32
+ else
33
+ input.to_s
34
+ end
35
+ end
36
+
37
+ def self.safe_get_keys(input)
38
+ if input.nil?
39
+ []
40
+ else
41
+ input.keys
42
+ end
43
+ end
44
+ end
27
45
  end
28
46
 
29
47
  Gelauto.paths << __FILE__
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gelauto
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-29 00:00:00.000000000 Z
11
+ date: 2020-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -95,7 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.0.4
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6.2
99
100
  signing_key:
100
101
  specification_version: 4
101
102
  summary: Automatically annotate your code with Sorbet type definitions.