Skip to content

Commit 151c7ff

Browse files
authored
fix wrong unused imports that reference exports of gts files (#258)
1 parent c04cc11 commit 151c7ff

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

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

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,42 @@ import com.intellij.util.Processor
2323

2424

2525
class GtsReferenceSearcher : QueryExecutorBase<PsiReference?, ReferencesSearch.SearchParameters>(true) {
26-
override fun processQuery(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor<in PsiReference?>) {
26+
override fun processQuery(
27+
queryParameters: ReferencesSearch.SearchParameters,
28+
consumer: Processor<in PsiReference?>
29+
) {
2730
var element = queryParameters.elementToSearch
2831
if (element is JSPsiNamedElementBase) {
2932
val name = element.name
3033
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-
}
37-
val effectiveScope = if (element is JSVariable && (queryParameters.effectiveSearchScope as? LocalSearchScope)?.scope?.size == 1) {
38-
LocalSearchScope(element.containingFile)
39-
} else {
40-
queryParameters.effectiveSearchScope
34+
if (element.containingFile is GtsFile) {
35+
if (element is JSVariable) {
36+
val psi = element.containingFile.viewProvider.getPsi(JavaScriptSupportLoader.TYPESCRIPT)
37+
?: element.containingFile.viewProvider.getPsi(JavaScriptSupportLoader.ECMA_SCRIPT_6)
38+
element = psi.findElementAt(element.textOffset)?.parent ?: element
39+
}
4140
}
41+
val effectiveScope =
42+
if (element is JSVariable && (queryParameters.effectiveSearchScope as? LocalSearchScope)?.scope?.size == 1) {
43+
LocalSearchScope(element.containingFile)
44+
} else {
45+
queryParameters.effectiveSearchScope
46+
}
4247
val collector = queryParameters.optimizer
43-
collector.searchWord(name, effectiveScope, 1.toShort(), true, element, MyProcessor(queryParameters, element))
48+
collector.searchWord(
49+
name,
50+
effectiveScope,
51+
1.toShort(),
52+
true,
53+
element,
54+
MyProcessor(queryParameters, element)
55+
)
4456
}
4557
}
4658
}
4759

48-
private class MyProcessor(queryParameters: ReferencesSearch.SearchParameters, elementToSearch: PsiElement) : RequestResultProcessor() {
60+
private class MyProcessor(queryParameters: ReferencesSearch.SearchParameters, elementToSearch: PsiElement) :
61+
RequestResultProcessor() {
4962
private val myQueryParameters: ReferencesSearch.SearchParameters
5063
private val myOwnCollector: SearchRequestCollector
5164
private val myElementToSearch: PsiElement
@@ -56,16 +69,21 @@ class GtsReferenceSearcher : QueryExecutorBase<PsiReference?, ReferencesSearch.S
5669
myOwnCollector = SearchRequestCollector(queryParameters.optimizer.searchSession)
5770
}
5871

59-
override fun processTextOccurrence(element: PsiElement, offsetInElement: Int, consumer: Processor<in PsiReference>): Boolean {
72+
override fun processTextOccurrence(
73+
element: PsiElement,
74+
offsetInElement: Int,
75+
consumer: Processor<in PsiReference>
76+
): Boolean {
6077
if (myElementToSearch.containingFile is GtsFile) {
6178
consumer.process(ResolvedReference(myElementToSearch, myElementToSearch))
6279
return false
6380
}
64-
return if ((element is HbPsiElement && element.elementType == HbTokenTypes.ID) || (element is XmlToken && element.parent is HtmlTag)) {
81+
return if ((element is HbPsiElement && element.elementType == HbTokenTypes.ID) || (element is XmlToken && element.parent is HtmlTag) || element is HtmlTag) {
6582
val elem = (element is XmlToken && element.parent is HtmlTag).ifTrue { element.parent } ?: element
66-
var foundRef = (elem.reference?.isReferenceTo(myElementToSearch) == true).ifTrue { elem.reference } ?: elem.references.find {
67-
it.isReferenceTo(myElementToSearch)
68-
}
83+
var foundRef = (elem.reference?.isReferenceTo(myElementToSearch) == true).ifTrue { elem.reference }
84+
?: elem.references.find {
85+
it.isReferenceTo(myElementToSearch)
86+
}
6987

7088
if (foundRef != null) {
7189
consumer.process(foundRef)
@@ -83,13 +101,20 @@ class GtsReferenceSearcher : QueryExecutorBase<PsiReference?, ReferencesSearch.S
83101
}
84102
foundRef = (resolved as? ES6ImportSpecifier)?.let {
85103
val results = it.multiResolve(false)
86-
results.find { it.element == myElementToSearch }?.let { ResolvedReference(element, myElementToSearch) }
104+
results.find {
105+
it.element?.containingFile?.viewProvider == myElementToSearch.containingFile.viewProvider &&
106+
it.element?.textOffset == myElementToSearch.textOffset
107+
}?.let { ResolvedReference(element, myElementToSearch) }
108+
?: results.find { it.element == myElementToSearch }
109+
?.let { ResolvedReference(element, myElementToSearch) }
87110
}
111+
88112
if (foundRef != null) {
89113
consumer.process(foundRef)
90114
return false
91115
}
92-
val ref = elem.references.find { it.resolve() is ES6ImportSpecifier || it.resolve() is EmberNamedElement }
116+
val ref =
117+
elem.references.find { it.resolve() is ES6ImportSpecifier || it.resolve() is EmberNamedElement }
93118
resolved = ref?.resolve()
94119
if (resolved is EmberNamedElement) {
95120
resolved = resolved.target
@@ -99,9 +124,9 @@ class GtsReferenceSearcher : QueryExecutorBase<PsiReference?, ReferencesSearch.S
99124
return false
100125
}
101126
val found = (resolved as? ES6ImportSpecifier)?.let {
102-
val results = it.multiResolve(false)
103-
results.any { it.element == myElementToSearch }
104-
} ?: false
127+
val results = it.multiResolve(false)
128+
results.any { it.element == myElementToSearch }
129+
} ?: false
105130
if (found) {
106131
consumer.process(ref)
107132
}

src/main/kotlin/com/emberjs/utils/EmberUtils.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class EmberUtils {
151151

152152
if (r is JSVariable) {
153153
val v = r.children.getOrNull(1)
154-
if (v != null) {
154+
if (v != null) {
155155
val viewProvider = file.containingFile.viewProvider
156156
val ts = viewProvider.findElementAt(v.textOffset, JavaScriptSupportLoader.TYPESCRIPT)
157157
val js = viewProvider.findElementAt(v.textOffset, JavaScriptSupportLoader.ECMA_SCRIPT_6)
@@ -348,6 +348,10 @@ class EmberUtils {
348348
}
349349

350350
if (element is ES6ImportedBinding) {
351+
val res = element.multiResolve(true).firstOrNull()
352+
if (res != null && res.element != null) {
353+
return res.element
354+
}
351355
var ref:PsiReference? = element.declaration?.fromClause?.references?.findLast { it is EmberJSModuleReference && it.rangeInElement.endOffset == it.element.textLength - 1 && it.resolve() != null } as EmberJSModuleReference?
352356
if (ref == null) {
353357
ref = element.declaration?.fromClause?.references?.findLast { it is JSFileModuleReference }

src/main/kotlin/com/emberjs/xml/EmberTagNameProvider.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,13 @@ class EmberTagNameProvider : XmlTagNameProvider {
218218
val hashYield = yieldblock.children.find { it is HbHash && it.hashName == "to"}
219219
val tsProp = yieldblock as? TypeScriptPropertySignature
220220
var namedYields = hashYield?.let { (it as HbHash).children.last().text.replace(Regex("\"|'"), "") }
221-
namedYields = namedYields ?: (yieldblock as? HbPsiElement)?.children?.any { it is HbHash && it.hashName == "to"}.ifFalse { "default" }
222221
namedYields = namedYields ?: tsProp?.name
222+
namedYields = namedYields ?: (yieldblock as? HbPsiElement)?.children?.any { it is HbHash && it.hashName == "to"}.ifFalse { "default" }
223223
val names: String
224224

225225
// if the tag has already colon, then remove it from the lookup elements, otherwise intellij will
226226
// add it again and it wil turn into <::name
227-
if (element.name.startsWith(":")) {
228-
names = namedYields!!
229-
} else {
230-
names = namedYields.let { ":$it" }
231-
}
227+
names = namedYields.let { ":$it" }
232228
// needs prioritization to appear before common html tags
233229
result.add(names.let { PrioritizedLookupElement.withPriority(LookupElementBuilder.create(it), 2.0) })
234230
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ class GtsFileTest : BasePlatformTestCase() {
7474
val otherGts = """
7575
export const OtherComponent = 2;
7676
export const other = 2;
77+
export const Comment: TOC<
78+
SomeType
79+
> = <template></template>;
7780
""".trimIndent()
7881
val gts = """
7982
import { OtherComponent } from './other-component';
83+
import { Comment } from './other-component';
8084
import { other } from './other-component';
8185
import x from "a";
8286
import { y, quux } from "a";
@@ -92,6 +96,7 @@ class GtsFileTest : BasePlatformTestCase() {
9296
9397
export default <template>
9498
<OtherComponent />
99+
<Comment />
95100
<Foo />
96101
<Baz />
97102
<Grault />

0 commit comments

Comments
 (0)