Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Replace DocComment tree with dedicated JSDocConstructor tree #4923

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

Merged
merged 1 commit into from
Jan 29, 2024
Merged
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 @@ -43,7 +43,8 @@ private class ClosureAstTransformer(featureSet: FeatureSet,

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

wrapTransform(tree) {
case JSDocConstructor(tree) =>
val node = transformStat(tree)
// The @constructor must be propagated through an ExprResult node
val trg =
if (node.isExprResult()) node.getChildAtIndex(0)
else node
val ctorDoc = {
val b = JSDocInfo.builder()
b.recordConstructor()
b.build()
}
trg.setJSDocInfo(ctorDoc)
node

case VarDef(ident, optRhs) =>
val node = transformName(ident)
optRhs.foreach(rhs => node.addChildToFront(transformExpr(rhs)))
Expand Down Expand Up @@ -448,45 +463,11 @@ private class ClosureAstTransformer(featureSet: FeatureSet,

def transformBlock(stats: List[Tree], blockPos: Position): Node = {
val block = new Node(Token.BLOCK)
for (node <- transformBlockStats(stats)(blockPos))
block.addChildToBack(node)
for (stat <- stats)
block.addChildToBack(transformStat(stat)(blockPos))
block
}

def transformBlockStats(stats: List[Tree])(
implicit parentPos: Position): List[Node] = {

@inline def ctorDoc(): JSDocInfo = {
val b = JSDocInfo.builder()
b.recordConstructor()
b.build()
}

// The Rhino IR attaches DocComments to the following nodes (rather than
// having individual nodes). We preprocess these here.
@tailrec
def loop(ts: List[Tree], nextIsCtor: Boolean, acc: List[Node]): List[Node] = ts match {
case DocComment(text) :: tss =>
loop(tss, nextIsCtor = text.startsWith("@constructor"), acc)

case t :: tss =>
val node = transformStat(t)
if (nextIsCtor) {
// The @constructor must be propagated through an ExprResult node
val trg =
if (node.isExprResult()) node.getChildAtIndex(0)
else node
trg.setJSDocInfo(ctorDoc())
}
loop(tss, nextIsCtor = false, node :: acc)

case Nil =>
acc.reverse
}

loop(stats, nextIsCtor = false, Nil)
}

@inline
private def wrapTransform(tree: Tree)(body: Tree => Node)(
implicit pos: Position): Node = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
} yield {
(
// Real constructor
js.DocComment("@constructor") ::
realCtorDef :::
js.JSDocConstructor(realCtorDef.head) ::
realCtorDef.tail :::
chainProto :::
(genIdentBracketSelect(ctorVar.prototype, "constructor") := ctorVar) ::

// Inheritable constructor
js.DocComment("@constructor") ::
inheritableCtorDef :::
js.JSDocConstructor(inheritableCtorDef.head) ::
inheritableCtorDef.tail :::
(globalVar(VarField.h, className).prototype := ctorVar.prototype) :: Nil
)
}
Expand Down Expand Up @@ -251,8 +251,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {

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

js.DocComment("@constructor") ::
(ctorVar := ctorFun) ::
js.JSDocConstructor(ctorVar := ctorFun) ::
chainPrototypeWithLocalCtor(className, ctorVar, superCtor) :::
(genIdentBracketSelect(ctorVar.prototype, "constructor") := ctorVar) :: Nil
}
Expand Down Expand Up @@ -344,8 +343,7 @@ private[emitter] final class ClassEmitter(sjsGen: SJSGen) {
val dummyCtor = fileLevelVar(VarField.hh, genName(className))

List(
js.DocComment("@constructor"),
genConst(dummyCtor.ident, js.Function(false, Nil, None, js.Skip())),
js.JSDocConstructor(genConst(dummyCtor.ident, js.Function(false, Nil, None, js.Skip()))),
dummyCtor.prototype := superCtor.prototype,
ctorVar.prototype := js.New(dummyCtor, Nil)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,11 @@ object Printers {
}

tree match {
// Comments

case DocComment(text) =>
val lines = text.split("\n").toList
if (lines.tail.isEmpty) {
print("/** ")
print(lines.head)
print(" */")
} else {
print("/** ")
print(lines.head)
println(); printIndent()
var rest = lines.tail
while (rest.nonEmpty) {
print(" * ")
print(rest.head)
println(); printIndent()
rest = rest.tail
}
print(" */")
}
case JSDocConstructor(tree) =>
print("/** @constructor */")
println(); printIndent()
// not printStat: we must not print the trailing newline.
printTree(tree, isStat = true)

// Definitions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ object Trees {
}
}

// Comments
// Constructor comment / annotation.

sealed case class DocComment(text: String)(implicit val pos: Position)
sealed case class JSDocConstructor(tree: Tree)(implicit val pos: Position)
extends Tree

// Identifiers and properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ class PrintersTest {
)
}

@Test def printDocComment(): Unit = {
@Test def printJSDocConstructor(): Unit = {
assertPrintEquals(
"""
| /** test */
|/** @constructor */
|ctor = (function() {
|});
""",
DocComment("test")
JSDocConstructor(Assign(VarRef("ctor"), Function(false, Nil, None, Skip())))
)
}

Expand Down