fix(derive): suggest valid component scopes
This commit is contained in:
+27
-1
@@ -723,8 +723,14 @@ fn component_contract_errors(
|
||||
let mut errors = Vec::new();
|
||||
|
||||
if let Some(component) = component.filter(|_| !implemented.is_empty() && generated.is_empty()) {
|
||||
let available = syms_components(path);
|
||||
let repair = if available.is_empty() {
|
||||
"no generated components with handles are available; add a data-hemx-handle to the component template or check build.rs generation".to_owned()
|
||||
} else {
|
||||
format!("available generated components: {}", available.join(", "))
|
||||
};
|
||||
errors.push(format!(
|
||||
"#[hemx::component({component:?})] does not match any generated handles; check the .heml file name or component name"
|
||||
"#[hemx::component({component:?})] does not match any generated handles; {repair}"
|
||||
));
|
||||
}
|
||||
|
||||
@@ -860,6 +866,26 @@ fn syms_handles(path: &PathBuf, component: Option<&str>) -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn syms_components(path: &PathBuf) -> Vec<String> {
|
||||
let Ok(syms) = std::fs::read_to_string(path) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut components = syms
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
let mut fields = line.split('\t');
|
||||
matches!(fields.next(), Some("handle"))
|
||||
.then(|| fields.next())
|
||||
.flatten()
|
||||
.and_then(symbol_component)
|
||||
.map(ToOwned::to_owned)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
components.sort_unstable();
|
||||
components.dedup();
|
||||
components
|
||||
}
|
||||
|
||||
fn symbol_component(symbol: &str) -> Option<&str> {
|
||||
let path = symbol.split_once("::")?.0;
|
||||
path.rsplit_once('/')
|
||||
|
||||
@@ -223,6 +223,10 @@ mod handlers {
|
||||
stderr.contains("#[hemx::component(\"todos\")] does not match any generated handles"),
|
||||
"missing unknown component diagnostic in stderr:\n{stderr}"
|
||||
);
|
||||
assert!(
|
||||
stderr.contains("available generated components: admin"),
|
||||
"unknown scope diagnostic must name the valid repair choice:\n{stderr}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user