Closed15

Rails RBSを試す

あっきーあっきー

Gemfileにrbs関係を追加、bundle install

Gemfile
  gem "rbs-inline", require: false
  gem "rbs_rails", require: false
  gem "steep", require: false
あっきーあっきー

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.
あっきーあっきー

lib/tasks/rbs.rake sig/ が作られた

$ g st
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        lib/tasks/rbs.rake
        sig/
あっきーあっきー

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. 🫖

特に問題なし

あっきーあっきー

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
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~
このスクラップは1ヶ月前にクローズされました