Skip to content

Commit

Permalink
feat(javascript): add function signature and props to DsComponent #81
Browse files Browse the repository at this point in the history
- Added the ability to extract function signature and props from TypeScript function expressions and JS functions.
- Updated the `DsComponent` data class to include the new fields `signature` and `props`.
- Modified the `ReactPsiUtilTest` to include assertions for the extracted function signature and props.
  • Loading branch information
phodal committed Jan 25, 2024
1 parent b425406 commit 8ab5cd4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Expand Up @@ -9,6 +9,7 @@ import kotlinx.serialization.Serializable
data class DsComponent(
val name: String,
val path: String,
val signature: String = "",
val props: List<String> = emptyList(),
val events: List<String> = emptyList(),
)
Expand Up @@ -3,6 +3,7 @@ package cc.unitmesh.ide.javascript.util
import cc.unitmesh.ide.javascript.flow.DsComponent
import com.intellij.lang.ecmascript6.psi.ES6ExportDeclaration
import com.intellij.lang.ecmascript6.psi.ES6ExportDefaultAssignment
import com.intellij.lang.javascript.presentable.JSFormatUtil
import com.intellij.lang.javascript.psi.JSFile
import com.intellij.lang.javascript.psi.JSFunctionExpression
import com.intellij.lang.javascript.psi.JSReferenceExpression
Expand Down Expand Up @@ -53,30 +54,36 @@ object ReactPsiUtil {
val funcExpr = PsiTreeUtil.findChildrenOfType(psiElement, TypeScriptFunctionExpression::class.java)
.firstOrNull() ?: return@map null

val map = funcExpr.parameterList?.parameters?.mapNotNull { parameter ->
if (parameter.typeElement != null) {
parameter.typeElement
} else {
null
val signature = JSFormatUtil.buildFunctionSignaturePresentation(funcExpr)
val props: List<String> = funcExpr.parameterList?.parameters?.mapNotNull { parameter ->
val typeElement = parameter.typeElement ?: return@mapNotNull null
when (typeElement) {
is TypeScriptSingleType -> {
val resolve = typeElement.referenceExpression?.resolve()
resolve?.text
}

else -> {
println("unknown type: ${typeElement::class.java}")
null
}
}
} ?: emptyList()

DsComponent(name = name, path)
DsComponent(name = name, path, props = props, signature = signature)
}

is JSVariable -> {
val funcExpr = PsiTreeUtil.findChildrenOfType(psiElement, JSFunctionExpression::class.java)
.firstOrNull() ?: return@map null

val map = funcExpr.parameterList?.parameters?.mapNotNull { parameter ->
val signature = JSFormatUtil.buildFunctionSignaturePresentation(funcExpr)
val props: List<String> = funcExpr.parameterList?.parameters?.mapNotNull { parameter ->
val typeElement = parameter.typeElement ?: return@mapNotNull null
when (typeElement) {
is TypeScriptSingleType -> {
typeElement.referenceExpression?.let {
val resolveReference = JSResolveResult.resolveReference(it).firstOrNull()
println("resolveReference: ${resolveReference?.text}")
resolveReference
}
val resolve = typeElement.referenceExpression?.resolve()
resolve?.text
}

else -> {
Expand All @@ -86,7 +93,7 @@ object ReactPsiUtil {
}
} ?: emptyList()

DsComponent(name = name, path)
DsComponent(name = name, path, props = props, signature = signature)
}

else -> {
Expand Down
@@ -1,7 +1,6 @@
package cc.unitmesh.ide.javascript.util;

import com.intellij.lang.javascript.JavascriptLanguage
import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect
import com.intellij.lang.javascript.psi.JSFunction
import com.intellij.lang.javascript.psi.ecmal4.JSClass
import com.intellij.psi.PsiFileFactory
Expand Down
Expand Up @@ -24,5 +24,7 @@ class ReactPsiUtilTest : LightPlatformTestCase() {
val result = ReactPsiUtil.tsxComponentToComponent(file as JSFile)

assertEquals(1, result.size)
assertEquals("MyApp", result.first().name)
assertEquals("({ Component, pageProps }: AppProps)", result.first().signature)
}
}

0 comments on commit 8ab5cd4

Please sign in to comment.