forked from scala-ts/scala-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use less specific
val
type, sort types by references in code rather…
… than by traversing types.
- Loading branch information
Showing
6 changed files
with
41 additions
and
213 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,49 @@ | ||
package scalats | ||
|
||
import cats.Eval | ||
import scala.util.matching.Regex | ||
|
||
/** | ||
* A helper to sort generated types, see [[scalats.TypeSorter.sort]] | ||
*/ | ||
object TypeSorter { | ||
private case class GeneratedWithIndex(gen: Generated, index: Int) | ||
|
||
private val constRx = """(?:const ([^\s]+))""".r | ||
private val functionRx = """(?:function ([^\s<\(]+))""".r | ||
private val typeRx = """(?:type ([^\s<]+))""".r | ||
|
||
private def extractNames(gen: GeneratedWithIndex, rx: Regex): Map[String, GeneratedWithIndex] = | ||
rx.findAllMatchIn(gen.gen.code).toList.map(_.group(1)).map((_, gen)).toMap | ||
|
||
/** | ||
* Sort a set of generated types based on references between them | ||
* | ||
* @param types The generated types | ||
* @params refs The mapping of type to other types it refers to | ||
* @return A list of generated code sorted properly | ||
*/ | ||
def sort(types: List[(Option[TypeName], Generated)], refs: ReferencedTypes): List[Generated] = { | ||
val all = types.flatMap { case (typeName, generated) => typeName.map(n => (n.full, generated)) }.toMap | ||
def sort(types: List[(Option[TypeName], Generated)]): List[Generated] = { | ||
val gwis = types.zipWithIndex.map { case ((_, gen), idx) => GeneratedWithIndex(gen, idx) } | ||
val definitions = gwis.flatMap(gen => | ||
extractNames(gen, constRx) ++ extractNames(gen, functionRx) ++ extractNames(gen, typeRx) | ||
).toMap | ||
// Names can be referenced either as `foo` or `importedN_foo` where `N` is a number | ||
val definitionNamesRx = ("""(?:imported\d+_|\b)(""" ++ | ||
definitions.keys.map(name => Regex.quote(name) ++ "\\b").mkString("|") ++ | ||
")").r | ||
|
||
def addType( | ||
acc: Eval[(List[Generated], Set[String])], | ||
typeName: TypeName, | ||
generated: Generated, | ||
): Eval[(List[Generated], Set[String])] = | ||
acc.flatMap { case (accGen, accSkip) => | ||
if (accSkip.contains(typeName.full)) | ||
Eval.now((accGen, accSkip)) | ||
else | ||
refs.get(typeName) match { | ||
case Some(types) => | ||
types.foldLeft(Eval.now((accGen, accSkip + typeName.full)))( | ||
(acc, ref) => all.get(ref.full).fold(acc)(addType(acc, ref, _)) | ||
).map { case (x, y) => (x :+ generated, y) } | ||
case None => | ||
Eval.now((accGen :+ generated, accSkip + typeName.full)) | ||
} | ||
def addGenerated(acc: Eval[(List[Generated], Set[Int])], gen: GeneratedWithIndex): Eval[(List[Generated], Set[Int])] = | ||
acc.flatMap { case acc @ (accGen, accSkip) => | ||
if (accSkip.contains(gen.index)) | ||
Eval.now(acc) | ||
else { | ||
val refNames = definitionNamesRx.findAllMatchIn(gen.gen.code).map(_.group(1)).toSet | ||
refNames.foldLeft(Eval.now((accGen, accSkip + gen.index)))( | ||
(acc, refName) => definitions.get(refName).fold(acc)(addGenerated(acc, _)) | ||
).map { case (x, y) => (x :+ gen.gen, y) } | ||
} | ||
} | ||
|
||
types.foldLeft(Eval.now(List.empty[Generated], Set.empty[String])) { | ||
case (acc, (Some(typeName), generated)) => addType(acc, typeName, generated) | ||
case (acc, (None, generated)) => acc.map { case (accGen, accSkip) => (accGen :+ generated, accSkip) } | ||
}.value._1 | ||
gwis.foldLeft(Eval.now(List.empty[Generated], Set.empty[Int]))(addGenerated).value._1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters