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
//! vue/no-duplicate-attributes
//!
//! Disallow duplicate attributes on the same element.
//!
//! ## Examples
//!
//! ### Invalid
//! ```vue
//! <div id="foo" id="bar"></div>
//! <div :class="foo" class="bar"></div>
//! ```
//!
//! ### Valid
//! ```vue
//! <div id="foo"></div>
//! <div :class="foo"></div>
//! ```
use crate::context::LintContext;
use crate::diagnostic::Severity;
use crate::rule::{Rule, RuleCategory, RuleMeta};
use vize_carton::FxHashSet;
use vize_relief::ast::{ElementNode, PropNode};
static META: RuleMeta = RuleMeta {
name: "vue/no-duplicate-attributes",
description: "Disallow duplicate attributes on the same element",
category: RuleCategory::Essential,
fixable: false,
default_severity: Severity::Error,
};
/// Disallow duplicate attributes
pub struct NoDuplicateAttributes {
/// Allow :class and class to coexist
pub allow_coexist_class: bool,
/// Allow :style and style to coexist
pub allow_coexist_style: bool,
}
impl Default for NoDuplicateAttributes {
fn default() -> Self {
Self {
allow_coexist_class: true,
allow_coexist_style: true,
}
}
}
impl Rule for NoDuplicateAttributes {
fn meta(&self) -> &'static RuleMeta {
&META
}
fn enter_element<'a>(&self, ctx: &mut LintContext<'a>, element: &ElementNode<'a>) {
let mut seen_attrs: FxHashSet<std::string::String> = FxHashSet::default();
let mut seen_directives: FxHashSet<std::string::String> = FxHashSet::default();
for prop in element.props.iter() {
match prop {
PropNode::Attribute(attr) => {
let name = attr.name.as_str().to_lowercase();
// Check for duplicate static attributes
if seen_attrs.contains(&name) {
ctx.error_with_help(
ctx.t_fmt("vue/no-duplicate-attributes.message", &[("attr", &name)]),
&attr.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
} else {
seen_attrs.insert(name.clone());
}
// Check for coexistence with directives (unless allowed)
if !self.allow_coexist_class
&& name == "class"
&& seen_directives.contains("class")
{
ctx.error_with_help(
ctx.t_fmt("vue/no-duplicate-attributes.message", &[("attr", "class")]),
&attr.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
}
if !self.allow_coexist_style
&& name == "style"
&& seen_directives.contains("style")
{
ctx.error_with_help(
ctx.t_fmt("vue/no-duplicate-attributes.message", &[("attr", "style")]),
&attr.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
}
}
PropNode::Directive(dir) => {
// Handle v-bind directives
if dir.name.as_str() == "bind" {
if let Some(ref arg) = dir.arg {
let arg_name = get_expression_content(arg).to_lowercase();
// Check for duplicate directives
if seen_directives.contains(&arg_name) {
ctx.error_with_help(
ctx.t_fmt(
"vue/no-duplicate-attributes.message",
&[("attr", &format!("v-bind:{}", arg_name))],
),
&dir.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
} else {
seen_directives.insert(arg_name.clone());
}
// Check for coexistence with static attributes (unless allowed)
if !self.allow_coexist_class
&& arg_name == "class"
&& seen_attrs.contains("class")
{
ctx.error_with_help(
ctx.t_fmt(
"vue/no-duplicate-attributes.message",
&[("attr", "v-bind:class")],
),
&dir.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
}
if !self.allow_coexist_style
&& arg_name == "style"
&& seen_attrs.contains("style")
{
ctx.error_with_help(
ctx.t_fmt(
"vue/no-duplicate-attributes.message",
&[("attr", "v-bind:style")],
),
&dir.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
}
}
}
// Handle v-on directives
else if dir.name.as_str() == "on" {
if let Some(ref arg) = dir.arg {
let event_name = get_expression_content(arg);
// Include modifiers in the key to allow @keydown.left and @keydown.right
let modifiers: Vec<&str> =
dir.modifiers.iter().map(|m| m.content.as_str()).collect();
let event_key = if modifiers.is_empty() {
format!("on:{}", event_name)
} else {
format!("on:{}.{}", event_name, modifiers.join("."))
};
if seen_directives.contains(&event_key) {
let display_name = if modifiers.is_empty() {
format!("v-on:{}", event_name)
} else {
format!("v-on:{}.{}", event_name, modifiers.join("."))
};
ctx.error_with_help(
ctx.t_fmt(
"vue/no-duplicate-attributes.message",
&[("attr", &display_name)],
),
&dir.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
} else {
seen_directives.insert(event_key);
}
}
}
// Handle v-model
else if dir.name.as_str() == "model" {
let model_key = if let Some(ref arg) = dir.arg {
format!("model:{}", get_expression_content(arg))
} else {
"model:modelValue".to_string()
};
if seen_directives.contains(&model_key) {
ctx.error_with_help(
ctx.t_fmt(
"vue/no-duplicate-attributes.message",
&[("attr", "v-model")],
),
&dir.loc,
ctx.t("vue/no-duplicate-attributes.help"),
);
} else {
seen_directives.insert(model_key);
}
}
}
}
}
}
}
/// Get content from ExpressionNode
fn get_expression_content(expr: &vize_relief::ast::ExpressionNode) -> String {
match expr {
vize_relief::ast::ExpressionNode::Simple(s) => s.content.to_string(),
vize_relief::ast::ExpressionNode::Compound(_) => "<dynamic>".to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::linter::Linter;
use crate::rule::RuleRegistry;
fn create_linter() -> Linter {
let mut registry = RuleRegistry::new();
registry.register(Box::new(NoDuplicateAttributes::default()));
Linter::with_registry(registry)
}
#[test]
fn test_valid_unique_attributes() {
let linter = create_linter();
let result = linter.lint_template(r#"<div id="foo" class="bar"></div>"#, "test.vue");
assert_eq!(result.error_count, 0);
}
#[test]
fn test_invalid_duplicate_id() {
let linter = create_linter();
let result = linter.lint_template(r#"<div id="foo" id="bar"></div>"#, "test.vue");
assert_eq!(result.error_count, 1);
assert!(result.diagnostics[0].message.contains("Duplicate"));
}
#[test]
fn test_valid_class_coexist() {
let linter = create_linter();
let result = linter.lint_template(r#"<div :class="foo" class="bar"></div>"#, "test.vue");
// Default allows coexistence
assert_eq!(result.error_count, 0);
}
#[test]
fn test_invalid_duplicate_v_bind() {
let linter = create_linter();
let result = linter.lint_template(r#"<div :id="foo" :id="bar"></div>"#, "test.vue");
assert_eq!(result.error_count, 1);
}
#[test]
fn test_valid_different_event_modifiers() {
let linter = create_linter();
let result = linter.lint_template(
r#"<div @keydown.left="goLeft" @keydown.right="goRight"></div>"#,
"test.vue",
);
assert_eq!(result.error_count, 0);
}
#[test]
fn test_valid_different_event_modifiers_multiple() {
let linter = create_linter();
let result = linter.lint_template(
r#"<div @click.stop="a" @click.prevent="b" @click.stop.prevent="c"></div>"#,
"test.vue",
);
assert_eq!(result.error_count, 0);
}
#[test]
fn test_invalid_duplicate_event_same_modifiers() {
let linter = create_linter();
let result =
linter.lint_template(r#"<div @click.stop="a" @click.stop="b"></div>"#, "test.vue");
assert_eq!(result.error_count, 1);
assert!(result.diagnostics[0].message.contains("click.stop"));
}
#[test]
fn test_invalid_duplicate_event_no_modifiers() {
let linter = create_linter();
let result = linter.lint_template(r#"<div @click="a" @click="b"></div>"#, "test.vue");
assert_eq!(result.error_count, 1);
}
}