Skip to content

Commit

Permalink
add $mount entrypoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Feb 26, 2024
1 parent ff6cc5b commit 482db5b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.intellij.lang.ecmascript6.psi.ES6FromClause;
import com.intellij.lang.ecmascript6.psi.ES6ImportDeclaration;
import com.intellij.lang.ecmascript6.psi.ES6ImportedBinding;
import com.intellij.lang.javascript.psi.JSArgumentList;
import com.intellij.lang.javascript.psi.JSCallExpression;
import com.intellij.lang.javascript.psi.JSExpression;
import com.intellij.lang.javascript.psi.JSReferenceExpression;
import com.intellij.lang.javascript.psi.*;
import com.intellij.lang.javascript.psi.impl.JSReferenceExpressionImpl;
import com.intellij.lang.javascript.psi.util.JSStubBasedPsiTreeUtil;
import com.intellij.openapi.util.text.StringUtil;
Expand All @@ -16,6 +13,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -44,26 +42,18 @@ public void visitElement(@NotNull PsiElement element) {
if (arguments.length > 0 && arguments[0] instanceof JSReferenceExpression jsReferenceExpression) {
String referenceName1 = JSReferenceExpressionImpl.getReferenceName(jsReferenceExpression.getNode());
if (referenceName1 != null) {
List<PsiElement> createApp = JSStubBasedPsiTreeUtil.resolveLocallyWithMergedResults(referenceName1, jsReferenceExpression);

for (PsiElement psiElement : createApp) {
if (psiElement instanceof ES6ImportedBinding es6ImportedBinding) {
ES6ImportDeclaration declaration = es6ImportedBinding.getDeclaration();
if (declaration != null) {
ES6FromClause fromClause = declaration.getFromClause();
if (fromClause != null) {
String referenceText1 = fromClause.getReferenceText();
if (referenceText1 != null) {
String referenceText = StringUtil.unquoteString(referenceText1);
if (referenceText.endsWith(".vue")) {
String fileName = PathUtil.getFileName(referenceText.substring(0, referenceText.length() - 4));
map.put(fileName, Arrays.asList(referenceText, referenceName));
}
}
}
}
}
}
visitImportReferenceByName(JSStubBasedPsiTreeUtil.resolveLocallyWithMergedResults(referenceName1, jsReferenceExpression), referenceName);
}
}
}
} else if("$mount".equals(referenceName)) {
PsiElement firstChild = methodExpression.getFirstChild();
if (firstChild instanceof JSNewExpression jsNewExpression) {
JSExpression methodExpression1 = jsNewExpression.getMethodExpression();
if (methodExpression1 != null) {
String referenceName1 = JSReferenceExpressionImpl.getReferenceName(methodExpression1.getNode());
if (referenceName1 != null) {
visitImportReferenceByName(JSStubBasedPsiTreeUtil.resolveLocallyWithMergedResults(referenceName1, jsNewExpression), referenceName);
}
}
}
Expand All @@ -73,4 +63,25 @@ public void visitElement(@NotNull PsiElement element) {

super.visitElement(element);
}

private void visitImportReferenceByName(@NotNull Collection<PsiElement> resolvedElements, @NotNull String referenceName) {
for (PsiElement psiElement : resolvedElements) {
if (psiElement instanceof ES6ImportedBinding es6ImportedBinding) {
ES6ImportDeclaration declaration = es6ImportedBinding.getDeclaration();
if (declaration != null) {
ES6FromClause fromClause = declaration.getFromClause();
if (fromClause != null) {
String referenceText1 = fromClause.getReferenceText();
if (referenceText1 != null) {
String referenceText = StringUtil.unquoteString(referenceText1);
if (referenceText.endsWith(".vue")) {
String fileName = PathUtil.getFileName(referenceText.substring(0, referenceText.length() - 4));
map.put(fileName, Arrays.asList(referenceText, referenceName));
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import App from './App.vue';

let app = new App();
app.$mount(el);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tests.de.espend.idea.vuejs.index.utils;

import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.xml.XmlFile;
import de.espend.idea.vuejs.index.utils.VueJsComponentIndexUtil;
import org.jetbrains.vuejs.codeInsight.VueUtilKt;
import org.jetbrains.vuejs.lang.html.VueFile;
import org.jetbrains.vuejs.lang.html.VueFileType;
import tests.de.espend.idea.vuejs.VueJsLightJavaCodeInsightFixtureTestCase;

import java.util.List;
import java.util.Map;

public class VueJsComponentIndexUtilTest extends VueJsLightJavaCodeInsightFixtureTestCase {
public String getTestDataPath() {
return "src/test/java/tests/de/espend/idea/vuejs/index/fixtures";
}

public void testFoo() {
PsiFile psiFile = myFixture.configureByFile("component-composition.vue");

//String text = psiFile.getText();
//PsiFile test = PsiFileFactory.getInstance(getProject()).createFileFromText("component-composition.vue", VueFileType.INSTANCE, text);

VueFile vueFileFromText = VueUtilKt.createVueFileFromText(getProject(), psiFile.getText());

// PsiFile psiFile = myFixture.configureByFile("component-composition.vue");

// Map<String, List<String>> componentsForIndex = VueJsComponentIndexUtil.getComponentsForIndex((XmlFile) test);

System.out.println(vueFileFromText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ public void testCreateappV2() {
assertEquals("./App.vue", app.get(0));
assertEquals("createElement", app.get(1));
}

public void testCreateappV2New() {
PsiFile psiFile = myFixture.configureByFile("appentry-mount-v2.js");

Map<String, List<String>> map = new HashMap<>();
psiFile.acceptChildren(new AppEntryPsiRecursiveElementVisitor(map));

List<String> app = map.get("App");

assertEquals("./App.vue", app.get(0));
assertEquals("$mount", app.get(1));
}
}

0 comments on commit 482db5b

Please sign in to comment.