|
| 1 | +package org.scalajs.dom.scalafix |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets |
| 4 | +import java.nio.file.{Paths, Files} |
| 5 | +import scala.meta._ |
| 6 | +import scalafix.v1._ |
| 7 | + |
| 8 | +class GenerateApiReport extends SemanticRule("GenerateApiReport") { |
| 9 | + import MutableState.{global => state, ScopeType} |
| 10 | + |
| 11 | + private[this] def enabled = state ne null |
| 12 | + |
| 13 | + override def beforeStart(): Unit = { |
| 14 | + Util.scalaSeriesVer match { |
| 15 | + case "2.11" => // disabled - can't read classfiles |
| 16 | + case _ => MutableState.global = new MutableState // can't set state= in early Scala versions |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + override def fix(implicit doc: SemanticDocument): Patch = { |
| 21 | + |
| 22 | + if (enabled) |
| 23 | + doc.tree.traverse { |
| 24 | + case a: Defn.Class => process(a.symbol, a.templ, ScopeType.Class) |
| 25 | + case a: Defn.Object => process(a.symbol, a.templ, ScopeType.Object) |
| 26 | + case a: Defn.Trait => process(a.symbol, a.templ, ScopeType.Trait) |
| 27 | + case a: Pkg.Object => process(a.symbol, a.templ, ScopeType.Object) |
| 28 | + case _ => |
| 29 | + } |
| 30 | + |
| 31 | + Patch.empty |
| 32 | + } |
| 33 | + |
| 34 | + private def process(sym: Symbol, body: Template, typ: ScopeType)(implicit doc: SemanticDocument): Unit = { |
| 35 | + // Skip non-public scopes |
| 36 | + val info = sym.info.get |
| 37 | + if (!info.isPublic && !info.isPackageObject) |
| 38 | + return |
| 39 | + |
| 40 | + val parents = Util.parents(sym).iterator.map(Util.typeSymbol).toList |
| 41 | + val domParents = parents.iterator.filter(isScalaJsDom).toSet |
| 42 | + val isJsType = parents.exists(isScalaJs) |
| 43 | + val s = state.register(sym, isJsType, typ, domParents) |
| 44 | + |
| 45 | + def letsSeeHowLazyWeCanBeLol(t: Tree): Unit = { |
| 46 | + // Skip non-public members |
| 47 | + if (!t.symbol.info.get.isPublic) |
| 48 | + return |
| 49 | + |
| 50 | + // Remove definition bodies |
| 51 | + val t2: Tree = |
| 52 | + t match { |
| 53 | + case Defn.Def(mods, name, tparams, paramss, Some(tpe), _) => Decl.Def(mods, name, tparams, paramss, tpe) |
| 54 | + case Defn.Val(mods, pats, Some(tpe), _) => Decl.Val(mods, pats, tpe) |
| 55 | + case Defn.Var(mods, pats, Some(tpe), _) => Decl.Var(mods, pats, tpe) |
| 56 | + case _ => t |
| 57 | + } |
| 58 | + |
| 59 | + val desc = |
| 60 | + t2 |
| 61 | + .toString |
| 62 | + .replace('\n', ' ') |
| 63 | + .replace("=", " = ") |
| 64 | + .replace("@inline ", "") |
| 65 | + .replaceAll(", *", ", ") |
| 66 | + .replaceAll(" {2,}", " ") |
| 67 | + .trim |
| 68 | + .stripSuffix(" = js.native") |
| 69 | + .replaceAll(" = js.native(?=[^\n])", "?") |
| 70 | + |
| 71 | + s.add(desc) |
| 72 | + } |
| 73 | + |
| 74 | + body.traverse { |
| 75 | + |
| 76 | + // Skip inner members that we collect at the outer scope |
| 77 | + case _: Defn.Class => |
| 78 | + case _: Defn.Object => |
| 79 | + case _: Defn.Trait => |
| 80 | + case _: Pkg.Object => |
| 81 | + |
| 82 | + case d: Decl => letsSeeHowLazyWeCanBeLol(d) |
| 83 | + case d: Defn => letsSeeHowLazyWeCanBeLol(d) |
| 84 | + |
| 85 | + case _ => |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private def isScalaJs(sym: Symbol): Boolean = |
| 90 | + sym.toString startsWith "scala/scalajs/js/" |
| 91 | + |
| 92 | + private def isScalaJsDom(sym: Symbol): Boolean = |
| 93 | + sym.toString startsWith "org/scalajs/dom/" |
| 94 | + |
| 95 | + override def afterComplete(): Unit = |
| 96 | + if (enabled) { |
| 97 | + saveReport() |
| 98 | + MutableState.global = null // can't set state= in early Scala versions |
| 99 | + } |
| 100 | + |
| 101 | + private def saveReport(): Unit = { |
| 102 | + val scalaVer = Util.scalaSeriesVer.replace('.', '_') |
| 103 | + val projectRoot = System.getProperty("user.dir") |
| 104 | + val reportFile = Paths.get(s"$projectRoot/api-reports/$scalaVer.txt") |
| 105 | + val api = state.result().iterator.map(_.stripPrefix("org/scalajs/dom/")).mkString("\n") |
| 106 | + |
| 107 | + val content = |
| 108 | + s"""|scala-js-dom API |
| 109 | + |================ |
| 110 | + | |
| 111 | + |This is generated automatically on compile via custom Scalafix rule '${name.value}'. |
| 112 | + | |
| 113 | + |Flags: |
| 114 | + | [J-] = JavaScript type |
| 115 | + | [S-] = Scala type |
| 116 | + | [-${ScopeType.Class.id}] = Class |
| 117 | + | [-${ScopeType.Trait.id}] = Trait |
| 118 | + | [-${ScopeType.Object.id}] = Object |
| 119 | + | |
| 120 | + | |
| 121 | + |$api |
| 122 | + |""".stripMargin |
| 123 | + |
| 124 | + println(s"[info] Generating $reportFile") |
| 125 | + Files.write(reportFile, content.getBytes(StandardCharsets.UTF_8)) |
| 126 | + } |
| 127 | +} |
0 commit comments