Ignore object literal internals (#85)

This commit is contained in:
Brian Norman
2023-03-30 18:48:13 -05:00
committed by GitHub
parent 9603492119
commit 0b398d64a2
2 changed files with 44 additions and 11 deletions
@@ -76,13 +76,6 @@ fun buildTree(expression: IrExpression): Node? {
val tree = RootNode()
expression.accept(object : IrElementVisitor<Unit, Node> {
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) {
@@ -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(),
)
}
}