-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
788497c
commit 91f0f47
Showing
9 changed files
with
160 additions
and
100 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package java2typescript.transformer | ||
|
||
import com.github.javaparser.ast.Modifier.Keyword | ||
import com.github.javaparser.ast.body.{BodyDeclaration, EnumDeclaration, FieldDeclaration, MethodDeclaration} | ||
import java2typescript.ast | ||
|
||
import scala.jdk.CollectionConverters.* | ||
import scala.jdk.OptionConverters.* | ||
|
||
def transformEnumDeclaration( | ||
context: FileContext|ClassContext, | ||
decl: EnumDeclaration, | ||
additionalModifiers: List[ast.Modifier] = List() | ||
): List[ast.Statement] = | ||
val enumMembers = decl.getEntries.asScala.map(e => ast.EnumMember(transformName(e.getName))).toList | ||
val classContext = context match | ||
case c: FileContext => ClassContext(c, Some(decl)) | ||
case c: ClassContext => ClassContext(c, Some(decl), Option(c)) | ||
|
||
val otherMembers = decl.getMembers.asScala | ||
.flatMap(transformOtherEnumMember.curried(classContext)) | ||
|
||
List(ast.EnumDeclaration( | ||
transformName(decl.getName), | ||
members = enumMembers, | ||
additionalModifiers | ||
)) ::: otherMembers.toList | ||
|
||
def transformOtherEnumMember(context: ClassContext, member: BodyDeclaration[?]): List[ast.Statement] = | ||
member match | ||
case member: FieldDeclaration => | ||
member.getVariables.asScala.toList.map(declarator => | ||
ast.VariableStatement(ast.VariableDeclarationList(List(transformDeclaratorToVariable(context, declarator)))) | ||
) | ||
case member: MethodDeclaration => | ||
List(transformEnumMethodDeclaration(context, member)) | ||
|
||
def transformEnumMethodDeclaration(context: ClassContext, decl: MethodDeclaration): ast.Statement = | ||
val methodParameters = decl.getParameters.asScala.map(transformParameter.curried(context)).toList | ||
val methodContext = ParameterContext(context, methodParameters.toBuffer) | ||
val methodBody = decl.getBody.toScala.map(body => | ||
ast.Block(body.getStatements.asScala.map(transformStatement.curried(methodContext)).toList) | ||
) | ||
val methodModifiers = if (decl.getModifiers.asScala.exists(m => m.getKeyword == Keyword.PUBLIC)) | ||
List(ast.ExportKeyword()) | ||
else | ||
List() | ||
|
||
ast.FunctionDeclaration( | ||
transformName(decl.getName), | ||
`type` = transformType(context, decl.getType), | ||
typeParameters = decl.getTypeParameters.asScala.map(transformType.curried(context)).map { | ||
t => t.get | ||
}.toList, | ||
parameters = methodParameters, | ||
body = methodBody, | ||
modifiers = methodModifiers | ||
) | ||
|
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package java2typescript.transformer | ||
|
||
import com.github.javaparser.ast.body.{ConstructorDeclaration, MethodDeclaration, Parameter} | ||
import java2typescript.ast | ||
|
||
import scala.jdk.CollectionConverters.* | ||
import scala.jdk.OptionConverters.* | ||
|
||
def transformConstructorDeclaration(context: ClassContext, declaration: ConstructorDeclaration) = | ||
val methodParameters = declaration.getParameters.asScala.map(transformParameter.curried(context)).toList | ||
val methodContext = ParameterContext(context, methodParameters.toBuffer) | ||
val methodBody = ast.Block( | ||
declaration.getBody.getStatements.asScala.map(transformStatement.curried(methodContext)).toList | ||
) | ||
val methodModifiers = declaration.getModifiers.asScala.flatMap(transformModifier).toList | ||
|
||
ast.Constructor( | ||
parameters = methodParameters, | ||
body = Some(methodBody), | ||
modifiers = methodModifiers | ||
) | ||
|
||
def transformMethodDeclaration(context: ClassContext, decl: MethodDeclaration) = | ||
val methodParameters = decl.getParameters.asScala.map(transformParameter.curried(context)).toList | ||
val methodContext = ParameterContext(context, methodParameters.toBuffer) | ||
val methodBody = decl.getBody.toScala.map(body => | ||
ast.Block(body.getStatements.asScala.map(transformStatement.curried(methodContext)).toList) | ||
) | ||
val originalMethodModifiers = decl.getModifiers.asScala.flatMap(transformModifier).toList | ||
val methodModifiers = if (context.isInterface) | ||
originalMethodModifiers ::: List(ast.PublicKeyword(), ast.AbstractKeyword()) | ||
else | ||
originalMethodModifiers | ||
|
||
ast.MethodDeclaration( | ||
transformName(decl.getName), | ||
`type` = transformType(context, decl.getType), | ||
typeParameters = decl.getTypeParameters.asScala.map(transformType.curried(context)).map { | ||
t => t.get | ||
}.toList, | ||
parameters = methodParameters, | ||
body = methodBody, | ||
modifiers = methodModifiers | ||
) | ||
|
||
def transformParameter(context: FileContext, param: Parameter) = | ||
ast.Parameter(transformName(param.getName), transformType(context, param.getType)) |
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