https://cookpad.connpass.com/event/282436/ 手を動かして振り返る RubyKaigi 2023でLTしました.
相生ゆらの #KaigieffectYla Aioi (@Little_Rubyist)
View Slide
今回話す人相生ゆら/Yla Aioi@Little_Rubyist @Little-RubyistRBSとRuboCopに興味があります2023/02~
- lramaにPR出した- 会社のプロジェクトにRBS導入を始めた- RBS導入時のポイントアジェンダ
今回のRubyKaigiは大パーサー時代でしたねlramaの話
lramaの話
というわけで
やったことlramaの話
rbs, steepを入れるlramaの話
挙動確認のために適当なファイルに型をつけるlramaの話
Github ActionにSteep checkを追加するlramaの話
PRを出して終わり :tada:lramaの話
ちなみに初contributeでしたlramaの話
あなたがlramaにRBSを追加したいときにやるべきことlramaの話
型付けするファイルを決めるlramaの話
(現状は)steepfileに狙いをつけたファイルをcheckさせるlramaの話
ファイルの中身を読むlramaの話
RBSを書くlramaの話
ruby/lramaにPR出して祈る :pray:lramaの話
lramaは金子さんがRBS入れようとしてくれていますが他gemはあんまりっぽいgem_rbs_collection
そういう時はgem_rbs_collectionに新しいgemを追加したり既存gemの型を足していきましょうgem_rbs_collection
え,gemは使ってるけどプロジェクトでは一部のmethodしか使ってない?gem_rbs_collection
そんな状況で1ファイルの型付けは厳しい?gem_rbs_collection
わかる〜〜〜!!!!!gem_rbs_collection
1method 1PRでも大丈夫です :+1:gem_rbs_collection
もし不安な時は手元でpatch的に型を付けてからPRを出しましょうgem_rbs_collection
私の例を話しましょう会社のプロジェクトにRBS入れた話
手順
steep, rbs_railsを入れる
rbs_rails:generate_rbs_for_modelsを走らせる
動かないので困る
composite-primary-keyを使っていた
rbs_railsのprimary_key部を変える
class Generator < RbsRails::ActiveRecord::Generatorprivatedef pk_typepk = klass.primary_keyreturn 'top' unless pkif pk.instance_of?(::CompositePrimaryKeys::CompositeKeys)sql_types = []pk.each do |x|col = klass.columns.find { |column| column.name == x }sql_types << sql_type_to_class(col.type)endreturn sql_types.join(' | ')endcol = klass.columns.find { |column| column.name == pk }sql_type_to_class(col.type)endend
通った :tada:
> bundle exec steep check> Detected 714 problems from 91files
Rails wayに沿ってないので余計にエラーが出る
これが3/2
大体は定義したら解決したけどどれだけ悩んでも分からない子も結構いた
included do…endの中で定義しているのでinclude先のclassが持ってるmethodが分からない
module SomeModuleextend ActiveSupport::Concernincluded do# @type self: ClassMethods & singleton(ApplicationRecord)...end型注釈してあげる
明らかにお前はModuleだろ…と思うのにdefine_methodを知らないと言う
module Models::UndefinedDateTimeAttributeextend ActiveSupport::Concernmodule ClassMethods : Moduledef attr_undefined_datetime: (untyped column) -> voidendend実はこれどういう時に使うか分かってない.moduleそのものの情報を足してあげる
read_attributeやscopeにblockが必須と言われる
必須ではないのでblockを持たない型を足してあげる
module ActiveRecordmodule AttributeMethodsmodule Readextend ActiveSupport::Concerndef read_attribute: (untyped attr_name) -> untyped| ...endendmodule Scopingmodule Namedextend ActiveSupport::Concernmodule ClassMethodsdef scope: (untyped name, untyped body) -> untyped| ...endendendend
みたいなのを延々とやりました
この辺は全部RubyKaigi中に@pockeと@soutaroに助けてもらいましたthanks!
一昨日
それなりに出来たのでkatakata_irb導入thanks @tompng
しかしちょっと問題があって
今回簡単なところからRBSを入れたのでmodels/しか対応していない
のであんまりkatakata_irbでは自前RBSの恩恵を受けられていなさそう
まあnilableとかのエラーは見つけたのでいいか!
型書くの楽しいです
便利command
bundle exec rbs -I sigmethod Hoge foo?
bundle exec rbs -I sigmethod Hoge foo?RBSファイルの定義場所
bundle exec rbs -I sigmethod Hoge foo?RBSファイルの定義場所method コマンド 最強
bundle exec rbs -I sigmethod Hoge foo?RBSファイルの定義場所method コマンド 最強探したいmethod
❯ bundle exec rbs -I sig method --singleton Hoge find::hoge.finddefined_in: ::_ActiveRecord_Relation_ClassMethodsimplementation: ::OfficialComicGroupaccessibility: publictypes:(::Integer id) -> ::Hoge| (::Array[::Integer]) -> ::Array[::Hoge]| (*::Integer) -> ::Array[::Hoge]便利だね〜〜〜
これで見つからなかったら型注釈を足すとか型定義を足すとかincludeとかしてあげるとか
何かどう足掻いても直せない時-> ruby-jp #typesに投げるとよいです
あまったら喋るやつ
今まだ直せてない謎エラー達
ガード文書いてるのにassociation系でchild.age?的なmethodやるとnilableなエラーが出る(一般的なコードにする時間なかった)
解決法がわかる人がいたら教えて下さい〜