Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add class category #311

Open
wants to merge 2 commits into
base: neo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
DEST_DIR="arkanalyzer"
MAX_RETRIES=10
RETRY_DELAY=3 # Delay between retries in seconds
BRANCH="neo/2025-03-20"
BRANCH="neo/2025-03-21"

for ((i=1; i<=MAX_RETRIES; i++)); do
git clone --depth=1 --branch $BRANCH $REPO_URL $DEST_DIR && break
Expand Down
15 changes: 15 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import org.jacodb.ets.base.EtsYieldExpr
import org.jacodb.ets.graph.EtsCfg
import org.jacodb.ets.model.EtsClass
import org.jacodb.ets.model.EtsClassCategory
import org.jacodb.ets.model.EtsClassImpl
import org.jacodb.ets.model.EtsClassSignature
import org.jacodb.ets.model.EtsDecorator
Expand Down Expand Up @@ -534,6 +535,7 @@
val methods = methodDtos.map { it.toEtsMethod() }
val ctor = ctorDto.toEtsMethod()

val category = category.toEtsClassCategory()
val typeParameters = typeParameters?.map { it.toEtsType() } ?: emptyList()

val modifiers = EtsModifiers(modifiers)
Expand All @@ -544,6 +546,7 @@
fields = fields,
methods = methods,
ctor = ctor,
category = category,
superClass = superClassSignature,
implementedInterfaces = implementedInterfaces,
typeParameters = typeParameters,
Expand Down Expand Up @@ -788,3 +791,15 @@
type = type.toEtsType(),
)
}

private fun Int.toEtsClassCategory() : EtsClassCategory {
return when (this) {
0 -> EtsClassCategory.CLASS
1 -> EtsClassCategory.STRUCT

Check warning on line 798 in jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt#L798

Added line #L798 was not covered by tests
2 -> EtsClassCategory.INTERFACE
3 -> EtsClassCategory.ENUM
4 -> EtsClassCategory.TYPE_LITERAL
5 -> EtsClassCategory.OBJECT
else -> error("Unknown class category: $this")

Check warning on line 803 in jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt#L803

Added line #L803 was not covered by tests
}
}
1 change: 1 addition & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ data class ClassDto(
val signature: ClassSignatureDto,
val modifiers: Int,
val decorators: List<DecoratorDto>,
val category: Int = 0,
val typeParameters: List<TypeDto>? = null,
val superClassName: String?,
val implementedInterfaceNames: List<String>,
Expand Down
2 changes: 1 addition & 1 deletion jacodb-ets/src/main/kotlin/org/jacodb/ets/graph/EtsCfg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class EtsCfg(
}

override fun predecessors(node: EtsStmt): Set<EtsStmt> {
return predecessorMap[node]!!
return predecessorMap[node].orEmpty()
}

companion object {
Expand Down
2 changes: 2 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/model/EtsClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface EtsClass : EtsBaseModel {
val fields: List<EtsField>
val methods: List<EtsMethod>
val ctor: EtsMethod
val category: EtsClassCategory
val superClass: EtsClassSignature?
val implementedInterfaces: List<EtsClassSignature>

Expand All @@ -36,6 +37,7 @@ class EtsClassImpl(
override val fields: List<EtsField>,
override val methods: List<EtsMethod>,
override val ctor: EtsMethod,
override val category: EtsClassCategory = EtsClassCategory.CLASS,
override val superClass: EtsClassSignature? = null,
override val implementedInterfaces: List<EtsClassSignature> = emptyList(),
override val typeParameters: List<EtsType> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.jacodb.ets.model

enum class EtsClassCategory {
CLASS,
STRUCT,
INTERFACE,
ENUM,
TYPE_LITERAL,
OBJECT,
}

Check warning on line 26 in jacodb-ets/src/main/kotlin/org/jacodb/ets/model/EtsClassCategory.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/model/EtsClassCategory.kt#L26

Added line #L26 was not covered by tests
18 changes: 15 additions & 3 deletions jacodb-ets/src/test/kotlin/org/jacodb/ets/test/EtsFromJsonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.jacodb.ets.dto.ValueDto
import org.jacodb.ets.dto.dtoModule
import org.jacodb.ets.dto.toEtsLocal
import org.jacodb.ets.dto.toEtsMethod
import org.jacodb.ets.model.EtsClassCategory
import org.jacodb.ets.model.EtsClassSignature
import org.jacodb.ets.model.EtsFile
import org.jacodb.ets.model.EtsFileSignature
Expand Down Expand Up @@ -478,8 +479,19 @@ class EtsFromJsonTest {
val path = "/samples/etsir/ast/lang/vararg.ts.json"
val file = loadEtsFileFromResource(path)
val method = file.classes.flatMap { it.methods }.first { it.name == "f" }
assertEquals(method.parameters.size, 2)
assertEquals(method.parameters[0].isRest, false)
assertEquals(method.parameters[1].isRest, true)
assertEquals(2, method.parameters.size)
assertEquals(false, method.parameters[0].isRest)
assertEquals(true, method.parameters[1].isRest)
}

@Test
fun testClassCategory() {
val path = "/samples/etsir/ast/lang/enum.ts.json"
val file = loadEtsFileFromResource(path)
val cls = file.classes.first { it.name == "Animal" }
assertEquals(EtsClassCategory.ENUM, cls.category)
assertEquals(2, cls.fields.size)
assertEquals("Cat", cls.fields[0].name)
assertEquals("Dog", cls.fields[1].name)
}
}
4 changes: 4 additions & 0 deletions jacodb-ets/src/test/resources/samples/source/lang/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Animal {
Cat,
Dog,
}