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 WORD ABOUT
ASTS
SO... AST?
Slide 25
Slide 25 text
ABSTRACT
SYNTAX
TREE
Slide 26
Slide 26 text
WHY ABSTRACT?
Slide 27
Slide 27 text
val answer = 42;
val answer = 42
val answer =
42
val
answer
=
42
Slide 28
Slide 28 text
ASTS...
WHY SHOULD I
CARE?
Slide 29
Slide 29 text
AST WORKFLOW 1: SCALAC
Slide 30
Slide 30 text
AST WORKFLOW 2: MACROS
Slide 31
Slide 31 text
AST WORKFLOW 3: SCALAFMT
Slide 32
Slide 32 text
AST WORKFLOW 4: SCALAFIX
Slide 33
Slide 33 text
AST WORKFLOW 4: SCALAFIX
Woops, copy-paste?
Slide 34
Slide 34 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 35
Slide 35 text
No content
Slide 36
Slide 36 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 37
Slide 37 text
"AST" WORKFLOW 4: SCALAFIX
Slide 38
Slide 38 text
OK, LET'S DO
THIS!
Slide 39
Slide 39 text
No content
Slide 40
Slide 40 text
Introducing
SCALAMETA
Slide 41
Slide 41 text
Scalameta is a modern
metaprogramming library for Scala
— scalameta.org
Slide 42
Slide 42 text
METAPROGRAMMING?
Slide 43
Slide 43 text
METAPROGRAMMING DEVTOOLS!
▸ code formatting
▸ code fixing
▸ code browsing
▸ and many more applications!
Slide 44
Slide 44 text
SCALAMETA VADEMECUM
▸ Trees
▸ Tokens
▸ Parser
▸ Tree manipulation primitives
▸ Semantic API
▸ (pretty-printer)
Slide 45
Slide 45 text
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;)
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
SCALAFIX
FINALLY!
Slide 48
Slide 48 text
Rule
Rewrite
Slide 49
Slide 49 text
Rule
A rule can be:
▸ checked
▸ fixed
Slide 50
Slide 50 text
Patch
Slide 51
Slide 51 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 52
Slide 52 text
RuleCtx
Slide 53
Slide 53 text
RuleCtx
The toolbox for writing rules.
It provides the API for producing patches.
Slide 54
Slide 54 text
SemanticdbIndex
Slide 55
Slide 55 text
SemanticdbIndex
A index for looking up data in a scalameta's Semantic Database.
It contains all the available semantic information.
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
LIVE
Slide 58
Slide 58 text
WHAT'S NEXT?
Slide 59
Slide 59 text
SHORT-TERM
▸ more linter rules
▸ rule "bundles"
▸ more robust expansion of inferred types / implicits