Slide 1

Slide 1 text

Move fast and BREAK FIX things

Slide 2

Slide 2 text

ME, HI!

Slide 3

Slide 3 text

STUFF I DO (BY DAY)

Slide 4

Slide 4 text

STUFF I DO (BY NIGHT) https://astexplorer.net/ https://github.com/gabro/vscode-scalafmt https://github.com/scalacenter/scalafix

Slide 5

Slide 5 text

AGENDA 1. Intro and context 2. ASTs and related workflows 3. Scalameta 4. Scalafix 5. Live examples

Slide 6

Slide 6 text

WHAT IS SCALAFIX Scalafix is a rewrite and linting tool for Scala — scalacenter.github.io/scalafix

Slide 7

Slide 7 text

EXAMPLE 1 - NOPROCEDURESYNTAX // before def main(args: Seq[String]) { println("Hello world!") } // after def main(args: Seq[String]): Unit = { println("Hello world!") }

Slide 8

Slide 8 text

EXAMPLE 2 - NOAUTOTUPLING // before def someMethod(t: (Int, String)) = ??? someMethod(1, "something") // after def someMethod(t: (Int, String)) = ??? someMethod((1, "something"))

Slide 9

Slide 9 text

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(_ + _)

Slide 10

Slide 10 text

EXAMPLE 4 - NOINFER val myList = List(Some(42), Right("foo")) // ^ // |__ ⚠ Product with Serializable

Slide 11

Slide 11 text

LET'S TALK ABOUT MAINTAINERS

Slide 12

Slide 12 text

PURE EVIL BREAK CODE WITHOUT DOCUMENTING WHAT BROKE

Slide 13

Slide 13 text

MILD EVIL BREAK CODE DOCUMENTING WHAT BROKE (OR LINKING TO A SUPER-LONG ISSUE)

Slide 14

Slide 14 text

GOOD EFFORT BREAK CODE AND DOCUMENT HOW TO FIX IT

Slide 15

Slide 15 text

YOU ROCK! BREAK CODE AFTER A DEPRECATION CYCLE AND EXPLAIN HOW TO FIX IT

Slide 16

Slide 16 text

SUPERHERO BREAK CODE AFTER A DEPRECATION CYCLE AND PROVIDE AN AUTOMATIC REWRITE

Slide 17

Slide 17 text

SCI-FI?

Slide 18

Slide 18 text

MEANWHILE IN SWIFT

Slide 19

Slide 19 text

MEANWHILE IN JAVASCRIPT

Slide 20

Slide 20 text

MEANWHILE IN JAVASCRIPT 3 paragraphs later

Slide 21

Slide 21 text

The JS tooling landscape is living in the future — Ólafur Páll Geirsson, author of Scalafix

Slide 22

Slide 22 text

IT'S TIME WE JUMP INTO THE FUTURE

Slide 23

Slide 23 text

HOW? LET'S TAKE A STEP BACK

Slide 24

Slide 24 text

A QUICK PRIMER TO THE AST

Slide 25

Slide 25 text

LET'S START FROM THE ANSWER val answer = 42

Slide 26

Slide 26 text

TOKENS val answer = 42 // | | | | | | | | // \_'val'_/ \_Name_/ \_'='_/ \_Literal_/

Slide 27

Slide 27 text

GRAMMAR val answer = 42 // | | | | | | | | // | | \__Term.Name__/ | \_Lit.Int_/ | // | | | | // | \_____Pat.Var.Term__/ | // | | // \_____________________Defn.Val____________________/

Slide 28

Slide 28 text

SO... AST?

Slide 29

Slide 29 text

ABSTRACT SYNTAX TREE

Slide 30

Slide 30 text

WHY ABSTRACT?

Slide 31

Slide 31 text

val answer = 42; val answer = 42 val answer = 42 val answer = 42

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

ASTS... WHY SHOULD I CARE?

Slide 34

Slide 34 text

AST WORKFLOW 1: SCALAC

