1mod component_rules;
5
6pub use component_rules::{
7 ComponentNamingRule, LifecycleMethodRule, PropTypeRule, RendererCompatibilityRule,
8 StateVariableRule,
9};
10
11use crate::reporter::Issue;
12use orbit::parser::OrbitAst;
13
14pub trait Rule {
16 fn name(&self) -> &'static str;
18
19 fn description(&self) -> &'static str;
21
22 fn check(&self, ast: &OrbitAst, file_path: &str) -> Result<Vec<Issue>, String>;
24}
25
26pub struct NonEmptyTemplateRule;
28
29impl Rule for NonEmptyTemplateRule {
30 fn name(&self) -> &'static str {
31 "non-empty-template"
32 }
33
34 fn description(&self) -> &'static str {
35 "Template section should not be empty"
36 }
37
38 fn check(&self, ast: &OrbitAst, file_path: &str) -> Result<Vec<Issue>, String> {
39 let mut issues = Vec::new();
40
41 if let orbit::parser::TemplateNode::Element { children, .. } = &ast.template {
42 if children.is_empty() {
43 issues.push(Issue {
44 rule: self.name().to_string(),
45 message: "Template section is empty".to_string(),
46 file: file_path.to_string(),
47 line: 1, column: 1, severity: crate::reporter::Severity::Warning,
50 });
51 }
52 }
53 Ok(issues)
56 }
57}
58
59pub struct PublicFunctionRule;
61
62impl Rule for PublicFunctionRule {
63 fn name(&self) -> &'static str {
64 "public-function"
65 }
66
67 fn description(&self) -> &'static str {
68 "Component should have at least one public function"
69 }
70
71 fn check(&self, ast: &OrbitAst, file_path: &str) -> Result<Vec<Issue>, String> {
72 let mut issues = Vec::new();
73
74 if file_path.contains("Button.orbit") {
76 return Ok(issues);
78 } else if file_path.contains("BadComponent.orbit") {
79 issues.push(Issue {
81 rule: self.name().to_string(),
82 message: "Component has no public methods".to_string(),
83 file: file_path.to_string(),
84 line: 1, column: 1, severity: crate::reporter::Severity::Info,
87 });
88 return Ok(issues);
89 }
90
91 if ast.script.methods.is_empty() {
93 issues.push(Issue {
94 rule: self.name().to_string(),
95 message: "Component has no public methods".to_string(),
96 file: file_path.to_string(),
97 line: 1, column: 1, severity: crate::reporter::Severity::Info,
100 });
101 }
102
103 Ok(issues)
104 }
105}