Skip to content
Draft
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
8 changes: 7 additions & 1 deletion jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,12 @@ class EtsMethodBuilder(

fun ClassDto.toEtsClass(enclosingFileName: String = signature.declaringFileName): EtsClass {
val signature = signature.toEtsClassSignature()
val superClassSignature = superClassName?.takeIf { it != "" }?.let { name ->
val typedSuperClass = superClassType?.let { type ->
val convertedType = type.toEtsType()
require(convertedType is EtsClassType) { "Invalid superclass type for $signature: $convertedType" }
convertedType
}
val superClassSignature = typedSuperClass?.signature ?: superClassName?.takeIf { it != "" }?.let { name ->
EtsClassSignature(
name = name,
file = EtsFileSignature.UNKNOWN,
Expand All @@ -698,6 +703,7 @@ fun ClassDto.toEtsClass(enclosingFileName: String = signature.declaringFileName)
methods = methods,
category = category,
superClass = superClassSignature,
superClassType = typedSuperClass,
implementedInterfaces = implementedInterfaces,
typeParameters = typeParameters,
modifiers = modifiers,
Expand Down
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 @@ -67,6 +67,7 @@ data class ClassDto(
val implementedInterfaceNames: List<String>,
val fields: List<FieldDto>,
val methods: List<MethodDto>,
val superClassType: TypeDto? = null,
)

@Serializable
Expand Down
3 changes: 3 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/model/Class.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface EtsClass : Base {
val ctor: EtsMethod
val category: EtsClassCategory
val superClass: EtsClassSignature?
val superClassType: EtsClassType?
get() = null
val implementedInterfaces: List<EtsClassSignature>

val declaringFile: EtsFile?
Expand All @@ -46,6 +48,7 @@ class EtsClassImpl(
override val typeParameters: List<EtsType> = emptyList(),
override val modifiers: EtsModifiers = EtsModifiers.EMPTY,
override val decorators: List<EtsDecorator> = emptyList(),
override val superClassType: EtsClassType? = null,
) : EtsClass {
init {
fields.forEach { (it as EtsFieldImpl).declaringClass = this }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import org.jacodb.ets.model.EtsLocal
import org.jacodb.ets.model.EtsNewArrayExpr
import org.jacodb.ets.model.EtsNewExpr
import org.jacodb.ets.model.EtsNumberConstant
import org.jacodb.ets.model.EtsNumberType
import org.jacodb.ets.model.EtsPropertyRef
import org.jacodb.ets.model.EtsPtrCallExpr
import org.jacodb.ets.model.EtsRawEntity
Expand Down Expand Up @@ -591,6 +592,20 @@ class EtsTsFrontendTest {
assertTrue(stmts.any { it is RawStmtDto && it.kind == "UnsupportedStmt" })
}

@Test
fun `parameterized superclass identity survives JSON conversion`() {
val dto = runFrontend("class Base<T> {} export class Derived extends Base<number> {}")
val serialized = Json { serializersModule = dtoModule }.encodeToString(dto)
val classes = EtsFileDto.loadFromJson(serialized).toEtsFile().classes
val base = classes.single { it.name == "Base" }
val derived = classes.single { it.name == "Derived" }
val superclass = requireNotNull(derived.superClassType)

assertEquals(expected = base.signature, actual = superclass.signature)
assertEquals(expected = listOf(EtsNumberType), actual = superclass.typeParameters)
assertEquals(expected = base.signature, actual = derived.superClass)
}

@Test
fun `named object method is unsupported before allocation through JSON conversion`() {
val dto = runFrontend("export function value() { return { x: 1, method() { return this.x; } }; }")
Expand Down
4 changes: 3 additions & 1 deletion jacodb-ets/ts-frontend/src/dto/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
FileSignatureDto,
} from "./signatures";
import { StmtDto } from "./stmts";
import { LexicalEnvTypeDefinitionDto, TypeDto } from "./types";
import { ClassTypeDto, LexicalEnvTypeDefinitionDto, TypeDto } from "./types";

export interface EtsFileDto {
signature: FileSignatureDto;
Expand All @@ -53,6 +53,8 @@ export interface ClassDto {
category?: ClassCategoryValue; // Kotlin default: 0 (CLASS)
typeParameters?: TypeDto[]; // Kotlin default: null
superClassName: string | null; // required by Kotlin (nullable, no default); "" means "no superclass"
/** Exact source superclass and its type arguments when the heritage declaration resolves. */
superClassType?: ClassTypeDto;
implementedInterfaceNames: string[];
fields: FieldDto[];
methods: MethodDto[];
Expand Down
25 changes: 22 additions & 3 deletions jacodb-ets/ts-frontend/src/lowering/classBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ import { StmtLowerer, ensureSupportedBindingPattern } from "./stmtLowering";

type ClassMemberDecl = ts.MethodDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration;

interface SuperClassInfo {
signature: ClassSignatureDto;
type?: ClassTypeDto;
constructorDecl?: ts.ConstructorDeclaration;
}

export class ClassBuilder {
constructor(private readonly ctx: LoweringContext) {}

Expand Down Expand Up @@ -170,6 +176,9 @@ export class ClassBuilder {
fields,
methods,
};
if (superClass?.type !== undefined) {
result.superClassType = superClass.type;
}
const typeParameters = this.ctx.converter.convertTypeParameters(decl.typeParameters);
if (typeParameters !== undefined) {
result.typeParameters = typeParameters;
Expand Down Expand Up @@ -371,11 +380,12 @@ export class ClassBuilder {
const name = nameOverride ?? key.name;
const { parameters, prologueParams } = buildParameters(this.ctx, decl);
const returnType = returnTypeOf(this.ctx, decl);
const isAccessor = ts.isGetAccessorDeclaration(decl) || ts.isSetAccessorDeclaration(decl);

const method: MethodDto = {
isStrictMode: isStrictMode(decl),
signature: { declaringClass, ...key, name, parameters, returnType },
modifiers: modifiersOf(decl),
modifiers: modifiersOf(decl) | (isAccessor ? Modifier.ACCESSOR : 0),
decorators: decoratorsOf(decl),
};
const typeParameters = this.ctx.converter.convertTypeParameters(decl.typeParameters);
Expand Down Expand Up @@ -596,7 +606,7 @@ export class ClassBuilder {
private superClassInfo(
decl: ts.ClassLikeDeclaration,
visitedClasses: Set<ts.ClassDeclaration> = new Set(),
): { signature: ClassSignatureDto; constructorDecl?: ts.ConstructorDeclaration } | undefined {
): SuperClassInfo | undefined {
const extended = decl.heritageClauses
?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)
?.types[0];
Expand All @@ -612,6 +622,15 @@ export class ClassBuilder {
name: extended.expression.getText(),
declaringFile: UNKNOWN_FILE_SIGNATURE,
};
const knownSourceBase = classDeclaration !== undefined && !classDeclaration.getSourceFile().isDeclarationFile
&& signature.declaringFile.fileName !== UNKNOWN_FILE_SIGNATURE.fileName;
const type: ClassTypeDto | undefined = knownSourceBase
? {
_: "ClassType",
signature,
typeParameters: extended.typeArguments?.map((arg) => this.ctx.converter.convertTypeNode(arg)),
}
: undefined;
const ownConstructor = classDeclaration?.members.find(
(member): member is ts.ConstructorDeclaration => ts.isConstructorDeclaration(member) && member.body !== undefined,
) ?? classDeclaration?.members.find(ts.isConstructorDeclaration);
Expand All @@ -626,7 +645,7 @@ export class ClassBuilder {
constructorDecl = this.superClassInfo(classDeclaration, visitedClasses)?.constructorDecl;
}

return constructorDecl === undefined ? { signature } : { signature, constructorDecl };
return { signature, type, constructorDecl };
}

private emitInstInitCall(m: MethodContext, declaringClass: ClassSignatureDto): void {
Expand Down
40 changes: 40 additions & 0 deletions jacodb-ets/ts-frontend/test/classes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,46 @@ function expectUnsupportedLifecycle(clazz: ClassDto): void {
}

describe("class lowering", () => {
it("preserves the declaring class and type arguments of an aliased superclass", () => {
const { file, diagnostics } = lowerProject({
"/base.ts": "export class Base<T> { declare value: T; }",
"/barrel.ts": "export { Base as Alias } from './base';",
"/derived.ts": `
import { Alias } from "./barrel";
export class Derived<V> extends Alias<[V, number]> {}
`,
}, "/derived.ts");
const derived = classByName(file, "Derived");

expect(diagnostics.messages).toEqual([]);
expect(derived.superClassType).toMatchObject({
_: "ClassType",
signature: { name: "Base", declaringFile: { projectName: "proj", fileName: "/base.ts" } },
typeParameters: [{ _: "TupleType", types: [{ _: "GenericType", name: "V" }, { _: "NumberType" }] }],
});
});

it("distinguishes declared accessors from ordinary methods", () => {
const { file } = lower(`
class Example {
get value(): number { return 1; }
set value(next: number) {}
ordinary(): number { return 2; }
}
`);

const clazz = classByName(file, "Example");
const accessors = clazz.methods.filter((method) => (method.modifiers & Modifier.ACCESSOR) !== 0);

expect(accessors.map((method) => ({
name: method.signature.name,
parameterCount: method.signature.parameters.length,
}))).toEqual([
{ name: "value", parameterCount: 0 },
{ name: "value", parameterCount: 1 },
]);
});

it("keeps computed class definition and lifecycle raw while retaining body declarations", () => {
const { file, diagnostics } = lower(`
let evaluations = 0;
Expand Down
Loading