generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): add ReactUtil and ReactUtilTest #81
This commit adds the `ReactUtil` object, which contains functions for extracting React components from JavaScript files. It also includes the corresponding unit test `ReactUtilTest` to ensure the functionality of the `ReactUtil` methods.
- Loading branch information
Showing
4 changed files
with
126 additions
and
72 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
94 changes: 94 additions & 0 deletions
94
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/util/ReactUtil.kt
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,94 @@ | ||
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.psi.JSFile | ||
import com.intellij.lang.javascript.psi.JSFunctionExpression | ||
import com.intellij.lang.javascript.psi.JSReferenceExpression | ||
import com.intellij.lang.javascript.psi.JSVariable | ||
import com.intellij.lang.javascript.psi.ecma6.TypeScriptClass | ||
import com.intellij.lang.javascript.psi.ecma6.TypeScriptFunction | ||
import com.intellij.lang.javascript.psi.ecma6.TypeScriptFunctionExpression | ||
import com.intellij.lang.javascript.psi.ecma6.TypeScriptVariable | ||
import com.intellij.lang.javascript.psi.resolve.JSResolveResult | ||
import com.intellij.psi.PsiNameIdentifierOwner | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
object ReactUtil { | ||
private fun getExportElements(file: JSFile): List<PsiNameIdentifierOwner> { | ||
val exportDeclarations = | ||
PsiTreeUtil.getChildrenOfTypeAsList(file, ES6ExportDeclaration::class.java) | ||
|
||
val map = exportDeclarations.map { exportDeclaration -> | ||
exportDeclaration.exportSpecifiers | ||
.asSequence() | ||
.mapNotNull { | ||
it.alias?.findAliasedElement() | ||
} | ||
.filterIsInstance<PsiNameIdentifierOwner>() | ||
.toList() | ||
}.flatten() | ||
|
||
val defaultAssignments = PsiTreeUtil.getChildrenOfTypeAsList(file, ES6ExportDefaultAssignment::class.java) | ||
val defaultAssignment = defaultAssignments.mapNotNull { | ||
val jsReferenceExpression = it.expression as? JSReferenceExpression ?: return@mapNotNull null | ||
val resolveReference = JSResolveResult.resolveReference(jsReferenceExpression) | ||
resolveReference.firstOrNull() as? PsiNameIdentifierOwner | ||
} | ||
|
||
return map + defaultAssignment | ||
} | ||
|
||
fun tsxComponentToComponent(jsFile: JSFile): List<DsComponent> { | ||
val exportElements = getExportElements(jsFile) | ||
return exportElements.map { psiElement -> | ||
val name = psiElement.name ?: return@map null | ||
val path = jsFile.virtualFile.canonicalPath ?: return@map null | ||
return@map when (psiElement) { | ||
is TypeScriptFunction -> { | ||
DsComponent(name = name, path) | ||
} | ||
|
||
is TypeScriptClass -> { | ||
DsComponent(name = name, path) | ||
} | ||
|
||
is TypeScriptVariable -> { | ||
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 | ||
} | ||
} ?: emptyList() | ||
|
||
DsComponent(name = name, path) | ||
} | ||
|
||
is JSVariable -> { | ||
val funcExpr = PsiTreeUtil.findChildrenOfType(psiElement, JSFunctionExpression::class.java) | ||
.firstOrNull() ?: return@map null | ||
|
||
val map = funcExpr.parameterList?.parameters?.mapNotNull { parameter -> | ||
if (parameter.typeElement != null) { | ||
parameter.typeElement | ||
} else { | ||
null | ||
} | ||
} ?: emptyList() | ||
|
||
DsComponent(name = name, path) | ||
} | ||
|
||
else -> { | ||
println("unknown type: ${psiElement::class.java}") | ||
null | ||
} | ||
} | ||
}.filterNotNull() | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
javascript/src/test/kotlin/cc/unitmesh/ide/javascript/util/ReactUtilTest.kt
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,28 @@ | ||
package cc.unitmesh.ide.javascript.util; | ||
|
||
import com.intellij.lang.javascript.JavascriptLanguage | ||
import com.intellij.lang.javascript.dialects.TypeScriptJSXLanguageDialect | ||
import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect | ||
import com.intellij.lang.javascript.psi.JSFile | ||
import com.intellij.lang.javascript.psi.ecmal4.JSClass | ||
import com.intellij.psi.PsiFileFactory | ||
import com.intellij.testFramework.LightPlatformTestCase | ||
|
||
class ReactUtilTest : LightPlatformTestCase() { | ||
fun testShouldHandleExportReactComponent() { | ||
val code = """ | ||
import type { AppProps } from 'next/app'; | ||
const MyApp = ({ Component, pageProps }: AppProps) => ( | ||
<Component {...pageProps} /> | ||
); | ||
export default MyApp; | ||
""".trimIndent() | ||
|
||
val file = PsiFileFactory.getInstance(project).createFileFromText(JavascriptLanguage.INSTANCE, code) | ||
val result = ReactUtil.tsxComponentToComponent(file as JSFile) | ||
|
||
assertEquals(1, result.size) | ||
} | ||
} |