From 13ee9a272bf1d7c18026520a6f94fb8d91accf9f Mon Sep 17 00:00:00 2001 From: patrick Date: Tue, 5 Nov 2024 16:29:21 +0100 Subject: [PATCH 1/5] fix unused --- gradle.properties | 1 + src/test/kotlin/com/emberjs/gts/GtsFileTest.kt | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index e5d9a95b..e874dde9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,2 @@ kotlin.stdlib.default.dependency = false +org.gradle.jvmargs=-Xmx4g diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index 50bce87f..e3352e7d 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -40,10 +40,13 @@ class GtsFileTest : BasePlatformTestCase() { function corge() {}; function corge2() {}; + export const grault = {}; + export const Grault = {}; export default """.trimIndent() myFixture.configureByText(GjsFileType.INSTANCE, gts) - myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection()) + myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project) val highlightInfos: List = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" } TestCase.assertEquals(4, highlightInfos.size) From 740620841c40e07268a39e2467bbb6cdf695fb52 Mon Sep 17 00:00:00 2001 From: patrickpircher Date: Tue, 5 Nov 2024 23:29:28 +0100 Subject: [PATCH 3/5] test --- src/test/kotlin/com/emberjs/gts/GtsFileTest.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index a21cb85a..53119add 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -57,6 +57,8 @@ class GtsFileTest : BasePlatformTestCase() { myFixture.configureByText(GjsFileType.INSTANCE, gts) myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project) + val unusedConstants = myFixture.doHighlighting().filter { it.description.startsWith("unused constant") } + TestCase.assertEquals(unusedConstants.size, 0) val highlightInfos: List = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" } TestCase.assertEquals(4, highlightInfos.size) TestCase.assertTrue(highlightInfos[0].description.contains("quux")) @@ -75,7 +77,10 @@ class GtsFileTest : BasePlatformTestCase() { const bar = () => null; - const Baz = {}; + const Baz = {}; + + export const grault = {}; + export const Grault = {}; export default """.trimIndent() myFixture.configureByText(GtsFileType.INSTANCE, gts) - myFixture.enableInspections(ES6UnusedImportsInspection()) + myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project) + val unusedConstants = myFixture.doHighlighting().filter { it.description.startsWith("unused constant") } + TestCase.assertEquals(unusedConstants.size, 0) val highlightInfos: List = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" } TestCase.assertEquals(highlightInfos.size, 2) TestCase.assertTrue(highlightInfos.first().description.contains("quux")) From b1e03f3e6dffed72d12e7ca576d43e2bcac227a4 Mon Sep 17 00:00:00 2001 From: patrick Date: Wed, 6 Nov 2024 11:22:28 +0100 Subject: [PATCH 4/5] fix unused --- .../com/emberjs/gts/GtsReferenceSearcher.kt | 83 ++++++++++++------- src/main/kotlin/com/emberjs/gts/GtsSupport.kt | 2 + .../kotlin/com/emberjs/gts/GtsFileTest.kt | 22 +++-- 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/src/main/kotlin/com/emberjs/gts/GtsReferenceSearcher.kt b/src/main/kotlin/com/emberjs/gts/GtsReferenceSearcher.kt index 05515905..d05ae86a 100644 --- a/src/main/kotlin/com/emberjs/gts/GtsReferenceSearcher.kt +++ b/src/main/kotlin/com/emberjs/gts/GtsReferenceSearcher.kt @@ -6,6 +6,7 @@ import com.emberjs.hbs.ResolvedReference import com.emberjs.psi.EmberNamedElement import com.emberjs.utils.ifTrue import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier +import com.intellij.lang.javascript.JavaScriptSupportLoader import com.intellij.lang.javascript.psi.JSPsiNamedElementBase import com.intellij.lang.javascript.psi.JSVariable import com.intellij.openapi.application.QueryExecutorBase @@ -23,66 +24,86 @@ import com.intellij.util.Processor class GtsReferenceSearcher : QueryExecutorBase(true) { override fun processQuery(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor) { - val element = queryParameters.elementToSearch + var element = queryParameters.elementToSearch if (element is JSPsiNamedElementBase) { val name = element.name if (name != null) { + if (element.containingFile is GtsFile){ + if (element is JSVariable) { + val psi = element.containingFile.viewProvider.getPsi(JavaScriptSupportLoader.TYPESCRIPT) ?: element.containingFile.viewProvider.getPsi(JavaScriptSupportLoader.ECMA_SCRIPT_6) + element = psi.findElementAt(element.textOffset)?.parent ?: element + } + } val effectiveScope = if (element is JSVariable && (queryParameters.effectiveSearchScope as? LocalSearchScope)?.scope?.size == 1) { LocalSearchScope(element.containingFile) } else { queryParameters.effectiveSearchScope } val collector = queryParameters.optimizer - collector.searchWord(name, effectiveScope, 1.toShort(), true, element, MyProcessor(queryParameters)) + collector.searchWord(name, effectiveScope, 1.toShort(), true, element, MyProcessor(queryParameters, element)) } } } - private class MyProcessor(queryParameters: ReferencesSearch.SearchParameters) : RequestResultProcessor() { + private class MyProcessor(queryParameters: ReferencesSearch.SearchParameters, elementToSearch: PsiElement) : RequestResultProcessor() { private val myQueryParameters: ReferencesSearch.SearchParameters private val myOwnCollector: SearchRequestCollector + private val myElementToSearch: PsiElement init { myQueryParameters = queryParameters + myElementToSearch = elementToSearch myOwnCollector = SearchRequestCollector(queryParameters.optimizer.searchSession) } override fun processTextOccurrence(element: PsiElement, offsetInElement: Int, consumer: Processor): Boolean { - if (myQueryParameters.elementToSearch.containingFile is GtsFile) { - consumer.process(ResolvedReference(myQueryParameters.elementToSearch, myQueryParameters.elementToSearch)) + if (myElementToSearch.containingFile is GtsFile) { + consumer.process(ResolvedReference(myElementToSearch, myElementToSearch)) return false } return if ((element is HbPsiElement && element.elementType == HbTokenTypes.ID) || (element is XmlToken && element.parent is HtmlTag)) { val elem = (element is XmlToken && element.parent is HtmlTag).ifTrue { element.parent } ?: element - var found = elem.reference?.isReferenceTo(myQueryParameters.elementToSearch) == true || elem.references.any { - it.isReferenceTo(myQueryParameters.elementToSearch) + var foundRef = (elem.reference?.isReferenceTo(myElementToSearch) == true).ifTrue { elem.reference } ?: elem.references.find { + it.isReferenceTo(myElementToSearch) + } + + if (foundRef != null) { + consumer.process(foundRef) + return false } - if (!found) { - var resolved = elem.reference?.resolve() - if (resolved is EmberNamedElement) { - resolved = resolved.target - } - if (resolved == myQueryParameters.elementToSearch) { - return false - } - found = (resolved as? ES6ImportSpecifier)?.let { - val results = it.multiResolve(false) - results.any { it.element == myQueryParameters.elementToSearch } - } ?: false + var resolved = elem.reference?.resolve() + if (resolved is EmberNamedElement) { + resolved = resolved.target + } + if (resolved == myElementToSearch) { + foundRef = elem.reference + consumer.process(foundRef) + return false + } + foundRef = (resolved as? ES6ImportSpecifier)?.let { + val results = it.multiResolve(false) + results.find { it.element == myElementToSearch }?.let { ResolvedReference(element, myElementToSearch) } + } + if (foundRef != null) { + consumer.process(foundRef) + return false + } + val ref = elem.references.find { it.resolve() is ES6ImportSpecifier || it.resolve() is EmberNamedElement } + resolved = ref?.resolve() + if (resolved is EmberNamedElement) { + resolved = resolved.target + } + if (resolved == myElementToSearch) { + consumer.process(ref) + return false } - if (!found) { - var resolved = elem.references.find { it.resolve() is ES6ImportSpecifier || it.resolve() is EmberNamedElement }?.resolve() - if (resolved is EmberNamedElement) { - resolved = resolved.target - } - if (resolved == myQueryParameters.elementToSearch) { - return false - } - found = (resolved as? ES6ImportSpecifier)?.let { - val results = it.multiResolve(false) - results.any { it.element == myQueryParameters.elementToSearch } - } ?: false + val found = (resolved as? ES6ImportSpecifier)?.let { + val results = it.multiResolve(false) + results.any { it.element == myElementToSearch } + } ?: false + if (found) { + consumer.process(ref) } return !found } else { diff --git a/src/main/kotlin/com/emberjs/gts/GtsSupport.kt b/src/main/kotlin/com/emberjs/gts/GtsSupport.kt index a98ed209..87ce534d 100644 --- a/src/main/kotlin/com/emberjs/gts/GtsSupport.kt +++ b/src/main/kotlin/com/emberjs/gts/GtsSupport.kt @@ -130,6 +130,8 @@ class GtsFile(viewProvider: FileViewProvider?, val isJS: Boolean =false) override fun toString(): String { return "GTS File" } + + } diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index 53119add..db074f19 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -41,7 +41,7 @@ class GtsFileTest : BasePlatformTestCase() { function corge() {}; function corge2() {}; - export const grault = {}; + export const grault2 = {}; export const Grault = {}; export default """.trimIndent() myFixture.configureByText(GjsFileType.INSTANCE, gts) myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project) - val unusedConstants = myFixture.doHighlighting().filter { it.description.startsWith("unused constant") } - TestCase.assertEquals(unusedConstants.size, 0) - val highlightInfos: List = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" } - TestCase.assertEquals(4, highlightInfos.size) + val highlighting = myFixture.doHighlighting() + val unusedConstants = highlighting.filter { it.description?.startsWith("Unused constant") == true } + TestCase.assertEquals(unusedConstants.toString(), 2, unusedConstants.size) + val highlightInfos: List = highlighting.filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" } + TestCase.assertEquals(highlighting.toString(), 4, highlightInfos.size) TestCase.assertTrue(highlightInfos[0].description.contains("quux")) TestCase.assertTrue(highlightInfos[1].description.contains("qux")) TestCase.assertTrue(highlightInfos[2].description.contains("grault")) @@ -89,15 +91,17 @@ class GtsFileTest : BasePlatformTestCase() { {{x}} {{y}} {{bar}} - {{grault}}q + {{grault}} """.trimIndent() myFixture.configureByText(GtsFileType.INSTANCE, gts) myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project) - val unusedConstants = myFixture.doHighlighting().filter { it.description.startsWith("unused constant") } - TestCase.assertEquals(unusedConstants.size, 0) - val highlightInfos: List = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" } + val highlighting = myFixture.doHighlighting() + System.out.println(highlighting) + val unusedConstants = highlighting.filter { it.description?.startsWith("Unused constant") == true } + TestCase.assertEquals(unusedConstants.toString(), 0, unusedConstants.size) + val highlightInfos: List = highlighting.filter { it.inspectionToolId == "ES6UnusedImports" } TestCase.assertEquals(highlightInfos.size, 2) TestCase.assertTrue(highlightInfos.first().description.contains("quux")) TestCase.assertTrue(highlightInfos.last().description.contains("qux")) From 2957512b3c9504621e088dc075846b31a04b6a15 Mon Sep 17 00:00:00 2001 From: patrick Date: Wed, 6 Nov 2024 13:20:26 +0100 Subject: [PATCH 5/5] fix test --- src/test/kotlin/com/emberjs/gts/GtsFileTest.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index db074f19..8ca03631 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -4,10 +4,9 @@ import com.emberjs.gts.GjsFileType import com.emberjs.gts.GtsFileType import com.intellij.codeInsight.daemon.impl.HighlightInfo import com.intellij.lang.javascript.inspections.ES6UnusedImportsInspection -import com.intellij.lang.javascript.inspections.JSUnusedLocalSymbolsInspection import com.intellij.lang.javascript.inspections.JSUnusedGlobalSymbolsInspection +import com.intellij.lang.javascript.inspections.JSUnusedLocalSymbolsInspection import com.intellij.lang.javascript.psi.impl.JSFileImpl -import com.intellij.testFramework.IndexingTestUtil import com.intellij.testFramework.fixtures.BasePlatformTestCase import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl import junit.framework.TestCase @@ -62,11 +61,12 @@ class GtsFileTest : BasePlatformTestCase() { val unusedConstants = highlighting.filter { it.description?.startsWith("Unused constant") == true } TestCase.assertEquals(unusedConstants.toString(), 2, unusedConstants.size) val highlightInfos: List = highlighting.filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" } - TestCase.assertEquals(highlighting.toString(), 4, highlightInfos.size) - TestCase.assertTrue(highlightInfos[0].description.contains("quux")) - TestCase.assertTrue(highlightInfos[1].description.contains("qux")) - TestCase.assertTrue(highlightInfos[2].description.contains("grault")) - TestCase.assertTrue(highlightInfos[3].description.contains("corge2")) + TestCase.assertEquals(highlighting.toString(), 5, highlightInfos.size) + TestCase.assertTrue(highlightInfos[0].description, highlightInfos[0].description.contains("quux")) + TestCase.assertTrue(highlightInfos[1].description, highlightInfos[1].description.contains("qux")) + TestCase.assertTrue(highlightInfos[2].description, highlightInfos[2].description.contains("grault")) + TestCase.assertTrue(highlightInfos[3].description, highlightInfos[3].description.contains("grault")) + TestCase.assertTrue(highlightInfos[4].description, highlightInfos[4].description.contains("corge2")) } @Test