181: Uast: weak consistency check and fixes for UIdentifiers
This commit is contained in:
committed by
Nikolay Krasko
parent
1bf12841ae
commit
8ce65dc715
@@ -7,7 +7,10 @@ import com.intellij.psi.impl.source.tree.LeafPsiElement
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.assertedCast
|
||||
import org.jetbrains.uast.*
|
||||
import org.jetbrains.uast.JvmDeclarationUElement
|
||||
import org.jetbrains.uast.UAnchorOwner
|
||||
import org.jetbrains.uast.UElement
|
||||
import org.jetbrains.uast.UFile
|
||||
import org.jetbrains.uast.kotlin.KOTLIN_CACHED_UELEMENT_KEY
|
||||
import org.jetbrains.uast.kotlin.KotlinUastLanguagePlugin
|
||||
import org.jetbrains.uast.test.common.RenderLogTestBase
|
||||
@@ -87,7 +90,7 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
|
||||
node.containingFile.assertedCast<KtFile> { "containingFile should be KtFile for ${node.asLogString()}" }
|
||||
}
|
||||
|
||||
val anchorPsi = (node as? UDeclaration)?.uastAnchor?.psi
|
||||
val anchorPsi = (node as? UAnchorOwner)?.uastAnchor?.sourcePsi
|
||||
if (anchorPsi != null) {
|
||||
anchorPsi.containingFile.assertedCast<KtFile> { "uastAnchor.containingFile should be KtFile for ${node.asLogString()}" }
|
||||
}
|
||||
|
||||
@@ -19,4 +19,8 @@ class KotlinUastIdentifiersTest : AbstractKotlinIdentifiersTest() {
|
||||
fun testConstructors() = doTest("Constructors")
|
||||
|
||||
|
||||
@Test
|
||||
fun testSimpleAnnotated() = doTest("SimpleAnnotated")
|
||||
|
||||
|
||||
}
|
||||
+37
-4
@@ -9,11 +9,10 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.uast.UFile
|
||||
import org.jetbrains.uast.UIdentifier
|
||||
import org.jetbrains.uast.sourcePsiElement
|
||||
import org.jetbrains.uast.*
|
||||
import org.jetbrains.uast.test.env.assertEqualsToFile
|
||||
import org.jetbrains.uast.toUElementOfType
|
||||
import org.jetbrains.uast.visitor.AbstractUastVisitor
|
||||
import org.junit.ComparisonFailure
|
||||
import java.io.File
|
||||
|
||||
interface IdentifiersTestBase {
|
||||
@@ -40,10 +39,44 @@ interface IdentifiersTestBase {
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
private fun UFile.testIdentifiersParents() {
|
||||
accept(object : AbstractUastVisitor() {
|
||||
override fun visitElement(node: UElement): Boolean {
|
||||
if (node is UAnchorOwner) {
|
||||
val uastAnchor = node.uastAnchor ?: return false
|
||||
assertParents(node, uastAnchor)
|
||||
val uastAnchorFromSource = (uastAnchor.sourcePsi ?: return false).toUElementOfType<UIdentifier>()
|
||||
assertParents(node, uastAnchorFromSource)
|
||||
}
|
||||
return false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun assertParents(node: UAnchorOwner, uastAnchor: UIdentifier?) {
|
||||
//skipping such elements because they are ambiguous (properties and getters for instance)
|
||||
if (node.sourcePsi.toUElement() != node) return
|
||||
if (uastAnchor == null)
|
||||
throw AssertionError("no uast anchor for ${node.uastAnchor?.sourcePsi}(${node.uastAnchor?.sourcePsi?.javaClass}) for node = $node")
|
||||
val nodeParents = node.withContainingElements.log()
|
||||
val anchorParentParents = uastAnchor.uastParent?.withContainingElements?.log() ?: ""
|
||||
// dropping node itself because we allow children to share identifiers with parents (primary constructor for instance)
|
||||
val parentsSuffix = node.withContainingElements.drop(1).log()
|
||||
if (!anchorParentParents.endsWith(parentsSuffix))
|
||||
throw ComparisonFailure(
|
||||
"wrong parents for '${uastAnchor.sourcePsi?.text}' owner: $node[${node.sourcePsi}[${node.sourcePsi?.text}]]",
|
||||
nodeParents,
|
||||
anchorParentParents
|
||||
)
|
||||
}
|
||||
|
||||
private fun Sequence<UElement>.log() = this.joinToString { it.asLogString() }
|
||||
|
||||
fun check(testName: String, file: UFile) {
|
||||
val valuesFile = getIdentifiersFile(testName)
|
||||
|
||||
assertEqualsToFile("Identifiers", valuesFile, file.asIdentifiersWithParents())
|
||||
file.testIdentifiersParents()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user