-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpragma.rs
More file actions
358 lines (313 loc) · 10.6 KB
/
Copy pathpragma.rs
File metadata and controls
358 lines (313 loc) · 10.6 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Pragma support for inline rule ignoring.
//!
//! Supports comment-based rule ignoring in Helm templates and YAML files:
//! - `# helmlint-ignore HL1001,HL1002` - ignore specific rules for next line
//! - `# helmlint-ignore-file` - ignore all rules for entire file
//! - `# helmlint-ignore-file HL1001` - ignore specific rule for entire file
//! - `{{/* helmlint-ignore HL1001 */}}` - template comment format
use std::collections::{HashMap, HashSet};
use crate::analyzer::helmlint::types::RuleCode;
/// State for pragma processing.
#[derive(Debug, Clone, Default)]
pub struct PragmaState {
/// Rules ignored for the entire file.
pub file_ignores: HashSet<String>,
/// Rules ignored for specific lines (line -> set of rule codes).
pub line_ignores: HashMap<u32, HashSet<String>>,
/// Whether the entire file is ignored.
pub file_disabled: bool,
}
impl PragmaState {
/// Create a new empty pragma state.
pub fn new() -> Self {
Self::default()
}
/// Check if a rule is ignored for a specific line.
pub fn is_ignored(&self, code: &RuleCode, line: u32) -> bool {
if self.file_disabled {
return true;
}
if self.file_ignores.contains(code.as_str()) {
return true;
}
// Check if the rule is ignored for this specific line
if let Some(ignores) = self.line_ignores.get(&line)
&& ignores.contains(code.as_str())
{
return true;
}
// Check if previous line has an ignore pragma for this line
if line > 1
&& let Some(ignores) = self.line_ignores.get(&(line - 1))
&& ignores.contains(code.as_str())
{
return true;
}
false
}
/// Add a file-level ignore for a rule.
pub fn add_file_ignore(&mut self, code: impl Into<String>) {
self.file_ignores.insert(code.into());
}
/// Add a line-level ignore for a rule.
pub fn add_line_ignore(&mut self, line: u32, code: impl Into<String>) {
self.line_ignores
.entry(line)
.or_default()
.insert(code.into());
}
/// Set the file as completely disabled.
pub fn disable_file(&mut self) {
self.file_disabled = true;
}
}
/// Extract pragmas from YAML content (values.yaml, Chart.yaml).
pub fn extract_yaml_pragmas(content: &str) -> PragmaState {
let mut state = PragmaState::new();
for (line_num, line) in content.lines().enumerate() {
let line_number = (line_num + 1) as u32;
let trimmed = line.trim();
// Check for YAML comments
if let Some(comment) = trimmed.strip_prefix('#') {
process_comment(comment.trim(), line_number, &mut state);
}
}
state
}
/// Extract pragmas from template content.
pub fn extract_template_pragmas(content: &str) -> PragmaState {
let mut state = PragmaState::new();
// Process YAML-style comments
for (line_num, line) in content.lines().enumerate() {
let line_number = (line_num + 1) as u32;
let trimmed = line.trim();
// Check for YAML comments (outside of templates)
if let Some(comment) = trimmed.strip_prefix('#') {
// Make sure it's not inside a template action
if !line.contains("{{") || line.find('#') < line.find("{{") {
process_comment(comment.trim(), line_number, &mut state);
}
}
}
// Process template comments {{/* ... */}}
let mut line_num: u32 = 1;
let mut i = 0;
let chars: Vec<char> = content.chars().collect();
while i < chars.len() {
if chars[i] == '\n' {
line_num += 1;
i += 1;
continue;
}
// Look for template comment start
if i + 4 < chars.len()
&& chars[i] == '{'
&& chars[i + 1] == '{'
&& (chars[i + 2] == '/'
|| (chars[i + 2] == '-' && i + 5 < chars.len() && chars[i + 3] == '/'))
{
let _comment_start = i;
let comment_line = line_num;
// Skip to comment content
i += 2;
if chars[i] == '-' {
i += 1;
}
i += 2; // skip /*
// Find comment end
let mut comment_content = String::new();
while i + 3 < chars.len() {
if chars[i] == '\n' {
line_num += 1;
}
if chars[i] == '*' && chars[i + 1] == '/' {
i += 2;
// Skip optional trim marker and closing braces
if i < chars.len() && chars[i] == '-' {
i += 1;
}
if i + 1 < chars.len() && chars[i] == '}' && chars[i + 1] == '}' {
i += 2;
}
break;
}
comment_content.push(chars[i]);
i += 1;
}
// Process the comment
process_comment(comment_content.trim(), comment_line, &mut state);
continue;
}
i += 1;
}
state
}
/// Process a comment for pragma directives.
fn process_comment(comment: &str, line: u32, state: &mut PragmaState) {
let lower = comment.to_lowercase();
// Check for file-level disable
if lower.starts_with("helmlint-ignore-file") || lower.starts_with("helmlint-disable-file") {
let rest = comment
.strip_prefix("helmlint-ignore-file")
.or_else(|| comment.strip_prefix("helmlint-disable-file"))
.unwrap_or("")
.trim();
if rest.is_empty() {
state.disable_file();
} else {
// Parse specific rules to ignore for the file
for code in parse_rule_list(rest) {
state.add_file_ignore(code);
}
}
return;
}
// Check for line-level ignore
if lower.starts_with("helmlint-ignore") || lower.starts_with("helmlint-disable") {
let rest = comment
.strip_prefix("helmlint-ignore")
.or_else(|| comment.strip_prefix("helmlint-disable"))
.unwrap_or("")
.trim();
if rest.is_empty() {
// Ignore all rules for next line - we'll use a special marker
state.add_line_ignore(line, "*");
} else {
for code in parse_rule_list(rest) {
state.add_line_ignore(line, code);
}
}
}
}
/// Parse a comma-separated list of rule codes.
fn parse_rule_list(input: &str) -> Vec<String> {
input
.split([',', ' '])
.map(|s| s.trim())
.filter(|s| !s.is_empty() && s.starts_with("HL"))
.map(|s| s.to_string())
.collect()
}
/// Check if content starts with a file-level disable comment.
pub fn starts_with_disable_file_comment(content: &str) -> bool {
for line in content.lines().take(10) {
let trimmed = line.trim();
if trimmed.is_empty() {
continue;
}
if let Some(comment) = trimmed.strip_prefix('#') {
let comment_lower = comment.trim().to_lowercase();
if comment_lower.starts_with("helmlint-ignore-file")
|| comment_lower.starts_with("helmlint-disable-file")
{
// Check if it's a full file disable (no specific rules)
let rest = comment
.trim()
.strip_prefix("helmlint-ignore-file")
.or_else(|| comment.trim().strip_prefix("helmlint-disable-file"))
.unwrap_or("")
.trim();
if rest.is_empty() {
return true;
}
}
}
// Only check the first non-empty, non-comment-only lines
if !trimmed.starts_with('#') {
break;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_yaml_pragma_ignore() {
let content = r#"
# helmlint-ignore HL1001
name: test-chart
version: 1.0.0
"#;
let state = extract_yaml_pragmas(content);
assert!(state.is_ignored(&RuleCode::new("HL1001"), 3));
assert!(!state.is_ignored(&RuleCode::new("HL1002"), 3));
}
#[test]
fn test_yaml_pragma_file_ignore() {
let content = r#"
# helmlint-ignore-file HL1001,HL1002
name: test-chart
"#;
let state = extract_yaml_pragmas(content);
assert!(state.is_ignored(&RuleCode::new("HL1001"), 3));
assert!(state.is_ignored(&RuleCode::new("HL1002"), 10));
assert!(!state.is_ignored(&RuleCode::new("HL1003"), 3));
}
#[test]
fn test_yaml_pragma_disable_file() {
let content = r#"
# helmlint-ignore-file
name: test-chart
"#;
let state = extract_yaml_pragmas(content);
assert!(state.file_disabled);
assert!(state.is_ignored(&RuleCode::new("HL1001"), 3));
assert!(state.is_ignored(&RuleCode::new("HL9999"), 100));
}
#[test]
fn test_template_pragma() {
let content = r#"
{{/* helmlint-ignore HL3001 */}}
{{ .Values.name }}
"#;
let state = extract_template_pragmas(content);
assert!(state.is_ignored(&RuleCode::new("HL3001"), 3));
}
#[test]
fn test_template_pragma_file_ignore() {
let content = r#"
{{/* helmlint-ignore-file HL3001 */}}
apiVersion: v1
kind: ConfigMap
"#;
let state = extract_template_pragmas(content);
assert!(state.is_ignored(&RuleCode::new("HL3001"), 3));
assert!(state.is_ignored(&RuleCode::new("HL3001"), 4));
}
#[test]
fn test_multiple_rules() {
let content = r#"
# helmlint-ignore HL1001, HL1002, HL1003
apiVersion: v2
"#;
let state = extract_yaml_pragmas(content);
assert!(state.is_ignored(&RuleCode::new("HL1001"), 3));
assert!(state.is_ignored(&RuleCode::new("HL1002"), 3));
assert!(state.is_ignored(&RuleCode::new("HL1003"), 3));
}
#[test]
fn test_starts_with_disable_file() {
let content = r#"# helmlint-ignore-file
apiVersion: v2
"#;
assert!(starts_with_disable_file_comment(content));
let content_with_rules = r#"# helmlint-ignore-file HL1001
apiVersion: v2
"#;
assert!(!starts_with_disable_file_comment(content_with_rules));
let content_normal = r#"apiVersion: v2
name: test
"#;
assert!(!starts_with_disable_file_comment(content_normal));
}
#[test]
fn test_disable_alias() {
let content = r#"
# helmlint-disable HL1001
apiVersion: v2
"#;
let state = extract_yaml_pragmas(content);
assert!(state.is_ignored(&RuleCode::new("HL1001"), 3));
}
}