Slide 35

Slide 35 text

AST WORKFLOW 2: MACROS

Slide 36

Slide 36 text

AST WORKFLOW 3: SCALAFMT

Slide 37

Slide 37 text

AST WORKFLOW 4: SCALAFIX

Slide 38

Slide 38 text

AST WORKFLOW 4: SCALAFIX Woops, copy-paste?

Slide 39

Slide 39 text

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 )

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

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) }

Slide 42

Slide 42 text

"AST" WORKFLOW 4: SCALAFIX

Slide 43

Slide 43 text

OK, LET'S DO THIS!

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Introducing SCALAMETA

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

METAPROGRAMMING?

Slide 48

Slide 48 text

METAPROGRAMMING DEVTOOLS! ▸ code formatting ▸ code fixing ▸ code browsing ▸ and many more applications!

Slide 49

Slide 49 text

SCALAMETA VADEMECUM ▸ Trees ▸ Tokens ▸ Parser ▸ Tree manipulation primitives ▸ Semantic API ▸ (pretty-printer)

Slide 50

Slide 50 text

TOKENS The "atoms"

Slide 51

Slide 51 text

TREES The fundamental data structure

Slide 52

Slide 52 text

PARSER ! ➡ # several dialects

Slide 53

Slide 53 text

TREE MANIPULATION PRIMITIVES ▸ transform ▸ traverse ▸ collect

Slide 54

Slide 54 text

SEMANTIC API: DATABASE ▸ Names ▸ Symbols ▸ Messages ▸ Synthetics

Slide 55

Slide 55 text

SEMANTIC API: NAMES package scalaworld.semantic Names: [8..18): scalaworld => _root_.scalaworld. [19..27): semantic => _root_.scalaworld.semantic.

Slide 56

Slide 56 text

SEMANTIC API: SYMBOLS List(1, 2, 3).head Symbols: _root_.scala.collection.immutable.List. => final object List _root_.scala.collection.IterableLike#head()Ljava/lang/Object;. => def head: A

Slide 57

Slide 57 text

SEMANTIC API: MESSAGES object A { 1 + 1 } Messages: [140..145): [warning] a pure expression does nothing in statement position; you may be omitting necessary parentheses

Slide 58

Slide 58 text

SEMANTIC API: SYNTHETICS (SUGARS) List(1, 2, 3) Synthetics: [2..7): apply => _root_.scala.collection.immutable.List.apply (Lscala/collection/Seq;) [8..11): Int => _root_.scala.Int# List.apply[Int](1, 2, 3)

Slide 59

Slide 59 text

RECAP 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;)

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

SCALAFIX FINALLY!

Slide 62

Slide 62 text

Rule Rewrite

Slide 63

Slide 63 text

Rule A rule can be: ▸ checked ▸ fixed

Slide 64

Slide 64 text

Patch

Slide 65

Slide 65 text

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.

Slide 66

Slide 66 text

LintMessage LintCategory

Slide 67

Slide 67 text

LintMessage / LintCategory A LintMessage is a warning/error that gets displayed to the user. Each LintMessage belongs to LintCategory (e.g. "MissingExplicitType" )

Slide 68

Slide 68 text

RuleCtx

Slide 69

Slide 69 text

RuleCtx The toolbox for writing rules. It provides the API for producing patches.

Slide 70

Slide 70 text

SemanticdbIndex

Slide 71

Slide 71 text

SemanticdbIndex A index for looking up data in a scalameta's Semantic Database. It contains all the available semantic information.

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

LIVE

Slide 74

Slide 74 text

WHAT'S NEXT?

Slide 75

Slide 75 text

SHORT-TERM ▸ more linter rules ▸ rule "bundles" ▸ more robust expansion of inferred types / implicits

Slide 76

Slide 76 text

MEDIUM TERM ▸ editor integrations ▸ stable API ▸ docs

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Questions? @gabro27 @buildoHQ @ScalaItaly