Closed15
Rails RBSを試す
こちらを参考にRBSを入れる
とりあえずRails8をAPIモードでインストール
Ruby 3.4.1
Rails 8.0.1
Gemfileにrbs関係を追加、bundle install
Gemfile
gem "rbs-inline", require: false
gem "rbs_rails", require: false
gem "steep", require: false
rbs初期化
🐑 ~/work/rails/rails8_rbs (main)
$ bundle exec rbs collection init
created: rbs_collection.yaml
🐑 ~/work/rails/rails8_rbs (main) x
$ bundle exec rbs collection install
rbs_collectionの説明にもあるように、.gem_rbs_collection/
を .gitignore
に追加
https://github.com/ruby/rbs/blob/master/docs/collection.md
$ echo /.gem_rbs_collection/ >> .gitignore
steep init
🐑 ~/work/rails/rails8_rbs (main)
$ steep init
Writing Steepfile...
Steepfile書き換え
Steepfile
target :app do
check 'app'
signature 'sig'
end
rbs_rails install
🐑 ~/work/rails/rails8_rbs (main)
$ rails g rbs_rails:install
create lib/tasks/rbs.rake
🐑 ~/work/rails/rails8_rbs (main) x
$ rails rbs_rails:all
warning: parser/current is loading parser/ruby34, which recognizes 3.4.0-compliant syntax, but you are running 3.4.1.
Please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
rbs-inline 実行
$ rbs-inline app --output=sig/rbs_inline/app --opt-out
inlineの型を書いていないので空っぽの型情報が出力された
sig/rbs_inline/app/controllers/posts_controller.rbs
# Generated from app/controllers/posts_controller.rb with RBS::Inline
class PostsController < ApplicationController
# GET /posts
def index: () -> untyped
# GET /posts/1
def show: () -> untyped
# POST /posts
def create: () -> untyped
# PATCH/PUT /posts/1
def update: () -> untyped
# DELETE /posts/1
def destroy: () -> untyped
private
# Use callbacks to share common setup or constraints between actions.
def set_post: () -> untyped
# Only allow a list of trusted parameters through.
def post_params: () -> untyped
end
型チェック実行
$ steep check
# Type checking files:
.............
No type error detected. 🫖
特に問題なし
rakeタスク用意
typecheckでエラーになってしまった
$ rails typecheck
warning: parser/current is loading parser/ruby34, which recognizes 3.4.0-compliant syntax, but you are running 3.4.1.
Please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
🎉 Generated 1 RBS files under sig/rbs_inline/app
# Type checking files:
.....F.......
sig/rbs_inline/app/controllers/posts_controller.rbs:21:10: [error] Cannot find type `::Post::ActiveRecord_Relation`
│ Diagnostic ID: RBS::UnknownTypeName
│
└ @posts: Post::ActiveRecord_Relation
~~~~~~~~~~~~~~~~~~~~~~~~~~~
steep 1.8.0以上で #:
シンタックスが使えるということで書き換え
- # @rbs () -> void
+ #: () -> void
このスクラップは1ヶ月前にクローズされました