Skip to content

Commit

Permalink
refactor(ast): improve null safety and readability of PythonAst liste…
Browse files Browse the repository at this point in the history
…ners

- Modify `PythonAstBaseListener.kt` to use safe call operator for default parameter value assignment.
- Change `buildAnnotation` function in `PythonAstBaseListener.kt` to private.
- Simplify import usage name assignment in `PythonFullIdentListener.kt`.
- Enhance null safety for alias name in import statement within `PythonFullIdentListener.kt`.
  • Loading branch information
phodal committed Nov 13, 2024
1 parent c71e01e commit fc370d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class PythonAstBaseListener : PythonParserBaseListener() {
)

if (defParaCtx.ASSIGN() != null) {
parameter.DefaultValue = defParaCtx.test().text
parameter.DefaultValue = defParaCtx?.test()?.text ?: ""
}

parameter
Expand Down Expand Up @@ -56,7 +56,7 @@ open class PythonAstBaseListener : PythonParserBaseListener() {
return annotations
}

fun buildAnnotation(node: PythonParser.DecoratorContext): CodeAnnotation {
private fun buildAnnotation(node: PythonParser.DecoratorContext): CodeAnnotation {
val codeAnnotation = CodeAnnotation(
Name = node.dotted_name().text
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() {

val codeImport = CodeImport(Source = sourceName)

ctx?.import_as_names()?.import_as_name()?.forEach { importAsNamecontext ->
val usageName = importAsNamecontext.name()[0].text
ctx?.import_as_names()?.import_as_name()?.forEach { nameContext ->
val usageName = nameContext.name()[0].text
codeImport.UsageName += usageName

importAsNamecontext.AS()?.let {
codeImport.AsName = importAsNamecontext.name()[1].text
nameContext.AS()?.let {
codeImport.AsName = nameContext.name().getOrNull(1)?.text ?: ""
}
}

Expand Down Expand Up @@ -126,6 +126,7 @@ class PythonFullIdentListener(var fileName: String) : PythonAstBaseListener() {
if (defaultNode.Functions.isNotEmpty()) {
this.codeContainer.DataStructures += defaultNode
}

return this.codeContainer
}
}

0 comments on commit fc370d5

Please sign in to comment.