Skip to content

Commit 57527d5

Browse files
committed
fix(javascript): add logger statements for null values #81
Add logger statements to handle null values in ReactPsiUtil and ReactAutoPage files. This will help in identifying and debugging issues related to null values.
1 parent 51dc43e commit 57527d5

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/actions/GenPageAction.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class GenPageAction : ChatBaseIntention() {
3434
val selectedText = editor.selectionModel.selectedText ?: return
3535

3636
val reactAutoPage = ReactAutoPage(project, selectedText, editor)
37+
3738
sendToChatPanel(project) { contentPanel, _ ->
3839
val llmProvider = LlmFactory().create(project)
3940
val context = AutoPageContext.build(reactAutoPage)

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/ReactAutoPage.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import com.intellij.lang.javascript.TypeScriptJSXFileType
77
import com.intellij.lang.javascript.dialects.ECMA6LanguageDialect
88
import com.intellij.lang.javascript.dialects.TypeScriptJSXLanguageDialect
99
import com.intellij.lang.javascript.psi.JSFile
10+
import com.intellij.openapi.diagnostic.logger
1011
import com.intellij.openapi.editor.Editor
1112
import com.intellij.openapi.project.Project
1213
import com.intellij.openapi.project.guessProjectDir
1314
import com.intellij.psi.search.FileTypeIndex
1415
import com.intellij.psi.search.GlobalSearchScope
1516
import com.intellij.psi.search.ProjectScope
1617
// keep this import
18+
import kotlinx.serialization.decodeFromString
1719
import kotlinx.serialization.json.Json
1820

1921
enum class RouterFile(val filename: String) {
@@ -78,14 +80,22 @@ class ReactAutoPage(
7880

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

81-
private fun buildComponent(jsFile: JSFile) = when (jsFile.language) {
82-
is TypeScriptJSXLanguageDialect,
83-
is ECMA6LanguageDialect
84-
-> {
85-
ReactPsiUtil.tsxComponentToComponent(jsFile)
83+
private fun buildComponent(jsFile: JSFile): List<DsComponent>? {
84+
return when (jsFile.language) {
85+
is TypeScriptJSXLanguageDialect,
86+
is ECMA6LanguageDialect
87+
-> {
88+
val dsComponents = ReactPsiUtil.tsxComponentToComponent(jsFile)
89+
if (dsComponents.isEmpty()) {
90+
logger<ReactAutoPage>().warn("no component found in ${jsFile.name}")
91+
}
92+
dsComponents
93+
}
94+
else -> {
95+
logger<ReactAutoPage>().warn("unknown language: ${jsFile.language}")
96+
null
97+
}
8698
}
87-
88-
else -> null
8999
}
90100

91101
override fun getRoutes(): List<String> = routes

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/util/ReactPsiUtil.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.lang.javascript.psi.JSReferenceExpression
1010
import com.intellij.lang.javascript.psi.JSVariable
1111
import com.intellij.lang.javascript.psi.ecma6.*
1212
import com.intellij.lang.javascript.psi.resolve.JSResolveResult
13+
import com.intellij.openapi.diagnostic.logger
1314
import com.intellij.psi.PsiNameIdentifierOwner
1415
import com.intellij.psi.util.PsiTreeUtil
1516

@@ -39,8 +40,12 @@ object ReactPsiUtil {
3940
}
4041

4142
fun tsxComponentToComponent(jsFile: JSFile): List<DsComponent> = getExportElements(jsFile).map { psiElement ->
42-
val name = psiElement.name ?: return@map null
43-
val path = jsFile.virtualFile.canonicalPath ?: return@map null
43+
val name = psiElement.name
44+
if (name == null) {
45+
logger<ReactPsiUtil>().warn("name is null")
46+
return@map null
47+
}
48+
val path = jsFile.virtualFile.canonicalPath ?: ""
4449
return@map when (psiElement) {
4550
is TypeScriptFunction -> {
4651
DsComponent(name = name, path)

src/main/resources/genius/page/page-gen-clarify.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ You are a professional Frontend developer.
22
According to the user's requirements, you should choose the best components for the user in List.
33

44
- Framework: React
5-
- User component: ${context.components}
5+
- User component: ${context.componentNames}, ${context.pageNames}
66

77
For example:
88

0 commit comments

Comments
 (0)