Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeProf for IDE: Enrich Development Experience...

Yusuke Endoh
September 09, 2021

TypeProf for IDE: Enrich Development Experience without Annotations

Yusuke Endoh

September 09, 2021
Tweet

More Decks by Yusuke Endoh

Other Decks in Programming

Transcript

  1. Yusuke Endoh / @mametter •A Ruby committer working at Cookpad

    w/ @ko1 • TypeProf: A static type analyzer for Ruby 3 •An esoteric Ruby programmer • See my RubyKaigi bio 2
  2. A recent contribution: error_highlight 3 json = { foo: {

    bar: { baz: 42 } } } json[:foo][:barr][:baz] # Ruby 3.0 $ ruby t.rb t.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) # Ruby 3.1 $ ruby t.rb t.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) json[:foo][:barr][:baz] ^^^^^^ Credit: The original author of its prototype is @yui-knk https://github.com/ruby/ruby/pull/4470
  3. "I love static typing" … or do you? •Static typing

    is a means, not an end •The end is the modern development experience • Demo: TypeScript 4
  4. "I love static typing" … or do you? •Static typing

    is a means, not an end • The end is the modern development experience • On-the-fly error reporting, go-to-definition, completion, argument hint, etc. •Q: Isn't it possible to achieve the experience without changing the Ruby language? • My answer is "Possible!" 10 (under some conditions)
  5. Today's topic: TypeProf for IDE •A VSCode extension for Ruby

    • Powered by TypeProf (a static type analyzer for Ruby) •Demo 11
  6. Agenda ➔ Recap: Types for Ruby 3  •TypeProf •

    TypeProf for IDE • Conclusion 18
  7. Recap: Types for Ruby 3 19 Name What RBS TypeProf

    Steep Sorbet The Ruby official type definition language A static type analyzer for Ruby A static type analyzer for Ruby A static type analyzer for Ruby
  8. RBS: Ruby official type definition language 20 class User def

    initialize: (String) -> void def name: () -> String end
  9. What a static type analyzer does •Infers the types of

    a given program • Reports a potential runtime type error • Supports IDE 21 msg = "Hello, " + user.name msg: String msg = "Hello, " + user.naame Error!
  10. Differences of each static analyzer 22 Type inference Error reporting

    IDE support Analysis speed TypeProf 🙂Strong ☹️Weak No → Yes ☹️Slow Steep ☹️Weak 🙂Strong Yes 🙂Fast
  11. Agenda •Recap: Types for Ruby 3 ➔ TypeProf  •

    TypeProf for IDE • Conclusion 23
  12. The key idea of TypeProf Runs a Ruby code in

    "type-level" Traditional interpreter def foo(n) n.to_s end foo(42) Calls w/ 42 Returns "42" TypeProf def foo(n) n.to_s end foo(42) Calls w/ Integer Returns String Object#foo :: (Integer) -> String 24
  13. The user-facing feature of TypeProf •Ruby 3.0 • A type

    inference tool to create a prototype of RBS • No IDE support • Ruby 3.1~ • + IDE support! 25
  14. Demo (memo) •Test instead of type annotations •Passing an unknown

    type • Manual RBS specification • Flow-sensitive analysis • Manual overloading •Block, RBSWiki example 26
  15. Demo: Passing an unknown type 28 The method signature is

    changed to accept a new type Error in the callee side
  16. Language Server Protocol (LSP) 36 LSP client (vscode) LSP server

    (TypeProf for IDE) code changed error found complete "5.ti" maybe "5.times" 5.ti| Do you mean: 5.times 1 + "str" 1 + "str" Is this a bug?
  17. Currently supported LSP features •Show method signatures •Show potential errors

    • Go to definition of methods/variables • Find references of methods/variables • Completion •Show hint about method arguments 37
  18. Not-implemented-yet LSP Features 38 Type hover Documentation • More challenging

    features • Auto refactoring (renaming), quick fix suggestion, etc. • Contribution is welcome!!!
  19. Future work: the parser needs more ♥ •An AST node

    knows only the beginning and end of the source code • The parser returns nothing when syntax is broken • Cannot complete an incomplete call 39 current implementation desired behavior foo(k:)
  20. About analysis performance •TypeProf is very slow • It traces

    "require 'foo'" (even if it is a gem) •TypeProf for IDE stops the analysis in one second • Nothing will be reported where it couldn't analyze within the time limit 40
  21. How to make TypeProf fast •Write RBS for gems •

    so that TypeProf don't have to analyze gem sources • Watch pocke's talk (Day 2) • Divide your application to components • and write RBS against their interfaces • We need experiment with a practical use case 41
  22. Agenda •Recap: Types for Ruby 3 •TypeProf • TypeProf for

    IDE • Implementation ➔Conclusion 42
  23. Release plan •TypeProf for IDE will be released in Ruby

    3.1 • Happy if you could play with it and give us feedback • How to run the development version https://gist.github.com/mame/86234de6a58352b9f994e0f8a6d6fbc2 (in Japanese) •Ready for production? • Experimental, but hopefully works for small programs • For large code base, please write RBS for gems first! 43
  24. Special thanks •Hideki Miura • Ruby committers: matz, akr, ko1,

    soutaro • Katsuhiro Ueno & Eijiro Sumii •Stripe team & Shopify team & Jeff Foster • Yuta Saito (@kateinoigakukun) • Many improvements of TypeProf for IDE 44
  25. Conclusion •The modern development experience is possible without full type

    annotations by TypeProf for IDE •Ruby 3.1 will bundle TypeProf for IDE 45