Skip to content

Commit

Permalink
Merge pull request #1045 from bohdan-harniuk/614-references-for-file-…
Browse files Browse the repository at this point in the history
…path-js-string-literals

614: Provided file path references for JS:STRING_LITERAL psi elements in the js files
  • Loading branch information
Vitaliy authored Mar 25, 2022
2 parents bb25894 + 229afbb commit db7ef2d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.reference.js;

import static com.intellij.patterns.StandardPatterns.string;
import static com.magento.idea.magento2plugin.util.RegExUtil.JsRegex;

import com.intellij.lang.javascript.JSTokenTypes;
import com.intellij.lang.javascript.JavascriptLanguage;
import com.intellij.lang.javascript.patterns.JSPatterns;
import com.intellij.psi.*;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiReferenceContributor;
import com.intellij.psi.PsiReferenceRegistrar;
import com.magento.idea.magento2plugin.reference.provider.FilePathReferenceProvider;
import com.magento.idea.magento2plugin.reference.provider.ModuleNameReferenceProvider;
import com.magento.idea.magento2plugin.reference.provider.RequireJsPreferenceReferenceProvider;
import com.magento.idea.magento2plugin.util.RegExUtil;
import org.jetbrains.annotations.NotNull;

import static com.intellij.patterns.StandardPatterns.string;

