rbs_rails 0.2.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +3 -2
  6. data/README.md +33 -35
  7. data/Rakefile +14 -1
  8. data/Steepfile +12 -1
  9. data/assets/sig/action_mailer.rbs +6 -3
  10. data/assets/sig/capybara.rbs +14 -0
  11. data/assets/sig/concurrent.rbs +4 -0
  12. data/assets/sig/erb.rbs +4 -0
  13. data/assets/sig/erubi.rbs +4 -0
  14. data/assets/sig/i18n.rbs +4 -0
  15. data/assets/sig/minitest.rbs +12 -1
  16. data/assets/sig/pg.rbs +5 -0
  17. data/assets/sig/que.rbs +4 -0
  18. data/assets/sig/queue_classic.rbs +4 -0
  19. data/assets/sig/racc.rbs +4 -0
  20. data/assets/sig/rack-test.rbs +6 -0
  21. data/assets/sig/rack.rbs +47 -0
  22. data/assets/sig/rails.rbs +7 -8
  23. data/assets/sig/rdoc.rbs +9 -0
  24. data/assets/sig/sidekiq.rbs +4 -0
  25. data/assets/sig/sneakers.rbs +4 -0
  26. data/assets/sig/stdlib.rbs +15 -5
  27. data/assets/sig/sucker_punch.rbs +4 -0
  28. data/assets/sig/thor.rbs +12 -0
  29. data/assets/sig/tzinfo.rbs +4 -0
  30. data/bin/add-type-params.rb +39 -13
  31. data/bin/postprocess.rb +137 -0
  32. data/bin/rbs +30 -0
  33. data/bin/rbs-prototype-rb.rb +195 -0
  34. data/bin/to-ascii.rb +5 -0
  35. data/lib/rbs_rails/active_record.rb +78 -33
  36. data/lib/rbs_rails/rake_task.rb +75 -0
  37. data/lib/rbs_rails/version.rb +1 -1
  38. data/rbs_rails.gemspec +1 -0
  39. data/sig/fileutils.rbs +1 -0
  40. data/sig/rake.rbs +6 -0
  41. data/sig/rbs_rails/active_record.rbs +4 -4
  42. data/sig/rbs_rails/rake_task.rbs +20 -0
  43. metadata +45 -12
  44. data/assets/sig/action_controller.rbs +0 -44
  45. data/assets/sig/action_view.rbs +0 -3
  46. data/assets/sig/active_record.rbs +0 -130
  47. data/assets/sig/generated/activemodel.rbs +0 -3877
  48. data/assets/sig/generated/activesupport.rbs +0 -11480
  49. data/bin/merge-duplicate-decls.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cc75e64602e5966e87f1bfe2b9530034f81999715b5d9e3697f4f424af9a6da
4
- data.tar.gz: 5d742b05e28d190c940d44585883be713133095d17d5778c6e64fbf2ef3b3719
3
+ metadata.gz: 5dd350161bbcf1358c43fbda2fb5b42aa106b42a14f079a26e60338e9ccbae92
4
+ data.tar.gz: b5a700311050c8500037f70be69d652c96bbf20ebc0334ad49d7c97964b977ec
5
5
  SHA512:
6
- metadata.gz: 5b07cb43cba9d2bf51beddbfdb45ad4c2e29bc6c80d6301259af7b289c8ab832b2908bbb4d96594cf7ca7d9318c54481c11daf07d6d20dc3826518b40f5b5144
7
- data.tar.gz: 2af365f0d0c891d677bd60f4322566aec9897edd93585dfc6871f5ad5923985f9fe8503520b884f98382c5954ca5ab2ce1ec35ed674727449305f35ca1260c02
6
+ metadata.gz: 414a6a9fa8feb585dbae5826c68e9043eedeb3b4e1c43d112efdcdcb61c10686d18dc76d57191c34f456ec43c36246cb4c096ceb59dba7cdf0bba4f10a5e4159
7
+ data.tar.gz: da5d5c7f919d5b2dedfb596bb8e207210ee9a3391db6af13372cc976b57f3ba89ca4b8a19578847f538b4b6adfd547c634f261ac8e21030f2ec98dee3e15147e
data/.gitignore CHANGED
@@ -7,3 +7,6 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /Gemfile.lock
10
+ /test/app/sig/rbs_rails
11
+ /test/app/sig/app
12
+ /test/app/sig/path_helpers.rbs
@@ -0,0 +1,3 @@
1
+ [submodule "gem_rbs"]
2
+ path = gem_rbs
3
+ url = https://github.com/ruby/gem_rbs.git
@@ -5,4 +5,7 @@ rvm:
5
5
  - 2.7.0
