feat(KT-51123): save comments from js-function call inside arguments list.

This commit is contained in:
Artem Kobzar
2022-06-13 12:58:18 +00:00
committed by Space
parent ec9d929532
commit e790607af5
19 changed files with 1858 additions and 172 deletions
+9
View File
@@ -47,6 +47,8 @@ message Expression {
optional bool synthetic = 3 [default = false];
optional SideEffects side_effects = 4 [default = AFFECTS_STATE];
optional JsImportedModule local_alias = 5;
repeated Comment before_comments = 6;
repeated Comment after_comments = 7;
oneof expression {
int32 simple_name_reference = 22;
@@ -128,6 +130,11 @@ message DocCommentTag {
}
}
message Comment {
required string text = 1;
required bool multiline = 2;
}
message BinaryOperation {
required Expression left = 1;
required Expression right = 2;
@@ -233,6 +240,8 @@ message Statement {
optional int32 fileId = 1;
optional Location location = 2;
optional bool synthetic = 3 [default = false];
repeated Comment before_comments = 4;
repeated Comment after_comments = 5;
oneof statement {
Return return_statement = 21;
@@ -28,7 +28,19 @@ abstract class JsAstDeserializerBase {
return statement
}
protected fun deserializeNoMetadata(proto: JsAstProtoBuf.Statement): JsStatement = when (proto.statementCase) {
protected fun deserializeNoMetadata(proto: JsAstProtoBuf.Statement): JsStatement {
return deserializeNoMetadataHelper(proto).apply {
if (proto.beforeCommentsCount != 0) {
commentsBeforeNode = proto.beforeCommentsList.map(::deserializeComment)
}
if (proto.afterCommentsCount != 0) {
commentsAfterNode = proto.afterCommentsList.map(::deserializeComment)
}
}
}
protected fun deserializeNoMetadataHelper(proto: JsAstProtoBuf.Statement): JsStatement = when (proto.statementCase) {
JsAstProtoBuf.Statement.StatementCase.RETURN_STATEMENT -> {
val returnProto = proto.returnStatement
JsReturn(if (returnProto.hasValue()) deserialize(returnProto.value) else null)
@@ -192,7 +204,19 @@ abstract class JsAstDeserializerBase {
)
}
protected fun deserializeNoMetadata(proto: JsAstProtoBuf.Expression): JsExpression = when (proto.expressionCase) {
protected fun deserializeNoMetadata(proto: JsAstProtoBuf.Expression): JsExpression {
return deserializeNoMetadataHelper(proto).apply {
if (proto.beforeCommentsCount != 0) {
commentsBeforeNode = proto.beforeCommentsList.map(::deserializeComment)
}
if (proto.afterCommentsCount != 0) {
commentsAfterNode = proto.afterCommentsList.map(::deserializeComment)
}
}
}
protected fun deserializeNoMetadataHelper(proto: JsAstProtoBuf.Expression): JsExpression = when (proto.expressionCase) {
JsAstProtoBuf.Expression.ExpressionCase.THIS_LITERAL -> JsThisRef()
JsAstProtoBuf.Expression.ExpressionCase.NULL_LITERAL -> JsNullLiteral()
JsAstProtoBuf.Expression.ExpressionCase.TRUE_LITERAL -> JsBooleanLiteral(true)
@@ -480,5 +504,13 @@ abstract class JsAstDeserializerBase {
return node
}
protected fun deserializeComment(comment: JsAstProtoBuf.Comment): JsComment {
return if (comment.multiline) {
JsMultiLineComment(comment.text)
} else {
JsSingleLineComment(comment.text)
}
}
protected abstract fun embedSources(deserializedLocation: JsLocation, file: String): JsLocationWithSource?
}
@@ -174,8 +174,10 @@ abstract class JsAstSerializerBase {
}
}
withLocation(statement, { visitor.builder.fileId = it }, { visitor.builder.location = it }) {
statement.accept(visitor)
withComments(statement, { visitor.builder.addBeforeComments(it) }, { visitor.builder.addAfterComments(it) }) {
withLocation(statement, { visitor.builder.fileId = it }, { visitor.builder.location = it }) {
statement.accept(visitor)
}
}
if (statement is HasMetadata && statement.synthetic) {
@@ -348,8 +350,10 @@ abstract class JsAstSerializerBase {
}
}
withLocation(expression, { visitor.builder.fileId = it }, { visitor.builder.location = it }) {
expression.accept(visitor)
withComments(expression, { visitor.builder.addBeforeComments(it) }, { visitor.builder.addAfterComments(it) }) {
withLocation(expression, { visitor.builder.fileId = it }, { visitor.builder.location = it }) {
expression.accept(visitor)
}
}
with(visitor.builder) {
@@ -522,6 +526,18 @@ abstract class JsAstSerializerBase {
result
}
protected fun serialize(comment: JsComment): JsAstProtoBuf.Comment {
val builder = JsAstProtoBuf.Comment.newBuilder().apply {
text = comment.text
multiline = when (comment) {
is JsSingleLineComment -> false
is JsMultiLineComment -> true
else -> error("Unknown type of comment ${comment.javaClass.name}")
}
}
return builder.build()
}
private inline fun withLocation(node: JsNode, fileConsumer: (Int) -> Unit, locationConsumer: (JsAstProtoBuf.Location) -> Unit, inner: () -> Unit) {
val location = extractLocation(node)
var fileChanged = false
@@ -546,5 +562,16 @@ abstract class JsAstSerializerBase {
}
}
private inline fun withComments(
node: JsNode,
beforeCommentsConsumer: (JsAstProtoBuf.Comment) -> Unit,
afterCommentsConsumer: (JsAstProtoBuf.Comment) -> Unit,
inner: () -> Unit
) {
node.commentsBeforeNode?.forEach { beforeCommentsConsumer(serialize(it))}
node.commentsAfterNode?.forEach { afterCommentsConsumer(serialize(it))}
inner()
}
abstract fun extractLocation(node: JsNode): JsLocation?
}