Kotlin UAST should visit property delegate expression
#KT-15164 Fixed
This commit is contained in:
+5
@@ -29,6 +29,11 @@ class SdCardTest {
|
|||||||
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>"
|
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>"
|
||||||
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>"
|
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>"
|
||||||
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>"
|
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>"
|
||||||
|
|
||||||
|
val sdCardPath by lazy { "<warning descr="Do not hardcode \"/sdcard/\"; use `Environment.getExternalStorageDirectory().getPath()` instead">/sdcard</warning>" }
|
||||||
|
fun localPropertyTest() {
|
||||||
|
val sdCardPathLocal by lazy { "<warning descr="Do not hardcode \"/sdcard/\"; use `Environment.getExternalStorageDirectory().getPath()` instead">/sdcard</warning>" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -20,8 +20,10 @@ import com.intellij.psi.*
|
|||||||
import org.jetbrains.kotlin.asJava.elements.KtLightElement
|
import org.jetbrains.kotlin.asJava.elements.KtLightElement
|
||||||
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtParameter
|
import org.jetbrains.kotlin.psi.KtParameter
|
||||||
|
import org.jetbrains.kotlin.psi.KtProperty
|
||||||
import org.jetbrains.kotlin.psi.KtVariableDeclaration
|
import org.jetbrains.kotlin.psi.KtVariableDeclaration
|
||||||
import org.jetbrains.uast.*
|
import org.jetbrains.uast.*
|
||||||
|
import org.jetbrains.uast.internal.acceptList
|
||||||
import org.jetbrains.uast.java.AbstractJavaUVariable
|
import org.jetbrains.uast.java.AbstractJavaUVariable
|
||||||
import org.jetbrains.uast.java.JavaAbstractUExpression
|
import org.jetbrains.uast.java.JavaAbstractUExpression
|
||||||
import org.jetbrains.uast.java.JavaUAnnotation
|
import org.jetbrains.uast.java.JavaUAnnotation
|
||||||
@@ -29,6 +31,7 @@ import org.jetbrains.uast.java.annotations
|
|||||||
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
|
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
|
||||||
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
|
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
|
||||||
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
|
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
|
||||||
|
import org.jetbrains.uast.visitor.UastVisitor
|
||||||
|
|
||||||
abstract class AbstractKotlinUVariable : AbstractJavaUVariable() {
|
abstract class AbstractKotlinUVariable : AbstractJavaUVariable() {
|
||||||
override val uastInitializer: UExpression?
|
override val uastInitializer: UExpression?
|
||||||
@@ -50,6 +53,17 @@ abstract class AbstractKotlinUVariable : AbstractJavaUVariable() {
|
|||||||
return getLanguagePlugin().convertElement(initializerExpression, this) as? UExpression ?: UastEmptyExpression
|
return getLanguagePlugin().convertElement(initializerExpression, this) as? UExpression ?: UastEmptyExpression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val delegateExpression: UExpression? by lz {
|
||||||
|
val psi = psi
|
||||||
|
val expression = when (psi) {
|
||||||
|
is KtLightElement<*, *> -> (psi.kotlinOrigin as? KtProperty)?.delegateExpression
|
||||||
|
is UastKotlinPsiVariable -> (psi.ktElement as? KtProperty)?.delegateExpression
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
expression?.let { getLanguagePlugin().convertElement(it, this) as? UExpression }
|
||||||
|
}
|
||||||
|
|
||||||
override fun getNameIdentifier(): PsiIdentifier {
|
override fun getNameIdentifier(): PsiIdentifier {
|
||||||
val kotlinOrigin = (psi as? KtLightElement<*, *>)?.kotlinOrigin
|
val kotlinOrigin = (psi as? KtLightElement<*, *>)?.kotlinOrigin
|
||||||
return UastLightIdentifier(psi, kotlinOrigin as KtNamedDeclaration?)
|
return UastLightIdentifier(psi, kotlinOrigin as KtNamedDeclaration?)
|
||||||
@@ -136,6 +150,14 @@ open class KotlinUField(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getContainingFile(): PsiFile? = (psi as? KtLightElement<*, *>)?.kotlinOrigin?.containingFile ?: psi.containingFile
|
override fun getContainingFile(): PsiFile? = (psi as? KtLightElement<*, *>)?.kotlinOrigin?.containingFile ?: psi.containingFile
|
||||||
|
|
||||||
|
override fun accept(visitor: UastVisitor) {
|
||||||
|
if (visitor.visitField(this)) return
|
||||||
|
annotations.acceptList(visitor)
|
||||||
|
uastInitializer?.accept(visitor)
|
||||||
|
delegateExpression?.accept(visitor)
|
||||||
|
visitor.afterVisitField(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
open class KotlinULocalVariable(
|
open class KotlinULocalVariable(
|
||||||
@@ -156,6 +178,14 @@ open class KotlinULocalVariable(
|
|||||||
override fun getNameIdentifier(): PsiIdentifier {
|
override fun getNameIdentifier(): PsiIdentifier {
|
||||||
return super.getNameIdentifier()
|
return super.getNameIdentifier()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun accept(visitor: UastVisitor) {
|
||||||
|
if (visitor.visitLocalVariable(this)) return
|
||||||
|
annotations.acceptList(visitor)
|
||||||
|
uastInitializer?.accept(visitor)
|
||||||
|
delegateExpression?.accept(visitor)
|
||||||
|
visitor.afterVisitLocalVariable(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
open class KotlinUEnumConstant(
|
open class KotlinUEnumConstant(
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
val sdCardPath by lazy { "/sdcard" }
|
||||||
|
|
||||||
|
fun localPropertyTest() {
|
||||||
|
val sdCardPathLocal by lazy { "/sdcard" }
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
UFile (package = )
|
||||||
|
UClass (name = PropertyDelegateKt)
|
||||||
|
UField (name = sdCardPath$delegate)
|
||||||
|
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||||
|
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1))
|
||||||
|
UIdentifier (Identifier (lazy))
|
||||||
|
USimpleNameReferenceExpression (identifier = lazy)
|
||||||
|
ULambdaExpression
|
||||||
|
UBlockExpression
|
||||||
|
ULiteralExpression (value = "/sdcard")
|
||||||
|
UAnnotationMethod (name = getSdCardPath)
|
||||||
|
UAnnotationMethod (name = localPropertyTest)
|
||||||
|
UBlockExpression
|
||||||
|
UDeclarationsExpression
|
||||||
|
ULocalVariable (name = sdCardPathLocal)
|
||||||
|
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1))
|
||||||
|
UIdentifier (Identifier (lazy))
|
||||||
|
USimpleNameReferenceExpression (identifier = lazy)
|
||||||
|
ULambdaExpression
|
||||||
|
UBlockExpression
|
||||||
|
ULiteralExpression (value = "/sdcard")
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
public final class PropertyDelegateKt {
|
||||||
|
private static final var sdCardPath$delegate: kotlin.Lazy
|
||||||
|
public static final fun getSdCardPath() : java.lang.String = UastEmptyExpression
|
||||||
|
public static final fun localPropertyTest() : void {
|
||||||
|
var sdCardPathLocal: <ErrorType>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,4 +26,6 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
|
|||||||
@Test fun testStringTemplate() = doTest("StringTemplate")
|
@Test fun testStringTemplate() = doTest("StringTemplate")
|
||||||
|
|
||||||
@Test fun testQualifiedConstructorCall() = doTest("QualifiedConstructorCall")
|
@Test fun testQualifiedConstructorCall() = doTest("QualifiedConstructorCall")
|
||||||
|
|
||||||
|
@Test fun testPropertyDelegate() = doTest("PropertyDelegate")
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user