Skip to content

Commit

Permalink
Fix warn messages in doc (#1255)
Browse files Browse the repository at this point in the history
* Fix warn messages in doc

* use -Wconf to silence warnings about non-exhaustive pattern matching in documentation

* add Xfatal-warnings to docs project
  • Loading branch information
mlachkar committed Oct 12, 2020
1 parent e79b791 commit 39d6cf6
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ lazy val docs = project
skip in publish := true,
moduleName := "scalafix-docs",
scalaVersion := scala213,
scalacOptions += "-Wconf:msg='match may not be exhaustive':s", // silence exhaustive pattern matching warning for documentation
scalacOptions += "-Xfatal-warnings",
mdoc := run.in(Compile).evaluated,
crossScalaVersions := List(scala213),
libraryDependencies ++= List(
Expand Down
51 changes: 25 additions & 26 deletions docs/developers/semantic-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ The variable `doc` in the code examples is an implicit instance of
```scala mdoc:passthrough
import scalafix.docs.PatchDocs
def println(a: Any): Unit = PatchDocs.println(a)
import scalafix.docs.PatchDocs._
implicit var doc: SemanticDocument = null
```

Expand All @@ -53,21 +52,21 @@ parameters
doc.tree.traverse {
// Option.apply
case option @ Term.Select(Term.Name("Option"), Term.Name("apply")) =>
println("synthetic = " + option.synthetic)
println("structure = " + option.synthetic.structure)
println("synthetic = " + option.synthetics)
println("structure = " + option.synthetics.structure)
}
```

The asterisk `*` represents an `OriginalTree` node that matches the enclosing
non-synthetic tree, which is `List` in this example.

The `.synthetic` method is only available on `Term` nodes, using the method on
The `.synthetics` method is only available on `Term` nodes, using the method on
other tree nodes such as types results in compilation error

```scala mdoc:fail
doc.tree.traverse {
case app @ Type.Name("App") =>
println(".synthetic = " + app.synthetic)
println(".synthetic = " + app.synthetics)
}
```

Expand All @@ -83,21 +82,21 @@ Option[Int](2) // inferred: Option.apply[Int](2)
""")
```

Use `Tree.synthetic` in combination with `SemanticTree.symbol` to get the symbol
Use `Tree.synthetics` in combination with `SemanticTree.symbol` to get the symbol
of those inferred `.apply` method calls.

```scala mdoc
doc.tree.traverse {
case Term.Apply(add @ q"add", List(q"2")) =>
println("add(2)")
println("synthetic = " + add.synthetic)
println("symbol = " + add.synthetic.flatMap(_.symbol).structure)
println("structure = " + add.synthetic.structure)
println("synthetic = " + add.synthetics)
println("symbol = " + add.synthetics.flatMap(_.symbol).structure)
println("structure = " + add.synthetics.structure)
case Term.ApplyType(option @ q"Option", List(t"Int")) =>
println("Option[Int]")
println("synthetic = " + option.synthetic)
println("symbol = " + option.synthetic.flatMap(_.symbol).structure)
println("structure = " + option.synthetic.structure)
println("synthetic = " + option.synthetics)
println("symbol = " + option.synthetics.flatMap(_.symbol).structure)
println("structure = " + option.synthetics.structure)
}
```

Expand All @@ -124,14 +123,14 @@ Main.run // implicit argument: message
""")
```

Use `Tree.synthetic` to look up an implicit argument for any `Term` node.
Use `Tree.synthetics` to look up an implicit argument for any `Term` node.

```scala mdoc
doc.tree.traverse {
case term: Term if term.synthetic.isDefined =>
case term: Term if term.synthetics.nonEmpty =>
println("term = " + term.syntax)
println("synthetic = " + term.synthetic)
println("structure = " + term.synthetic.structure)
println("synthetics = " + term.synthetics)
println("structure = " + term.synthetics.structure)
}
```

Expand All @@ -146,18 +145,18 @@ List(1) ++ List(2)
""")
```

Use the `Term.ApplyInfix.syntheticOperator` to look up inferred type parameters
Use the `Term.ApplyInfix.syntheticOperators` to look up inferred type parameters
of infix operators.

```scala mdoc
doc.tree.traverse {
case concat @ Term.ApplyInfix(_, Term.Name("++"), _, _) =>
println(".syntheticOperator = " + concat.syntheticOperator)
println(".structure = " + concat.syntheticOperator.structure)
println(".syntheticOperators = " + concat.syntheticOperators)
println(".structure = " + concat.syntheticOperators.structure)
}
```

The `.syntheticOperator` method is only available for `Term.ApplyInfix` nodes,
The `.syntheticOperators` method is only available for `Term.ApplyInfix` nodes,
using the method on other node types results in a compilation error

[comment]: <> (Todo: add mdoc:fail for this snippet!)
Expand All @@ -173,7 +172,7 @@ Beware that looking up synthetics for the infix operator name returns nothing
```scala mdoc
doc.tree.traverse {
case concat @ Term.Name("++") =>
println(".synthetic = " + concat.synthetic)
println(".synthetics = " + concat.synthetics)
}
```

Expand All @@ -191,18 +190,18 @@ for (number <- numbers) println(number)
""")
```

Use `Tree.synthetic` on the tree node `Term.ForYield` to inspect the desugared
Use `Tree.synthetics` on the tree node `Term.ForYield` to inspect the desugared
version of the `for { .. } yield` expression

```scala mdoc
doc.tree.traverse {
case forYield: Term.ForYield =>
println(".synthetic = " + forYield.synthetic)
println(".synthetics = " + forYield.synthetics)
}
```

The `orig(List(1, 2))` and `orig(1.to(i)` parts represent `OriginalSubTree`
nodes that match non-synthetic tree nodes from the original for-comprension.
nodes that match non-synthetic tree nodes from the original for-comprehension.

## Known limitations

Expand All @@ -228,7 +227,7 @@ Observe the empty `withFilter` body and `<unknown>` parameter symbol.
```scala mdoc
doc.tree.traverse {
case forYield: Term.ForYield =>
println(forYield.synthetic)
println(forYield.synthetics)
}
```

Expand All @@ -250,7 +249,7 @@ Observe the `<unknown>` parameter symbol to the final call to `map`.
```scala mdoc
doc.tree.traverse {
case forYield: Term.ForYield =>
println(forYield.synthetic)
println(forYield.synthetics)
}
```

Expand Down
12 changes: 6 additions & 6 deletions docs/developers/symbol-information.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ Use `ClassSignature.parents` and `TypeRef.symbol` to lookup the class hierarchy.
def getParentSymbols(symbol: Symbol): Set[Symbol] =
symbol.info.get.signature match {
case ClassSignature(_, parents, _, _) =>
Set(symbol) ++ parents.flatMap {
Set(symbol) ++ parents.collect {
case TypeRef(_, symbol, _) => getParentSymbols(symbol)
}
}.flatten
}
getParentSymbols(Symbol("java/lang/String#"))
```
Expand All @@ -256,9 +256,9 @@ def getClassMethods(symbol: Symbol): Set[SymbolInformation] =
symbol.info.get.signature match {
case ClassSignature(_, parents, _, declarations) =>
val methods = declarations.filter(_.isMethod)
methods.toSet ++ parents.flatMap {
methods.toSet ++ parents.collect {
case TypeRef(_, symbol, _) => getClassMethods(symbol)
}
}.flatten
case _ => Set.empty
}
getClassMethods(Symbol("scala/Some#")).take(5)
Expand Down Expand Up @@ -368,9 +368,9 @@ def getMethodOverloads(classSymbol: Symbol, methodName: String): Set[SymbolInfor
declaration.isMethod &&
declaration.displayName == methodName
}
overloadedMethods.toSet ++ parents.flatMap {
overloadedMethods.toSet ++ parents.collect {
case TypeRef(_, symbol, _) => getMethodOverloads(symbol, methodName)
}
}.flatten
case _ => Set.empty
}
getMethodOverloads(Symbol("java/lang/String#"), "substring")
Expand Down
1 change: 0 additions & 1 deletion docs/developers/symbol-matcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import scala.meta._
```scala mdoc:passthrough
import scalafix.docs.PatchDocs
def println(a: Any): Unit = PatchDocs.println(a)
import scalafix.docs.PatchDocs._
```

## SemanticDB
Expand Down
2 changes: 1 addition & 1 deletion docs/developers/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ trees.

```scala mdoc
println(q"complete(true)".structure) // line wrap at 80th column
println(q"complete(true)".structure(30)) // line wrap at 30th column
println(q"complete(true)".structureWidth(30)) // line wrap at 30th column
```

The output of `tree.structure` can be copy-pasted for use in pattern matching.
Expand Down
6 changes: 4 additions & 2 deletions scalafix-docs/src/main/scala/docs/website.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ package object website {
default: T
)(implicit settings: Settings[T], ev: T <:< Product): List[(Setting, Any)] = {
settings.settings
.zip(default.productIterator.toIterable)
.zip(default.productIterator.iterator.to(Iterable))
.filterNot { case (setting, _) => setting.isHidden }
.flatMap {
case (s, d: SimpleDefinitions) =>
(s, d.kinds.mkString("['", "', '", "']")) :: Nil
case (deepSetting, defaultSetting: Product)
if deepSetting.underlying.nonEmpty =>
deepSetting.flat.zip(defaultSetting.productIterator.toIterable)
deepSetting.flat.zip(
defaultSetting.productIterator.iterator.to(Iterable)
)
case (s, lst: Iterable[_]) =>
val rendered = lst.map(render)
val string =
Expand Down
2 changes: 1 addition & 1 deletion scalafix-docs/src/main/scala/scalafix/docs/PatchDocs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object PatchDocs {
doc
.nested(ctx.indentCount * ctx.indentStep)
.renderStream(ctx.leftOffset)
.toIterator
.iterator
}
x match {
case t: SemanticTree =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class ExplicitSynthetic(insertInfixTypeParam: Boolean)
case Term.Select(_, Term.Name("apply")) =>
// happens for explicit "List.apply" because Synthetic.symbol returns Some(symbol)
// for OriginalTree.
None
List()
case infix: Term.ApplyInfix if insertInfixTypeParam =>
for {
synthetic <- infix.syntheticOperator.collect {
synthetic <- infix.syntheticOperators.collect {
case tappl: TypeApplyTree => tappl
}
} yield {
Expand All @@ -43,7 +43,7 @@ class ExplicitSynthetic(insertInfixTypeParam: Boolean)
}
case t: Term =>
for {
synthetic <- t.synthetic
synthetic <- t.synthetics
sym <- synthetic.symbol
if sym.displayName == "apply"
} yield {
Expand Down

0 comments on commit 39d6cf6

Please sign in to comment.