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
//! vue/no-undefined-refs
//!
//! Disallow undefined variable references in templates.
//!
//! This rule requires semantic analysis (Croquis) to be effective.
//! Without analysis, it only checks v-for scope variables.
//!
//! ## Examples
//!
//! ### Invalid
//! ```vue
//! <template>
//! <!-- 'undefinedVar' is not defined in script -->
//! <div>{{ undefinedVar }}</div>
//! <span v-if="unknownFlag">...</span>
//! </template>
//! ```
//!
//! ### Valid
//! ```vue
//! <script setup>
//! const count = ref(0)
//! const user = reactive({ name: 'John' })
//! </script>
//!
//! <template>
//! <div>{{ count }}</div>
//! <span>{{ user.name }}</span>
//! <li v-for="item in items" :key="item.id">
//! {{ item.name }}
//! </li>
//! </template>
//! ```
use crate::context::LintContext;
use crate::diagnostic::Severity;
use crate::rule::{Rule, RuleCategory, RuleMeta};
use vize_croquis::builtins::is_js_global;
use vize_relief::ast::{ElementNode, ExpressionNode, InterpolationNode};
static META: RuleMeta = RuleMeta {
name: "vue/no-undefined-refs",
description: "Disallow undefined variable references in templates",
category: RuleCategory::Recommended,
fixable: false,
default_severity: Severity::Warning,
};
/// No undefined refs rule
#[derive(Default)]
pub struct NoUndefinedRefs;
impl NoUndefinedRefs {
/// Extract identifiers from an expression string
///
/// This is a simplified implementation that extracts top-level identifiers.
/// A full implementation would use a proper expression parser.
fn extract_identifiers(expr: &str) -> Vec<&str> {
let mut identifiers = Vec::new();
let expr = expr.trim();
// Skip empty expressions
if expr.is_empty() {
return identifiers;
}
// Simple tokenizer for identifiers
let mut chars = expr.char_indices().peekable();
while let Some((start, c)) = chars.next() {
// Start of identifier
if c.is_ascii_alphabetic() || c == '_' || c == '$' {
let mut end = start + c.len_utf8();
while let Some(&(i, next)) = chars.peek() {
if next.is_ascii_alphanumeric() || next == '_' || next == '$' {
end = i + next.len_utf8();
chars.next();
} else {
break;
}
}
let ident = &expr[start..end];
// Skip keywords and built-in globals
if !is_keyword(ident) && !is_js_global(ident) {
identifiers.push(ident);
}
}
}
identifiers
}
}
/// Check if a string is a JavaScript keyword
fn is_keyword(s: &str) -> bool {
matches!(
s,
"true"
| "false"
| "null"
| "undefined"
| "this"
| "if"
| "else"
| "for"
| "while"
| "do"
| "switch"
| "case"
| "break"
| "continue"
| "return"
| "throw"
| "try"
| "catch"
| "finally"
| "new"
| "delete"
| "typeof"
| "void"
| "in"
| "of"
| "instanceof"
| "function"
| "class"
| "const"
| "let"
| "var"
| "async"
| "await"
| "yield"
| "import"
| "export"
| "default"
| "from"
| "as"
)
}
impl Rule for NoUndefinedRefs {
fn meta(&self) -> &'static RuleMeta {
&META
}
fn check_interpolation<'a>(
&self,
ctx: &mut LintContext<'a>,
interpolation: &InterpolationNode<'a>,
) {
// Skip if no analysis available
if !ctx.has_analysis() {
return;
}
if let ExpressionNode::Simple(expr) = &interpolation.content {
let identifiers = Self::extract_identifiers(&expr.content);
for ident in identifiers {
if !ctx.is_variable_defined(ident) {
ctx.warn_with_help(
"Variable is not defined",
&interpolation.loc,
"Define in <script setup> or ensure it's imported",
);
}
}
}
}
fn enter_element<'a>(&self, ctx: &mut LintContext<'a>, element: &ElementNode<'a>) {
// Skip if no analysis available
if !ctx.has_analysis() {
return;
}
// Check directive expressions
for prop in &element.props {
if let vize_relief::ast::PropNode::Directive(dir) = prop {
// Skip v-for (defines its own variables)
if dir.name == "for" {
continue;
}
// Check expression
if let Some(ExpressionNode::Simple(expr)) = &dir.exp {
let identifiers = Self::extract_identifiers(&expr.content);
for ident in identifiers {
if !ctx.is_variable_defined(ident) {
ctx.warn_with_help(
"Variable is not defined",
&dir.loc,
"Define in <script setup> or ensure it's imported",
);
}
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::{is_keyword, NoUndefinedRefs};
#[test]
fn test_extract_identifiers() {
let ids = NoUndefinedRefs::extract_identifiers("count + 1");
assert_eq!(ids, vec!["count"]);
let ids = NoUndefinedRefs::extract_identifiers("user.name");
assert_eq!(ids, vec!["user", "name"]);
let ids = NoUndefinedRefs::extract_identifiers("items.map(item => item.id)");
assert!(ids.contains(&"items"));
let ids = NoUndefinedRefs::extract_identifiers("true && false");
assert!(ids.is_empty());
let ids = NoUndefinedRefs::extract_identifiers("console.log(msg)");
// console is a global (filtered out), but log and msg are extracted
// Note: This is a simplified tokenizer - a real implementation would
// understand that log is a property access, not a variable
assert_eq!(ids, vec!["log", "msg"]);
}
#[test]
fn test_is_keyword() {
assert!(is_keyword("true"));
assert!(is_keyword("false"));
assert!(is_keyword("null"));
assert!(is_keyword("this"));
assert!(!is_keyword("count"));
assert!(!is_keyword("user"));
}
}