1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Fallthrough attribute analysis.
//!
//! Detects issues with attribute inheritance across component boundaries:
//! - Attributes passed to component but not used
//! - `inheritAttrs: false` without explicit $attrs binding
//! - Multiple root elements without explicit $attrs
use crate::cross_file::diagnostics::{
CrossFileDiagnostic, CrossFileDiagnosticKind, DiagnosticSeverity,
};
use crate::cross_file::graph::DependencyGraph;
use crate::cross_file::registry::{FileId, ModuleRegistry};
use vize_carton::{cstr, CompactString, FxHashMap, FxHashSet};
/// Information about fallthrough attributes for a component.
#[derive(Debug, Clone)]
pub struct FallthroughInfo {
/// File ID of the component.
pub file_id: FileId,
/// Whether `inheritAttrs: false` is set.
pub inherit_attrs_disabled: bool,
/// Whether $attrs is used in template.
pub uses_attrs: bool,
/// Whether $attrs is explicitly bound (v-bind="$attrs").
pub binds_attrs: bool,
/// Number of root elements in template.
pub root_element_count: usize,
/// Attributes passed by parent components.
pub passed_attrs: FxHashSet<CompactString>,
/// Props declared by this component.
pub declared_props: FxHashSet<CompactString>,
/// Template content start offset (relative to template block).
pub template_start: u32,
/// Template content end offset (relative to template block).
pub template_end: u32,
}
impl FallthroughInfo {
/// Check if fallthrough may cause issues.
pub fn has_potential_issues(&self) -> bool {
// Multiple roots without explicit $attrs
if self.root_element_count > 1 && !self.binds_attrs {
return true;
}
// inheritAttrs: false but $attrs not used
if self.inherit_attrs_disabled && !self.uses_attrs && !self.binds_attrs {
return true;
}
// Attributes passed that aren't props
let fallthrough_attrs: Vec<_> = self
.passed_attrs
.iter()
.filter(|attr| !self.declared_props.contains(*attr))
.collect();
if !fallthrough_attrs.is_empty() && !self.uses_attrs && self.root_element_count > 1 {
return true;
}
false
}
}
/// Analyze fallthrough attributes across the component graph.
pub fn analyze_fallthrough(
registry: &ModuleRegistry,
graph: &DependencyGraph,
) -> (Vec<FallthroughInfo>, Vec<CrossFileDiagnostic>) {
let mut infos = Vec::new();
let mut diagnostics = Vec::new();
// Build a map of what attributes each component passes to its children
let mut passed_attrs_map: FxHashMap<FileId, FxHashMap<FileId, FxHashSet<CompactString>>> =
FxHashMap::default();
// First pass: collect information from each component
for entry in registry.vue_components() {
let analysis = &entry.analysis;
// Use precise template_info from static analysis
let template_info = &analysis.template_info;
// Check for inheritAttrs option (from defineOptions macro)
let inherit_attrs_disabled = check_inherit_attrs_disabled(analysis);
// Get declared props
let declared_props: FxHashSet<_> = analysis
.macros
.props()
.iter()
.map(|p| p.name.clone())
.collect();
let info = FallthroughInfo {
file_id: entry.id,
inherit_attrs_disabled,
uses_attrs: template_info.uses_attrs,
binds_attrs: template_info.binds_attrs_explicitly,
root_element_count: template_info.root_element_count,
passed_attrs: FxHashSet::default(), // Will be filled later
declared_props,
template_start: template_info.content_start,
template_end: template_info.content_end,
};
infos.push(info);
}
// Second pass: track attribute passing through component usage
for node in graph.nodes() {
// Look at component usage edges
for (child_id, edge_type) in &node.imports {
if *edge_type != crate::cross_file::graph::DependencyEdge::ComponentUsage {
continue;
}
// Get the parent's analysis to find what attrs are passed
if let Some(parent_entry) = registry.get(node.file_id) {
// In a real implementation, we'd parse the template to find
// exactly which attributes are passed. For now, we'll use
// a simplified approach based on used_directives.
let attrs = extract_passed_attrs(&parent_entry.analysis, child_id);
passed_attrs_map
.entry(*child_id)
.or_default()
.entry(node.file_id)
.or_default()
.extend(attrs);
}
}
}
// Merge passed attrs into infos
for info in &mut infos {
if let Some(parent_attrs) = passed_attrs_map.get(&info.file_id) {
for attrs in parent_attrs.values() {
info.passed_attrs.extend(attrs.iter().cloned());
}
}
}
// Generate diagnostics
for info in &infos {
// Check for multiple root elements without explicit $attrs binding
if info.root_element_count > 1 && !info.binds_attrs {
let has_fallthrough = info
.passed_attrs
.iter()
.any(|attr| !info.declared_props.contains(attr));
if has_fallthrough {
// Use offset 0 to point to <template> tag start (wasm.rs adds tag_start offset)
diagnostics.push(
CrossFileDiagnostic::with_span(
CrossFileDiagnosticKind::MultiRootMissingAttrs,
DiagnosticSeverity::Warning,
info.file_id,
0,
info.template_end - info.template_start,
"Component has multiple root elements but $attrs is not explicitly bound",
)
.with_suggestion(
"Add v-bind=\"$attrs\" to the intended root element or wrap in single root",
),
);
}
}
// Check for inheritAttrs: false without $attrs usage
if info.inherit_attrs_disabled && !info.uses_attrs && !info.binds_attrs {
// Use offset 0 to point to <template> tag start (wasm.rs adds tag_start offset)
diagnostics.push(
CrossFileDiagnostic::with_span(
CrossFileDiagnosticKind::InheritAttrsDisabledUnused,
DiagnosticSeverity::Warning,
info.file_id,
0,
info.template_end - info.template_start,
"inheritAttrs is disabled but $attrs is not used anywhere",
)
.with_suggestion("Use v-bind=\"$attrs\" or $attrs.class/$attrs.style in template"),
);
}
// Check for unused fallthrough attributes
let unused_attrs: Vec<_> = info
.passed_attrs
.iter()
.filter(|attr| {
!info.declared_props.contains(*attr)
&& !is_standard_html_attr(attr)
&& !info.uses_attrs
})
.cloned()
.collect();
if !unused_attrs.is_empty() && !info.binds_attrs && info.root_element_count > 1 {
// Use offset 0 to point to <template> tag start (wasm.rs adds tag_start offset)
diagnostics.push(
CrossFileDiagnostic::with_span(
CrossFileDiagnosticKind::UnusedFallthroughAttrs {
passed_attrs: unused_attrs.clone(),
},
DiagnosticSeverity::Info,
info.file_id,
0,
info.template_end - info.template_start,
cstr!(
"Attributes {:?} are passed but not used (component has multiple roots)",
unused_attrs
),
)
.with_suggestion("Bind $attrs explicitly or declare as props"),
);
}
}
(infos, diagnostics)
}
/// Check if inheritAttrs: false is set in the component options.
fn check_inherit_attrs_disabled(analysis: &crate::Croquis) -> bool {
// Look for defineOptions with inheritAttrs: false in runtime_args
analysis.macros.all_calls().iter().any(|call| {
if call.name != "defineOptions" {
return false;
}
// Check if runtime_args contains "inheritAttrs: false" or "inheritAttrs:false"
if let Some(ref args) = call.runtime_args {
args.contains("inheritAttrs") && args.contains("false")
} else {
false
}
})
}
/// Extract attributes passed to a child component.
/// Uses component_usages for precise static analysis.
fn extract_passed_attrs(analysis: &crate::Croquis, _child_id: &FileId) -> FxHashSet<CompactString> {
let mut attrs = FxHashSet::default();
// Get the child component name from registry (if available)
// For now, we'll collect all passed props from component usages
for usage in &analysis.component_usages {
for prop in &usage.props {
attrs.insert(prop.name.clone());
}
}
attrs
}
/// Check if an attribute is a standard HTML attribute.
fn is_standard_html_attr(attr: &str) -> bool {
matches!(
attr,
"class"
| "style"
| "id"
| "key"
| "ref"
| "data-*"
| "aria-*"
| "role"
| "tabindex"
| "title"
| "disabled"
| "hidden"
)
}
#[cfg(test)]
mod tests {
use super::FallthroughInfo;
use crate::cross_file::registry::FileId;
use vize_carton::FxHashSet;
#[test]
fn test_fallthrough_info_issues() {
// Single root element - no issue
let mut info = FallthroughInfo {
file_id: FileId::new(0),
inherit_attrs_disabled: false,
uses_attrs: false,
binds_attrs: false,
root_element_count: 1,
passed_attrs: FxHashSet::default(),
declared_props: FxHashSet::default(),
template_start: 0,
template_end: 0,
};
assert!(!info.has_potential_issues());
// Multiple roots without binds_attrs - this IS an issue
info.root_element_count = 2;
assert!(info.has_potential_issues());
// Multiple roots WITH binds_attrs - no issue
info.binds_attrs = true;
assert!(!info.has_potential_issues());
// Reset and test inheritAttrs disabled without using $attrs
info.binds_attrs = false;
info.root_element_count = 1;
info.inherit_attrs_disabled = true;
assert!(info.has_potential_issues());
// inheritAttrs disabled but $attrs is used - no issue
info.uses_attrs = true;
assert!(!info.has_potential_issues());
}
}