@@ -81,15 +81,17 @@ private[internals] object Renderer {
81
81
def apply (unit : CompilationUnit ): List [Result ] = {
82
82
val r = new Renderer (unit)
83
83
84
- val pack = Result (
85
- unit.namespace,
86
- " package" ,
87
- r.renderPackageContents.list
88
- .map(_.segments.toList.map(_.show).mkString)
89
- .mkString(
90
- System .lineSeparator()
91
- )
92
- )
84
+ val packageObjectFile = r.renderPackageContents.map { packageLines =>
85
+ Result (
86
+ unit.namespace,
87
+ " package" ,
88
+ packageLines.list
89
+ .map(_.segments.toList.map(_.show).mkString)
90
+ .mkString(
91
+ System .lineSeparator()
92
+ )
93
+ )
94
+ }
93
95
94
96
val classes = unit.declarations.map { decl =>
95
97
val renderResult = r.renderDecl(decl) ++ newline
@@ -161,13 +163,7 @@ private[internals] object Renderer {
161
163
Result (unit.namespace, decl.name, content)
162
164
}
163
165
164
- val packageApplicableDecls = unit.declarations.filter {
165
- case _ : TypeAlias | _ : Service => true
166
- case _ => false
167
- }
168
-
169
- if (packageApplicableDecls.isEmpty) classes
170
- else pack :: classes
166
+ packageObjectFile.toList ::: classes
171
167
}
172
168
173
169
}
@@ -271,7 +267,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self =>
271
267
}
272
268
}
273
269
274
- def renderPackageContents : Lines = {
270
+ def renderPackageContents : Option [ Lines ] = {
275
271
val typeAliases = compilationUnit.declarations
276
272
.collect {
277
273
case TypeAlias (_, name, _, _, _, hints) =>
@@ -288,24 +284,31 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self =>
288
284
)
289
285
}
290
286
291
- val blk =
292
- block(
293
- line " package object ${compilationUnit.namespace.split('.' ).last}"
294
- )(
295
- compilationUnit.declarations.map(renderDeclPackageContents),
296
- newline,
297
- typeAliases,
298
- newline
299
- )
287
+ val serviceAliases =
288
+ compilationUnit.declarations.map(renderDeclPackageContents)
300
289
301
- val parts = compilationUnit.namespace.split('.' ).filter(_.nonEmpty)
302
- if (parts.size > 1 ) {
303
- lines(
304
- line " package ${parts.dropRight(1 ).mkString(" ." )}" ,
305
- newline,
306
- blk
307
- )
308
- } else blk
290
+ val packageContents = lines(serviceAliases, newline, typeAliases, newline)
291
+
292
+ packageContents.some
293
+ .filterNot(_.isBlank)
294
+ .map { contents =>
295
+ block(
296
+ line " package object ${compilationUnit.namespace.split('.' ).last}"
297
+ )(
298
+ contents
299
+ )
300
+ }
301
+ .map { blk =>
302
+ val parts = compilationUnit.namespace.split('.' ).filter(_.nonEmpty)
303
+
304
+ if (parts.size > 1 ) {
305
+ lines(
306
+ line " package ${parts.dropRight(1 ).mkString(" ." )}" ,
307
+ newline,
308
+ blk
309
+ )
310
+ } else blk
311
+ }
309
312
}
310
313
311
314
private def renderDeclPackageContents (decl : Decl ): Lines = decl match {
0 commit comments