Skip to content

Commit e05682f

Browse files
authored
fix unused imports (#197)
* fix unused imports * Update GtsFileTest.kt * fix unused imports
1 parent f1ab56b commit e05682f

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/main/kotlin/com/emberjs/hbs/HbReference.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.emberjs.hbs
33
import com.dmarcotte.handlebars.psi.HbSimpleMustache
44
import com.dmarcotte.handlebars.psi.HbStringLiteral
55
import com.dmarcotte.handlebars.psi.impl.HbStatementsImpl
6+
import com.emberjs.gts.GjsLanguage
7+
import com.emberjs.gts.GtsLanguage
68
import com.emberjs.index.EmberNameIndex
79
import com.emberjs.psi.EmberNamedAttribute
810
import com.emberjs.psi.EmberNamedElement
@@ -11,6 +13,7 @@ import com.emberjs.resolver.EmberName
1113
import com.emberjs.utils.originalVirtualFile
1214
import com.emberjs.xml.EmberAttrDec
1315
import com.intellij.lang.Language
16+
import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier
1417
import com.intellij.openapi.util.TextRange
1518
import com.intellij.psi.PsiDirectory
1619
import com.intellij.psi.PsiElement
@@ -34,6 +37,28 @@ abstract class HbReference(element: PsiElement): PsiReferenceBase<PsiElement>(el
3437
if (res is EmberNamedElement) {
3538
res = res.target
3639
}
40+
if (res is ES6ImportSpecifier) {
41+
var resolved = res.containingFile.viewProvider.findReferenceAt(res.textOffset, GtsLanguage.INSTANCE)?.resolve()
42+
resolved = resolved ?: res.containingFile.viewProvider.findReferenceAt(res.textOffset, GjsLanguage.INSTANCE)?.resolve()
43+
if (element.manager.areElementsEquivalent(resolved, other)) {
44+
return true
45+
}
46+
if (element.manager.areElementsEquivalent(res.reference?.resolve(), other)) {
47+
return true
48+
}
49+
if (res.references.any {
50+
element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other)
51+
}) {
52+
return true
53+
}
54+
val results = res.multiResolve(false)
55+
val r = results.any {
56+
element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other)
57+
}
58+
if (r) {
59+
return true
60+
}
61+
}
3762
return element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other)
3863
}
3964
}

src/main/kotlin/com/emberjs/hbs/TagReferencesProvider.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import com.dmarcotte.handlebars.psi.impl.HbStatementsImpl
1212
import com.emberjs.xml.EmberAttrDec
1313
import com.emberjs.xml.EmberXmlElementDescriptor
1414
import com.emberjs.glint.GlintLanguageServiceProvider
15+
import com.emberjs.gts.GjsLanguage
1516
import com.emberjs.gts.GtsFileViewProvider
17+
import com.emberjs.gts.GtsLanguage
1618
import com.emberjs.index.EmberNameIndex
1719
import com.emberjs.psi.EmberNamedAttribute
1820
import com.emberjs.psi.EmberNamedElement
@@ -23,6 +25,7 @@ import com.emberjs.utils.originalVirtualFile
2325
import com.intellij.injected.editor.VirtualFileWindow
2426
import com.intellij.lang.Language
2527
import com.intellij.lang.ecmascript6.psi.ES6ImportDeclaration
28+
import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier
2629
import com.intellij.lang.injection.InjectedLanguageManager
2730
import com.intellij.lang.javascript.JavaScriptSupportLoader
2831
import com.intellij.lang.javascript.psi.JSObjectLiteralExpression
@@ -226,6 +229,31 @@ class TagReference(val element: XmlTag, val fullName: String, val rangeInElem: T
226229
var res = resolve()
227230
if (res is EmberNamedElement) {
228231
res = res.target
232+
if (element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other)) {
233+
return true
234+
}
235+
}
236+
if (res is ES6ImportSpecifier) {
237+
var resolved = res.containingFile.viewProvider.findReferenceAt(res.textOffset, GtsLanguage.INSTANCE)?.resolve()
238+
resolved = resolved ?: res.containingFile.viewProvider.findReferenceAt(res.textOffset, GjsLanguage.INSTANCE)?.resolve()
239+
if (element.manager.areElementsEquivalent(resolved, other)) {
240+
return true
241+
}
242+
if (element.manager.areElementsEquivalent(res.reference?.resolve(), other)) {
243+
return true
244+
}
245+
if (res.references.any {
246+
element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other)
247+
}) {
248+
return true
249+
}
250+
val results = res.multiResolve(false)
251+
val r = results.any {
252+
element.manager.areElementsEquivalent(it.element, other) || super.isReferenceTo(other)
253+
}
254+
if (r) {
255+
return true
256+
}
229257
}
230258
return element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other)
231259
}

src/test/kotlin/com/emberjs/gts/GtsFileTest.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ class GtsFileTest : BasePlatformTestCase() {
7171

7272
@Test
7373
fun testGtsImportUsed() {
74+
val otherGts = """
75+
export const OtherComponent = 2;
76+
export const other = 2;
77+
""".trimIndent()
7478
val gts = """
79+
import { OtherComponent } from './other-component';
80+
import { other } from './other-component';
7581
import x from "a";
7682
import { y, quux } from "a";
7783
import qux from "a";
@@ -85,24 +91,28 @@ class GtsFileTest : BasePlatformTestCase() {
8591
export const Grault = {};
8692
8793
export default <template>
94+
<OtherComponent />
8895
<Foo />
8996
<Baz />
9097
<Grault />
9198
{{x}}
9299
{{y}}
93100
{{bar}}
94101
{{grault}}
102+
{{other}}
95103
</template>
96104
""".trimIndent()
97-
myFixture.configureByText(GtsFileType.INSTANCE, gts)
105+
myFixture.addFileToProject("other-component.js", otherGts)
106+
myFixture.addFileToProject("main.gts", gts)
107+
myFixture.configureByFile("main.gts")
98108
myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection())
99109
CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project)
100110
val highlighting = myFixture.doHighlighting()
101111
System.out.println(highlighting)
102112
val unusedConstants = highlighting.filter { it.description?.startsWith("Unused constant") == true }
103113
TestCase.assertEquals(unusedConstants.toString(), 0, unusedConstants.size)
104114
val highlightInfos: List<HighlightInfo> = highlighting.filter { it.inspectionToolId == "ES6UnusedImports" }
105-
TestCase.assertEquals(highlightInfos.size, 2)
115+
TestCase.assertEquals(highlightInfos.toString(), 2, highlightInfos.size)
106116
TestCase.assertTrue(highlightInfos.first().description.contains("quux"))
107117
TestCase.assertTrue(highlightInfos.last().description.contains("qux"))
108118
}

0 commit comments

Comments
 (0)