public class JsReferenceContributor extends PsiReferenceContributor {

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
public void registerReferenceProviders(final @NotNull PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(
JSPatterns.jsLiteralExpression()
.withText(string().matches(".*" + RegExUtil.Magento.MODULE_NAME + ".*")),
new ModuleNameReferenceProvider()
);

// Targets property value -> {test: 'Sandbox_Test/js/test'}
registrar.registerReferenceProvider(
JSPatterns.jsLiteralExpression().withText(string().matches(".*\\W" + RegExUtil.FILE_PATH + ".*")),
JSPatterns.jsLiteralExpression()
.withText(string().matches(JsRegex.FILE_PATH)),
new FilePathReferenceProvider()
);

// Targets property key (JS:STRING_LITERAL) -> {'Sandbox_Test/js/test': true}
registrar.registerReferenceProvider(
PlatformPatterns.psiElement(JSTokenTypes.STRING_LITERAL)
.withText(string().matches(JsRegex.FILE_PATH))
.withLanguage(JavascriptLanguage.INSTANCE),
new FilePathReferenceProvider()
);

registrar.registerReferenceProvider(
JSPatterns.jsLiteralExpression().withText(string().matches(".*\\W" + RegExUtil.FILE_PATH + ".*")),
JSPatterns.jsLiteralExpression()
.withText(string().matches(".*\\W" + RegExUtil.FILE_PATH + ".*")),
new RequireJsPreferenceReferenceProvider()
);
}
Expand Down
7 changes: 7 additions & 0 deletions src/com/magento/idea/magento2plugin/util/RegExUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public static class XmlRegex {
"\\\\?" + PhpRegex.FQN + "(" + CLASS_MEMBER_NAME + ")?.*";
}

public static class JsRegex {

// Targets paths like `'Sandbox_Test/js/test'`
public static final String FILE_PATH
= "(\\W{1}[A-Z][a-zA-Z0-9]+_[A-Z][a-zA-Z0-9]+[\\/\\w*-]{1,}\\W{1})";
}

public static class CustomTheme {
public static final String MODULE_NAME =
"app\\/design\\/(adminhtml|frontend)\\/\\w*\\/\\w*\\/\\w*";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var config = {
config: {
mixins: {
'Magento_Checkout/js/view/shipping': {
'Foo_Bar/js/file<caret>': true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.ResolveResult;
import com.intellij.psi.impl.file.PsiDirectoryImpl;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.inspections.BaseInspectionsTestCase;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings({
Expand All @@ -36,6 +42,7 @@ protected void setUp() throws Exception {
myFixture.setTestDataPath(testDataFolderPath);
}

@SuppressWarnings("PMD.CognitiveComplexity")
protected void assertHasReferenceToXmlAttributeValue(final String reference) {
final PsiElement element = getElementFromCaret();
for (final PsiReference psiReference: element.getReferences()) {
Expand Down Expand Up @@ -70,6 +77,7 @@ protected void assertHasReferenceToXmlAttributeValue(final String reference) {
fail(String.format(referenceNotFound, reference));
}

@SuppressWarnings("PMD.CognitiveComplexity")
protected void assertHasReferenceToXmlTag(final String tagName) {
final PsiElement element = getElementFromCaret();
for (final PsiReference psiReference: element.getReferences()) {
Expand Down Expand Up @@ -106,7 +114,40 @@ protected void assertHasReferenceToXmlTag(final String tagName) {

protected void assertHasReferenceToFile(final String reference) {
final PsiElement element = getElementFromCaret();
for (final PsiReference psiReference : element.getReferences()) {

assertHasReferenceToFile(reference, Arrays.asList(element.getReferences()));
}

protected void assertHasReferenceToFile(
final String reference,
final Class<? extends PsiReferenceProvider> providerClass
) {
final PsiElement element = getLeafElementFromCaret();
final List<PsiReference> references = new ArrayList<>();

try {
final PsiReferenceProvider provider = providerClass.getConstructor().newInstance();
references.addAll(
Arrays.asList(
provider.getReferencesByElement(element, new ProcessingContext())
)
);
} catch (NoSuchMethodException
| IllegalAccessException
| InvocationTargetException
| InstantiationException exception
) {
references.addAll(Arrays.asList(element.getReferences()));
}

assertHasReferenceToFile(reference, references);
}

protected void assertHasReferenceToFile(
final String reference,
final List<PsiReference> references
) {
for (final PsiReference psiReference : references) {
final PsiElement resolved = psiReference.resolve();
if (!(resolved instanceof PsiFile)) {
continue;
Expand Down Expand Up @@ -250,4 +291,8 @@ protected void assertEmptyReference() {
private PsiElement getElementFromCaret() {
return myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent();
}

private PsiElement getLeafElementFromCaret() {
return myFixture.getFile().findElementAt(myFixture.getCaretOffset());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,61 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.reference.js;

import com.magento.idea.magento2plugin.reference.provider.FilePathReferenceProvider;

public class RequireJsReferenceRegistrarTest extends ReferenceJsFixtureTestCase {

private static final String FIXTURE_PATH = "test.js";
private static final String MIXIN_FIXTURE_PATH = "requirejs-config.js";

/**
* Mapped parameters should have reference to file.
*/
public void testMappedInjectionParameterMustHaveReference() {
String filePath = this.getFixturePath("test.js");
myFixture.configureByFile(filePath);
myFixture.configureByFile(getFixturePath(FIXTURE_PATH));

assertHasReferenceToFile("app/code/Foo/Bar/view/frontend/web/js/file.js");
}

/**
* Path parameters should have reference to file.
*/
public void testPathInjectionParameterMustHaveReference() {
String filePath = this.getFixturePath("test.js");
myFixture.configureByFile(filePath);
myFixture.configureByFile(getFixturePath(FIXTURE_PATH));

assertHasReferenceToFile("app/code/Foo/Bar/view/frontend/web/js/file2.js");
}

/**
* The Magento resource file path parameters should have reference to file.
*/
public void testFileInjectionParameterMustHaveReference() {
String filePath = this.getFixturePath("test.js");
myFixture.configureByFile(filePath);
myFixture.configureByFile(getFixturePath(FIXTURE_PATH));

assertHasReferenceToFile("app/code/Foo/Bar/view/frontend/web/js/file.js");
}

/**
* Lib resource parameters should have reference to file.
*/
public void testLibInjectionParameterMustHaveReference() {
String filePath = this.getFixturePath("test.js");
myFixture.configureByFile(filePath);
myFixture.configureByFile(getFixturePath(FIXTURE_PATH));

assertHasReferenceToFile("/lib/web/testjs.js");
}

/**
* Mixin declaration parameters should have reference to file.
*/
public void testFilePathInMixinDeclarationMustHaveReference() {
myFixture.configureByFile(getFixturePath(MIXIN_FIXTURE_PATH));

assertHasReferenceToFile(
"app/code/Foo/Bar/view/frontend/web/js/file.js",
FilePathReferenceProvider.class
);
}
}

0 comments on commit db7ef2d

Please sign in to comment.