Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: handle if with/without braces consistently #1752

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1090,22 +1090,23 @@ class Router(formatOps: FormatOps) {
}
val jsNative = isJsNative(right)
val noNewline = jsNative
val spacePolicy: Policy = rhs match {
case _: Term.If => twoBranches
case _: Term.ForYield => twoBranches
val (spacePolicy, spaceIndent) = rhs match {
case t: Term.If if style.activeForEdition_2020_03 =>
SingleLineBlock(t.cond.tokens.last) -> t.elsep.tokens.nonEmpty
case _: Term.If => twoBranches -> false
case _: Term.ForYield => twoBranches -> false
case _: Term.Try | _: Term.TryWithHandler
if style.activeForEdition_2019_11 && !noNewline =>
null // we force newlines in try/catch/finally
case _ => NoPolicy
(null, false) // we force newlines in try/catch/finally
case _ => NoPolicy -> false
}
val noSpace = null == spacePolicy ||
val noSpace = null == spacePolicy || isSingleLineComment(right) ||
(!jsNative && newlines > 0 && leftOwner.isInstanceOf[Defn])
val spaceIndent = if (isSingleLineComment(right)) 2 else 0
Seq(
Split(Space, 0, policy = spacePolicy)
.notIf(noSpace)
.withOptimalToken(optimal)
.withIndent(spaceIndent, expire, Left),
.withIndent(if (spaceIndent) 2 else 0, expire, Left),
Split(Newline, 1 + penalty)
.notIf(noNewline)
.withIndent(2, expire, Left)
Expand Down
55 changes: 28 additions & 27 deletions scalafmt-tests/src/test/resources/default/Advanced.stat
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,34 @@ val createAsStat = if (semantics.asInstanceOfs == Unchecked) {
}
>>>
val createAsStat = if (semantics.asInstanceOfs == Unchecked) {
js.Skip()
} else {
envFieldDef(
"as",
className,
js.Function(List(objParam),
js.Return(className match {
case Definitions.ObjectClass => obj
case _ =>
val throwError = {
genCallHelper("throwClassCastException",
obj,
js.StringLiteral(displayName))
}
if (className == RuntimeNothingClass) {
// Always throw for .asInstanceOf[Nothing], even for null
throwError
} else {
js.If(js.Apply(envField("is", className), List(obj)) ||
(obj === js.Null()), {
obj
}, {
throwError
})
}
})))
}
js.Skip()
} else {
envFieldDef(
"as",
className,
js.Function(
List(objParam),
js.Return(className match {
case Definitions.ObjectClass => obj
case _ =>
val throwError = {
genCallHelper("throwClassCastException",
obj,
js.StringLiteral(displayName))
}
if (className == RuntimeNothingClass) {
// Always throw for .asInstanceOf[Nothing], even for null
throwError
} else {
js.If(js.Apply(envField("is", className), List(obj)) ||
(obj === js.Null()), {
obj
}, {
throwError
})
}
})))
}
<<< processOptions
def processOptions(): Unit = {
if (option.startsWith("relSourceMap:")) {
Expand Down
107 changes: 107 additions & 0 deletions scalafmt-tests/src/test/resources/default/If.stat
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,110 @@ else
CustomHttp.okhttpClient.value
)
}
<<< #1747 1: one line with else
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 = if (a > 10) { Some(a) } else { None }
}
>>>
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 = if (a > 10) { Some(a) }
else { None }
}
<<< #1747 2: one line without else
object a {
val ok1 = if (a > 10) Some(a)
val ok2 = if (a > 10) { Some(a) }
}
>>>
object a {
val ok1 = if (a > 10) Some(a)
val ok2 = if (a > 10) { Some(a) }
}
<<< #1747 3: split on then with else
object a {
val ok1 = if (a > 10)
Some(a)
else
None
val ok2 = if (a > 10) {
Some(a)
} else {
None
}
}
>>>
object a {
val ok1 = if (a > 10)
Some(a)
else
None
val ok2 = if (a > 10) {
Some(a)
} else {
None
}
}
<<< #1747 4: split on else
object a {
val ok1 = if (a > 10) Some(a) else
None
val ok2 = if (a > 10) { Some(a) } else {
None
}
}
>>>
object a {
val ok1 = if (a > 10) Some(a)
else
None
val ok2 = if (a > 10) { Some(a) }
else {
None
}
}
<<< #1747 5: split on then without else
object a {
val ok1 = if (a > 10)
Some(a)
val ok2 = if (a > 10) {
Some(a) }
}
>>>
object a {
val ok1 = if (a > 10)
Some(a)
val ok2 = if (a > 10) {
Some(a)
}
}
<<< #1747 6: split on if
object a {
val ok1 =
if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) } else { None }
}
>>>
object a {
val ok1 =
if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) }
else { None }
}
<<< #1747 7: split on if without else
object a {
val ok1 =
if (a > 10) Some(a)
val ok2 =
if (a > 10) { Some(a) }
}
>>>
object a {
val ok1 =
if (a > 10) Some(a)
val ok2 =
if (a > 10) { Some(a) }
}
32 changes: 16 additions & 16 deletions scalafmt-tests/src/test/resources/default/Val.stat
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ val wasBOM = if (endianness == AutoEndian) {
} else false
>>>
val wasBOM = if (endianness == AutoEndian) {
// Read BOM
if (b1 == 0xfe && b2 == 0xff) {
endianness = BigEndian
true
} else if (b1 == 0xff && b2 == 0xfe) {
endianness = LittleEndian
true
} else {
// Not a valid BOM: default to BigEndian and start reading
endianness = BigEndian
false
}
} else false
// Read BOM
if (b1 == 0xfe && b2 == 0xff) {
endianness = BigEndian
true
} else if (b1 == 0xff && b2 == 0xfe) {
endianness = LittleEndian
true
} else {
// Not a valid BOM: default to BigEndian and start reading
endianness = BigEndian
false
}
} else false
<<< #269 3
val route =
scheme("https") { completeOk } ~
Expand Down Expand Up @@ -141,9 +141,9 @@ val predefinedType = if (clazz.getTypeParameters.length == 1) {
ScDesignatorType(clazz)
>>>
val predefinedType = if (clazz.getTypeParameters.length == 1) {
ScParameterizedType(ScDesignatorType(clazz), undefines)
} else
ScDesignatorType(clazz)
ScParameterizedType(ScDesignatorType(clazz), undefines)
} else
ScDesignatorType(clazz)
<<< myRoute
val myRoute =
path("") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ forAll(table)((reqProto, headReq, reqCH, resProto, resCH, resCD, renCH, close)
response = HttpResponse(
200,
headers = resCH.toList,
entity =
if (resCD)
entity = if (resCD)
HttpEntity.CloseDelimited(
ContentTypes.`text/plain(UTF-8)`,
Source.single(ByteString("ENTITY"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ object a {
>>>
object a {
val select = if (max) { (a: CTuple, b: CTuple) => (a.compareTo(b) >= 0) }
else { (a: CTuple, b: CTuple) => (a.compareTo(b) <= 0) }
else { (a: CTuple, b: CTuple) => (a.compareTo(b) <= 0) }
}
<<< #1633 2.2: function block in a val statement
object a {
Expand Down
4 changes: 2 additions & 2 deletions scalafmt-tests/src/test/resources/unit/If.stat
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
val newColumn = if(split == Newline) newIndent
else column + split.length
>>>
val newColumn =
if (split == Newline) newIndent
val newColumn = if (split == Newline)
newIndent
else column + split.length
<<< Egyptian curlies
if (optimalAt.contains(tok.left)) {
Expand Down