6
6
  - ruby-head
7
7
 
8
+ before_install:
9
+ - gem install bundler -v 2.1.4
10
+
8
11
  cache: bundler
data/Gemfile CHANGED
@@ -4,5 +4,6 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
- gem 'rbs', '>= 0.4.0'
8
- gem 'steep'
7
+ gem 'rbs', '1.0.0'
8
+ gem 'steep', '>= 0.34.0', git: 'https://github.com/soutaro/steep.git'
9
+ gem 'minitest'
data/README.md CHANGED
@@ -7,7 +7,7 @@ RBS files generator for Ruby on Rails.
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'rbs_rails'
10
+ gem 'rbs_rails', require: false
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -20,57 +20,55 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- ### For Active Record models
23
+ Put the following code to `lib/tasks/rbs.rake`.
24
24
 
25
- It has two tasks.
25
+ ```ruby
26
+ require 'rbs_rails/rake_task'
26
27
 
27
- * `copy_signature_files`: Copy type definition files for Rails from rbs_rails.
28
- * `generate_rbs_for_model`: Generate RBS files from model classes.
28
+ RbsRails::RakeTask.new
29
+ ```
29
30
 
30
- ```ruby
31
- # Rakefile
31
+ Then, the following four tasks are available.
32
+
33
+ * `rbs_rails:copy_signature_files`: Copy RBS files for rbs_rails
34
+ * `rbs_rails:generate_rbs_for_models`: Generate RBS files for Active Record models
35
+ * `rbs_rails:generate_rbs_for_path_helpers`: Generate RBS files for path helpers
36
+ * `rbs_rails:all`: Execute all tasks of RBS Rails
32
37
 
33
- task copy_signature_files: :environment do
34
- require 'rbs_rails'
35
38
 
36
- to = Rails.root.join('sig/rbs_rails/')
37
- to.mkpath unless to.exist?
38
- RbsRails.copy_signatures(to: to)
39
- end
40
39
 
41
- task generate_rbs_for_model: :environment do
42
- require 'rbs_rails'
43
40
 
44
- out_dir = Rails.root / 'sig'
45
- out_dir.mkdir unless out_dir.exist?
41
+ ### Steep integration
46
42
 
47
- Rails.application.eager_load!
43
+ You need to specify the following libraries by `Steepfile`.
48
44
 
49
- ActiveRecord::Base.descendants.each do |klass|
50
- next if klass.abstract_class?
45
+ ```ruby
46
+ # Steepfile
51
47
 
52
- path = out_dir / "app/models/#{klass.name.underscore}.rbs"
53
- FileUtils.mkdir_p(path.dirname)
48
+ target :app do
49
+ signature 'sig'
54
50
 
55
- sig = RbsRails::ActiveRecord.class_to_rbs(klass, mode: :extension)
56
- path.write sig
57
- end
58
- end
59
- ```
51
+ check 'app'
60
52
 
61
- ### For path helpers
53
+ repo_path "path/to/rbs_repo"
62
54
 
63
- ```ruby
64
- # Rakefile
55
+ library 'pathname'
56
+ library 'logger'
57
+ library 'mutex_m'
58
+ library 'date'
65
59
 
66
- task generate_rbs_for_path_helpers: :environment do
67
- require 'rbs_rails'
68
- out_path = Rails.root.join 'sig/path_helpers.rbs'
69
- rbs = RbsRails::PathHelpers.generate
70
- out_path.write rbs
60
+ library 'activesupport'
61
+ library 'actionpack'
62
+ library 'activejob'
63
+ library 'activemodel'
64
+ library 'actionview'
65
+ library 'activerecord'
66
+ library 'railties'
71
67
  end
