fix: cover JVM annotation and Scala callable edge cases

This commit is contained in:
Shawn
2026-05-04 16:07:01 +08:00
parent 019eba0583
commit 6a76ad4782
2 changed files with 24 additions and 1 deletions
+7 -1
View File
@@ -532,7 +532,9 @@ function extractFromJvm(
function stripJvmAnnotations(text: string): string {
return text
.split("\n")
.filter((line) => !line.trim().startsWith("@"))
.map((line) =>
line.replace(/^\s*(?:@(?:[\w$]+:)?[\w$.]+(?:\([^)]*\))?\s*)+/, "")
)
.join("\n");
}
@@ -551,6 +553,10 @@ function extractJvmCallableName(text: string): string | null {
.split("{", 1)[0]
.split("=", 1)[0]
.trim();
const scalaDefMatches = Array.from(signature.matchAll(/\bdef\s+([A-Za-z_$][\w$]*)\b/g));
if (scalaDefMatches.length > 0) {
return scalaDefMatches[scalaDefMatches.length - 1][1];
}
const matches = Array.from(signature.matchAll(/([A-Za-z_$][\w$]*)\s*\(/g));
return matches.length > 0 ? matches[matches.length - 1][1] : null;
}
+17
View File
@@ -184,6 +184,19 @@ class SecurityAuthClientRequireSubjectTest {
expect(names).not.toContain("Test");
});
it("preserves Java declarations when annotations share the same line", () => {
const src = `
class InlineAnnotationTest {
@Test void cleanup() {}
}
`;
const out = extractSymbolsAndCalls(src, "java" as unknown as Lang, ".java", "InlineAnnotationTest.java");
const names = out.symbols.map((s) => s.name);
expect(names).toContain("InlineAnnotationTest");
expect(names).toContain("cleanup");
expect(names).not.toContain("Test");
});
it("extracts Kotlin top-level fun and class methods", () => {
const src = `
fun greet(name: String): String = "Hi"
@@ -203,6 +216,8 @@ class Bar {
const src = `
class Foo {
def bar(): Int = 1
def size: Int = 1
def now = Instant.now()
}
object Main {
@@ -212,6 +227,8 @@ object Main {
const out = extractSymbolsAndCalls(src, "scala" as unknown as Lang, ".scala", "Main.scala");
const names = out.symbols.map((s) => s.name);
expect(names).toContain("bar");
expect(names).toContain("size");
expect(names).toContain("now");
expect(names).toContain("main");
});
});