You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/_posts/2017-02-25-scalafix-v0.3.md
+6-5Lines changed: 6 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ A symbol is a unique identifier of a single definition.
30
30
For example, `println` from the standard library has the symbol `_root_.scala.Predef.println(Ljava/lang/Object;)V.`.
31
31
The compiler is responsible for resolving names to symbols.
32
32
33
-
Scalahost is a compiler plugin in the scala.meta project that extracts symbols from the compiler and maps them to scala.meta syntax trees.
33
+
[Scalahost][] is a compiler plugin in the scala.meta project that extracts symbols from the compiler and maps them to scala.meta syntax trees.
34
34
Scalahost emits the extracted symbols into a "semantic database".
35
35
The semantic database can be persisted to files on disk and loaded for later analysis.
36
36
Semantic databases from different compilation units, potentially produced by different
@@ -39,16 +39,18 @@ This opens possibilities for large-scale code analysis.
39
39
40
40
The introduction of the scala.meta semantic API is a game changer for scalafix.
41
41
The ability to resolve names to symbols opens possibilities for many scalafix rewrites.
42
+
Before we cover a few example rewrites, let's look closer at what exactly "rewrite" means.
42
43
43
44
## Rewrite: meta.Tree => Seq[Patch]
44
-
45
+
In a nutshell, a scalafix `Rewrite` is a `scala.meta.Tree => Seq[Patch]` function.
46
+
The tree is backed by the scala.meta semantic API, so the rewrite is able to query for compiler information such as symbols.
45
47
A scalafix `Patch` is a small operation that can produce a diff on a Scala source file.
46
48
A patch can either be a "token patch" or a "tree patch".
47
49
48
50
Token patches are low-level but give full control over how every detail in a source file is handled, for example formatting and comments.
49
51
Example token patches are `Remove(token)` and `AddLeft(token, toAdd: String)`, which removes or prepends a string to `token`, respectively.
50
52
51
-
Tree patches are high-level and allow rewrite author to declaratively explain what operation to perform.
53
+
Tree patches are high-level and allow the rewrite author to declaratively explain what operation to perform.
52
54
An example tree patch is `AddGlobalImport(importer)`, which adds a new import to the top of a file if it does not exist.
53
55
Observe that `AddGlobalImport` does not worry about token-level details such as whether the user groups imports by prefix (`import a.{b, c}`) or not (`import a.b; import a.c`).
54
56
@@ -60,8 +62,6 @@ It can be harder to resolve conflicts on the token level since the original inte
60
62
Unsolvable conflicts abort the refactoring.
61
63
In the future, we hope to support more advanced conflict resolution strategies.
62
64
63
-
In a nutshell, a scalafix `Rewrite` is a `scala.meta.Tree => Seq[Patch]` function.
64
-
The tree is backed by the scala.meta semantic API, so the rewrite is able to query for compiler information such as symbols.
65
65
To demonstrate how rewrites are implemented with scalafix v0.3, let's step through an example use-case.
66
66
67
67
## Example: Xor to Either
@@ -138,6 +138,7 @@ the early days of scalafix development and come up with several brilliant ideas
0 commit comments