Skip to content

Commit 11806e1

Browse files
authored
fix unused exported vars & functions (#196)
1 parent c1b89fa commit 11806e1

File tree

4 files changed

+82
-41
lines changed

4 files changed

+82
-41
lines changed

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
kotlin.stdlib.default.dependency = false
2+
org.gradle.jvmargs=-Xmx4g

src/main/kotlin/com/emberjs/gts/GtsReferenceSearcher.kt

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.emberjs.hbs.ResolvedReference
66
import com.emberjs.psi.EmberNamedElement
77
import com.emberjs.utils.ifTrue
88
import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier
9+
import com.intellij.lang.javascript.JavaScriptSupportLoader
910
import com.intellij.lang.javascript.psi.JSPsiNamedElementBase
1011
import com.intellij.lang.javascript.psi.JSVariable
1112
import com.intellij.openapi.application.QueryExecutorBase
@@ -23,66 +24,86 @@ import com.intellij.util.Processor
2324

2425
class GtsReferenceSearcher : QueryExecutorBase<PsiReference?, ReferencesSearch.SearchParameters>(true) {
2526
override fun processQuery(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor<in PsiReference?>) {
26-
val element = queryParameters.elementToSearch
27+
var element = queryParameters.elementToSearch
2728
if (element is JSPsiNamedElementBase) {
2829
val name = element.name
2930
if (name != null) {
31+
if (element.containingFile is GtsFile){
32+
if (element is JSVariable) {
33+
val psi = element.containingFile.viewProvider.getPsi(JavaScriptSupportLoader.TYPESCRIPT) ?: element.containingFile.viewProvider.getPsi(JavaScriptSupportLoader.ECMA_SCRIPT_6)
34+
element = psi.findElementAt(element.textOffset)?.parent ?: element
35+
}
36+
}
3037
val effectiveScope = if (element is JSVariable && (queryParameters.effectiveSearchScope as? LocalSearchScope)?.scope?.size == 1) {
3138
LocalSearchScope(element.containingFile)
3239
} else {
3340
queryParameters.effectiveSearchScope
3441
}
3542
val collector = queryParameters.optimizer
36-
collector.searchWord(name, effectiveScope, 1.toShort(), true, element, MyProcessor(queryParameters))
43+
collector.searchWord(name, effectiveScope, 1.toShort(), true, element, MyProcessor(queryParameters, element))
3744
}
3845
}
3946
}
4047

41-
private class MyProcessor(queryParameters: ReferencesSearch.SearchParameters) : RequestResultProcessor() {
48+
private class MyProcessor(queryParameters: ReferencesSearch.SearchParameters, elementToSearch: PsiElement) : RequestResultProcessor() {
4249
private val myQueryParameters: ReferencesSearch.SearchParameters
4350
private val myOwnCollector: SearchRequestCollector
51+
private val myElementToSearch: PsiElement
4452

4553
init {
4654
myQueryParameters = queryParameters
55+
myElementToSearch = elementToSearch
4756
myOwnCollector = SearchRequestCollector(queryParameters.optimizer.searchSession)
4857
}
4958

5059
override fun processTextOccurrence(element: PsiElement, offsetInElement: Int, consumer: Processor<in PsiReference>): Boolean {
51-
if (myQueryParameters.elementToSearch.containingFile is GtsFile) {
52-
consumer.process(ResolvedReference(myQueryParameters.elementToSearch, myQueryParameters.elementToSearch))
60+
if (myElementToSearch.containingFile is GtsFile) {
61+
consumer.process(ResolvedReference(myElementToSearch, myElementToSearch))
5362
return false
5463
}
5564
return if ((element is HbPsiElement && element.elementType == HbTokenTypes.ID) || (element is XmlToken && element.parent is HtmlTag)) {
5665
val elem = (element is XmlToken && element.parent is HtmlTag).ifTrue { element.parent } ?: element
57-
var found = elem.reference?.isReferenceTo(myQueryParameters.elementToSearch) == true || elem.references.any {
58-
it.isReferenceTo(myQueryParameters.elementToSearch)
66+
var foundRef = (elem.reference?.isReferenceTo(myElementToSearch) == true).ifTrue { elem.reference } ?: elem.references.find {
67+
it.isReferenceTo(myElementToSearch)
68+
}
69+
70+
if (foundRef != null) {
71+
consumer.process(foundRef)
72+
return false
5973
}
6074

61-
if (!found) {
62-
var resolved = elem.reference?.resolve()
63-
if (resolved is EmberNamedElement) {
64-
resolved = resolved.target
65-
}
66-
if (resolved == myQueryParameters.elementToSearch) {
67-
return false
68-
}
69-
found = (resolved as? ES6ImportSpecifier)?.let {
70-
val results = it.multiResolve(false)
71-
results.any { it.element == myQueryParameters.elementToSearch }
72-
} ?: false
75+
var resolved = elem.reference?.resolve()
76+
if (resolved is EmberNamedElement) {
77+
resolved = resolved.target
78+
}
79+
if (resolved == myElementToSearch) {
80+
foundRef = elem.reference
81+
consumer.process(foundRef)
82+
return false
83+
}
84+
foundRef = (resolved as? ES6ImportSpecifier)?.let {
85+
val results = it.multiResolve(false)
86+
results.find { it.element == myElementToSearch }?.let { ResolvedReference(element, myElementToSearch) }
87+
}
88+
if (foundRef != null) {
89+
consumer.process(foundRef)
90+
return false
91+
}
92+
val ref = elem.references.find { it.resolve() is ES6ImportSpecifier || it.resolve() is EmberNamedElement }
93+
resolved = ref?.resolve()
94+
if (resolved is EmberNamedElement) {
95+
resolved = resolved.target
96+
}
97+
if (resolved == myElementToSearch) {
98+
consumer.process(ref)
99+
return false
73100
}
74-
if (!found) {
75-
var resolved = elem.references.find { it.resolve() is ES6ImportSpecifier || it.resolve() is EmberNamedElement }?.resolve()
76-
if (resolved is EmberNamedElement) {
77-
resolved = resolved.target
78-
}
79-
if (resolved == myQueryParameters.elementToSearch) {
80-
return false
81-
}
82-
found = (resolved as? ES6ImportSpecifier)?.let {
83-
val results = it.multiResolve(false)
84-
results.any { it.element == myQueryParameters.elementToSearch }
85-
} ?: false
101+
val found = (resolved as? ES6ImportSpecifier)?.let {
102+
val results = it.multiResolve(false)
103+
results.any { it.element == myElementToSearch }
104+
} ?: false
105+
if (found) {
106+
consumer.process(ref)
86107
}
87108
return !found
88109
} else {

src/main/kotlin/com/emberjs/gts/GtsSupport.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class GtsFile(viewProvider: FileViewProvider?, val isJS: Boolean =false)
130130
override fun toString(): String {
131131
return "GTS File"
132132
}
133+
134+
133135
}
134136

135137

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import com.emberjs.gts.GjsFileType
44
import com.emberjs.gts.GtsFileType
55
import com.intellij.codeInsight.daemon.impl.HighlightInfo
66
import com.intellij.lang.javascript.inspections.ES6UnusedImportsInspection
7+
import com.intellij.lang.javascript.inspections.JSUnusedGlobalSymbolsInspection
78
import com.intellij.lang.javascript.inspections.JSUnusedLocalSymbolsInspection
89
import com.intellij.lang.javascript.psi.impl.JSFileImpl
9-
import com.intellij.testFramework.IndexingTestUtil
1010
import com.intellij.testFramework.fixtures.BasePlatformTestCase
1111
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl
1212
import junit.framework.TestCase
@@ -40,25 +40,33 @@ class GtsFileTest : BasePlatformTestCase() {
4040
function corge() {};
4141
function corge2() {};
4242
43+
export const grault2 = {};
44+
export const Grault = {};
4345
4446
export default <template>
4547
<Foo />
4648
<Baz />
49+
<Grault />
4750
{{x}}
4851
{{y}}
4952
{{corge}}
5053
{{bar}}
54+
{{grault2}}
5155
</template>
5256
""".trimIndent()
5357
myFixture.configureByText(GjsFileType.INSTANCE, gts)
54-
myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection())
58+
myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection())
5559
CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project)
56-
val highlightInfos: List<HighlightInfo> = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" }
57-
TestCase.assertEquals(4, highlightInfos.size)
58-
TestCase.assertTrue(highlightInfos[0].description.contains("quux"))
59-
TestCase.assertTrue(highlightInfos[1].description.contains("qux"))
60-
TestCase.assertTrue(highlightInfos[2].description.contains("grault"))
61-
TestCase.assertTrue(highlightInfos[3].description.contains("corge2"))
60+
val highlighting = myFixture.doHighlighting()
61+
val unusedConstants = highlighting.filter { it.description?.startsWith("Unused constant") == true }
62+
TestCase.assertEquals(unusedConstants.toString(), 2, unusedConstants.size)
63+
val highlightInfos: List<HighlightInfo> = highlighting.filter { it.inspectionToolId == "ES6UnusedImports" || it.inspectionToolId == "JSUnusedLocalSymbols" }
64+
TestCase.assertEquals(highlighting.toString(), 5, highlightInfos.size)
65+
TestCase.assertTrue(highlightInfos[0].description, highlightInfos[0].description.contains("quux"))
66+
TestCase.assertTrue(highlightInfos[1].description, highlightInfos[1].description.contains("qux"))
67+
TestCase.assertTrue(highlightInfos[2].description, highlightInfos[2].description.contains("grault"))
68+
TestCase.assertTrue(highlightInfos[3].description, highlightInfos[3].description.contains("grault"))
69+
TestCase.assertTrue(highlightInfos[4].description, highlightInfos[4].description.contains("corge2"))
6270
}
6371

6472
@Test
@@ -73,18 +81,27 @@ class GtsFileTest : BasePlatformTestCase() {
7381
7482
const Baz = {};
7583
84+
export const grault = {};
85+
export const Grault = {};
86+
7687
export default <template>
7788
<Foo />
7889
<Baz />
90+
<Grault />
7991
{{x}}
8092
{{y}}
8193
{{bar}}
94+
{{grault}}
8295
</template>
8396
""".trimIndent()
8497
myFixture.configureByText(GtsFileType.INSTANCE, gts)
85-
myFixture.enableInspections(ES6UnusedImportsInspection())
98+
myFixture.enableInspections(ES6UnusedImportsInspection(), JSUnusedLocalSymbolsInspection(), JSUnusedGlobalSymbolsInspection())
8699
CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project)
87-
val highlightInfos: List<HighlightInfo> = myFixture.doHighlighting().filter { it.inspectionToolId == "ES6UnusedImports" }
100+
val highlighting = myFixture.doHighlighting()
101+
System.out.println(highlighting)
102+
val unusedConstants = highlighting.filter { it.description?.startsWith("Unused constant") == true }
103+
TestCase.assertEquals(unusedConstants.toString(), 0, unusedConstants.size)
104+
val highlightInfos: List<HighlightInfo> = highlighting.filter { it.inspectionToolId == "ES6UnusedImports" }
88105
TestCase.assertEquals(highlightInfos.size, 2)
89106
TestCase.assertTrue(highlightInfos.first().description.contains("quux"))
90107
TestCase.assertTrue(highlightInfos.last().description.contains("qux"))

0 commit comments

Comments
 (0)