Skip to content

Commit

Permalink
fix(javascript): add logger statements for null values #81
Browse files Browse the repository at this point in the history
Add logger statements to handle null values in ReactPsiUtil and ReactAutoPage files. This will help in identifying and debugging issues related to null values.
  • Loading branch information
phodal committed Jan 26, 2024
1 parent 51dc43e commit 57527d5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
Expand Up @@ -34,6 +34,7 @@ class GenPageAction : ChatBaseIntention() {
val selectedText = editor.selectionModel.selectedText ?: return

val reactAutoPage = ReactAutoPage(project, selectedText, editor)

sendToChatPanel(project) { contentPanel, _ ->
val llmProvider = LlmFactory().create(project)
val context = AutoPageContext.build(reactAutoPage)
Expand Down
Expand Up @@ -7,13 +7,15 @@ import com.intellij.lang.javascript.TypeScriptJSXFileType
import com.intellij.lang.javascript.dialects.ECMA6LanguageDialect
import com.intellij.lang.javascript.dialects.TypeScriptJSXLanguageDialect
import com.intellij.lang.javascript.psi.JSFile
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.ProjectScope
// keep this import
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

enum class RouterFile(val filename: String) {
Expand Down Expand Up @@ -78,14 +80,22 @@ class ReactAutoPage(

override fun getComponents(): List<DsComponent> = components

private fun buildComponent(jsFile: JSFile) = when (jsFile.language) {
is TypeScriptJSXLanguageDialect,
is ECMA6LanguageDialect
-> {
ReactPsiUtil.tsxComponentToComponent(jsFile)
private fun buildComponent(jsFile: JSFile): List<DsComponent>? {
return when (jsFile.language) {
is TypeScriptJSXLanguageDialect,
is ECMA6LanguageDialect
-> {
val dsComponents = ReactPsiUtil.tsxComponentToComponent(jsFile)
if (dsComponents.isEmpty()) {
logger<ReactAutoPage>().warn("no component found in ${jsFile.name}")
}
dsComponents
}
else -> {
logger<ReactAutoPage>().warn("unknown language: ${jsFile.language}")
null
}
}

else -> null
}

override fun getRoutes(): List<String> = routes
Expand Down
Expand Up @@ -10,6 +10,7 @@ import com.intellij.lang.javascript.psi.JSReferenceExpression
import com.intellij.lang.javascript.psi.JSVariable
import com.intellij.lang.javascript.psi.ecma6.*
import com.intellij.lang.javascript.psi.resolve.JSResolveResult
import com.intellij.openapi.diagnostic.logger
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.util.PsiTreeUtil

Expand Down Expand Up @@ -39,8 +40,12 @@ object ReactPsiUtil {
}

fun tsxComponentToComponent(jsFile: JSFile): List<DsComponent> = getExportElements(jsFile).map { psiElement ->
val name = psiElement.name ?: return@map null
val path = jsFile.virtualFile.canonicalPath ?: return@map null
val name = psiElement.name
if (name == null) {
logger<ReactPsiUtil>().warn("name is null")
return@map null
}
val path = jsFile.virtualFile.canonicalPath ?: ""
return@map when (psiElement) {
is TypeScriptFunction -> {
DsComponent(name = name, path)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/genius/page/page-gen-clarify.vm
Expand Up @@ -2,7 +2,7 @@ You are a professional Frontend developer.
According to the user's requirements, you should choose the best components for the user in List.

- Framework: React
- User component: ${context.components}
- User component: ${context.componentNames}, ${context.pageNames}

For example:

Expand Down

0 comments on commit 57527d5

Please sign in to comment.