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
//! vue/no-mutating-props
//!
//! Disallow mutating component props.
//!
//! Vue's one-way data flow means props should be treated as read-only.
//! Mutating props can lead to unexpected behavior and makes the data flow
//! harder to understand.
//!
//! ## Examples
//!
//! ### Invalid
//! ```vue
//! <script setup>
//! const props = defineProps(['count'])
//!
//! // Direct mutation
//! props.count = 5
//!
//! // Mutation via method
//! props.items.push('new')
//! </script>
//!
//! <template>
//! <!-- v-model on prop is also mutation -->
//! <input v-model="count" />
//! </template>
//! ```
//!
//! ### Valid
//! ```vue
//! <script setup>
//! const props = defineProps(['initialCount'])
//! const count = ref(props.initialCount)
//!
//! const emit = defineEmits(['update:count'])
//! </script>
//!
//! <template>
//! <input :value="count" @input="emit('update:count', $event.target.value)" />
//! </template>
//! ```
use crate::context::LintContext;
use crate::diagnostic::Severity;
use crate::rule::{Rule, RuleCategory, RuleMeta};
use vize_carton::FxHashSet;
use vize_relief::ast::{DirectiveNode, ElementNode, PropNode, RootNode, TemplateChildNode};
use vize_relief::BindingType;
static META: RuleMeta = RuleMeta {
name: "vue/no-mutating-props",
description: "Disallow mutating component props",
category: RuleCategory::Essential,
fixable: false,
default_severity: Severity::Error,
};
/// Disallow mutating props
#[derive(Default)]
pub struct NoMutatingProps;
impl NoMutatingProps {
/// Check if an expression mutates a prop
fn check_v_model_mutation<'a>(
&self,
ctx: &mut LintContext<'a>,
directive: &DirectiveNode<'a>,
prop_names: &FxHashSet<&str>,
) {
if directive.name.as_str() != "model" {
return;
}
// Get the v-model expression
if let Some(ref exp) = directive.exp {
let content = match exp {
vize_relief::ast::ExpressionNode::Simple(s) => s.content.as_str(),
vize_relief::ast::ExpressionNode::Compound(c) => c.loc.source.as_str(),
};
// Check if the expression references a prop
// Simple check: v-model="propName" or v-model="props.propName"
let is_prop_mutation = prop_names.contains(content)
|| content.starts_with("props.") && prop_names.contains(&content[6..]);
if is_prop_mutation {
ctx.report(
crate::diagnostic::LintDiagnostic::error(
ctx.current_rule,
format!("Unexpected mutation of prop '{}' via v-model", content),
directive.loc.start.offset,
directive.loc.end.offset,
)
.with_help(
"Use a local ref or emit an event instead of mutating props directly",
),
);
}
}
}
/// Recursively check template for prop mutations
fn check_children<'a>(
&self,
ctx: &mut LintContext<'a>,
children: &[TemplateChildNode<'a>],
prop_names: &FxHashSet<&str>,
) {
for child in children {
match child {
TemplateChildNode::Element(el) => {
self.check_element(ctx, el, prop_names);
}
TemplateChildNode::If(if_node) => {
for branch in if_node.branches.iter() {
self.check_children(ctx, &branch.children, prop_names);
}
}
TemplateChildNode::For(for_node) => {
self.check_children(ctx, &for_node.children, prop_names);
}
_ => {}
}
}
}
/// Check an element for prop mutations
fn check_element<'a>(
&self,
ctx: &mut LintContext<'a>,
element: &ElementNode<'a>,
prop_names: &FxHashSet<&str>,
) {
// Check directives
for prop in element.props.iter() {
if let PropNode::Directive(dir) = prop {
self.check_v_model_mutation(ctx, dir, prop_names);
}
}
// Check children
self.check_children(ctx, &element.children, prop_names);
}
}
impl Rule for NoMutatingProps {
fn meta(&self) -> &'static RuleMeta {
&META
}
fn run_on_template<'a>(&self, ctx: &mut LintContext<'a>, root: &RootNode<'a>) {
// Skip if no analysis available
if !ctx.has_analysis() {
return;
}
// Collect prop names first (to avoid borrow conflicts)
let prop_names: FxHashSet<String> = {
let analysis = ctx.analysis().unwrap();
let mut names: FxHashSet<String> = FxHashSet::default();
// From defineProps
for prop in analysis.macros.props() {
names.insert(prop.name.to_string());
}
// From destructured props
for (name, binding_type) in analysis.bindings.iter() {
if matches!(binding_type, BindingType::Props | BindingType::PropsAliased) {
names.insert(name.to_string());
}
}
names
};
// If no props, nothing to check
if prop_names.is_empty() {
return;
}
// Convert to &str set for checking
let prop_names_ref: FxHashSet<&str> = prop_names.iter().map(|s| s.as_str()).collect();
// Check template
self.check_children(ctx, &root.children, &prop_names_ref);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_meta() {
let rule = NoMutatingProps;
assert_eq!(rule.meta().name, "vue/no-mutating-props");
assert_eq!(rule.meta().category, RuleCategory::Essential);
assert_eq!(rule.meta().default_severity, Severity::Error);
}
}