8000 Merge pull request #4923 from gzm0/no-doc-comment · scala-js/scala-js@51a363e · GitHub
[go: up one dir, main page]

Skip to content

Commit 51a363e

Browse files
authored
Merge pull request #4923 from gzm0/no-doc-comment
Replace DocComment tree with dedicated JSDocConstructor tree
2 parents 586966e + 4b52fa0 commit 51a363e

File tree

5 files changed

+36
-71
lines changed

5 files changed

+36
-71
lines changed

linker/jvm/src/main/scala/org/scalajs/linker/backend/closure/ClosureAstTransformer.scala

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ private class ClosureAstTransformer(featureSet: FeatureSet,
4343

4444
def transformScript(topLevelTrees: List[Tree]): Node = {
4545
val script = setNodePosition(new Node(Token.SCRIPT), NoPosition)
46-
transformBlockStats(topLevelTrees)(NoPosition).foreach(script.addChildToBack(_))
46+
for (stat <- topLevelTrees)
47+
script.addChildToBack(transformStat(stat)(NoPosition))
4748
script.putProp(Node.FEATURE_SET, featureSet)
4849
script
4950
}
@@ -55,6 +56,20 @@ private class ClosureAstTransformer(featureSet: FeatureSet,
5556
implicit val pos = pos_in
5657

5758
wrapTransform(tree) {
59+
case JSDocConstructor(tree) =>
60+
val node = transformStat(tree)
61+
// The @constructor must be propagated through an ExprResult node
62+
val trg =
63+
if (node.isExprResult()) node.getChildAtIndex(0)
64+
else node
65+
val ctorDoc = {
66+
val b = JSDocInfo.builder()
67+
b.recordConstructor()
68+
b.build()
69+
}
70+
trg.setJSDocInfo(ctorDoc)
71+
node
72+
5873
case VarDef(ident, optRhs) =>
5974
val node = transformName(ident)
6075
optRhs.foreach(rhs => node.addChildToFront(transformExpr(rhs)))
@@ -448,45 +463,11 @@ private class ClosureAstTransformer(featureSet: FeatureSet,
448463

449464
def transformBlock(stats: List[Tree], blockPos: Position): Node = {
450465
val block = new Node(Token.BLOCK)
451-
for (node <- transformBlockStats(stats)(blockPos))
452-
block.addChildToBack(node)
466+
for (stat <- stats)
467+
block.addChildToBack(transformStat(stat)(blockPos))
453468
block
454469
}
455470

456-
def transformBlockStats(stats: List[Tree])(
457-
implicit parentPos: Position): List[Node] = {
458-
459-
@inline def ctorDoc(): JSDocInfo = {
460-
val b = JSDocInfo.builder()
461-
b.recordConstructor()
462-
b.build()
463-
}
464-
465-
// The Rhino IR attaches DocComments to the following nodes (rather than
466-
// having individual nodes). We preprocess these here.
467-
@tailrec
468-
def loop(ts: List[Tree], nextIsCtor: Boolean, acc: List[Node]): List[Node] = ts match {
469-
case DocComment(text) :: tss =>
470-
loop(tss, nextIsCtor = text.startsWith("@constructor"), acc)
471-
472-
case t :: tss =>
473-
val node = transformStat(t)
474-
if (nextIsCtor) {
475-
// The @constructor must be propagated through an ExprResult node
476-
val trg =
477-
if (node.isExprResult()) node.getChildAtIndex(0)
478-
else node
479-
trg.setJSDocInfo(ctorDoc())
480-
}
481-
loop(tss, nextIsCtor = false, node :: acc)
482-
483-
case Nil =>
484-
acc.reverse
485-
}
486-
487-
loop(stats, nextIsCtor = false, Nil)
488-
}
489-
490471
@inline
491472
private def wrapTransform(tree: Tree)(body: Tree => Node)(
492473
implicit pos: Position): Node = {

linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/ClassEmitter.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
202202
} yield {
203203
(
204204
// Real constructor
205-
js.DocComment("@constructor") ::
206-
realCtorDef :::
205+
js.JSDocConstructor(realCtorDef.head) ::
206+
realCtorDef.tail :::
207207
chainProto :::
208208
(genIdentBracketSelect(ctorVar.prototype, "constructor") := ctorVar) ::
209209

210210
// Inheritable constructor
211-
js.DocComment("@constructor") ::
212-
inheritableCtorDef :::
211+
js.JSDocConstructor(inheritableCtorDef.head) ::
212+
inheritableCtorDef.tail :::
213213
(globalVar(VarField.h, className).prototype := ctorVar.prototype) :: Nil
214214
)
215215
}
@@ -239,8 +239,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
239239

240240
val ctorVar = fileLevelVar(VarField.b, genName(className))
241241

242-
js.DocComment("@constructor") ::
243-
(ctorVar := ctorFun) ::
242+
js.JSDocConstructor(ctorVar := ctorFun) ::
244243
chainPrototypeWithLocalCtor(className, ctorVar, superCtor) :::
245244
(genIdentBracketSelect(ctorVar.prototype, "constructor") := ctorVar) :: Nil
246245
}
@@ -340,8 +339,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
340339
val dummyCtor = fileLevelVar(VarField.hh, genName(className))
341340

342341
List(
343-
js.DocComment("@constructor"),
344-
genConst(dummyCtor.ident, js.Function(false, Nil, None, js.Skip())),
342+
js.JSDocConstructor(genConst(dummyCtor.ident, js.Function(false, Nil, None, js.Skip()))),
345343
dummyCtor.prototype := superCtor.prototype,
346344
ctorVar.prototype := js.New(dummyCtor, Nil)
347345
)

linker/shared/src/main/scala/org/scalajs/linker/backend/javascript/Printers.scala

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,11 @@ object Printers {
139139
}
140140

141141
tree match {
142-
// Comments
143-
144-
case DocComment(text) =>
145-
val lines = text.split("\n").toList
146-
if (lines.tail.isEmpty) {
147-
print("/** ")
148-
print(lines.head)
149-
print(" */")
150-
} else {
151-
print("/** ")
152-
print(lines.head)
153-
println(); printIndent()
154-
var rest = lines.tail
155-
while (rest.nonEmpty) {
156-
print(" * ")
157-
print(rest.head)
158-
println(); printIndent()
159-
rest = rest.tail
160-
}
161-
print(" */")
162-
}
142+
case JSDocConstructor(tree) =>
143+
print("/** @constructor */")
144+
println(); printIndent()
145+
// not printStat: we must not print the trailing newline.
146+
printTree(tree, isStat = true)
163147

164148
// Definitions
165149

linker/shared/src/main/scala/org/scalajs/linker/backend/javascript/Trees.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ object Trees {
3939
}
4040
}
4141

42-
// Comments
42+
// Constructor comment / annotation.
4343

44-
sealed case class DocComment(text: String)(implicit val pos: Position)
44+
sealed case class JSDocConstructor(tree: Tree)(implicit val pos: Position)
4545
extends Tree
4646

4747
// Identifiers and properties

linker/shared/src/test/scala/org/scalajs/linker/backend/javascript/PrintersTest.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ class PrintersTest {
8989
)
9090
}
9191

92-
@Test def printDocComment(): Unit = {
92+
@Test def printJSDocConstructor(): Unit = {
9393
assertPrintEquals(
9494
"""
95-
| /** test */
95+
|/** @constructor */
96+
|ctor = (function() {
97+
|});
9698
""",
97-
DocComment("test")
99+
JSDocConstructor(Assign(VarRef("ctor"), Function(false, Nil, None, Skip())))
98100
)
99101
}
100102

0 commit comments

Comments
 (0)
0