@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,243 +0,0 @@
1
- use crate::config::YamlLintConfig;
2
-
3
- pub const ID: &str = "comments-indentation";
4
- pub const MESSAGE: &str = "comment not indented like content";
5
-
6
- #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
7
- pub struct Config;
8
-
9
- impl Config {
10
- #[must_use]
11
- pub const fn resolve(_cfg: &YamlLintConfig) -> Self {
12
- Self
13
- }
14
- }
15
-
16
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
17
- pub struct Violation {
18
- pub line: usize,
19
- pub column: usize,
20
- }
21
-
22
- #[must_use]
23
- pub fn check(buffer: &str, _cfg: &Config) -> Vec<Violation> {
24
- let mut diagnostics: Vec<Violation> = Vec::new();
25
- if buffer.is_empty() {
26
- return diagnostics;
27
- }
28
-
29
- let mut block_tracker = BlockScalarTracker::default();
30
- let mut lines: Vec<LineInfo> = Vec::new();
31
-
32
- for raw_line in buffer.lines() {
33
- let line = raw_line.trim_end_matches('\r');
34
- let indent = leading_whitespace_width(line);
35
- let content = &line[indent..];
36
-
37
- let consumed = block_tracker.consume_line(indent, content);
38
- let kind = if consumed {
39
- LineKind::BlockScalarContent
40
- } else {
41
- classify_line_kind(content)
42
- };
43
-
44
- lines.push(LineInfo { indent, kind });
45
- block_tracker.observe_indicator(indent, content);
46
- }
47
-
48
- let prev_content_indents = compute_prev_content_indents(&lines);
49
- let next_content_indents = compute_next_content_indents(&lines);
50
-
51
- let mut last_comment_indent: Option<usize> = None;
52
-
53
- for (idx, line) in lines.iter().enumerate() {
54
- match line.kind {
55
- LineKind::Comment => {
56
- let prev_indent = prev_content_indents[idx].unwrap_or(0);
57
- let next_indent = next_content_indents[idx].unwrap_or(0);
58
-
59
- let reference_indent =
60
- last_comment_indent.unwrap_or_else(|| prev_indent.max(next_indent));
61
-
62
- if line.indent != reference_indent && line.indent != next_indent {
63
- diagnostics.push(Violation {
64
- line: idx + 1,
65
- column: line.indent + 1,
66
- });
67
- }
68
-
69
- last_comment_indent = Some(line.indent);
70
- }
71
- LineKind::Other | LineKind::DirectiveComment => {
72
- last_comment_indent = None;
73
- }
74
- LineKind::Empty | LineKind::BlockScalarContent => {}
75
- }
76
- }
77
-
78
- diagnostics
79
- }
80
-
81
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
82
- struct LineInfo {
83
- indent: usize,
84
- kind: LineKind,
85
- }
86
-
87
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
88
- enum LineKind {
89
- Empty,
90
- Comment,
91
- DirectiveComment,
92
- BlockScalarContent,
93
- Other,
94
- }
95
-
96
- fn classify_line_kind(content: &str) -> LineKind {
97
- let trimmed = content.trim_start_matches([' ', '\t']);
98
-
99
- if trimmed.is_empty() {
100
- LineKind::Empty
101
- } else if trimmed.starts_with("# yamllint ") {
102
- LineKind::DirectiveComment
103
- } else if trimmed.starts_with('#') {
104
- LineKind::Comment
105
- } else {
106
- LineKind::Other
107
- }
108
- }
109
-
110
- fn leading_whitespace_width(line: &str) -> usize {
111
- line.chars()
112
- .take_while(|ch| matches!(ch, ' ' | '\t'))
113
- .count()
114
- }
115
-
116
- #[derive(Debug, Default)]
117
- struct BlockScalarTracker {
118
- state: Option<BlockScalarState>,
119
- }
120
-
121
- #[derive(Debug)]
122
- struct BlockScalarState {
123
- indicator_indent: usize,
124
- content_indent: Option<usize>,
125
- }
126
-
127
- impl BlockScalarTracker {
128
- fn consume_line(&mut self, indent: usize, content: &str) -> bool {
129
- let Some(state) = self.state.as_mut() else {
130
- return false;
131
- };
132
-
133
- if content.trim().is_empty() {
134
- return true;
135
- }
136
-
137
- let updated_indent = if let Some(content_indent) = state.content_indent {
138
- if indent >= content_indent {
139
- return true;
140
- }
141
- if indent <= state.indicator_indent {
142
- self.state = None;
143
- return false;
144
- }
145
- content_indent.min(indent)
146
- } else {
147
- if indent <= state.indicator_indent {
148
- self.state = None;
149
- return false;
150
- }
151
- indent
152
- };
153
- state.content_indent = Some(updated_indent);
154
- true
155
- }
156
-
157
- fn observe_indicator(&mut self, indent: usize, content: &str) {
158
- let candidate = strip_trailing_comment_for_block(content).trim_end();
159
- if is_block_scalar_indicator(candidate) {
160
- self.state = Some(BlockScalarState {
161
- indicator_indent: indent,
162
- content_indent: None,
163
- });
164
- }
165
- }
166
- }
167
-
168
- fn compute_prev_content_indents(lines: &[LineInfo]) -> Vec<Option<usize>> {
169
- let mut result: Vec<Option<usize>> = Vec::with_capacity(lines.len());
170
- let mut latest: Option<usize> = None;
171
- for line in lines {
172
- if line.kind == LineKind::Other {
173
- latest = Some(line.indent);
174
- }
175
- result.push(latest);
176
- }
177
- result
178
- }
179
-
180
- fn compute_next_content_indents(lines: &[LineInfo]) -> Vec<Option<usize>> {
181
- let mut result: Vec<Option<usize>> = vec![None; lines.len()];
182
- let mut upcoming: Option<usize> = None;
183
- for (idx, line) in lines.iter().enumerate().rev() {
184
- if line.kind == LineKind::Other {
185
- upcoming = Some(line.indent);
186
- }
187
- result[idx] = upcoming;
188
- }
189
- result
190
- }
191
-
192
- fn strip_trailing_comment_for_block(content: &str) -> &str {
193
- let mut in_single = false;
194
- let mut in_double = false;
195
- let mut escaped = false;
196
- for (idx, ch) in content.char_indices() {
197
- if ch == '\\' && !in_single {
198
- escaped = !escaped;
199
- continue;
200
- }
201
-
202
- if escaped {
203
- escaped = false;
204
- continue;
205
- }
206
-
207
- match ch {
208
- '\'' if !in_double => {
209
- in_single = !in_single;
210
- }
211
- '"' if !in_single => {
212
- in_double = !in_double;
213
- }
214
- '#' if !in_single && !in_double => {
215
- return content[..idx].trim_end();
216
- }
217
- _ => {}
218
- }
219
- }
220
- content.trim_end()
221
- }
222
-
223
- fn is_block_scalar_indicator(content: &str) -> bool {
224
- if content.is_empty() {
225
- return false;
226
- }
227
-
228
- let trimmed = content.trim_end_matches(|ch: char| ch.is_whitespace());
229
- let marker_idx = if trimmed.ends_with("|-")
230
- || trimmed.ends_with("|+")
231
- || trimmed.ends_with(">-")
232
- || trimmed.ends_with(">+")
233
- {
234
- trimmed.len().saturating_sub(2)
235
- } else if trimmed.ends_with('|') || trimmed.ends_with('>') {
236
- trimmed.len().saturating_sub(1)
237
- } else {
238
- return false;
239
- };
240
-
241
- let prefix = trimmed[..marker_idx].trim_end();
242
- prefix.ends_with(':') || prefix.ends_with('-') || prefix.ends_with('?')
243
- }
@@ -1,175 +0,0 @@
1
- use std::cmp;
2
-
3
- use saphyr_parser::{Event, Parser, Span, SpannedEventReceiver};
4
-
5
- use crate::config::YamlLintConfig;
6
-
7
- pub const ID: &str = "document-end";
8
- pub const MISSING_MESSAGE: &str = "missing document end \"...\"";
9
- pub const FORBIDDEN_MESSAGE: &str = "found forbidden document end \"...\"";
10
-
11
- #[must_use]
12
- pub fn classify_document_end_marker_bytes(bytes: &[u8]) -> Option<&'static str> {
13
- let trimmed = trim_ascii(bytes);
14
- if trimmed == b"..." {
15
- Some("...")
16
- } else if trimmed == b"---" {
17
- Some("---")
18
- } else {
19
- None
20
- }
21
- }
22
-
23
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
24
- pub struct Config {
25
- present: bool,
26
- }
27
-
28
- impl Config {
29
- #[must_use]
30
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
31
- let present = cfg
32
- .rule_option(ID, "present")
33
- .and_then(saphyr::YamlOwned::as_bool)
34
- .unwrap_or(true);
35
- Self { present }
36
- }
37
-
38
- #[must_use]
39
- pub const fn new_for_tests(present: bool) -> Self {
40
- Self { present }
41
- }
42
-
43
- #[must_use]
44
- pub const fn requires_marker(&self) -> bool {
45
- self.present
46
- }
47
- }
48
-
49
- #[derive(Debug, Clone, PartialEq, Eq)]
50
- pub struct Violation {
51
- pub line: usize,
52
- pub column: usize,
53
- pub message: String,
54
- }
55
-
56
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
57
- enum Marker {
58
- ExplicitEnd,
59
- DocumentStart,
60
- Other,
61
- }
62
-
63
- #[must_use]
64
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
65
- let mut parser = Parser::new_from_str(buffer);
66
- let mut receiver = DocumentEndReceiver::new(buffer, cfg);
67
- let _ = parser.load(&mut receiver, true);
68
- receiver.violations
69
- }
70
-
71
- struct DocumentEndReceiver<'src, 'cfg> {
72
- source: &'src str,
73
- config: &'cfg Config,
74
- violations: Vec<Violation>,
75
- pending_stream_end_violation: bool,
76
- }
77
-
78
- impl<'src, 'cfg> DocumentEndReceiver<'src, 'cfg> {
79
- const fn new(source: &'src str, config: &'cfg Config) -> Self {
80
- Self {
81
- source,
82
- config,
83
- violations: Vec::new(),
84
- pending_stream_end_violation: false,
85
- }
86
- }
87
-
88
- fn handle_document_end(&mut self, span: Span) {
89
- let marker = self.marker(span);
90
-
91
- if !self.config.requires_marker() {
92
- self.pending_stream_end_violation = false;
93
- if matches!(marker, Marker::ExplicitEnd) {
94
- self.violations.push(Violation {
95
- line: span.start.line(),
96
- column: span.start.col() + 1,
97
- message: FORBIDDEN_MESSAGE.to_string(),
98
- });
99
- }
100
- return;
101
- }
102
-
103
- match marker {
104
- Marker::ExplicitEnd => {
105
- self.pending_stream_end_violation = false;
106
- }
107
- Marker::DocumentStart => {
108
- self.pending_stream_end_violation = false;
109
- self.violations.push(Violation {
110
- line: span.start.line(),
111
- column: 1,
112
- message: MISSING_MESSAGE.to_string(),
113
- });
114
- }
115
- Marker::Other => {
116
- self.pending_stream_end_violation = true;
117
- }
118
- }
119
- }
120
-
121
- fn handle_stream_end(&mut self, span: Span) {
122
- if !self.config.requires_marker() || !self.pending_stream_end_violation {
123
- return;
124
- }
125
-
126
- let raw_line = span.start.line();
127
- let line = cmp::max(1, raw_line.saturating_sub(1));
128
- self.violations.push(Violation {
129
- line,
130
- column: 1,
131
- message: MISSING_MESSAGE.to_string(),
132
- });
133
- self.pending_stream_end_violation = false;
134
- }
135
-
136
- fn marker(&self, span: Span) -> Marker {
137
- let start = span.start.index().min(self.source.len());
138
- let end = span.end.index().min(self.source.len());
139
- let slice = if start < end {
140
- &self.source.as_bytes()[start..end]
141
- } else {
142
- &[]
143
- };
144
-
145
- match classify_document_end_marker_bytes(slice) {
146
- Some("...") => Marker::ExplicitEnd,
147
- Some("---") => Marker::DocumentStart,
148
- _ => Marker::Other,
149
- }
150
- }
151
- }
152
-
153
- impl SpannedEventReceiver<'_> for DocumentEndReceiver<'_, '_> {
154
- fn on_event(&mut self, event: Event<'_>, span: Span) {
155
- match event {
156
- Event::DocumentEnd => self.handle_document_end(span),
157
- Event::StreamEnd => self.handle_stream_end(span),
158
- _ => {}
159
- }
160
- }
161
- }
162
-
163
- fn trim_ascii(bytes: &[u8]) -> &[u8] {
164
- let mut start = 0usize;
165
- let mut end = bytes.len();
166
-
167
- while start < end && bytes[start].is_ascii_whitespace() {
168
- start += 1;
169
- }
170
- while start < end && bytes[end - 1].is_ascii_whitespace() {
171
- end -= 1;
172
- }
173
-
174
- &bytes[start..end]
175
- }
@@ -1,84 +0,0 @@
1
- use saphyr_parser::{Event, Parser, Span, SpannedEventReceiver};
2
-
3
- use crate::config::YamlLintConfig;
4
-
5
- pub const ID: &str = "document-start";
6
- pub const MISSING_MESSAGE: &str = "missing document start \"---\"";
7
- pub const FORBIDDEN_MESSAGE: &str = "found forbidden document start \"---\"";
8
-
9
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
10
- pub struct Config {
11
- present: bool,
12
- }
13
-
14
- impl Config {
15
- #[must_use]
16
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
17
- let present = cfg
18
- .rule_option(ID, "present")
19
- .and_then(saphyr::YamlOwned::as_bool)
20
- .unwrap_or(true);
21
- Self { present }
22
- }
23
-
24
- #[must_use]
25
- pub const fn new_for_tests(present: bool) -> Self {
26
- Self { present }
27
- }
28
-
29
- #[must_use]
30
- pub const fn requires_marker(&self) -> bool {
31
- self.present
32
- }
33
- }
34
-
35
- #[derive(Debug, Clone, PartialEq, Eq)]
36
- pub struct Violation {
37
- pub line: usize,
38
- pub column: usize,
39
- pub message: String,
40
- }
41
-
42
- #[must_use]
43
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
44
- let mut parser = Parser::new_from_str(buffer);
45
- let mut receiver = DocumentStartReceiver::new(cfg);
46
- let _ = parser.load(&mut receiver, true);
47
- receiver.violations
48
- }
49
-
50
- struct DocumentStartReceiver<'cfg> {
51
- config: &'cfg Config,
52
- violations: Vec<Violation>,
53
- }
54
-
55
- impl<'cfg> DocumentStartReceiver<'cfg> {
56
- const fn new(config: &'cfg Config) -> Self {
57
- Self {
58
- config,
59
- violations: Vec::new(),
60
- }
61
- }
62
- }
63
-
64
- impl SpannedEventReceiver<'_> for DocumentStartReceiver<'_> {
65
- fn on_event(&mut self, event: Event<'_>, span: Span) {
66
- if let Event::DocumentStart(explicit) = event {
67
- if self.config.requires_marker() {
68
- if !explicit {
69
- self.violations.push(Violation {
70
- line: span.start.line(),
71
- column: 1,
72
- message: MISSING_MESSAGE.to_string(),
73
- });
74
- }
75
- } else if explicit {
76
- self.violations.push(Violation {
77
- line: span.start.line(),
78
- column: span.start.col() + 1,
79
- message: FORBIDDEN_MESSAGE.to_string(),
80
- });
81
- }
82
- }
83
- }
84
- }
@@ -1,152 +0,0 @@
1
- use std::convert::TryFrom;
2
-
3
- use saphyr::YamlOwned;
4
-
5
- use crate::config::YamlLintConfig;
6
-
7
- pub const ID: &str = "empty-lines";
8
-
9
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
10
- pub struct Config {
11
- max: i64,
12
- max_start: i64,
13
- max_end: i64,
14
- }
15
-
16
- impl Config {
17
- #[must_use]
18
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
19
- let max = cfg
20
- .rule_option(ID, "max")
21
- .and_then(YamlOwned::as_integer)
22
- .unwrap_or(2);
23
- let max_start = cfg
24
- .rule_option(ID, "max-start")
25
- .and_then(YamlOwned::as_integer)
26
- .unwrap_or(0);
27
- let max_end = cfg
28
- .rule_option(ID, "max-end")
29
- .and_then(YamlOwned::as_integer)
30
- .unwrap_or(0);
31
-
32
- Self {
33
- max,
34
- max_start,
35
- max_end,
36
- }
37
- }
38
-
39
- const fn max(&self) -> i64 {
40
- self.max
41
- }
42
-
43
- const fn max_start(&self) -> i64 {
44
- self.max_start
45
- }
46
-
47
- const fn max_end(&self) -> i64 {
48
- self.max_end
49
- }
50
- }
51
-
52
- #[derive(Debug, Clone, PartialEq, Eq)]
53
- pub struct Violation {
54
- pub line: usize,
55
- pub column: usize,
56
- pub message: String,
57
- }
58
-
59
- #[must_use]
60
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
61
- let mut violations = Vec::new();
62
-
63
- let mut iter = buffer.split_inclusive('\n').peekable();
64
- let mut seen_nonblank = false;
65
- let mut blank_run_len = 0usize;
66
- let mut blank_run_start = 0usize;
67
- let mut blank_run_is_start = false;
68
- let mut offset = 0usize;
69
- let total_len = buffer.len();
70
- let mut line_no = 1usize;
71
-
72
- while let Some(segment) = iter.next() {
73
- let seg_len = segment.len();
74
- let next_offset = offset + seg_len;
75
- let is_blank_line = matches!(segment, "\n" | "\r\n");
76
-
77
- if is_blank_line {
78
- if blank_run_len == 0 {
79
- blank_run_start = line_no;
80
- blank_run_is_start = !seen_nonblank;
81
- }
82
- blank_run_len += 1;
83
-
84
- let next_is_blank = iter
85
- .peek()
86
- .copied()
87
- .is_some_and(|next_segment| matches!(next_segment, "\n" | "\r\n"));
88
-
89
- if !next_is_blank {
90
- let is_end = next_offset == total_len;
91
- finalize_run(
92
- buffer,
93
- cfg,
94
- blank_run_start,
95
- blank_run_len,
96
- blank_run_is_start,
97
- is_end,
98
- &mut violations,
99
- );
100
- blank_run_len = 0;
101
- blank_run_is_start = false;
102
- }
103
- } else {
104
- seen_nonblank = true;
105
- }
106
-
107
- offset = next_offset;
108
-
109
- if segment.ends_with('\n') {
110
- if !is_blank_line {
111
- seen_nonblank = true;
112
- }
113
- line_no += 1;
114
- }
115
- }
116
-
117
- violations
118
- }
119
-
120
- fn finalize_run(
121
- buffer: &str,
122
- cfg: &Config,
123
- start_line: usize,
124
- length: usize,
125
- is_start: bool,
126
- is_end: bool,
127
- out: &mut Vec<Violation>,
128
- ) {
129
- if is_end && matches!(buffer, "\n" | "\r\n") {
130
- return;
131
- }
132
-
133
- let allowed = if is_end {
134
- cfg.max_end()
135
- } else if is_start {
136
- cfg.max_start()
137
- } else {
138
- cfg.max()
139
- };
140
-
141
- let run_len = i64::try_from(length).unwrap_or(i64::MAX);
142
- if run_len <= allowed {
143
- return;
144
- }
145
-
146
- let last_line = start_line + length - 1;
147
- out.push(Violation {
148
- line: last_line,
149
- column: 1,
150
- message: format!("too many blank lines ({run_len} > {allowed})"),
151
- });
152
- }