Move fast and fix things

Move fast and fix things

An introduction to Scalafix, presented at Scala IO 2017

C2bb0454c4af1a61e7f173d54ce17b0b?s=128

Gabriele Petronella

November 02, 2017
Tweet

Transcript

  1. Move fast and BREAK FIX things

  2. ME, HI!

  3. STUFF I DO (BY DAY)

  4. STUFF I DO (BY NIGHT) Scala and JS open-source

  5. AGENDA 1. Intro and context 2. ASTs and related workflows

    3. Scalameta 4. Scalafix 5. Live examples
  6. WHAT IS SCALAFIX Scalafix is a rewrite and linting tool

    for Scala — scalacenter.github.io/scalafix
  7. EXAMPLE 1 - NOPROCEDURESYNTAX // before def main(args: Seq[String]) {

    println("Hello world!") } // after def main(args: Seq[String]): Unit = { println("Hello world!") }
  8. EXAMPLE 2 - NOAUTOTUPLING // before def someMethod(t: (Int, String))

    = ??? someMethod(1, "something") // after def someMethod(t: (Int, String)) = ??? someMethod((1, "something"))
  9. EXAMPLE 3 - REMOVECARTESIANBUILDER // before import cats.syntax.cartesian._ val o1:

    Option[Int] = Some(42) val o2: Option[String] = Some("hello") o1 |@| o2 map (_ + _) // after import cats.syntax.apply._ val o1: Option[Int] = Some(42) val o2: Option[String] = Some("hello") (o1, o2).mapN(_ + _)
  10. EXAMPLE 4 - NOINFER val myList = List(Some(42), Right("foo")) //

    ^ // |__ ⚠ Product with Serializable
  11. LET'S TALK ABOUT MAINTAINERS

  12. PURE EVIL BREAK CODE WITHOUT DOCUMENTING WHAT BROKE

  13. MILD EVIL BREAK CODE DOCUMENTING WHAT BROKE (OR LINKING TO

    A SUPER-LONG ISSUE)
  14. GOOD EFFORT BREAK CODE AND DOCUMENT HOW TO FIX IT

  15. YOU ROCK! BREAK CODE AFTER A DEPRECATION CYCLE AND EXPLAIN

    HOW TO FIX IT
  16. SUPERHERO BREAK CODE AFTER A DEPRECATION CYCLE AND PROVIDE AN

    AUTOMATIC REWRITE
  17. SCI-FI?

  18. MEANWHILE IN SWIFT

  19. MEANWHILE IN JAVASCRIPT

  20. MEANWHILE IN JAVASCRIPT 3 paragraphs later

  21. The JS tooling landscape is living in the future —

    Ólafur Páll Geirsson, author of Scalafix
  22. IT'S TIME WE JUMP INTO THE FUTURE

  23. HOW? LET'S TAKE A STEP BACK

  24. A WORD ABOUT ASTS SO... AST?

  25. ABSTRACT SYNTAX TREE

  26. WHY ABSTRACT?

  27. val answer = 42; val answer = 42 val answer

    = 42 val answer = 42
  28. ASTS... WHY SHOULD I CARE?

  29. AST WORKFLOW 1: SCALAC

  30. AST WORKFLOW 2: MACROS

  31. AST WORKFLOW 3: SCALAFMT

  32. AST WORKFLOW 4: SCALAFIX

  33. AST WORKFLOW 4: SCALAFIX Woops, copy-paste?

  34. WHAT SCALAFIX COULD DO: // before def someMethod(string: String) {

    // I like trees println(string.trim.split("/").lastOption) } // after def someMethod(string: String): Unit = println( string .trim .split("/") .lastOption )
  35. None
  36. WHAT SCALAFIX DOES INSTEAD: // before def someMethod(string: String) {

    // I like trees println(string.trim.split("/").lastOption) } // after def someMethod(string: String): Unit = { // I like trees println(string.trim.split("/").lastOption) }
  37. "AST" WORKFLOW 4: SCALAFIX

  38. OK, LET'S DO THIS!

  39. None
  40. Introducing SCALAMETA

  41. Scalameta is a modern metaprogramming library for Scala — scalameta.org

  42. METAPROGRAMMING?

  43. METAPROGRAMMING DEVTOOLS! ▸ code formatting ▸ code fixing ▸ code

    browsing ▸ and many more applications!
  44. SCALAMETA VADEMECUM ▸ Trees ▸ Tokens ▸ Parser ▸ Tree

    manipulation primitives ▸ Semantic API ▸ (pretty-printer)
  45. SEMANTIC INFORMATION List(1, 2, 3) // Symbol _root_.scala.collection.immutable.List. // Denotation

    final object List // Synthetic [2..7): apply => _root_.scala.collection.immutable.List.apply (Lscala/collection/Seq;)
  46. None
  47. SCALAFIX FINALLY!

  48. Rule Rewrite

  49. Rule A rule can be: ▸ checked ▸ fixed

  50. Patch

  51. Patch A Scalafix Patch is a patch in the diff

    sense of patch. It boils down to a list of -/+ to apply to the source code.
  52. RuleCtx

  53. RuleCtx The toolbox for writing rules. It provides the API

    for producing patches.
  54. SemanticdbIndex

  55. SemanticdbIndex A index for looking up data in a scalameta's

    Semantic Database. It contains all the available semantic information.
  56. None
  57. LIVE

  58. WHAT'S NEXT?

  59. SHORT-TERM ▸ more linter rules ▸ rule "bundles" ▸ more

    robust expansion of inferred types / implicits
  60. MEDIUM TERM ▸ editor integrations ▸ stable API

  61. None
  62. Questions? @gabro27 @buildoHQ @ScalaItaly