@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,475 @@
|
|
|
1
|
+
use std::ops::Range;
|
|
2
|
+
|
|
3
|
+
use saphyr_parser::{Event, Parser, Span, SpannedEventReceiver};
|
|
4
|
+
|
|
5
|
+
use crate::config::YamlLintConfig;
|
|
6
|
+
use crate::rules::span_utils::{ranges_to_char_indices, span_char_index_to_byte};
|
|
7
|
+
|
|
8
|
+
pub const ID: &str = "colons";
|
|
9
|
+
const TOO_MANY_BEFORE: &str = "too many spaces before colon";
|
|
10
|
+
const TOO_MANY_AFTER: &str = "too many spaces after colon";
|
|
11
|
+
const TOO_MANY_AFTER_QUESTION: &str = "too many spaces after question mark";
|
|
12
|
+
|
|
13
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
14
|
+
pub struct Config {
|
|
15
|
+
max_spaces_before: i64,
|
|
16
|
+
max_spaces_after: i64,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
impl Config {
|
|
20
|
+
const DEFAULT_MAX_BEFORE: i64 = 0;
|
|
21
|
+
const DEFAULT_MAX_AFTER: i64 = 1;
|
|
22
|
+
|
|
23
|
+
#[must_use]
|
|
24
|
+
pub fn resolve(cfg: &YamlLintConfig) -> Self {
|
|
25
|
+
let max_spaces_before = cfg
|
|
26
|
+
.rule_option(ID, "max-spaces-before")
|
|
27
|
+
.and_then(saphyr::YamlOwned::as_integer)
|
|
28
|
+
.unwrap_or(Self::DEFAULT_MAX_BEFORE);
|
|
29
|
+
let max_spaces_after = cfg
|
|
30
|
+
.rule_option(ID, "max-spaces-after")
|
|
31
|
+
.and_then(saphyr::YamlOwned::as_integer)
|
|
32
|
+
.unwrap_or(Self::DEFAULT_MAX_AFTER);
|
|
33
|
+
|
|
34
|
+
Self {
|
|
35
|
+
max_spaces_before,
|
|
36
|
+
max_spaces_after,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#[must_use]
|
|
41
|
+
pub const fn new_for_tests(max_spaces_before: i64, max_spaces_after: i64) -> Self {
|
|
42
|
+
Self {
|
|
43
|
+
max_spaces_before,
|
|
44
|
+
max_spaces_after,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#[must_use]
|
|
49
|
+
pub const fn max_spaces_before(&self) -> i64 {
|
|
50
|
+
self.max_spaces_before
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#[must_use]
|
|
54
|
+
pub const fn max_spaces_after(&self) -> i64 {
|
|
55
|
+
self.max_spaces_after
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
60
|
+
pub struct Violation {
|
|
61
|
+
pub line: usize,
|
|
62
|
+
pub column: usize,
|
|
63
|
+
pub message: String,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
struct ScalarRangeCollector {
|
|
67
|
+
ranges: Vec<Range<usize>>,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
impl ScalarRangeCollector {
|
|
71
|
+
const fn new() -> Self {
|
|
72
|
+
Self { ranges: Vec::new() }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn push_range(&mut self, span: Span) {
|
|
76
|
+
let start = span.start.index();
|
|
77
|
+
let end = span.end.index();
|
|
78
|
+
if start < end {
|
|
79
|
+
self.ranges.push(start..end);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fn into_sorted(mut self) -> Vec<Range<usize>> {
|
|
84
|
+
self.ranges.sort_by(|a, b| a.start.cmp(&b.start));
|
|
85
|
+
self.ranges
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
impl SpannedEventReceiver<'_> for ScalarRangeCollector {
|
|
90
|
+
fn on_event(&mut self, ev: Event<'_>, span: Span) {
|
|
91
|
+
if matches!(ev, Event::Scalar(..)) {
|
|
92
|
+
self.push_range(span);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
enum BeforeResult {
|
|
98
|
+
SameLine {
|
|
99
|
+
spaces: usize,
|
|
100
|
+
preceding_char: Option<usize>,
|
|
101
|
+
},
|
|
102
|
+
Ignored,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
enum AfterResult {
|
|
106
|
+
SameLine { spaces: usize, next_char: usize },
|
|
107
|
+
Ignored,
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#[must_use]
|
|
111
|
+
pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
|
|
112
|
+
if buffer.is_empty() {
|
|
113
|
+
return Vec::new();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let mut parser = Parser::new_from_str(buffer);
|
|
117
|
+
let mut collector = ScalarRangeCollector::new();
|
|
118
|
+
let _ = parser.load(&mut collector, true);
|
|
119
|
+
let scalar_ranges = collector.into_sorted();
|
|
120
|
+
|
|
121
|
+
let chars: Vec<(usize, char)> = buffer.char_indices().collect();
|
|
122
|
+
let buffer_len = buffer.len();
|
|
123
|
+
let scalar_ranges = ranges_to_char_indices(scalar_ranges, &chars, buffer_len);
|
|
124
|
+
let line_starts = build_line_starts(buffer);
|
|
125
|
+
|
|
126
|
+
let mut scalar_idx = 0usize;
|
|
127
|
+
let mut idx = 0usize;
|
|
128
|
+
let mut violations = Vec::new();
|
|
129
|
+
|
|
130
|
+
while idx < chars.len() {
|
|
131
|
+
let (byte_idx, ch) = chars[idx];
|
|
132
|
+
|
|
133
|
+
while scalar_idx < scalar_ranges.len()
|
|
134
|
+
&& span_char_index_to_byte(
|
|
135
|
+
&chars,
|
|
136
|
+
scalar_ranges[scalar_idx].end,
|
|
137
|
+
buffer_len,
|
|
138
|
+
) <= byte_idx
|
|
139
|
+
{
|
|
140
|
+
scalar_idx += 1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if let Some(range) = scalar_ranges.get(scalar_idx) {
|
|
144
|
+
let start_byte = span_char_index_to_byte(&chars, range.start, buffer_len);
|
|
145
|
+
let end_byte = span_char_index_to_byte(&chars, range.end, buffer_len);
|
|
146
|
+
if byte_idx >= start_byte && byte_idx < end_byte {
|
|
147
|
+
idx = range.end;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
match ch {
|
|
153
|
+
'#' => {
|
|
154
|
+
idx = skip_comment(&chars, idx);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
':' => {
|
|
158
|
+
evaluate_colon(cfg, &mut violations, &chars, idx, &line_starts);
|
|
159
|
+
}
|
|
160
|
+
'?' => {
|
|
161
|
+
evaluate_question_mark(cfg, &mut violations, &chars, idx, &line_starts);
|
|
162
|
+
}
|
|
163
|
+
_ => {}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
idx += 1;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
violations
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
fn skip_comment(chars: &[(usize, char)], mut idx: usize) -> usize {
|
|
173
|
+
idx += 1;
|
|
174
|
+
while idx < chars.len() {
|
|
175
|
+
let ch = chars[idx].1;
|
|
176
|
+
if ch == '\n' {
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
if ch == '\r' {
|
|
180
|
+
if chars.get(idx + 1).is_some_and(|(_, ch)| *ch == '\n') {
|
|
181
|
+
idx += 1;
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
idx += 1;
|
|
186
|
+
}
|
|
187
|
+
idx
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn evaluate_colon(
|
|
191
|
+
cfg: &Config,
|
|
192
|
+
violations: &mut Vec<Violation>,
|
|
193
|
+
chars: &[(usize, char)],
|
|
194
|
+
colon_idx: usize,
|
|
195
|
+
line_starts: &[usize],
|
|
196
|
+
) {
|
|
197
|
+
let mut skip_after_check = false;
|
|
198
|
+
|
|
199
|
+
if let BeforeResult::SameLine {
|
|
200
|
+
spaces,
|
|
201
|
+
preceding_char,
|
|
202
|
+
} = compute_spaces_before(chars, colon_idx)
|
|
203
|
+
{
|
|
204
|
+
if let Some(preceding_idx) = preceding_char
|
|
205
|
+
&& spaces == 0
|
|
206
|
+
&& alias_immediately_before(chars, preceding_idx)
|
|
207
|
+
{
|
|
208
|
+
skip_after_check = true;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if !skip_after_check && cfg.max_spaces_before >= 0 {
|
|
212
|
+
let spaces_i64 = i64::try_from(spaces).unwrap_or(i64::MAX);
|
|
213
|
+
if spaces_i64 > cfg.max_spaces_before {
|
|
214
|
+
let colon_byte = chars[colon_idx].0;
|
|
215
|
+
let (line, column) = line_and_column(line_starts, colon_byte);
|
|
216
|
+
let highlight_column = column.saturating_sub(1).max(1);
|
|
217
|
+
violations.push(Violation {
|
|
218
|
+
line,
|
|
219
|
+
column: highlight_column,
|
|
220
|
+
message: TOO_MANY_BEFORE.to_string(),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if !skip_after_check
|
|
227
|
+
&& cfg.max_spaces_after >= 0
|
|
228
|
+
&& let AfterResult::SameLine { spaces, next_char } =
|
|
229
|
+
compute_spaces_after(chars, colon_idx)
|
|
230
|
+
{
|
|
231
|
+
if chars[next_char].1 == '#' {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
let spaces_i64 = i64::try_from(spaces).unwrap_or(i64::MAX);
|
|
235
|
+
if spaces_i64 > cfg.max_spaces_after {
|
|
236
|
+
let next_byte = chars[next_char].0;
|
|
237
|
+
let (line, column) = line_and_column(line_starts, next_byte);
|
|
238
|
+
let highlight_column = column.saturating_sub(1).max(1);
|
|
239
|
+
violations.push(Violation {
|
|
240
|
+
line,
|
|
241
|
+
column: highlight_column,
|
|
242
|
+
message: TOO_MANY_AFTER.to_string(),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
fn evaluate_question_mark(
|
|
249
|
+
cfg: &Config,
|
|
250
|
+
violations: &mut Vec<Violation>,
|
|
251
|
+
chars: &[(usize, char)],
|
|
252
|
+
question_idx: usize,
|
|
253
|
+
line_starts: &[usize],
|
|
254
|
+
) {
|
|
255
|
+
if cfg.max_spaces_after >= 0
|
|
256
|
+
&& is_explicit_question_mark(chars, question_idx)
|
|
257
|
+
&& let AfterResult::SameLine { spaces, next_char } =
|
|
258
|
+
compute_spaces_after(chars, question_idx)
|
|
259
|
+
{
|
|
260
|
+
let spaces_i64 = i64::try_from(spaces).unwrap_or(i64::MAX);
|
|
261
|
+
if spaces_i64 > cfg.max_spaces_after {
|
|
262
|
+
let next_byte = chars[next_char].0;
|
|
263
|
+
let (line, column) = line_and_column(line_starts, next_byte);
|
|
264
|
+
let highlight_column = column.saturating_sub(1).max(1);
|
|
265
|
+
violations.push(Violation {
|
|
266
|
+
line,
|
|
267
|
+
column: highlight_column,
|
|
268
|
+
message: TOO_MANY_AFTER_QUESTION.to_string(),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn compute_spaces_before(chars: &[(usize, char)], colon_idx: usize) -> BeforeResult {
|
|
275
|
+
let mut spaces = 0usize;
|
|
276
|
+
let mut idx = colon_idx;
|
|
277
|
+
|
|
278
|
+
loop {
|
|
279
|
+
let Some(prev) = idx.checked_sub(1) else {
|
|
280
|
+
return BeforeResult::SameLine {
|
|
281
|
+
spaces,
|
|
282
|
+
preceding_char: None,
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
let ch = chars[prev].1;
|
|
287
|
+
if matches!(ch, ' ' | '\t') {
|
|
288
|
+
spaces += 1;
|
|
289
|
+
idx = prev;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if matches!(ch, '\n' | '\r') {
|
|
293
|
+
return BeforeResult::Ignored;
|
|
294
|
+
}
|
|
295
|
+
return BeforeResult::SameLine {
|
|
296
|
+
spaces,
|
|
297
|
+
preceding_char: Some(prev),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
fn compute_spaces_after(chars: &[(usize, char)], start_idx: usize) -> AfterResult {
|
|
303
|
+
let mut spaces = 0usize;
|
|
304
|
+
let mut idx = start_idx + 1;
|
|
305
|
+
|
|
306
|
+
while idx < chars.len() {
|
|
307
|
+
let ch = chars[idx].1;
|
|
308
|
+
match ch {
|
|
309
|
+
' ' | '\t' => {
|
|
310
|
+
spaces += 1;
|
|
311
|
+
idx += 1;
|
|
312
|
+
}
|
|
313
|
+
'\n' => return AfterResult::Ignored,
|
|
314
|
+
'\r' => {
|
|
315
|
+
if idx + 1 < chars.len() && chars[idx + 1].1 == '\n' {
|
|
316
|
+
return AfterResult::Ignored;
|
|
317
|
+
}
|
|
318
|
+
return AfterResult::Ignored;
|
|
319
|
+
}
|
|
320
|
+
_ => {
|
|
321
|
+
return AfterResult::SameLine {
|
|
322
|
+
spaces,
|
|
323
|
+
next_char: idx,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
AfterResult::Ignored
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
fn alias_immediately_before(chars: &[(usize, char)], preceding_idx: usize) -> bool {
|
|
333
|
+
let mut idx = preceding_idx;
|
|
334
|
+
loop {
|
|
335
|
+
let ch = chars[idx].1;
|
|
336
|
+
if ch == '*' {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
if is_alias_identifier_char(ch) {
|
|
340
|
+
if idx == 0 {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
idx -= 1;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const fn is_alias_identifier_char(ch: char) -> bool {
|
|
351
|
+
ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_')
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
fn is_explicit_question_mark(chars: &[(usize, char)], idx: usize) -> bool {
|
|
355
|
+
let next = chars.get(idx + 1).map_or('\0', |(_, ch)| *ch);
|
|
356
|
+
if !(matches!(next, ' ' | '\t' | '\n' | '\r')) {
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
match prev_non_ws_same_line(chars, idx) {
|
|
361
|
+
None => true,
|
|
362
|
+
Some((prev_idx, prev_ch)) => {
|
|
363
|
+
matches!(prev_ch, '[' | '{' | ',' | '?')
|
|
364
|
+
|| (prev_ch == '-' && is_sequence_indicator(chars, prev_idx))
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
fn prev_non_ws_same_line(chars: &[(usize, char)], idx: usize) -> Option<(usize, char)> {
|
|
370
|
+
let mut cursor = idx;
|
|
371
|
+
while let Some(prev) = cursor.checked_sub(1) {
|
|
372
|
+
let ch = chars[prev].1;
|
|
373
|
+
match ch {
|
|
374
|
+
' ' | '\t' => {
|
|
375
|
+
cursor = prev;
|
|
376
|
+
}
|
|
377
|
+
'\n' | '\r' => return None,
|
|
378
|
+
_ => return Some((prev, ch)),
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
None
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
fn is_sequence_indicator(chars: &[(usize, char)], hyphen_idx: usize) -> bool {
|
|
385
|
+
let mut cursor = hyphen_idx;
|
|
386
|
+
while let Some(prev) = cursor.checked_sub(1) {
|
|
387
|
+
let ch = chars[prev].1;
|
|
388
|
+
match ch {
|
|
389
|
+
' ' | '\t' => cursor = prev,
|
|
390
|
+
'\n' | '\r' => return true,
|
|
391
|
+
_ => return false,
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
true
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
fn build_line_starts(buffer: &str) -> Vec<usize> {
|
|
398
|
+
let mut starts = Vec::new();
|
|
399
|
+
starts.push(0);
|
|
400
|
+
|
|
401
|
+
let bytes = buffer.as_bytes();
|
|
402
|
+
let mut idx = 0usize;
|
|
403
|
+
while idx < bytes.len() {
|
|
404
|
+
match bytes[idx] {
|
|
405
|
+
b'\n' => {
|
|
406
|
+
starts.push(idx + 1);
|
|
407
|
+
idx += 1;
|
|
408
|
+
}
|
|
409
|
+
b'\r' => {
|
|
410
|
+
if idx + 1 < bytes.len() && bytes[idx + 1] == b'\n' {
|
|
411
|
+
starts.push(idx + 2);
|
|
412
|
+
idx += 2;
|
|
413
|
+
} else {
|
|
414
|
+
starts.push(idx + 1);
|
|
415
|
+
idx += 1;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
_ => idx += 1,
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
starts
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
fn line_and_column(line_starts: &[usize], byte_idx: usize) -> (usize, usize) {
|
|
426
|
+
let mut left = 0usize;
|
|
427
|
+
let mut right = line_starts.len();
|
|
428
|
+
|
|
429
|
+
while left + 1 < right {
|
|
430
|
+
let mid = usize::midpoint(left, right);
|
|
431
|
+
if line_starts[mid] <= byte_idx {
|
|
432
|
+
left = mid;
|
|
433
|
+
} else {
|
|
434
|
+
right = mid;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
let line_start = line_starts[left];
|
|
439
|
+
(left + 1, byte_idx - line_start + 1)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
#[doc(hidden)]
|
|
443
|
+
#[must_use]
|
|
444
|
+
pub fn coverage_is_explicit_question_mark(chars: &[(usize, char)], idx: usize) -> bool {
|
|
445
|
+
is_explicit_question_mark(chars, idx)
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
#[doc(hidden)]
|
|
449
|
+
#[must_use]
|
|
450
|
+
pub fn coverage_is_sequence_indicator(chars: &[(usize, char)], idx: usize) -> bool {
|
|
451
|
+
is_sequence_indicator(chars, idx)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
#[doc(hidden)]
|
|
455
|
+
#[must_use]
|
|
456
|
+
pub fn coverage_evaluate_question_mark(buffer: &str, cfg: &Config) -> Vec<Violation> {
|
|
457
|
+
let chars: Vec<(usize, char)> = buffer.char_indices().collect();
|
|
458
|
+
let mut violations = Vec::new();
|
|
459
|
+
let line_starts = build_line_starts(buffer);
|
|
460
|
+
if let Some((idx, _)) = chars.iter().enumerate().find(|(_, (_, ch))| *ch == '?') {
|
|
461
|
+
evaluate_question_mark(cfg, &mut violations, &chars, idx, &line_starts);
|
|
462
|
+
} else {
|
|
463
|
+
// explicit branch to ensure coverage marks the absence case
|
|
464
|
+
let () = ();
|
|
465
|
+
}
|
|
466
|
+
violations
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
#[doc(hidden)]
|
|
470
|
+
#[must_use]
|
|
471
|
+
pub fn coverage_skip_comment(buffer: &str) -> bool {
|
|
472
|
+
let chars: Vec<(usize, char)> = buffer.char_indices().collect();
|
|
473
|
+
let idx = skip_comment(&chars, 0);
|
|
474
|
+
chars.get(idx).is_some_and(|(_, ch)| *ch == '\n')
|
|
475
|
+
}
|