@owenlamont/ryl 0.4.1
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.
- package/.github/CODEOWNERS +1 -0
- package/.github/dependabot.yml +13 -0
- package/.github/workflows/ci.yml +107 -0
- package/.github/workflows/release.yml +613 -0
- package/.github/workflows/update_dependencies.yml +61 -0
- package/.github/workflows/update_linters.yml +56 -0
- package/.pre-commit-config.yaml +87 -0
- package/.yamllint +4 -0
- package/AGENTS.md +200 -0
- package/Cargo.lock +908 -0
- package/Cargo.toml +32 -0
- package/LICENSE +21 -0
- package/README.md +230 -0
- package/bin/ryl.js +1 -0
- package/clippy.toml +1 -0
- package/docs/config-presets.md +100 -0
- package/img/benchmark-5x5-5runs.svg +2176 -0
- package/package.json +28 -0
- package/pyproject.toml +42 -0
- package/ruff.toml +107 -0
- package/rumdl.toml +20 -0
- package/rust-toolchain.toml +3 -0
- package/rustfmt.toml +3 -0
- package/scripts/benchmark_perf_vs_yamllint.py +400 -0
- package/scripts/coverage-missing.ps1 +80 -0
- package/scripts/coverage-missing.sh +60 -0
- package/src/bin/discover_config_bin.rs +24 -0
- package/src/cli_support.rs +33 -0
- package/src/conf/mod.rs +85 -0
- package/src/config.rs +2099 -0
- package/src/decoder.rs +326 -0
- package/src/discover.rs +31 -0
- package/src/lib.rs +19 -0
- package/src/lint.rs +558 -0
- package/src/main.rs +535 -0
- package/src/migrate.rs +233 -0
- package/src/rules/anchors.rs +517 -0
- package/src/rules/braces.rs +77 -0
- package/src/rules/brackets.rs +77 -0
- package/src/rules/colons.rs +475 -0
- package/src/rules/commas.rs +372 -0
- package/src/rules/comments.rs +299 -0
- package/src/rules/comments_indentation.rs +243 -0
- package/src/rules/document_end.rs +175 -0
- package/src/rules/document_start.rs +84 -0
- package/src/rules/empty_lines.rs +152 -0
- package/src/rules/empty_values.rs +255 -0
- package/src/rules/float_values.rs +259 -0
- package/src/rules/flow_collection.rs +562 -0
- package/src/rules/hyphens.rs +104 -0
- package/src/rules/indentation.rs +803 -0
- package/src/rules/key_duplicates.rs +218 -0
- package/src/rules/key_ordering.rs +303 -0
- package/src/rules/line_length.rs +326 -0
- package/src/rules/mod.rs +25 -0
- package/src/rules/new_line_at_end_of_file.rs +23 -0
- package/src/rules/new_lines.rs +95 -0
- package/src/rules/octal_values.rs +121 -0
- package/src/rules/quoted_strings.rs +577 -0
- package/src/rules/span_utils.rs +37 -0
- package/src/rules/trailing_spaces.rs +65 -0
- package/src/rules/truthy.rs +420 -0
- package/tests/brackets_carriage_return.rs +114 -0
- package/tests/build_global_cfg_error.rs +23 -0
- package/tests/cli_anchors_rule.rs +143 -0
- package/tests/cli_braces_rule.rs +104 -0
- package/tests/cli_brackets_rule.rs +104 -0
- package/tests/cli_colons_rule.rs +65 -0
- package/tests/cli_commas_rule.rs +104 -0
- package/tests/cli_comments_indentation_rule.rs +61 -0
- package/tests/cli_comments_rule.rs +67 -0
- package/tests/cli_config_data_error.rs +30 -0
- package/tests/cli_config_flags.rs +66 -0
- package/tests/cli_config_migrate.rs +229 -0
- package/tests/cli_document_end_rule.rs +92 -0
- package/tests/cli_document_start_rule.rs +92 -0
- package/tests/cli_empty_lines_rule.rs +87 -0
- package/tests/cli_empty_values_rule.rs +68 -0
- package/tests/cli_env_config.rs +34 -0
- package/tests/cli_exit_and_errors.rs +41 -0
- package/tests/cli_file_encoding.rs +203 -0
- package/tests/cli_float_values_rule.rs +64 -0
- package/tests/cli_format_options.rs +316 -0
- package/tests/cli_global_cfg_relaxed.rs +20 -0
- package/tests/cli_hyphens_rule.rs +104 -0
- package/tests/cli_indentation_rule.rs +65 -0
- package/tests/cli_invalid_project_config.rs +39 -0
- package/tests/cli_key_duplicates_rule.rs +104 -0
- package/tests/cli_key_ordering_rule.rs +59 -0
- package/tests/cli_line_length_rule.rs +85 -0
- package/tests/cli_list_files.rs +29 -0
- package/tests/cli_new_line_rule.rs +141 -0
- package/tests/cli_new_lines_rule.rs +119 -0
- package/tests/cli_octal_values_rule.rs +60 -0
- package/tests/cli_quoted_strings_rule.rs +47 -0
- package/tests/cli_toml_config.rs +119 -0
- package/tests/cli_trailing_spaces_rule.rs +77 -0
- package/tests/cli_truthy_rule.rs +83 -0
- package/tests/cli_yaml_files_negation.rs +45 -0
- package/tests/colons_rule.rs +303 -0
- package/tests/common/compat.rs +114 -0
- package/tests/common/fake_env.rs +93 -0
- package/tests/common/mod.rs +1 -0
- package/tests/conf_builtin.rs +9 -0
- package/tests/config_anchors.rs +84 -0
- package/tests/config_braces.rs +121 -0
- package/tests/config_brackets.rs +127 -0
- package/tests/config_commas.rs +79 -0
- package/tests/config_comments.rs +65 -0
- package/tests/config_comments_indentation.rs +20 -0
- package/tests/config_deep_merge_nonstring_key.rs +24 -0
- package/tests/config_document_end.rs +54 -0
- package/tests/config_document_start.rs +55 -0
- package/tests/config_empty_lines.rs +48 -0
- package/tests/config_empty_values.rs +35 -0
- package/tests/config_env_errors.rs +23 -0
- package/tests/config_env_invalid_inline.rs +15 -0
- package/tests/config_env_missing.rs +63 -0
- package/tests/config_env_shim.rs +301 -0
- package/tests/config_explicit_file_parse_error.rs +55 -0
- package/tests/config_extended_features.rs +225 -0
- package/tests/config_extends_inline.rs +185 -0
- package/tests/config_extends_sequence.rs +18 -0
- package/tests/config_find_project_home_boundary.rs +54 -0
- package/tests/config_find_project_two_files_in_cwd.rs +47 -0
- package/tests/config_float_values.rs +34 -0
- package/tests/config_from_yaml_paths.rs +32 -0
- package/tests/config_hyphens.rs +51 -0
- package/tests/config_ignore_errors.rs +243 -0
- package/tests/config_ignore_overrides.rs +83 -0
- package/tests/config_indentation.rs +65 -0
- package/tests/config_invalid_globs.rs +16 -0
- package/tests/config_invalid_types.rs +19 -0
- package/tests/config_key_duplicates.rs +34 -0
- package/tests/config_key_ordering.rs +70 -0
- package/tests/config_line_length.rs +65 -0
- package/tests/config_locale.rs +111 -0
- package/tests/config_merge.rs +26 -0
- package/tests/config_new_lines.rs +89 -0
- package/tests/config_octal_values.rs +33 -0
- package/tests/config_quoted_strings.rs +195 -0
- package/tests/config_rule_level.rs +147 -0
- package/tests/config_rules_non_string_keys.rs +23 -0
- package/tests/config_scalar_overrides.rs +27 -0
- package/tests/config_to_toml.rs +110 -0
- package/tests/config_toml_coverage.rs +80 -0
- package/tests/config_toml_discovery.rs +304 -0
- package/tests/config_trailing_spaces.rs +152 -0
- package/tests/config_truthy.rs +77 -0
- package/tests/config_yaml_files.rs +62 -0
- package/tests/config_yaml_files_all_non_string.rs +15 -0
- package/tests/config_yaml_files_empty.rs +30 -0
- package/tests/coverage_commas.rs +46 -0
- package/tests/decoder_decode.rs +338 -0
- package/tests/discover_config_bin_all.rs +66 -0
- package/tests/discover_config_bin_env_invalid_yaml.rs +26 -0
- package/tests/discover_config_bin_project_config_parse_error.rs +24 -0
- package/tests/discover_config_bin_user_global_error.rs +26 -0
- package/tests/discover_module.rs +30 -0
- package/tests/discover_per_file_dir.rs +10 -0
- package/tests/discover_per_file_project_config_error.rs +21 -0
- package/tests/float_values.rs +43 -0
- package/tests/lint_multi_errors.rs +32 -0
- package/tests/main_yaml_ok_filtering.rs +30 -0
- package/tests/migrate_module.rs +259 -0
- package/tests/resolve_ctx_empty_parent.rs +16 -0
- package/tests/rule_anchors.rs +442 -0
- package/tests/rule_braces.rs +258 -0
- package/tests/rule_brackets.rs +217 -0
- package/tests/rule_commas.rs +205 -0
- package/tests/rule_comments.rs +197 -0
- package/tests/rule_comments_indentation.rs +127 -0
- package/tests/rule_document_end.rs +118 -0
- package/tests/rule_document_start.rs +60 -0
- package/tests/rule_empty_lines.rs +96 -0
- package/tests/rule_empty_values.rs +102 -0
- package/tests/rule_float_values.rs +109 -0
- package/tests/rule_hyphens.rs +65 -0
- package/tests/rule_indentation.rs +455 -0
- package/tests/rule_key_duplicates.rs +76 -0
- package/tests/rule_key_ordering.rs +207 -0
- package/tests/rule_line_length.rs +200 -0
- package/tests/rule_new_lines.rs +51 -0
- package/tests/rule_octal_values.rs +53 -0
- package/tests/rule_quoted_strings.rs +290 -0
- package/tests/rule_trailing_spaces.rs +41 -0
- package/tests/rule_truthy.rs +236 -0
- package/tests/user_global_invalid_yaml.rs +32 -0
- package/tests/yamllint_compat_anchors.rs +280 -0
- package/tests/yamllint_compat_braces.rs +411 -0
- package/tests/yamllint_compat_brackets.rs +364 -0
- package/tests/yamllint_compat_colons.rs +298 -0
- package/tests/yamllint_compat_colors.rs +80 -0
- package/tests/yamllint_compat_commas.rs +375 -0
- package/tests/yamllint_compat_comments.rs +167 -0
- package/tests/yamllint_compat_comments_indentation.rs +281 -0
- package/tests/yamllint_compat_config.rs +170 -0
- package/tests/yamllint_compat_document_end.rs +243 -0
- package/tests/yamllint_compat_document_start.rs +136 -0
- package/tests/yamllint_compat_empty_lines.rs +117 -0
- package/tests/yamllint_compat_empty_values.rs +179 -0
- package/tests/yamllint_compat_float_values.rs +216 -0
- package/tests/yamllint_compat_hyphens.rs +223 -0
- package/tests/yamllint_compat_indentation.rs +398 -0
- package/tests/yamllint_compat_key_duplicates.rs +139 -0
- package/tests/yamllint_compat_key_ordering.rs +170 -0
- package/tests/yamllint_compat_line_length.rs +375 -0
- package/tests/yamllint_compat_list.rs +127 -0
- package/tests/yamllint_compat_new_line.rs +133 -0
- package/tests/yamllint_compat_newline_types.rs +185 -0
- package/tests/yamllint_compat_octal_values.rs +172 -0
- package/tests/yamllint_compat_quoted_strings.rs +154 -0
- package/tests/yamllint_compat_syntax.rs +200 -0
- package/tests/yamllint_compat_trailing_spaces.rs +162 -0
- package/tests/yamllint_compat_truthy.rs +130 -0
- package/tests/yamllint_compat_yaml_files.rs +81 -0
- package/typos.toml +2 -0
|
@@ -0,0 +1,372 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
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
|
+
}
|