1use dictator_decree_abi::{BoxDecree, Decree, Diagnostic, Diagnostics, Span};
4use dictator_supreme::SupremeConfig;
5use memchr::memchr_iter;
6
7#[derive(Debug, Clone)]
9pub struct RubyConfig {
10 pub max_lines: usize,
11 pub ignore_comments: bool,
12 pub comment_spacing: bool,
13}
14
15impl Default for RubyConfig {
16 fn default() -> Self {
17 Self {
18 max_lines: 300,
19 ignore_comments: false,
20 comment_spacing: true,
21 }
22 }
23}
24
25#[must_use]
27pub fn lint_source(source: &str) -> Diagnostics {
28 lint_source_with_configs(source, &RubyConfig::default(), &SupremeConfig::default())
29}
30
31#[must_use]
33pub fn lint_source_with_config(source: &str, config: &RubyConfig) -> Diagnostics {
34 let mut diags = Diagnostics::new();
35
36 diags.extend(dictator_supreme::lint_source_with_owner(
37 source,
38 &SupremeConfig::default(),
39 "ruby",
40 ));
41
42 diags.extend(lint_ruby_specific(source, config));
44
45 diags
46}
47
48#[must_use]
49pub fn lint_source_with_configs(
50 source: &str,
51 ruby_config: &RubyConfig,
52 supreme_config: &SupremeConfig,
53) -> Diagnostics {
54 let mut diags = Diagnostics::new();
55
56 let supreme_diags = dictator_supreme::lint_source_with_owner(source, supreme_config, "ruby");
57
58 if ruby_config.ignore_comments {
59 diags.extend(dictator_supreme::retain_long_line_diags(
60 source,
61 supreme_diags,
62 "ruby/line-too-long",
63 "#",
64 ));
65 } else {
66 diags.extend(supreme_diags);
67 }
68
69 diags.extend(lint_ruby_specific(source, ruby_config));
71
72 diags
73}
74
75fn lint_ruby_specific(source: &str, config: &RubyConfig) -> Diagnostics {
76 let mut diags = Diagnostics::new();
77
78 check_file_line_count(source, config.max_lines, &mut diags);
80
81 let bytes = source.as_bytes();
82 let mut line_start: usize = 0;
83 let mut line_idx: usize = 0;
84
85 for nl in memchr_iter(b'\n', bytes) {
86 process_line(
87 source,
88 line_start,
89 nl,
90 line_idx,
91 config.comment_spacing,
92 &mut diags,
93 );
94 line_start = nl + 1;
95 line_idx += 1;
96 }
97
98 if line_start < bytes.len() {
99 process_line(
101 source,
102 line_start,
103 bytes.len(),
104 line_idx,
105 config.comment_spacing,
106 &mut diags,
107 );
108 }
109
110 diags
111}
112
113fn check_file_line_count(source: &str, max_lines: usize, diags: &mut Diagnostics) {
115 let code_lines = dictator_supreme::count_code_lines(source, |trimmed| trimmed.starts_with('#'));
116
117 if code_lines > max_lines {
118 diags.push(Diagnostic {
119 rule: "ruby/file-too-long".to_string(),
120 message: format!("{code_lines} code lines (max {max_lines})"),
121 enforced: false,
122 span: Span::new(0, source.len().min(100)),
123 });
124 }
125}
126
127#[derive(Default)]
128pub struct RubyHygiene {
129 config: RubyConfig,
130 supreme: SupremeConfig,
131}
132
133impl RubyHygiene {
134 #[must_use]
135 pub const fn new(config: RubyConfig, supreme: SupremeConfig) -> Self {
136 Self { config, supreme }
137 }
138}
139
140impl Decree for RubyHygiene {
141 fn name(&self) -> &'static str {
142 "ruby"
143 }
144
145 fn lint(&self, _path: &str, source: &str) -> Diagnostics {
146 lint_source_with_configs(source, &self.config, &self.supreme)
147 }
148
149 fn metadata(&self) -> dictator_decree_abi::DecreeMetadata {
150 dictator_decree_abi::DecreeMetadata {
151 abi_version: dictator_decree_abi::ABI_VERSION.to_string(),
152 decree_version: env!("CARGO_PKG_VERSION").to_string(),
153 description: "Ruby code structure and hygiene".to_string(),
154 persona: "Matz".to_string(),
155 dectauthors: Some(env!("CARGO_PKG_AUTHORS").to_string()),
156 supported_extensions: vec!["rb".to_string(), "rake".to_string(), "gemspec".to_string()],
157 supported_filenames: vec![
158 "Gemfile".to_string(),
159 "Rakefile".to_string(),
160 "Guardfile".to_string(),
161 "Capfile".to_string(),
162 "Brewfile".to_string(),
163 "Dangerfile".to_string(),
164 "Podfile".to_string(),
165 "Fastfile".to_string(),
166 "Appfile".to_string(),
167 "Matchfile".to_string(),
168 "Berksfile".to_string(),
169 "Thorfile".to_string(),
170 "Vagrantfile".to_string(),
171 ".pryrc".to_string(),
172 ".irbrc".to_string(),
173 ],
174 skip_filenames: vec!["Gemfile.lock".to_string(), "Podfile.lock".to_string()],
175 file_scope_rules: dictator_supreme::file_scope_rules(&["file-too-long"]),
176 capabilities: vec![dictator_decree_abi::Capability::Lint],
177 }
178 }
179}
180
181#[must_use]
183pub fn init_decree() -> BoxDecree {
184 Box::new(RubyHygiene::default())
185}
186
187#[must_use]
189pub fn init_decree_with_config(config: RubyConfig) -> BoxDecree {
190 Box::new(RubyHygiene::new(config, SupremeConfig::default()))
191}
192
193#[must_use]
195pub fn init_decree_with_configs(config: RubyConfig, supreme: SupremeConfig) -> BoxDecree {
196 Box::new(RubyHygiene::new(config, supreme))
197}
198
199#[must_use]
201pub fn config_from_decree_settings(settings: &dictator_core::DecreeSettings) -> RubyConfig {
202 RubyConfig {
203 max_lines: settings.max_lines.unwrap_or(300),
204 ignore_comments: settings.ignore_comments.unwrap_or(false),
205 comment_spacing: settings.comment_spacing.unwrap_or(true),
206 }
207}
208
209fn process_line(
210 source: &str,
211 start: usize,
212 end: usize,
213 line_idx: usize,
214 comment_spacing: bool,
215 diags: &mut Diagnostics,
216) {
217 if !comment_spacing {
218 return;
219 }
220
221 let line = &source[start..end];
222
223 let trimmed = line.trim_start_matches(' ');
225 if let Some(stripped) = trimmed.strip_prefix('#')
226 && !is_comment_directive(stripped, line_idx)
227 && !stripped.starts_with(' ')
228 && !stripped.is_empty()
229 {
230 let hash_offset = start + (line.len() - trimmed.len());
232 diags.push(Diagnostic {
233 rule: "ruby/comment-space".to_string(),
234 message: "Comments should start with '# '".to_string(),
235 enforced: true,
236 span: Span::new(hash_offset, hash_offset + 1),
237 });
238 }
239}
240
241fn is_comment_directive(rest: &str, line_idx: usize) -> bool {
242 let rest = rest.trim_start();
243
244 rest.starts_with('!') || rest.starts_with("encoding")
246 || rest.starts_with("frozen_string_literal")
247 || rest.starts_with("rubocop")
248 || rest.starts_with("typed")
249 || (line_idx == 0 && rest.starts_with(" language"))
250}
251
252#[cfg(test)]
253mod tests;