72
68
  ```
73
69
 
70
+ You need to put RBS repo to `path/to/rbs_repo`. See https://github.com/ruby/gem_rbs
71
+
74
72
  ## Development
75
73
 
76
74
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,7 +1,20 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :steep
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:rbs_validate, :steep, :test]
3
5
 
4
6
  desc 'run Steep'
5
7
  task :steep do
6
8
  sh 'steep', 'check'
7
9
  end
10
+
11
+ task :rbs_validate do
12
+ rbs = Pathname(__dir__).join('bin/rbs').to_s
13
+ sh "ruby #{rbs} validate --silent"
14
+ end
15
+
16
+ Rake::TestTask.new do |test|
17
+ test.libs << 'test'
18
+ test.test_files = Dir['test/**/*_test.rb']
19
+ test.verbose = true
20
+ end
data/Steepfile CHANGED
@@ -3,7 +3,18 @@ target :lib do
3
3
  signature 'assets/sig'
4
4
 
5
5
  check "lib" # Directory name
6
+ repo_path ENV['RBS_REPO_DIR'] || './gem_rbs/gems'
6
7
 
7
8
  library "pathname"
8
- # library "strong_json" # Gems
9
+ library "logger"
10
+ library "mutex_m"
11
+ library "date"
12
+
13
+ library 'activesupport'
14
+ library 'actionpack'
15
+ library 'activejob'
16
+ library 'activemodel'
17
+ library 'actionview'
18
+ library 'activerecord'
19
+ library 'railties'
9
20
  end
@@ -1,5 +1,8 @@
1
- class ActionMailer::Base
2
- extend ActionView::Layouts::ClassMethods
1
+ module ActionMailer
2
+ class Base
3
+ # TODO
4
+ # extend ActionView::Layouts::ClassMethods
3
5
 
4
- def self.default: (?Hash[Symbol, untyped] value) -> Hash[Symbol, untyped]
6
+ def self.default: (?Hash[Symbol, untyped] value) -> Hash[Symbol, untyped]
7
+ end
5
8
  end
@@ -0,0 +1,14 @@
1
+ module Capybara
2
+ module DSL
3
+ end
4
+
5
+ module Minitest
6
+ module Assertions
7
+ end
8
+ end
9
+
10
+ module Poltergeist
11
+ class Driver
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Concurrent
2
+ class Map
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ class ERB
2
+ class Compiler
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Erubi
2
+ class Engine
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module I18n
2
+ class Config
3
+ end
4
+ end
@@ -1,2 +1,13 @@
1
- class Minitest::Test
1
+ module Minitest
2
+ class Test
3
+ end
4
+
5
+ class SummaryReporter
6
+ end
7
+
8
+ class StatisticsReporter
9
+ end
10
+
11
+ module Assertions
12
+ end
2
13
  end
@@ -0,0 +1,5 @@
1
+ module PG
2
+ class Connection
3
+ def async_exec: (*untyped) -> untyped
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Que
2
+ class Job
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ class QC
2
+ class Queue
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Racc
2
+ class Parser
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Rack
2
+ module Test
3
+ class UploadedFile
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,47 @@
1
+ module Rack
2
+ class Server
3
+ end
4
+
5
+ class Response
6
+ module Helpers
7
+ def location: () -> untyped
8
+ end
9
+ end
10
+
11
+ class Request
12
+ module Helpers
13
+ end
14
+
15
+ module Env
16
+ end
17
+ end
18
+
19
+ module Session
20
+ module Abstract
21
+ class SessionHash
22
+ end
23
+
24
+ class PersistedSecure
25
+ class SecureSessionHash
26
+ end
27
+ end
28
+
29
+ class Persisted
30
+ end
31
+ end
32
+
33
+ class SessionId
34
+ end
35
+
36
+ class Dalli
37
+ end
38
+ end
39
+
40
+ module Cache
41
+ class MetaStore
42
+ end
43
+
44
+ class EntityStore
45
+ end
46
+ end
47
+ end
@@ -1,11 +1,10 @@
1
1
  module Rails
2
- def self.root: () -> Pathname
3
- def self.application: () -> Rails::Application
4
- end
5
-
6
- class Rails::Application
7
- def routes: () -> untyped
8
- end
2
+ def self.env: () -> ActiveSupport::StringInquirer | ...
9
3
 
10
- class Rails::Railtie
4
+ module Dom
5
+ module Testing
6
+ module Assertions
7
+ end
8
+ end
9
+ end
11
10
  end
@@ -0,0 +1,9 @@
1
+ module RDoc
2
+ module Generator
3
+ class SDoc
4
+ end
5
+ end
6
+
7
+ class Task
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Sidekiq
2
+ module Worker
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Sneakers
2
+ module Worker
3
+ end
4
+ end
@@ -1,14 +1,24 @@
1
- class Logger
1
+ module DRb
2
2
  end
3
3
 
4
- class Logger::Formatter
4
+ module DRb::DRbUndumped
5
5
  end
6
6
 
7
- module Mutex_m
7
+ module TSort
8
8
  end
9
9
 
10
- module DRb
10
+ class SimpleDelegator
11
11
  end
12
12
 
13
- module DRb::DRbUndumped
13
+ module Gem
14
+ class Version
15
+ end
16
+ end
17
+
18
+ module OpenSSL
19
+ class Cipher
20
+ end
21
+ end
22
+
23
+ module FileUtils
14
24
  end
@@ -0,0 +1,4 @@
1
+ module SuckerPunch
2
+ module Job
3
+ end
4
+ end
@@ -0,0 +1,12 @@
1
+ class Thor
2
+ class Group
3
+ end
4
+
5
+ module Actions
6
+ class CreateFile
7
+ end
8
+ end
9
+
10
+ class Error
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module TZInfo
2
+ class Timezone
3
+ end
4
+ end
@@ -1,15 +1,22 @@
1
+ #!ruby
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+ gem 'rbs', '1.0.0'
8
+ end
9
+
1
10
  require 'rbs'
2
- rbs = ARGF.read
3
- decls = RBS::Parser.parse_signature(rbs)
4
11
 
5
12
  def args(n)
6
13
  (:T..).take(n)
7
14
  end
8
15
 
9
16
  def env
10
- @env ||= RBS::Environment.new.tap do |env|
17
+ @env ||= begin
11
18
  loader = RBS::EnvironmentLoader.new()
12
- loader.load(env: env)
19
+ RBS::Environment.from_loader(loader).resolve_type_names
13
20
  end
14
21
  end
15
22
 
@@ -17,7 +24,7 @@ def apply_to_superclass(decl)
17
24
  return unless decl.super_class
18
25
 
19
26
  name = decl.super_class.name
20
- type = env.find_class(name) || env.find_class(name.absolute!)
27
+ type = env.class_decls[name] || env.class_decls[name.absolute!]
21
28
  return unless type
22
29
  return if type.type_params.empty?
23
30
 
@@ -32,12 +39,21 @@ def apply_to_superclass(decl)
32
39
  decl.instance_variable_set(:@type_params, type_params)
33
40
  end
34
41
 
42
+ def apply_to_itself(decl)
43
+ name = decl.name
44
+ type = env.class_decls[name] || env.class_decls[name.absolute!]
45
+ return unless type
46
+ return if type.type_params.empty?
47
+
48
+ decl.instance_variable_set(:@type_params, type.type_params.dup)
49
+ end
50
+
35
51
  def apply_to_includes(decl)
36
52
  decl.members.each do |member|
37
53
  next unless member.is_a?(RBS::AST::Members::Mixin)
38
54
 
39
55
  name = member.name
40
- type = env.find_class(name) || env.find_class(name.absolute!)
56
+ type = env.class_decls[name] || env.class_decls[name.absolute!]
41
57
  next unless type
42
58
  next if type.type_params.empty?
43
59
 
@@ -46,14 +62,24 @@ def apply_to_includes(decl)
46
62
  end
47
63
  end
48
64
 
49
- decls.each do |decl|
50
- case decl
51
- when RBS::AST::Declarations::Class
52
- apply_to_superclass(decl)
53
- apply_to_includes(decl)
54
- when RBS::AST::Declarations::Module, RBS::AST::Declarations::Interface, RBS::AST::Declarations::Extension
55
- apply_to_includes(decl)
65
+ def analyze(decls)
66
+ decls.each do |decl|
67
+ case decl
68
+ when RBS::AST::Declarations::Class
69
+ apply_to_itself(decl)
70
+ apply_to_superclass(decl)
71
+ apply_to_includes(decl)
72
+ analyze(decl.members)
73
+ when RBS::AST::Declarations::Module, RBS::AST::Declarations::Interface
74
+ apply_to_itself(decl)
75
+ apply_to_includes(decl)
76
+ analyze(decl.members)
77
+ end
56
78
  end
57
79
  end
58
80
 
81
+ rbs = ARGF.read
82
+ decls = RBS::Parser.parse_signature(rbs)
83
+ analyze(decls)
84
+
59
85
  puts RBS::Writer.new(out: $stdout).write(decls)