From 364620ccf0d667f2e8834f79bef63a0b1df8eb73 Mon Sep 17 00:00:00 2001 From: patrickpircher Date: Wed, 6 Nov 2024 20:05:29 +0100 Subject: [PATCH 1/3] fix unused imports --- .../com/emberjs/hbs/TagReferencesProvider.kt | 21 +++++++++++++++++++ .../kotlin/com/emberjs/gts/GtsFileTest.kt | 13 ++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt b/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt index 67c14b7a..40c81dcd 100644 --- a/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt +++ b/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt @@ -23,6 +23,7 @@ import com.emberjs.utils.originalVirtualFile import com.intellij.injected.editor.VirtualFileWindow import com.intellij.lang.Language import com.intellij.lang.ecmascript6.psi.ES6ImportDeclaration +import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier import com.intellij.lang.injection.InjectedLanguageManager import com.intellij.lang.javascript.JavaScriptSupportLoader import com.intellij.lang.javascript.psi.JSObjectLiteralExpression @@ -226,6 +227,26 @@ class TagReference(val element: XmlTag, val fullName: String, val rangeInElem: T var res = resolve() if (res is EmberNamedElement) { res = res.target + if (element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other)) { + return true + } + } + if (res is ES6ImportSpecifier) { + if (element.manager.areElementsEquivalent(res.reference?.resolve(), other)) { + return true + } + if (res.references.any { + element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other) + }) { + return true + } + val results = res.multiResolve(false) + val r = results.any { + element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other) + } + if (r) { + return true + } } return element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other) } diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index 8ca03631..fde12721 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -71,7 +71,12 @@ class GtsFileTest : BasePlatformTestCase() { @Test fun testGtsImportUsed() { + val otherGts = """ + export const OtherComponent = 2; + export const other = 2; + """.trimIndent() val gts = """ + import { OtherComponent, other } from './other-component'; import x from "a"; import { y, quux } from "a"; import qux from "a"; @@ -85,6 +90,7 @@ class GtsFileTest : BasePlatformTestCase() { export const Grault = {}; export default """.trimIndent() - myFixture.configureByText(GtsFileType.INSTANCE, gts) + myFixture.addFileToProject("other-component.ts", otherGts) + myFixture.addFileToProject("main.gts", gts) + myFixture.configureByFile("main.gts") myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project) val highlighting = myFixture.doHighlighting() @@ -102,7 +111,7 @@ class GtsFileTest : BasePlatformTestCase() { 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.assertEquals(2, highlightInfos.size) TestCase.assertTrue(highlightInfos.first().description.contains("quux")) TestCase.assertTrue(highlightInfos.last().description.contains("qux")) } From bcd2396fcb716a878a68b006783232b9f67bf3d3 Mon Sep 17 00:00:00 2001 From: Patrick Pircher Date: Wed, 6 Nov 2024 20:54:55 +0100 Subject: [PATCH 2/3] Update GtsFileTest.kt --- src/test/kotlin/com/emberjs/gts/GtsFileTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index fde12721..3b7abf87 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -101,7 +101,7 @@ class GtsFileTest : BasePlatformTestCase() { {{other}} """.trimIndent() - myFixture.addFileToProject("other-component.ts", otherGts) + myFixture.addFileToProject("other-component.js", otherGts) myFixture.addFileToProject("main.gts", gts) myFixture.configureByFile("main.gts") myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection()) From bac52189e21bf24d34d589ad2b924b2156cfbd73 Mon Sep 17 00:00:00 2001 From: patrickpircher Date: Thu, 7 Nov 2024 00:00:59 +0100 Subject: [PATCH 3/3] fix unused imports --- .../kotlin/com/emberjs/hbs/HbReference.kt | 25 +++++++++++++++++++ .../com/emberjs/hbs/TagReferencesProvider.kt | 7 ++++++ .../kotlin/com/emberjs/gts/GtsFileTest.kt | 5 ++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/emberjs/hbs/HbReference.kt b/src/main/kotlin/com/emberjs/hbs/HbReference.kt index 32ffe109..e54f979a 100644 --- a/src/main/kotlin/com/emberjs/hbs/HbReference.kt +++ b/src/main/kotlin/com/emberjs/hbs/HbReference.kt @@ -3,6 +3,8 @@ package com.emberjs.hbs import com.dmarcotte.handlebars.psi.HbSimpleMustache import com.dmarcotte.handlebars.psi.HbStringLiteral import com.dmarcotte.handlebars.psi.impl.HbStatementsImpl +import com.emberjs.gts.GjsLanguage +import com.emberjs.gts.GtsLanguage import com.emberjs.index.EmberNameIndex import com.emberjs.psi.EmberNamedAttribute import com.emberjs.psi.EmberNamedElement @@ -11,6 +13,7 @@ import com.emberjs.resolver.EmberName import com.emberjs.utils.originalVirtualFile import com.emberjs.xml.EmberAttrDec import com.intellij.lang.Language +import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiElement @@ -34,6 +37,28 @@ abstract class HbReference(element: PsiElement): PsiReferenceBase(el if (res is EmberNamedElement) { res = res.target } + if (res is ES6ImportSpecifier) { + var resolved = res.containingFile.viewProvider.findReferenceAt(res.textOffset, GtsLanguage.INSTANCE)?.resolve() + resolved = resolved ?: res.containingFile.viewProvider.findReferenceAt(res.textOffset, GjsLanguage.INSTANCE)?.resolve() + if (element.manager.areElementsEquivalent(resolved, other)) { + return true + } + if (element.manager.areElementsEquivalent(res.reference?.resolve(), other)) { + return true + } + if (res.references.any { + element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other) + }) { + return true + } + val results = res.multiResolve(false) + val r = results.any { + element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other) + } + if (r) { + return true + } + } return element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other) } } diff --git a/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt b/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt index 40c81dcd..abf5c1bf 100644 --- a/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt +++ b/src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt @@ -12,7 +12,9 @@ import com.dmarcotte.handlebars.psi.impl.HbStatementsImpl import com.emberjs.xml.EmberAttrDec import com.emberjs.xml.EmberXmlElementDescriptor import com.emberjs.glint.GlintLanguageServiceProvider +import com.emberjs.gts.GjsLanguage import com.emberjs.gts.GtsFileViewProvider +import com.emberjs.gts.GtsLanguage import com.emberjs.index.EmberNameIndex import com.emberjs.psi.EmberNamedAttribute import com.emberjs.psi.EmberNamedElement @@ -232,6 +234,11 @@ class TagReference(val element: XmlTag, val fullName: String, val rangeInElem: T } } if (res is ES6ImportSpecifier) { + var resolved = res.containingFile.viewProvider.findReferenceAt(res.textOffset, GtsLanguage.INSTANCE)?.resolve() + resolved = resolved ?: res.containingFile.viewProvider.findReferenceAt(res.textOffset, GjsLanguage.INSTANCE)?.resolve() + if (element.manager.areElementsEquivalent(resolved, other)) { + return true + } if (element.manager.areElementsEquivalent(res.reference?.resolve(), other)) { return true } diff --git a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt index 3b7abf87..2b4bbe95 100644 --- a/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt +++ b/src/test/kotlin/com/emberjs/gts/GtsFileTest.kt @@ -76,7 +76,8 @@ class GtsFileTest : BasePlatformTestCase() { export const other = 2; """.trimIndent() val gts = """ - import { OtherComponent, other } from './other-component'; + import { OtherComponent } from './other-component'; + import { other } from './other-component'; import x from "a"; import { y, quux } from "a"; import qux from "a"; @@ -111,7 +112,7 @@ class GtsFileTest : BasePlatformTestCase() { 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(2, highlightInfos.size) + TestCase.assertEquals(highlightInfos.toString(), 2, highlightInfos.size) TestCase.assertTrue(highlightInfos.first().description.contains("quux")) TestCase.assertTrue(highlightInfos.last().description.contains("qux")) }