@owenlamont/ryl 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/README.md +13 -0
  2. package/bin/ryl.js +195 -1
  3. package/package.json +35 -13
  4. package/.github/CODEOWNERS +0 -1
  5. package/.github/dependabot.yml +0 -13
  6. package/.github/workflows/ci.yml +0 -107
  7. package/.github/workflows/release.yml +0 -613
  8. package/.github/workflows/update_dependencies.yml +0 -61
  9. package/.github/workflows/update_linters.yml +0 -56
  10. package/.pre-commit-config.yaml +0 -87
  11. package/.yamllint +0 -4
  12. package/AGENTS.md +0 -200
  13. package/Cargo.lock +0 -908
  14. package/Cargo.toml +0 -32
  15. package/clippy.toml +0 -1
  16. package/docs/config-presets.md +0 -100
  17. package/img/benchmark-5x5-5runs.svg +0 -2176
  18. package/pyproject.toml +0 -42
  19. package/ruff.toml +0 -107
  20. package/rumdl.toml +0 -20
  21. package/rust-toolchain.toml +0 -3
  22. package/rustfmt.toml +0 -3
  23. package/scripts/benchmark_perf_vs_yamllint.py +0 -400
  24. package/scripts/coverage-missing.ps1 +0 -80
  25. package/scripts/coverage-missing.sh +0 -60
  26. package/src/bin/discover_config_bin.rs +0 -24
  27. package/src/cli_support.rs +0 -33
  28. package/src/conf/mod.rs +0 -85
  29. package/src/config.rs +0 -2099
  30. package/src/decoder.rs +0 -326
  31. package/src/discover.rs +0 -31
  32. package/src/lib.rs +0 -19
  33. package/src/lint.rs +0 -558
  34. package/src/main.rs +0 -535
  35. package/src/migrate.rs +0 -233
  36. package/src/rules/anchors.rs +0 -517
  37. package/src/rules/braces.rs +0 -77
  38. package/src/rules/brackets.rs +0 -77
  39. package/src/rules/colons.rs +0 -475
  40. package/src/rules/commas.rs +0 -372
  41. package/src/rules/comments.rs +0 -299
  42. package/src/rules/comments_indentation.rs +0 -243
  43. package/src/rules/document_end.rs +0 -175
  44. package/src/rules/document_start.rs +0 -84
  45. package/src/rules/empty_lines.rs +0 -152
  46. package/src/rules/empty_values.rs +0 -255
  47. package/src/rules/float_values.rs +0 -259
  48. package/src/rules/flow_collection.rs +0 -562
  49. package/src/rules/hyphens.rs +0 -104
  50. package/src/rules/indentation.rs +0 -803
  51. package/src/rules/key_duplicates.rs +0 -218
  52. package/src/rules/key_ordering.rs +0 -303
  53. package/src/rules/line_length.rs +0 -326
  54. package/src/rules/mod.rs +0 -25
  55. package/src/rules/new_line_at_end_of_file.rs +0 -23
  56. package/src/rules/new_lines.rs +0 -95
  57. package/src/rules/octal_values.rs +0 -121
  58. package/src/rules/quoted_strings.rs +0 -577
  59. package/src/rules/span_utils.rs +0 -37
  60. package/src/rules/trailing_spaces.rs +0 -65
  61. package/src/rules/truthy.rs +0 -420
  62. package/tests/brackets_carriage_return.rs +0 -114
  63. package/tests/build_global_cfg_error.rs +0 -23
  64. package/tests/cli_anchors_rule.rs +0 -143
  65. package/tests/cli_braces_rule.rs +0 -104
  66. package/tests/cli_brackets_rule.rs +0 -104
  67. package/tests/cli_colons_rule.rs +0 -65
  68. package/tests/cli_commas_rule.rs +0 -104
  69. package/tests/cli_comments_indentation_rule.rs +0 -61
  70. package/tests/cli_comments_rule.rs +0 -67
  71. package/tests/cli_config_data_error.rs +0 -30
  72. package/tests/cli_config_flags.rs +0 -66
  73. package/tests/cli_config_migrate.rs +0 -229
  74. package/tests/cli_document_end_rule.rs +0 -92
  75. package/tests/cli_document_start_rule.rs +0 -92
  76. package/tests/cli_empty_lines_rule.rs +0 -87
  77. package/tests/cli_empty_values_rule.rs +0 -68
  78. package/tests/cli_env_config.rs +0 -34
  79. package/tests/cli_exit_and_errors.rs +0 -41
  80. package/tests/cli_file_encoding.rs +0 -203
  81. package/tests/cli_float_values_rule.rs +0 -64
  82. package/tests/cli_format_options.rs +0 -316
  83. package/tests/cli_global_cfg_relaxed.rs +0 -20
  84. package/tests/cli_hyphens_rule.rs +0 -104
  85. package/tests/cli_indentation_rule.rs +0 -65
  86. package/tests/cli_invalid_project_config.rs +0 -39
  87. package/tests/cli_key_duplicates_rule.rs +0 -104
  88. package/tests/cli_key_ordering_rule.rs +0 -59
  89. package/tests/cli_line_length_rule.rs +0 -85
  90. package/tests/cli_list_files.rs +0 -29
  91. package/tests/cli_new_line_rule.rs +0 -141
  92. package/tests/cli_new_lines_rule.rs +0 -119
  93. package/tests/cli_octal_values_rule.rs +0 -60
  94. package/tests/cli_quoted_strings_rule.rs +0 -47
  95. package/tests/cli_toml_config.rs +0 -119
  96. package/tests/cli_trailing_spaces_rule.rs +0 -77
  97. package/tests/cli_truthy_rule.rs +0 -83
  98. package/tests/cli_yaml_files_negation.rs +0 -45
  99. package/tests/colons_rule.rs +0 -303
  100. package/tests/common/compat.rs +0 -114
  101. package/tests/common/fake_env.rs +0 -93
  102. package/tests/common/mod.rs +0 -1
  103. package/tests/conf_builtin.rs +0 -9
  104. package/tests/config_anchors.rs +0 -84
  105. package/tests/config_braces.rs +0 -121
  106. package/tests/config_brackets.rs +0 -127
  107. package/tests/config_commas.rs +0 -79
  108. package/tests/config_comments.rs +0 -65
  109. package/tests/config_comments_indentation.rs +0 -20
  110. package/tests/config_deep_merge_nonstring_key.rs +0 -24
  111. package/tests/config_document_end.rs +0 -54
  112. package/tests/config_document_start.rs +0 -55
  113. package/tests/config_empty_lines.rs +0 -48
  114. package/tests/config_empty_values.rs +0 -35
  115. package/tests/config_env_errors.rs +0 -23
  116. package/tests/config_env_invalid_inline.rs +0 -15
  117. package/tests/config_env_missing.rs +0 -63
  118. package/tests/config_env_shim.rs +0 -301
  119. package/tests/config_explicit_file_parse_error.rs +0 -55
  120. package/tests/config_extended_features.rs +0 -225
  121. package/tests/config_extends_inline.rs +0 -185
  122. package/tests/config_extends_sequence.rs +0 -18
  123. package/tests/config_find_project_home_boundary.rs +0 -54
  124. package/tests/config_find_project_two_files_in_cwd.rs +0 -47
  125. package/tests/config_float_values.rs +0 -34
  126. package/tests/config_from_yaml_paths.rs +0 -32
  127. package/tests/config_hyphens.rs +0 -51
  128. package/tests/config_ignore_errors.rs +0 -243
  129. package/tests/config_ignore_overrides.rs +0 -83
  130. package/tests/config_indentation.rs +0 -65
  131. package/tests/config_invalid_globs.rs +0 -16
  132. package/tests/config_invalid_types.rs +0 -19
  133. package/tests/config_key_duplicates.rs +0 -34
  134. package/tests/config_key_ordering.rs +0 -70
  135. package/tests/config_line_length.rs +0 -65
  136. package/tests/config_locale.rs +0 -111
  137. package/tests/config_merge.rs +0 -26
  138. package/tests/config_new_lines.rs +0 -89
  139. package/tests/config_octal_values.rs +0 -33
  140. package/tests/config_quoted_strings.rs +0 -195
  141. package/tests/config_rule_level.rs +0 -147
  142. package/tests/config_rules_non_string_keys.rs +0 -23
  143. package/tests/config_scalar_overrides.rs +0 -27
  144. package/tests/config_to_toml.rs +0 -110
  145. package/tests/config_toml_coverage.rs +0 -80
  146. package/tests/config_toml_discovery.rs +0 -304
  147. package/tests/config_trailing_spaces.rs +0 -152
  148. package/tests/config_truthy.rs +0 -77
  149. package/tests/config_yaml_files.rs +0 -62
  150. package/tests/config_yaml_files_all_non_string.rs +0 -15
  151. package/tests/config_yaml_files_empty.rs +0 -30
  152. package/tests/coverage_commas.rs +0 -46
  153. package/tests/decoder_decode.rs +0 -338
  154. package/tests/discover_config_bin_all.rs +0 -66
  155. package/tests/discover_config_bin_env_invalid_yaml.rs +0 -26
  156. package/tests/discover_config_bin_project_config_parse_error.rs +0 -24
  157. package/tests/discover_config_bin_user_global_error.rs +0 -26
  158. package/tests/discover_module.rs +0 -30
  159. package/tests/discover_per_file_dir.rs +0 -10
  160. package/tests/discover_per_file_project_config_error.rs +0 -21
  161. package/tests/float_values.rs +0 -43
  162. package/tests/lint_multi_errors.rs +0 -32
  163. package/tests/main_yaml_ok_filtering.rs +0 -30
  164. package/tests/migrate_module.rs +0 -259
  165. package/tests/resolve_ctx_empty_parent.rs +0 -16
  166. package/tests/rule_anchors.rs +0 -442
  167. package/tests/rule_braces.rs +0 -258
  168. package/tests/rule_brackets.rs +0 -217
  169. package/tests/rule_commas.rs +0 -205
  170. package/tests/rule_comments.rs +0 -197
  171. package/tests/rule_comments_indentation.rs +0 -127
  172. package/tests/rule_document_end.rs +0 -118
  173. package/tests/rule_document_start.rs +0 -60
  174. package/tests/rule_empty_lines.rs +0 -96
  175. package/tests/rule_empty_values.rs +0 -102
  176. package/tests/rule_float_values.rs +0 -109
  177. package/tests/rule_hyphens.rs +0 -65
  178. package/tests/rule_indentation.rs +0 -455
  179. package/tests/rule_key_duplicates.rs +0 -76
  180. package/tests/rule_key_ordering.rs +0 -207
  181. package/tests/rule_line_length.rs +0 -200
  182. package/tests/rule_new_lines.rs +0 -51
  183. package/tests/rule_octal_values.rs +0 -53
  184. package/tests/rule_quoted_strings.rs +0 -290
  185. package/tests/rule_trailing_spaces.rs +0 -41
  186. package/tests/rule_truthy.rs +0 -236
  187. package/tests/user_global_invalid_yaml.rs +0 -32
  188. package/tests/yamllint_compat_anchors.rs +0 -280
  189. package/tests/yamllint_compat_braces.rs +0 -411
  190. package/tests/yamllint_compat_brackets.rs +0 -364
  191. package/tests/yamllint_compat_colons.rs +0 -298
  192. package/tests/yamllint_compat_colors.rs +0 -80
  193. package/tests/yamllint_compat_commas.rs +0 -375
  194. package/tests/yamllint_compat_comments.rs +0 -167
  195. package/tests/yamllint_compat_comments_indentation.rs +0 -281
  196. package/tests/yamllint_compat_config.rs +0 -170
  197. package/tests/yamllint_compat_document_end.rs +0 -243
  198. package/tests/yamllint_compat_document_start.rs +0 -136
  199. package/tests/yamllint_compat_empty_lines.rs +0 -117
  200. package/tests/yamllint_compat_empty_values.rs +0 -179
  201. package/tests/yamllint_compat_float_values.rs +0 -216
  202. package/tests/yamllint_compat_hyphens.rs +0 -223
  203. package/tests/yamllint_compat_indentation.rs +0 -398
  204. package/tests/yamllint_compat_key_duplicates.rs +0 -139
  205. package/tests/yamllint_compat_key_ordering.rs +0 -170
  206. package/tests/yamllint_compat_line_length.rs +0 -375
  207. package/tests/yamllint_compat_list.rs +0 -127
  208. package/tests/yamllint_compat_new_line.rs +0 -133
  209. package/tests/yamllint_compat_newline_types.rs +0 -185
  210. package/tests/yamllint_compat_octal_values.rs +0 -172
  211. package/tests/yamllint_compat_quoted_strings.rs +0 -154
  212. package/tests/yamllint_compat_syntax.rs +0 -200
  213. package/tests/yamllint_compat_trailing_spaces.rs +0 -162
  214. package/tests/yamllint_compat_truthy.rs +0 -130
  215. package/tests/yamllint_compat_yaml_files.rs +0 -81
  216. package/typos.toml +0 -2
