diff --git a/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt b/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt index 79a7512f330..1dd5d4b4778 100644 --- a/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt +++ b/kotlin-power-assert-plugin/src/main/kotlin/com/bnorm/power/diagram/ExpressionTree.kt @@ -76,13 +76,6 @@ fun buildTree(expression: IrExpression): Node? { val tree = RootNode() expression.accept(object : IrElementVisitor { - val INCREMENT_DECREMENT_OPERATORS = setOf( - IrStatementOrigin.PREFIX_INCR, - IrStatementOrigin.PREFIX_DECR, - IrStatementOrigin.POSTFIX_INCR, - IrStatementOrigin.POSTFIX_DECR - ) - override fun visitElement(element: IrElement, data: Node) { element.acceptChildren(this, data) } @@ -96,13 +89,14 @@ fun buildTree(expression: IrExpression): Node? { } override fun visitContainerExpression(expression: IrContainerExpression, data: Node) { - if (expression.origin in INCREMENT_DECREMENT_OPERATORS) { + if (expression.origin is IrStatementOrigin.SAFE_CALL) { + // Null safe expressions can be correctly navigated + super.visitContainerExpression(expression, data) + } else { + // Everything else is considered unsafe and terminates the expression tree val node = data as? ExpressionNode ?: ExpressionNode().also { data.addChild(it) } node.add(expression) - return // Skip the internals of increment/decrement operations } - - super.visitContainerExpression(expression, data) } override fun visitTypeOperator(expression: IrTypeOperatorCall, data: Node) { diff --git a/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/ObjectLiteralTest.kt b/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/ObjectLiteralTest.kt new file mode 100644 index 00000000000..c7d04fbdae7 --- /dev/null +++ b/kotlin-power-assert-plugin/src/test/kotlin/com/bnorm/power/ObjectLiteralTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2023 Brian Norman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.bnorm.power + +import kotlin.test.Test + +class ObjectLiteralTest { + @Test + fun `internals of object literal should not be separated`() { + assertMessage( + """ + fun main() { + assert(object { override fun toString() = "ANONYMOUS" }.toString() == "toString()") + }""", + """ + Assertion failed + assert(object { override fun toString() = "ANONYMOUS" }.toString() == "toString()") + | | | + | | false + | ANONYMOUS + ANONYMOUS + """.trimIndent(), + ) + } +}