Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: enhance TypeScriptParser — Interface/Method kinds, CONTAINS edges, visibility/signature attrs

riley0227edc656734713
diff --git a/memory-layer/src/main/scala/ix/memory/ingestion/parsers/TypeScriptParser.scala b/memory-layer/src/main/scala/ix/memory/ingestion/parsers/TypeScriptParser.scala
index 4b648b40..5bc7d3f0 100644
--- a/memory-layer/src/main/scala/ix/memory/ingestion/parsers/TypeScriptParser.scala
+++ b/memory-layer/src/main/scala/ix/memory/ingestion/parsers/TypeScriptParser.scala
@@ -58,6 +58,7 @@ class TypeScriptParser extends Parser {
lineEnd = classEnd
)
relationships = relationships :+ ParsedRelationship(fileName, className, "DEFINES")
+ relationships = relationships :+ ParsedRelationship(fileName, className, "CONTAINS")
classRanges = classRanges :+ (className, lineNum, classEnd)
// Extract methods from this class
@@ -76,8 +77,10 @@ class TypeScriptParser extends Parser {
name = funcName,
kind = NodeKind.Function,
attrs = Map(
- "language" -> Json.fromString("typescript"),
- "summary" -> Json.fromString(summary)
+ "language" -> Json.fromString("typescript"),
+ "summary" -> Json.fromString(summary),
+ "signature" -> Json.fromString(summary),
+ "visibility" -> Json.fromString("public")
),
lineStart = lineNum,
lineEnd = funcEnd
@@ -103,8 +106,10 @@ class TypeScriptParser extends Parser {
name = funcName,
kind = NodeKind.Function,
attrs = Map(
- "language" -> Json.fromString("typescript"),
- "summary" -> Json.fromString(summary)
+ "language" -> Json.fromString("typescript"),
+ "summary" -> Json.fromString(summary),
+ "signature" -> Json.fromString(summary),
+ "visibility" -> Json.fromString("public")
),
lineStart = lineNum,
lineEnd = funcEnd
@@ -151,11 +156,11 @@ class TypeScriptParser extends Parser {
// Check for interface definition
InterfacePattern.findFirstMatchIn(line).foreach { m =>
val ifaceName = m.group(1)
- if (!entities.exists(e => e.name == ifaceName && e.kind == NodeKind.Class)) {
+ if (!entities.exists(e => e.name == ifaceName && e.kind == NodeKind.Interface)) {
val ifaceEnd = findBraceBlockEnd(lines, idx)
entities = entities :+ ParsedEntity(
name = ifaceName,
- kind = NodeKind.Class, // model interfaces as Class kind
+ kind = NodeKind.Interface,
attrs = Map(
"language" -> Json.fromString("typescript"),
"ts_kind" -> Json.fromString("interface")
@@ -213,6 +218,12 @@ class TypeScriptParser extends Parser {
private def insideClass(lineNum: Int, classRanges: Vector[(String, Int, Int)]): Boolean =
classRanges.exists { case (_, start, end) => lineNum > start && lineNum <= end }
+ private def extractVisibility(trimmedLine: String): String = {
+ if (trimmedLine.startsWith("private ") || trimmedLine.startsWith("private\t")) "private"
+ else if (trimmedLine.startsWith("protected ") || trimmedLine.startsWith("protected\t")) "protected"
+ else "public"
+ }
+
/**
* Extract methods from a class body.
*/
@@ -237,17 +248,20 @@ class TypeScriptParser extends Parser {
if (!TypeScriptKeywords.contains(methodName)) {
val methodEnd = findBraceBlockEnd(lines, i)
val summary = line.trim.take(120)
+ val visibility = extractVisibility(line.trim)
entities = entities :+ ParsedEntity(
name = methodName,
- kind = NodeKind.Function,
+ kind = NodeKind.Method,
attrs = Map(
- "language" -> Json.fromString("typescript"),
- "summary" -> Json.fromString(summary)
+ "language" -> Json.fromString("typescript"),
+ "summary" -> Json.fromString(summary),
+ "signature" -> Json.fromString(summary),
+ "visibility" -> Json.fromString(visibility)
),
lineStart = lineNum,
lineEnd = methodEnd
)
- relationships = relationships :+ ParsedRelationship(className, methodName, "DEFINES")
+ relationships = relationships :+ ParsedRelationship(className, methodName, "CONTAINS")
// Extract calls within this method
val calls = extractCalls(lines, i, methodEnd, methodName)
diff --git a/memory-layer/src/test/scala/ix/memory/ingestion/parsers/TypeScriptParserSpec.scala b/memory-layer/src/test/scala/ix/memory/ingestion/parsers/TypeScriptParserSpec.scala
index ff95e02a..bb9a652a 100644
--- a/memory-layer/src/test/scala/ix/memory/ingestion/parsers/TypeScriptParserSpec.scala
+++ b/memory-layer/src/test/scala/ix/memory/ingestion/parsers/TypeScriptParserSpec.scala
@@ -22,7 +22,8 @@ class TypeScriptParserSpec extends AnyFlatSpec with Matchers {
it should "extract methods from classes" in {
val result = parser.parse("api.ts", sampleCode)
- val names = result.entities.map(_.name)
+ val methods = result.entities.filter(_.kind == NodeKind.Method)
+ val names = methods.map(_.name)
names should contain("getUser")
names should contain("updateUser")
names should contain("parseResponse")
@@ -40,11 +41,11 @@ class TypeScriptParserSpec extends AnyFlatSpec with Matchers {
imports.size should be >= 2
}
- it should "extract class-to-method DEFINES edges" in {
+ it should "extract class-to-method CONTAINS edges" in {
val result = parser.parse("api.ts", sampleCode)
- val defines = result.relationships.filter(_.predicate == "DEFINES")
- val classDefines = defines.filter(_.srcName == "ApiClient")
- classDefines.size should be >= 3
+ val contains = result.relationships.filter(_.predicate == "CONTAINS")
+ val classContains = contains.filter(_.srcName == "ApiClient")
+ classContains.size should be >= 3
}
it should "extract function call relationships" in {
@@ -61,7 +62,7 @@ class TypeScriptParserSpec extends AnyFlatSpec with Matchers {
it should "store method/function signature as summary attr" in {
val result = parser.parse("api.ts", sampleCode)
- val funcs = result.entities.filter(_.kind == NodeKind.Function)
+ val funcs = result.entities.filter(e => e.kind == NodeKind.Function || e.kind == NodeKind.Method)
funcs should not be empty
funcs.foreach { f =>
f.attrs.get("summary") shouldBe defined
@@ -141,7 +142,7 @@ class TypeScriptParserSpec extends AnyFlatSpec with Matchers {
val result = parser.parse("types.ts", source)
val iface = result.entities.find(e => e.name == "UserProps")
iface shouldBe defined
- iface.get.kind shouldBe NodeKind.Class
+ iface.get.kind shouldBe NodeKind.Interface
iface.get.attrs.get("ts_kind").map(_.noSpaces) shouldBe Some("\"interface\"")
}
@@ -179,8 +180,10 @@ class TypeScriptParserSpec extends AnyFlatSpec with Matchers {
|}
""".stripMargin
val result = parser.parse("api.ts", source)
- result.relationships.exists(r => r.srcName == "Api" && r.dstName == "fetch" && r.predicate == "DEFINES") shouldBe true
- result.relationships.exists(r => r.srcName == "Api" && r.dstName == "parse" && r.predicate == "DEFINES") shouldBe true
+ result.entities.exists(e => e.name == "fetch" && e.kind == NodeKind.Method) shouldBe true
+ result.entities.exists(e => e.name == "parse" && e.kind == NodeKind.Method) shouldBe true
+ result.relationships.exists(r => r.srcName == "Api" && r.dstName == "fetch" && r.predicate == "CONTAINS") shouldBe true
+ result.relationships.exists(r => r.srcName == "Api" && r.dstName == "parse" && r.predicate == "CONTAINS") shouldBe true
}
it should "extract function call relationships from function bodies" in {
@@ -226,4 +229,93 @@ class TypeScriptParserSpec extends AnyFlatSpec with Matchers {
result.entities.exists(e => e.name == "Props") shouldBe true
result.relationships.exists(r => r.dstName == "react" && r.predicate == "IMPORTS") shouldBe true
}
+
+ it should "use Interface kind for interfaces" in {
+ val source = """
+ |export interface UserProps {
+ | name: string;
+ | age: number;
+ |}
+ """.stripMargin
+ val result = parser.parse("types.ts", source)
+ val iface = result.entities.find(e => e.name == "UserProps")
+ iface shouldBe defined
+ iface.get.kind shouldBe NodeKind.Interface
+ }
+
+ it should "use Method kind for class methods" in {
+ val source = """
+ |class Api {
+ | async fetch(url: string) {
+ | return this.http.get(url);
+ | }
+ | private parse(data: string) {
+ | return data.split(',');
+ | }
+ |}
+ """.stripMargin
+ val result = parser.parse("api.ts", source)
+ val methods = result.entities.filter(_.kind == NodeKind.Method)
+ methods.map(_.name) should contain allOf ("fetch", "parse")
+ }
+
+ it should "emit CONTAINS edges from file to class" in {
+ val source = """
+ |export class UserService {
+ | getUser(id: string): User {
+ | return this.db.find(id);
+ | }
+ |}
+ """.stripMargin
+ val result = parser.parse("service.ts", source)
+ result.relationships.exists(r =>
+ r.srcName == "service.ts" && r.dstName == "UserService" && r.predicate == "CONTAINS"
+ ) shouldBe true
+ }
+
+ it should "emit CONTAINS edges from class to method" in {
+ val source = """
+ |class Api {
+ | fetch(url: string) {
+ | return url;
+ | }
+ |}
+ """.stripMargin
+ val result = parser.parse("api.ts", source)
+ result.relationships.exists(r =>
+ r.srcName == "Api" && r.dstName == "fetch" && r.predicate == "CONTAINS"
+ ) shouldBe true
+ }
+
+ it should "store visibility attr on methods" in {
+ val source = """
+ |class Api {
+ | private parse(data: string) {
+ | return data;
+ | }
+ | public fetch(url: string) {
+ | return url;
+ | }
+ |}
+ """.stripMargin
+ val result = parser.parse("api.ts", source)
+ val parse = result.entities.find(_.name == "parse").get
+ parse.attrs.get("visibility").flatMap(_.asString) shouldBe Some("private")
+ val fetch = result.entities.find(_.name == "fetch").get
+ fetch.attrs.get("visibility").flatMap(_.asString) shouldBe Some("public")
+ }
+
+ it should "store signature attr on methods" in {
+ val source = """
+ |class Api {
+ | async fetch(url: string): Promise<Response> {
+ | return url;
+ | }
+ |}
+ """.stripMargin
+ val result = parser.parse("api.ts", source)
+ val fetch = result.entities.find(_.name == "fetch").get
+ fetch.attrs.get("signature").flatMap(_.asString) shouldBe defined
+ fetch.attrs("signature").asString.get should include("fetch")
+ }
}