@@ -1,372 +0,0 @@
1
- use std::ops::Range;
2
-
3
- use saphyr_parser::{Event, Marker, Parser, Span, SpannedEventReceiver};
4
-
5
- use crate::config::YamlLintConfig;
6
- use crate::rules::span_utils::span_char_index_to_byte;
7
-
8
- pub const ID: &str = "commas";
9
- const TOO_MANY_BEFORE: &str = "too many spaces before comma";
10
- const TOO_FEW_AFTER: &str = "too few spaces after comma";
11
- const TOO_MANY_AFTER: &str = "too many spaces after comma";
12
-
13
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
14
- pub struct Config {
15
- max_spaces_before: i64,
16
- min_spaces_after: i64,
17
- max_spaces_after: i64,
18
- }
19
-
20
- impl Config {
21
- const DEFAULT_MAX_BEFORE: i64 = 0;
22
- const DEFAULT_MIN_AFTER: i64 = 1;
23
- const DEFAULT_MAX_AFTER: i64 = 1;
24
-
25
- #[must_use]
26
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
27
- let max_spaces_before = cfg
28
- .rule_option(ID, "max-spaces-before")
29
- .and_then(saphyr::YamlOwned::as_integer)
30
- .unwrap_or(Self::DEFAULT_MAX_BEFORE);
31
- let min_spaces_after = cfg
32
- .rule_option(ID, "min-spaces-after")
33
- .and_then(saphyr::YamlOwned::as_integer)
34
- .unwrap_or(Self::DEFAULT_MIN_AFTER);
35
- let max_spaces_after = cfg
36
- .rule_option(ID, "max-spaces-after")
37
- .and_then(saphyr::YamlOwned::as_integer)
38
- .unwrap_or(Self::DEFAULT_MAX_AFTER);
39
-
40
- Self {
41
- max_spaces_before,
42
- min_spaces_after,
43
- max_spaces_after,
44
- }
45
- }
46
-
47
- #[must_use]
48
- pub const fn new_for_tests(
49
- max_spaces_before: i64,
50
- min_spaces_after: i64,
51
- max_spaces_after: i64,
52
- ) -> Self {
53
- Self {
54
- max_spaces_before,
55
- min_spaces_after,
56
- max_spaces_after,
57
- }
58
- }
59
-
60
- #[must_use]
61
- pub const fn max_spaces_before(&self) -> i64 {
62
- self.max_spaces_before
63
- }
64
-
65
- #[must_use]
66
- pub const fn min_spaces_after(&self) -> i64 {
67
- self.min_spaces_after
68
- }
69
-
70
- #[must_use]
71
- pub const fn max_spaces_after(&self) -> i64 {
72
- self.max_spaces_after
73
- }
74
- }
75
-
76
- #[derive(Debug, Clone, PartialEq, Eq)]
77
- pub struct Violation {
78
- pub line: usize,
79
- pub column: usize,
80
- pub message: String,
81
- }
82
-
83
- enum FlowKind {
84
- Sequence,
85
- Mapping,
86
- }
87
-
88
- struct ScalarRangeCollector {
89
- ranges: Vec<Range<usize>>,
90
- }
91
-
92
- impl ScalarRangeCollector {
93
- const fn new() -> Self {
94
- Self { ranges: Vec::new() }
95
- }
96
-
97
- fn push_range(&mut self, span: Span) {
98
- let start = span.start.index();
99
- let end = span.end.index();
100
- if start < end {
101
- self.ranges.push(start..end);
102
- }
103
- }
104
-
105
- fn into_sorted(self) -> Vec<Range<usize>> {
106
- let mut ranges = self.ranges;
107
- ranges.sort_by(|a, b| a.start.cmp(&b.start));
108
- ranges
109
- }
110
- }
111
-
112
- impl SpannedEventReceiver<'_> for ScalarRangeCollector {
113
- fn on_event(&mut self, ev: Event<'_>, span: Span) {
114
- if matches!(ev, Event::Scalar(..)) {
115
- self.push_range(span);
116
- }
117
- }
118
- }
119
-
120
- enum BeforeResult {
121
- SameLine { spaces: usize },
122
- Ignored,
123
- }
124
-
125
- enum AfterResult {
126
- SameLine { spaces: usize, next_char: usize },
127
- Ignored,
128
- }
129
-
130
- #[must_use]
131
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
132
- if buffer.is_empty() {
133
- return Vec::new();
134
- }
135
-
136
- let mut parser = Parser::new_from_str(buffer);
137
- let mut collector = ScalarRangeCollector::new();
138
- let _ = parser.load(&mut collector, true);
139
- let scalar_ranges = collector.into_sorted();
140
-
141
- let chars: Vec<(usize, char)> = buffer.char_indices().collect();
142
- let buffer_len = buffer.len();
143
-
144
- let line_starts = build_line_starts(buffer);
145
-
146
- let mut violations = Vec::new();
147
- let mut contexts: Vec<FlowKind> = Vec::new();
148
- let mut i = 0usize;
149
- let mut range_idx = 0usize;
150
-
151
- while i < chars.len() {
152
- let (byte_idx, ch) = chars[i];
153
-
154
- while range_idx < scalar_ranges.len()
155
- && span_char_index_to_byte(&chars, scalar_ranges[range_idx].end, buffer_len)
156
- <= byte_idx
157
- {
158
- range_idx += 1;
159
- }
160
-
161
- if let Some(range) = scalar_ranges.get(range_idx) {
162
- let start_byte = span_char_index_to_byte(&chars, range.start, buffer_len);
163
- let end_byte = span_char_index_to_byte(&chars, range.end, buffer_len);
164
- if byte_idx >= start_byte && byte_idx < end_byte {
165
- i = range.end;
166
- continue;
167
- }
168
- }
169
-
170
- match ch {
171
- '[' => contexts.push(FlowKind::Sequence),
172
- '{' => contexts.push(FlowKind::Mapping),
173
- ']' | '}' => {
174
- contexts.pop();
175
- }
176
- '#' => {
177
- i = skip_comment(&chars, i);
178
- continue;
179
- }
180
- ',' => {
181
- if !contexts.is_empty() {
182
- evaluate_comma(cfg, &mut violations, &chars, i, &line_starts);
183
- }
184
- }
185
- _ => {}
186
- }
187
-
188
- i += 1;
189
- }
190
-
191
- violations
192
- }
193
-
194
- fn skip_comment(chars: &[(usize, char)], mut idx: usize) -> usize {
195
- idx += 1;
196
- while idx < chars.len() {
197
- let ch = chars[idx].1;
198
- if ch == '\n' {
199
- break;
200
- }
201
- if ch == '\r' {
202
- if idx + 1 < chars.len() && chars[idx + 1].1 == '\n' {
203
- idx += 1;
204
- }
205
- break;
206
- }
207
- idx += 1;
208
- }
209
- idx
210
- }
211
-
212
- fn evaluate_comma(
213
- cfg: &Config,
214
- violations: &mut Vec<Violation>,
215
- chars: &[(usize, char)],
216
- comma_idx: usize,
217
- line_starts: &[usize],
218
- ) {
219
- if let BeforeResult::SameLine { spaces } = compute_spaces_before(chars, comma_idx)
220
- && cfg.max_spaces_before >= 0
221
- {
222
- let spaces_i64 = i64::try_from(spaces).unwrap_or(i64::MAX);
223
- if spaces_i64 > cfg.max_spaces_before {
224
- let comma_byte = chars[comma_idx].0;
225
- let (line, column) = line_and_column(line_starts, comma_byte);
226
- let highlight_column = column.saturating_sub(1).max(1);
227
- violations.push(Violation {
228
- line,
229
- column: highlight_column,
230
- message: TOO_MANY_BEFORE.to_string(),
231
- });
232
- }
233
- }
234
-
235
- if let AfterResult::SameLine { spaces, next_char } =
236
- compute_spaces_after(chars, comma_idx)
237
- {
238
- let spaces_i64 = i64::try_from(spaces).unwrap_or(i64::MAX);
239
- let next_byte = chars[next_char].0;
240
- let (line, column) = line_and_column(line_starts, next_byte);
241
- if cfg.max_spaces_after >= 0 && spaces_i64 > cfg.max_spaces_after {
242
- let highlight_column = column.saturating_sub(1).max(1);
243
- violations.push(Violation {
244
- line,
245
- column: highlight_column,
246
- message: TOO_MANY_AFTER.to_string(),
247
- });
248
- }
249
- if cfg.min_spaces_after >= 0 && spaces_i64 < cfg.min_spaces_after {
250
- violations.push(Violation {
251
- line,
252
- column,
253
- message: TOO_FEW_AFTER.to_string(),
254
- });
255
- }
256
- }
257
- }
258
-
259
- fn compute_spaces_before(chars: &[(usize, char)], comma_idx: usize) -> BeforeResult {
260
- let mut spaces = 0usize;
261
- let mut idx = comma_idx;
262
-
263
- loop {
264
- let Some(prev) = idx.checked_sub(1) else {
265
- return BeforeResult::SameLine { spaces };
266
- };
267
-
268
- let ch = chars[prev].1;
269
- if matches!(ch, ' ' | '\t') {
270
- spaces += 1;
271
- idx = prev;
272
- continue;
273
- }
274
- if matches!(ch, '\n' | '\r') {
275
- return BeforeResult::Ignored;
276
- }
277
- return BeforeResult::SameLine { spaces };
278
- }
279
- }
280
-
281
- fn compute_spaces_after(chars: &[(usize, char)], comma_idx: usize) -> AfterResult {
282
- let mut spaces = 0usize;
283
- let mut idx = comma_idx + 1;
284
- while idx < chars.len() {
285
- match chars[idx].1 {
286
- ' ' | '\t' => {
287
- spaces += 1;
288
- idx += 1;
289
- }
290
- '\n' | '\r' | '#' => return AfterResult::Ignored,
291
- _ => {
292
- return AfterResult::SameLine {
293
- spaces,
294
- next_char: idx,
295
- };
296
- }
297
- }
298
- }
299
- AfterResult::Ignored
300
- }
301
-
302
- fn build_line_starts(buffer: &str) -> Vec<usize> {
303
- let mut starts = Vec::new();
304
- starts.push(0);
305
- let bytes = buffer.as_bytes();
306
- let mut idx = 0usize;
307
- while idx < bytes.len() {
308
- match bytes[idx] {
309
- b'\n' => {
310
- starts.push(idx + 1);
311
- idx += 1;
312
- }
313
- b'\r' => {
314
- if idx + 1 < bytes.len() && bytes[idx + 1] == b'\n' {
315
- starts.push(idx + 2);
316
- idx += 2;
317
- } else {
318
- starts.push(idx + 1);
319
- idx += 1;
320
- }
321
- }
322
- _ => idx += 1,
323
- }
324
- }
325
- starts
326
- }
327
-
328
- fn line_and_column(line_starts: &[usize], byte_idx: usize) -> (usize, usize) {
329
- let mut left = 0usize;
330
- let mut right = line_starts.len();
331
- while left + 1 < right {
332
- let mid = usize::midpoint(left, right);
333
- if line_starts[mid] <= byte_idx {
334
- left = mid;
335
- } else {
336
- right = mid;
337
- }
338
- }
339
- let line_start = line_starts[left];
340
- (left + 1, byte_idx - line_start + 1)
341
- }
342
-
343
- #[doc(hidden)]
344
- #[must_use]
345
- pub fn coverage_compute_spaces_before(buffer: &str, comma_idx: usize) -> Option<usize> {
346
- let chars: Vec<(usize, char)> = buffer.char_indices().collect();
347
- debug_assert!(comma_idx < chars.len());
348
- match compute_spaces_before(&chars, comma_idx) {
349
- BeforeResult::SameLine { spaces } => Some(spaces),
350
- BeforeResult::Ignored => None,
351
- }
352
- }
353
-
354
- #[doc(hidden)]
355
- #[must_use]
356
- pub fn coverage_skip_zero_length_span() -> usize {
357
- let mut collector = ScalarRangeCollector::new();
358
- collector.push_range(Span::empty(Marker::default()));
359
- collector.into_sorted().len()
360
- }
361
-
362
- #[doc(hidden)]
363
- #[must_use]
364
- pub fn coverage_skip_comment_crlf() -> (usize, usize) {
365
- let chars_crlf: Vec<(usize, char)> = "#\r\n".char_indices().collect();
366
- let idx_crlf = skip_comment(&chars_crlf, 0);
367
-
368
- let chars_cr: Vec<(usize, char)> = "#\r".char_indices().collect();
369
- let idx_cr = skip_comment(&chars_cr, 0);
370
-
371
- (idx_crlf, idx_cr)
372
- }
@@ -1,299 +0,0 @@
1
- use saphyr::YamlOwned;
2
-
3
- use crate::config::YamlLintConfig;
4
-
5
- pub const ID: &str = "comments";
6
-
7
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
8
- pub struct Config {
9
- require_starting_space: bool,
10
- ignore_shebangs: bool,
11
- min_spaces_from_content: Option<usize>,
12
- }
13
-
14
- impl Config {
15
- #[must_use]
16
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
17
- let require_starting_space = cfg
18
- .rule_option(ID, "require-starting-space")
19
- .and_then(YamlOwned::as_bool)
20
- .unwrap_or(true);
21
-
22
- let ignore_shebangs = cfg
23
- .rule_option(ID, "ignore-shebangs")
24
- .and_then(YamlOwned::as_bool)
25
- .unwrap_or(true);
26
-
27
- let min_spaces_value = cfg
28
- .rule_option(ID, "min-spaces-from-content")
29
- .and_then(YamlOwned::as_integer)
30
- .unwrap_or(2);
31
-
32
- let min_spaces_from_content = if min_spaces_value < 0 {
33
- None
34
- } else {
35
- Some(usize::try_from(min_spaces_value).unwrap_or(usize::MAX))
36
- };
37
-
38
- Self {
39
- require_starting_space,
40
- ignore_shebangs,
41
- min_spaces_from_content,
42
- }
43
- }
44
-
45
- const fn require_starting_space(&self) -> bool {
46
- self.require_starting_space
47
- }
48
-
49
- const fn ignore_shebangs(&self) -> bool {
50
- self.ignore_shebangs
51
- }
52
-
53
- const fn min_spaces_from_content(&self) -> Option<usize> {
54
- self.min_spaces_from_content
55
- }
56
- }
57
-
58
- #[derive(Debug, Clone, PartialEq, Eq)]
59
- pub struct Violation {
60
- pub line: usize,
61
- pub column: usize,
62
- pub message: String,
63
- }
64
-
65
- #[must_use]
66
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
67
- let mut violations = Vec::new();
68
- let mut quote_state = QuoteState::default();
69
- let mut block_tracker = BlockScalarTracker::default();
70
-
71
- for (line_idx, line) in buffer.lines().enumerate() {
72
- let indent = leading_indent_width(line);
73
- let content = &line[indent..];
74
-
75
- if block_tracker.consume_line(indent, content) {
76
- continue;
77
- }
78
-
79
- let Some(comment_start) = find_comment_start(line, &mut quote_state) else {
80
- block_tracker.observe_indicator(indent, content);
81
- continue;
82
- };
83
-
84
- if let Some(required) = cfg.min_spaces_from_content()
85
- && is_inline_comment(line, comment_start)
86
- && inline_spacing_width(line, comment_start) < required
87
- {
88
- violations.push(Violation {
89
- line: line_idx + 1,
90
- column: column_at(line, comment_start),
91
- message: format!("too few spaces before comment: expected {required}"),
92
- });
93
- }
94
-
95
- if !cfg.require_starting_space() {
96
- continue;
97
- }
98
-
99
- let after_hash_idx = comment_start + skip_hashes(&line[comment_start..]);
100
- if after_hash_idx >= line.len() {
101
- continue;
102
- }
103
-
104
- let next_char = line[after_hash_idx..].chars().next().unwrap_or(' ');
105
-
106
- if cfg.ignore_shebangs()
107
- && line_idx == 0
108
- && comment_start == 0
109
- && next_char == '!'
110
- {
111
- continue;
112
- }
113
-
114
- if next_char != ' ' {
115
- violations.push(Violation {
116
- line: line_idx + 1,
117
- column: column_at(line, after_hash_idx),
118
- message: "missing starting space in comment".to_string(),
119
- });
120
- }
121
-
122
- block_tracker.observe_indicator(indent, content);
123
- }
124
-
125
- violations
126
- }
127
-
128
- #[derive(Debug, Default)]
129
- struct BlockScalarTracker {
130
- state: Option<BlockScalarState>,
131
- }
132
-
133
- #[derive(Debug)]
134
- struct BlockScalarState {
135
- indicator_indent: usize,
136
- content_indent: Option<usize>,
137
- }
138
-
139
- impl BlockScalarTracker {
140
- fn consume_line(&mut self, indent: usize, content: &str) -> bool {
141
- let Some(state) = self.state.as_mut() else {
142
- return false;
143
- };
144
-
145
- if content.trim().is_empty() {
146
- return true;
147
- }
148
-
149
- if let Some(content_indent) = state.content_indent {
150
- if indent >= content_indent {
151
- return true;
152
- }
153
-
154
- if indent <= state.indicator_indent {
155
- self.state = None;
156
- return false;
157
- }
158
-
159
- state.content_indent = Some(content_indent.min(indent));
160
- return true;
161
- }
162
-
163
- if indent > state.indicator_indent {
164
- state.content_indent = Some(indent);
165
- return true;
166
- }
167
-
168
- self.state = None;
169
- false
170
- }
171
-
172
- fn observe_indicator(&mut self, indent: usize, content: &str) {
173
- let candidate = strip_trailing_comment_for_block(content).trim_end();
174
- if is_block_scalar_indicator(candidate) {
175
- self.state = Some(BlockScalarState {
176
- indicator_indent: indent,
177
- content_indent: None,
178
- });
179
- }
180
- }
181
- }
182
-
183
- #[derive(Debug, Default, Clone, Copy)]
184
- struct QuoteState {
185
- in_single: bool,
186
- in_double: bool,
187
- escaped: bool,
188
- }
189
-
190
- fn find_comment_start(line: &str, state: &mut QuoteState) -> Option<usize> {
191
- for (idx, ch) in line.char_indices() {
192
- if ch == '\\' && !state.in_single {
193
- state.escaped = !state.escaped;
194
- continue;
195
- }
196
-
197
- if state.escaped {
198
- state.escaped = false;
199
- continue;
200
- }
201
-
202
- match ch {
203
- '\'' if !state.in_double => {
204
- state.in_single = !state.in_single;
205
- }
206
- '"' if !state.in_single => {
207
- state.in_double = !state.in_double;
208
- }
209
- '#' if !state.in_single && !state.in_double => {
210
- if is_comment_position(line, idx) {
211
- return Some(idx);
212
- }
213
- }
214
- _ => {}
215
- }
216
- }
217
-
218
- state.escaped = false;
219
- None
220
- }
221
-
222
- fn is_inline_comment(line: &str, comment_start: usize) -> bool {
223
- !line[..comment_start].trim().is_empty()
224
- }
225
-
226
- fn inline_spacing_width(line: &str, comment_start: usize) -> usize {
227
- line[..comment_start]
228
- .chars()
229
- .rev()
230
- .take_while(|ch| ch.is_whitespace())
231
- .count()
232
- }
233
-
234
- fn skip_hashes(slice: &str) -> usize {
235
- slice
236
- .chars()
237
- .take_while(|ch| *ch == '#')
238
- .map(char::len_utf8)
239
- .sum()
240
- }
241
-
242
- fn column_at(line: &str, byte_idx: usize) -> usize {
243
- line[..byte_idx].chars().count() + 1
244
- }
245
-
246
- fn leading_indent_width(line: &str) -> usize {
247
- line.chars()
248
- .take_while(|ch| matches!(ch, ' ' | '\t'))
249
- .count()
250
- }
251
-
252
- fn is_comment_position(line: &str, idx: usize) -> bool {
253
- line[..idx].chars().last().is_none_or(char::is_whitespace)
254
- }
255
-
256
- fn strip_trailing_comment_for_block(content: &str) -> &str {
257
- let mut in_single = false;
258
- let mut in_double = false;
259
- let mut escaped = false;
260
- for (idx, ch) in content.char_indices() {
261
- if ch == '\\' && !in_single {
262
- escaped = !escaped;
263
- continue;
264
- }
265
-
266
- if escaped {
267
- escaped = false;
268
- continue;
269
- }
270
-
271
- match ch {
272
- '\'' if !in_double => {
273
- in_single = !in_single;
274
- }
275
- '"' if !in_single => {
276
- in_double = !in_double;
277
- }
278
- '#' if !in_single && !in_double => {
279
- return content[..idx].trim_end();
280
- }
281
- _ => {}
282
- }
283
- }
284
- content.trim_end()
285
- }
286
-
287
- fn is_block_scalar_indicator(content: &str) -> bool {
288
- if content.is_empty() {
289
- return false;
290
- }
291
-
292
- let trimmed = content.trim_end_matches(|ch: char| ch.is_whitespace());
293
- trimmed.ends_with("|-")
294
- || trimmed.ends_with("|+")
295
- || trimmed.ends_with('|')
296
- || trimmed.ends_with(">-")
297
- || trimmed.ends_with(">+")
298
- || trimmed.ends_with('>')
299
- }