Skip to content

armgit / Ix

Public Imported from GitHub
0

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

feat(parser): add XML parser support (.xml, .csproj, .xsd, ...) (closes #133) (#253)

Joseph Mikhail79f9c7df1b62
diff --git a/core-ingestion/package.json b/core-ingestion/package.json
index cba097cf..a3f2da50 100644
--- a/core-ingestion/package.json
+++ b/core-ingestion/package.json
@@ -34,6 +34,7 @@
"optionalDependencies": {
"@davisvaughan/tree-sitter-r": "^1.2.0",
"@tree-sitter-grammars/tree-sitter-lua": "^0.2.0",
+ "@tree-sitter-grammars/tree-sitter-xml": "^0.7.0",
"@tree-sitter-grammars/tree-sitter-zig": "^1.1.2",
"tree-sitter-bash": "^0.21.0",
"tree-sitter-elixir": "^0.3.5",
diff --git a/core-ingestion/src/__tests__/queries.xml.test.ts b/core-ingestion/src/__tests__/queries.xml.test.ts
new file mode 100644
index 00000000..b60e2e92
--- /dev/null
+++ b/core-ingestion/src/__tests__/queries.xml.test.ts
@@ -0,0 +1,35 @@
+import { describe, it, expect } from 'vitest';
+import { parseFile } from '../index.js';
+import { SupportedLanguages } from '../languages.js';
+
+const x = parseFile('/repo/a.xml', '<root/>\n');
+const describeFn = x && x.language === SupportedLanguages.XML ? describe : describe.skip;
+
+describeFn('XML queries', () => {
+ it('detects XML by .xml and project/config extensions', () => {
+ expect(parseFile('/r/a.xml', '<root/>')!.language).toBe(SupportedLanguages.XML);
+ expect(parseFile('/r/App.csproj', '<Project/>')!.language).toBe(SupportedLanguages.XML);
+ });
+
+ it('emits IMPORTS for MSBuild Include, Spring resource and XInclude href', () => {
+ const result = parseFile('/r/App.csproj', `
+<Project>
+ <ItemGroup>
+ <PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
+ <ProjectReference Include="../Core/Core.csproj"/>
+ </ItemGroup>
+ <xi:include href="config.xml"/>
+ <import resource="beans.xml"/>
+</Project>
+`);
+ expect(result).not.toBeNull();
+ const imports = result!.relationships
+ .filter(r => r.predicate === 'IMPORTS')
+ .map(r => r.importRaw ?? r.dstName);
+ expect(imports).toEqual(
+ expect.arrayContaining([
+ 'Newtonsoft.Json', '../Core/Core.csproj', 'config.xml', 'beans.xml',
+ ]),
+ );
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index cd0b1fb6..c8011c14 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -40,6 +40,9 @@ const Bash = tryLoadGrammar('tree-sitter-bash');
const Haskell = tryLoadGrammar('tree-sitter-haskell');
const Zig = tryLoadGrammar('@tree-sitter-grammars/tree-sitter-zig');
const Html = tryLoadGrammar('tree-sitter-html');
+// tree-sitter-xml exports { xml, dtd }; we use the xml sub-grammar.
+const XmlPkg = tryLoadGrammar('@tree-sitter-grammars/tree-sitter-xml');
+const Xml = XmlPkg?.xml ?? null;
// tree-sitter-sas uses ESM bindings with top-level await — incompatible with tryLoadGrammar (CJS require)
let SAS: any = null;
// @ts-ignore
@@ -185,6 +188,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {
...(Haskell ? { [SupportedLanguages.Haskell]: Haskell } : {}),
...(Zig ? { [SupportedLanguages.Zig]: Zig } : {}),
...(Html ? { [SupportedLanguages.HTML]: Html } : {}),
+ ...(Xml ? { [SupportedLanguages.XML]: Xml } : {}),
};
// Capture key prefix → NodeKind string
diff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.ts
index 69f0b618..69dd02f5 100644
--- a/core-ingestion/src/languages.ts
+++ b/core-ingestion/src/languages.ts
@@ -28,6 +28,7 @@ export enum SupportedLanguages {
Haskell = 'haskell',
Zig = 'zig',
HTML = 'html',
+ XML = 'xml',
}
const EXT_MAP: Record<string, SupportedLanguages> = {
@@ -80,6 +81,17 @@ const EXT_MAP: Record<string, SupportedLanguages> = {
'.html': SupportedLanguages.HTML,
'.htm': SupportedLanguages.HTML,
'.xhtml': SupportedLanguages.HTML,
+ '.xml': SupportedLanguages.XML,
+ '.xsd': SupportedLanguages.XML,
+ '.xsl': SupportedLanguages.XML,
+ '.xslt': SupportedLanguages.XML,
+ '.wsdl': SupportedLanguages.XML,
+ '.csproj': SupportedLanguages.XML,
+ '.vbproj': SupportedLanguages.XML,
+ '.fsproj': SupportedLanguages.XML,
+ '.props': SupportedLanguages.XML,
+ '.targets': SupportedLanguages.XML,
+ '.plist': SupportedLanguages.XML,
};
export function languageFromPath(filePath: string): SupportedLanguages | null {
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 4478087f..df17ea5d 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -1605,6 +1605,19 @@ export const R_QUERIES = `
(#eq? @_src "source")) @import
`;
+// XML. The useful cross-file signal in XML config is its dependency references,
+// so the queries extract IMPORTS from the attributes that name another file or
+// package: href (XInclude), resource (Spring), schemaLocation (XSD), src, and
+// Include (MSBuild PackageReference/ProjectReference). Quotes are stripped by the
+// shared import-specifier normalization.
+export const XML_QUERIES = `
+((element (STag (Attribute (Name) @_a (AttValue) @import.source)))
+ (#match? @_a "^(href|resource|schemaLocation|src|Include)$")) @import
+
+((element (EmptyElemTag (Attribute (Name) @_a (AttValue) @import.source)))
+ (#match? @_a "^(href|resource|schemaLocation|src|Include)$")) @import
+`;
+
// HTML. Resource dependencies as imports (<script src>, <link href>, and
// <a/img/iframe/source/use href|src>), and custom elements (hyphenated tag names
// = web components) as definitions.
@@ -1740,5 +1753,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.Haskell]: HASKELL_QUERIES,
[SupportedLanguages.Zig]: ZIG_QUERIES,
[SupportedLanguages.HTML]: HTML_QUERIES,
+ [SupportedLanguages.XML]: XML_QUERIES,
};