Skip to content

Commit

Permalink
introduce tests for entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Feb 25, 2024
1 parent f123141 commit 3b58b4b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 62 deletions.
62 changes: 2 additions & 60 deletions src/main/java/de/espend/idea/vuejs/index/AppEntrypointIndex.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
package de.espend.idea.vuejs.index;

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.JavaScriptFileType;
import com.intellij.lang.javascript.TypeScriptFileType;
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.impl.JSReferenceExpressionImpl;
import com.intellij.lang.javascript.psi.util.JSStubBasedPsiTreeUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiRecursiveElementVisitor;
import com.intellij.util.PathUtil;
import com.intellij.util.indexing.*;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.KeyDescriptor;
import de.espend.idea.vuejs.index.externalizer.StringListDataExternalizer;
import de.espend.idea.vuejs.index.visitor.AppEntryPsiRecursiveElementVisitor;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -47,52 +34,7 @@ public class AppEntrypointIndex extends FileBasedIndexExtension<String, List<Str
public @NotNull Map<String, List<String>> map(@NotNull FileContent fileContent) {
Map<String, List<String>> map = new HashMap<>();

fileContent.getPsiFile().acceptChildren(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof JSCallExpression jsCallExpression) {
JSExpression methodExpression = jsCallExpression.getMethodExpression();
if (methodExpression != null) {
String referenceName = JSReferenceExpressionImpl.getReferenceName(methodExpression.getNode());

if ("createApp".equals(referenceName) || "createElement".equals(referenceName)) {
JSArgumentList argumentList = jsCallExpression.getArgumentList();
if (argumentList != null) {
JSExpression[] arguments = argumentList.getArguments();

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));
}
}
}
}
}
}
}
}
}
}
}
}

super.visitElement(element);
}
});
fileContent.getPsiFile().acceptChildren(new AppEntryPsiRecursiveElementVisitor(map));

return map;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package de.espend.idea.vuejs.index.visitor;

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.impl.JSReferenceExpressionImpl;
import com.intellij.lang.javascript.psi.util.JSStubBasedPsiTreeUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiRecursiveElementVisitor;
import com.intellij.util.PathUtil;
import org.jetbrains.annotations.NotNull;

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

/**
* @author Daniel Espendiller <[email protected]>
*/
public class AppEntryPsiRecursiveElementVisitor extends PsiRecursiveElementVisitor {
private final Map<String, List<String>> map;

public AppEntryPsiRecursiveElementVisitor(@NotNull Map<String, List<String>> map) {
this.map = map;
}

@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof JSCallExpression jsCallExpression) {
JSExpression methodExpression = jsCallExpression.getMethodExpression();
if (methodExpression != null) {
String referenceName = JSReferenceExpressionImpl.getReferenceName(methodExpression.getNode());

if ("createApp".equals(referenceName) || "createElement".equals(referenceName)) {
JSArgumentList argumentList = jsCallExpression.getArgumentList();
if (argumentList != null) {
JSExpression[] arguments = argumentList.getArguments();

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));
}
}
}
}
}
}
}
}
}
}
}
}

super.visitElement(element);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tests.de.espend.idea.vuejs.index;

import de.espend.idea.vuejs.index.ComponentUsageIndex;
import tests.de.espend.idea.vuejs.VueJsLightJavaCodeInsightFixtureTestCase;

/**
Expand All @@ -18,6 +17,8 @@ public String getTestDataPath() {
}

public void testFoo() {
assertIndexContains(ComponentUsageIndex.KEY, "IconDocumentation");
// no infrastructure yet
assertTrue(true);
//assertIndexContains(ComponentUsageIndex.KEY, "IconDocumentation");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tests.de.espend.idea.vuejs.index.visitor;

import com.intellij.psi.PsiFile;
import de.espend.idea.vuejs.index.visitor.AppEntryPsiRecursiveElementVisitor;
import tests.de.espend.idea.vuejs.VueJsLightJavaCodeInsightFixtureTestCase;

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

/**
* @author Daniel Espendiller <[email protected]>
*/
public class AppEntryPsiRecursiveElementVisitorTest extends VueJsLightJavaCodeInsightFixtureTestCase {
public String getTestDataPath() {
return "src/test/java/tests/de/espend/idea/vuejs/index/fixtures";
}

public void testCreateapp() {
PsiFile psiFile = myFixture.configureByFile("appentry-createapp.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("createApp", app.get(1));
}

public void testCreateappV2() {
PsiFile psiFile = myFixture.configureByFile("appentry-createelement-v2.ts");

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("createElement", app.get(1));
}
}

0 comments on commit 3b58b4b

Please sign in to comment.