Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(autoindex): index Vue and Svelte components as code, not XML (#2732) (#2735)

Cristian Groza32708f81f1e2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 88381d5b7..1c3f3e545 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -94,6 +94,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
in from the incoming id. The map's estimated heap (hash tables plus key
bytes) is reported as the new `version_map` category. On the 1 GB gate's
search node the map held ~0.5 GB that no budget saw. (#2427)
+- `xerj autoindex`: Vue (`.vue`) and Svelte (`.svelte`) single-file components are indexed as code instead of being junk-filed as malformed XML. Their `<script>` blocks are parsed with the TypeScript or JavaScript grammar (by `lang`), so their declarations are symbols on the component's own lines, and the record's `language` is `vue` or `svelte`. (#2732)
- `_reindex` applies `source._source` field filtering to the documents it
copies, so an excluded secret or large field no longer reaches the
destination. `_source:false` is refused with ES's sentence, and a `script`
diff --git a/engine/crates/xerj-autoindex/src/extract/code.rs b/engine/crates/xerj-autoindex/src/extract/code.rs
index 1879acca9..78edaa65f 100644
--- a/engine/crates/xerj-autoindex/src/extract/code.rs
+++ b/engine/crates/xerj-autoindex/src/extract/code.rs
@@ -67,6 +67,57 @@ pub fn is_code_ext(ext: &str) -> bool {
registry().iter().any(|d| d.exts.contains(&e.as_str()))
}
+/// #2732: Vue and Svelte single-file components. No grammar claims the whole
+/// file — markup, script and style in one — so it is not a registry row; its
+/// `<script>` blocks are parsed with the JS/TS grammar instead
+/// ([`component_script`]) and the file keeps its own `language`.
+pub fn is_component_ext(ext: &str) -> bool {
+ matches!(ext.to_ascii_lowercase().as_str(), "vue" | "svelte")
+}
+
+/// The component with everything outside its `<script>` blocks blanked to
+/// spaces, newlines kept, so every byte offset and line of a captured
+/// declaration is the component's own; plus the grammar that reads it —
+/// `typescript` when a block says `lang="ts"` (`tsx` for `lang="tsx"`),
+/// `javascript` otherwise.
+fn component_script(text: &str) -> (String, &'static str) {
+ let lower = text.to_ascii_lowercase();
+ let mut keep = vec![false; text.len()];
+ let mut grammar = "javascript";
+ let mut from = 0;
+ while let Some(open) = lower[from..].find("<script").map(|i| from + i) {
+ let Some(tag_end) = lower[open..].find('>').map(|i| open + i + 1) else {
+ break;
+ };
+ let tag = &lower[open..tag_end];
+ for (needle, name) in [
+ ("tsx", "tsx"),
+ ("ts", "typescript"),
+ ("typescript", "typescript"),
+ ] {
+ if [format!("lang=\"{needle}\""), format!("lang='{needle}'")]
+ .iter()
+ .any(|attr| tag.contains(attr.as_str()))
+ {
+ grammar = name;
+ }
+ }
+ let close = lower[tag_end..]
+ .find("</script")
+ .map_or(text.len(), |i| tag_end + i);
+ keep[tag_end..close].iter_mut().for_each(|k| *k = true);
+ from = close;
+ }
+ // Only ASCII is written outside the kept spans, and the spans start and
+ // end at ASCII `>` / `<`, so the result is still UTF-8.
+ let masked: Vec<u8> = text
+ .bytes()
+ .zip(&keep)
+ .map(|(b, &k)| if k || b == b'\n' { b } else { b' ' })
+ .collect();
+ (String::from_utf8(masked).unwrap_or_default(), grammar)
+}
+
/// #2205: the `language` a code record carries is the registry row's NAME
/// (`javascript`, `typescript`, `python` …), so `--lang js` matched nothing
/// and `xerj code` answered "No passage". Resolve what a user types — a
@@ -373,6 +424,22 @@ pub fn extract(path: &Path, sn: &crate::sniff::Sniffed, sink: Sink) -> Result<Ex
if crate::sniff::is_sql_filename(named) {
return super::sqlsrc::extract(path, named, sn.gzip, sink);
}
+ if is_component_ext(&ext) {
+ let Some(bytes) = super::read_whole(path, false, CODE_CAP)? else {
+ stats.junk += 1;
+ stats.refusal = Some(code_cap_refusal(path));
+ return Ok(stats);
+ };
+ let (text, _) = crate::sniff::decode_text(&bytes);
+ let (script, grammar) = component_script(&text);
+ let symbols = registry()
+ .iter()
+ .find(|d| d.name == grammar)
+ .and_then(|d| parse_symbols(d, &script))
+ .unwrap_or_default();
+ emit_code_doc(named, &ext, &text, &symbols, &mut stats, sink);
+ return Ok(stats);
+ }
let claimants: Vec<&LangDef> = registry()
.iter()
.filter(|d| d.exts.contains(&ext.as_str()))
@@ -2466,6 +2533,54 @@ const SCHEME_Q: &str = r#"
mod tests {
use super::*;
+ /// #2732: a Vue/Svelte component is code, not junk-filed XML, and its
+ /// script declarations are symbols on the component's own lines.
+ #[test]
+ fn vue_and_svelte_components_index_their_script_declarations() {
+ let vue = "<template>\n <div>{{ count }}</div>\n</template>\n\n\
+ <script setup lang=\"ts\">\nimport { ref } from 'vue'\n\
+ export function useCounter(start: number) {\n return ref(start)\n}\n\
+ </script>\n\n<style scoped>\ndiv { color: red; }\n</style>\n";
+ let svelte = "<script>\n export let name = 'world';\n \
+ function greet() { return name; }\n</script>\n\n<h1>Hello {name}!</h1>\n";
+ let dir = tempfile::tempdir().unwrap();
+ for (file, src, want, line) in [
+ ("Counter.vue", vue, "useCounter", 7),
+ ("Hello.svelte", svelte, "greet", 3),
+ ] {
+ let f = dir.path().join(file);
+ std::fs::write(&f, src).unwrap();
+ let sn = crate::sniff::sniff_with_name(&f, Path::new(file)).unwrap();
+ assert_eq!(sn.family, crate::sniff::Family::Code, "{file}");
+ let mut records: Vec<RawRecord> = Vec::new();
+ let stats = extract(&f, &sn, &mut |record| {
+ records.push(record);
+ true
+ })
+ .unwrap();
+ assert_eq!(stats.junk, 0, "{file}");
+ let ext = file.rsplit('.').next().unwrap();
+ assert!(
+ records
+ .iter()
+ .any(|r| r.fields.get("language").is_some_and(|l| l == ext)),
+ "{file}: the file keeps its own language"
+ );
+ let decl = records
+ .iter()
+ .find(|r| r.fields.get("name").is_some_and(|n| n == want))
+ .unwrap_or_else(|| panic!("{file}: no {want} declaration"));
+ assert_eq!(decl.fields["line"], line, "{file}");
+ }
+ // The grammar follows the block's `lang`; markup and styles are blanked.
+ let (script, grammar) = component_script(vue);
+ assert_eq!(grammar, "typescript");
+ assert_eq!(component_script(svelte).1, "javascript");
+ assert_eq!(script.len(), vue.len());
+ assert_eq!(script.lines().count(), vue.lines().count());
+ assert!(!script.contains("color") && !script.contains("<div>"));
+ }
+
/// #2205: `--lang js` must filter on the stored `javascript`, not on a
/// value no record carries.
#[test]
diff --git a/engine/crates/xerj-autoindex/src/sniff.rs b/engine/crates/xerj-autoindex/src/sniff.rs
index 2fb2be005..45e2ecd4b 100644
--- a/engine/crates/xerj-autoindex/src/sniff.rs
+++ b/engine/crates/xerj-autoindex/src/sniff.rs
@@ -470,7 +470,9 @@ fn sniff_bytes(
// routes to the tree-sitter AST extractor (crate::extract::code). Extension
// is the right signal — code vs prose is not reliably content-sniffable.
if let Some(ext) = logical_path.extension().and_then(|e| e.to_str()) {
- if crate::extract::code::is_code_ext(ext) {
+ // #2732: a Vue/Svelte component opens with `<template>` or `<script>`
+ // and used to fall to the XML branch below, whose extractor junked it.
+ if crate::extract::code::is_code_ext(ext) || crate::extract::code::is_component_ext(ext) {
let mut s = mk(Family::Code);
s.encoding = encoding;
return Ok(s);
@@ -3001,6 +3003,16 @@ mod unity_sniff_tests {
sniff(&path).unwrap().family
}
+ /// #2732: the extension decides a component, not its leading `<template>`;
+ /// the same bytes in a `.xml` file are still XML.
+ #[test]
+ fn a_vue_or_svelte_component_is_code_not_xml() {
+ let src = b"<template>\n <p>{{ msg }}</p>\n</template>\n<script>\nexport default { data() { return { msg: 'x' } } }\n</script>\n";
+ assert_eq!(sniff_file(src, "App.vue"), Family::Code);
+ assert_eq!(sniff_file(src, "App.svelte"), Family::Code);
+ assert_eq!(sniff_file(src, "App.xml"), Family::Xml);
+ }
+
/// Regression (round 2): a guard on "lossy windows-1252 AND over 30%
/// non-ASCII" is not a test for pixel data, it is a test for "not written
/// in Latin script". windows-1252 is the fallback EVERY legacy 8-bit