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