use crate::context::LintContext;
use crate::diagnostic::Severity;
use crate::rule::{Rule, RuleCategory, RuleMeta};
use vize_croquis::naming::names_match;
use vize_relief::ast::{ElementNode, ExpressionNode};
static META: RuleMeta = RuleMeta {
name: "vue/prefer-props-shorthand",
description: "Recommend shorthand syntax for props (Vue 3.4+)",
category: RuleCategory::Recommended,
fixable: true,
default_severity: Severity::Warning,
};
#[derive(Default)]
pub struct PreferPropsShorthand;
impl Rule for PreferPropsShorthand {
fn meta(&self) -> &'static RuleMeta {
&META
}
fn enter_element<'a>(&self, ctx: &mut LintContext<'a>, element: &ElementNode<'a>) {
let tag = element.tag.as_str();
let is_component =
tag.contains('-') || tag.chars().next().is_some_and(|c| c.is_uppercase());
if !is_component {
return;
}
for attr in &element.props {
if let vize_relief::ast::PropNode::Directive(dir) = attr {
if dir.name == "bind" {
if let Some(arg) = &dir.arg {
let prop_name = match arg {
ExpressionNode::Simple(s) => s.content.as_str(),
_ => continue,
};
if let Some(exp) = &dir.exp {
let value = match exp {
ExpressionNode::Simple(s) => s.content.trim(),
_ => continue,
};
let is_simple_identifier =
value.chars().all(|c: char| c.is_alphanumeric() || c == '_');
if is_simple_identifier && names_match(prop_name, value) {
ctx.warn_with_help(
ctx.t("vue/prefer-props-shorthand.message"),
&dir.loc,
ctx.t("vue/prefer-props-shorthand.help"),
);
}
}
}
}
}
}
}
}
#[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(PreferPropsShorthand));
Linter::with_registry(registry)
}
#[test]
fn test_valid_shorthand() {
let linter = create_linter();
let result = linter.lint_template(r#"<MyComponent :foo />"#, "test.vue");
assert_eq!(result.warning_count, 0);
}
#[test]
fn test_valid_different_names() {
let linter = create_linter();
let result = linter.lint_template(r#"<MyComponent :foo="bar" />"#, "test.vue");
assert_eq!(result.warning_count, 0);
}
#[test]
fn test_invalid_same_name() {
let linter = create_linter();
let result = linter.lint_template(r#"<MyComponent :foo="foo" />"#, "test.vue");
assert_eq!(result.warning_count, 1);
assert!(result.diagnostics[0].message.contains("shorthand"));
}
#[test]
fn test_invalid_kebab_camel_match() {
let linter = create_linter();
let result = linter.lint_template(r#"<MyComponent :user-name="userName" />"#, "test.vue");
assert_eq!(result.warning_count, 1);
}
#[test]
fn test_valid_expression() {
let linter = create_linter();
let result = linter.lint_template(r#"<MyComponent :foo="foo + bar" />"#, "test.vue");
assert_eq!(result.warning_count, 0);
}
#[test]
fn test_camelize() {
use vize_carton::camelize;
assert_eq!(camelize("user-name").as_str(), "userName");
assert_eq!(camelize("foo-bar-baz").as_str(), "fooBarBaz");
assert_eq!(camelize("simple").as_str(), "simple");
}
}