Skip to content

Commit 643e358

Browse files
committed
Kotlin UAST should visit property delegate expression
#KT-15164 Fixed
1 parent 15bfd44 commit 643e358

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

idea/testData/android/lint/sdCardTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class SdCardTest {
2929
val mypath = "<warning descr="Do not hardcode \"`/data/`\"; use `Context.getFilesDir().getPath()` instead"><warning descr="Do not hardcode \"`/data/`\"; use `Context.getFilesDir().getPath()` instead">/data/data/foo</warning></warning>"
3030
val base = "<warning descr="Do not hardcode \"`/data/`\"; use `Context.getFilesDir().getPath()` instead"><warning descr="Do not hardcode \"`/data/`\"; use `Context.getFilesDir().getPath()` instead">/data/data/foo.bar/test-profiling</warning></warning>"
3131
val s = "<warning descr="Do not hardcode \"/sdcard/\"; use `Environment.getExternalStorageDirectory().getPath()` instead"><warning descr="Do not hardcode \"/sdcard/\"; use `Environment.getExternalStorageDirectory().getPath()` instead">file://sdcard/foo</warning></warning>"
32+
33+
val sdCardPath by lazy { "<warning descr="Do not hardcode \"/sdcard/\"; use `Environment.getExternalStorageDirectory().getPath()` instead">/sdcard</warning>" }
34+
fun localPropertyTest() {
35+
val sdCardPathLocal by lazy { "<warning descr="Do not hardcode \"/sdcard/\"; use `Environment.getExternalStorageDirectory().getPath()` instead">/sdcard</warning>" }
36+
}
3237
}
3338
3439
companion object {

plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUVariable.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ import com.intellij.psi.*
2020
import org.jetbrains.kotlin.asJava.elements.KtLightElement
2121
import org.jetbrains.kotlin.psi.KtNamedDeclaration
2222
import org.jetbrains.kotlin.psi.KtParameter
23+
import org.jetbrains.kotlin.psi.KtProperty
2324
import org.jetbrains.kotlin.psi.KtVariableDeclaration
2425
import org.jetbrains.uast.*
26+
import org.jetbrains.uast.internal.acceptList
2527
import org.jetbrains.uast.java.AbstractJavaUVariable
2628
import org.jetbrains.uast.java.JavaAbstractUExpression
2729
import org.jetbrains.uast.java.JavaUAnnotation
2830
import org.jetbrains.uast.java.annotations
2931
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
3032
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
3133
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
34+
import org.jetbrains.uast.visitor.UastVisitor
3235

3336
abstract class AbstractKotlinUVariable : AbstractJavaUVariable() {
3437
override val uastInitializer: UExpression?
@@ -50,6 +53,17 @@ abstract class AbstractKotlinUVariable : AbstractJavaUVariable() {
5053
return getLanguagePlugin().convertElement(initializerExpression, this) as? UExpression ?: UastEmptyExpression
5154
}
5255

56+
val delegateExpression: UExpression? by lz {
57+
val psi = psi
58+
val expression = when (psi) {
59+
is KtLightElement<*, *> -> (psi.kotlinOrigin as? KtProperty)?.delegateExpression
60+
is UastKotlinPsiVariable -> (psi.ktElement as? KtProperty)?.delegateExpression
61+
else -> null
62+
}
63+
64+
expression?.let { getLanguagePlugin().convertElement(it, this) as? UExpression }
65+
}
66+
5367
override fun getNameIdentifier(): PsiIdentifier {
5468
val kotlinOrigin = (psi as? KtLightElement<*, *>)?.kotlinOrigin
5569
return UastLightIdentifier(psi, kotlinOrigin as KtNamedDeclaration?)
@@ -136,6 +150,14 @@ open class KotlinUField(
136150
}
137151

138152
override fun getContainingFile(): PsiFile? = (psi as? KtLightElement<*, *>)?.kotlinOrigin?.containingFile ?: psi.containingFile
153+
154+
override fun accept(visitor: UastVisitor) {
155+
if (visitor.visitField(this)) return
156+
annotations.acceptList(visitor)
157+
uastInitializer?.accept(visitor)
158+
delegateExpression?.accept(visitor)
159+
visitor.afterVisitField(this)
160+
}
139161
}
140162

141163
open class KotlinULocalVariable(
@@ -156,6 +178,14 @@ open class KotlinULocalVariable(
156178
override fun getNameIdentifier(): PsiIdentifier {
157179
return super.getNameIdentifier()
158180
}
181+
182+
override fun accept(visitor: UastVisitor) {
183+
if (visitor.visitLocalVariable(this)) return
184+
annotations.acceptList(visitor)
185+
uastInitializer?.accept(visitor)
186+
delegateExpression?.accept(visitor)
187+
visitor.afterVisitLocalVariable(this)
188+
}
159189
}
160190

161191
open class KotlinUEnumConstant(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val sdCardPath by lazy { "/sdcard" }
2+
3+
fun localPropertyTest() {
4+
val sdCardPathLocal by lazy { "/sdcard" }
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
UFile (package = )
2+
UClass (name = PropertyDelegateKt)
3+
UField (name = sdCardPath$delegate)
4+
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
5+
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1))
6+
UIdentifier (Identifier (lazy))
7+
USimpleNameReferenceExpression (identifier = lazy)
8+
ULambdaExpression
9+
UBlockExpression
10+
ULiteralExpression (value = "/sdcard")
11+
UAnnotationMethod (name = getSdCardPath)
12+
UAnnotationMethod (name = localPropertyTest)
13+
UBlockExpression
14+
UDeclarationsExpression
15+
ULocalVariable (name = sdCardPathLocal)
16+
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1))
17+
UIdentifier (Identifier (lazy))
18+
USimpleNameReferenceExpression (identifier = lazy)
19+
ULambdaExpression
20+
UBlockExpression
21+
ULiteralExpression (value = "/sdcard")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public final class PropertyDelegateKt {
2+
private static final var sdCardPath$delegate: kotlin.Lazy
3+
public static final fun getSdCardPath() : java.lang.String = UastEmptyExpression
4+
public static final fun localPropertyTest() : void {
5+
var sdCardPathLocal: <ErrorType>
6+
}
7+
}

plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
2626
@Test fun testStringTemplate() = doTest("StringTemplate")
2727

2828
@Test fun testQualifiedConstructorCall() = doTest("QualifiedConstructorCall")
29+
30+
@Test fun testPropertyDelegate() = doTest("PropertyDelegate")
2931
}

0 commit comments

Comments
 (0)