1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-FileCopyrightText: 2026 Sébastien Helleu <flashcode@flashtux.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
//! Implementation of the `double-quotes` rule: check missing/extra double quotes.
use crate::checker::Checker;
use crate::diagnostic::{Diagnostic, Severity};
use crate::po::entry::Entry;
use crate::po::message::Message;
use crate::rules::rule::RuleChecker;
pub const DOUBLE_QUOTES: [char; 8] = [
'"', // U+0022: quotation mark
'«', // U+00AB: left pointing double angle quotation mark
'»', // U+00BB: right pointing double angle quotation mark
'“', // U+201C: left double quotation mark
'”', // U+201D: right double quotation mark
'„', // U+201E: double low quotation mark
'‟', // U+201F: double high-reversed-9 quotation mark
'"', // U+FF02: fullwidth quotation mark
];
pub struct DoubleQuotesRule;
impl RuleChecker for DoubleQuotesRule {
fn name(&self) -> &'static str {
"double-quotes"
}
fn is_default(&self) -> bool {
true
}
fn is_check(&self) -> bool {
true
}
fn severity(&self) -> Severity {
Severity::Info
}
/// Check for missing or extra double quotes in the translation.
///
/// The following quotes are considered:
/// - quotation mark: '"' (U+0022)
/// - left pointing double angle quotation mark: '«' (U+00AB)
/// - right pointing double angle quotation mark: '»' (U+00BB)
/// - left double quotation mark: '“' (U+201C)
/// - right double quotation mark: '”' (U+201D)
/// - double low quotation mark: '„' (U+201E)
/// - double high-reversed-9 quotation mark: '‟' (U+201F)
/// - fullwidth quotation mark: '"' (U+FF02)
///
/// Wrong entry:
/// ```text
/// msgid "this is a \"test\""
/// msgstr "ceci est un test"
/// ```
///
/// Correct entry:
/// ```text
/// msgid "this is a \"test\""
/// msgstr "ceci est un \"test\""
/// ```
///
/// Diagnostics reported with severity [`info`](Severity::Info):
/// - `missing double quotes (# / #)`
/// - `extra double quotes (# / #)`
fn check_msg(
&self,
checker: &Checker,
_entry: &Entry,
msgid: &Message,
msgstr: &Message,
) -> Vec<Diagnostic> {
let id_quotes: Vec<_> = msgid
.value
.match_indices(DOUBLE_QUOTES)
.map(|(idx, value)| (idx, idx + value.len()))
.collect();
let str_quotes: Vec<_> = msgstr
.value
.match_indices(DOUBLE_QUOTES)
.map(|(idx, value)| (idx, idx + value.len()))
.collect();
match id_quotes.len().cmp(&str_quotes.len()) {
std::cmp::Ordering::Greater => {
vec![
self.new_diag(
checker,
format!(
"missing double quotes ({} / {})",
id_quotes.len(),
str_quotes.len()
),
)
.with_msgs_hl(msgid, &id_quotes, msgstr, &str_quotes),
]
}
std::cmp::Ordering::Less => {
vec![
self.new_diag(
checker,
format!(
"extra double quotes ({} / {})",
id_quotes.len(),
str_quotes.len()
),
)
.with_msgs_hl(msgid, &id_quotes, msgstr, &str_quotes),
]
}
std::cmp::Ordering::Equal => vec![],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{diagnostic::Diagnostic, rules::rule::Rules};
fn check_double_quotes(content: &str) -> Vec<Diagnostic> {
let mut checker = Checker::new(content.as_bytes());
let rules = Rules::new(vec![Box::new(DoubleQuotesRule {})]);
checker.do_all_checks(&rules);
checker.diagnostics
}
#[test]
fn test_no_double_quotes() {
let diags = check_double_quotes(
r#"
msgid "tested"
msgstr "testé"
"#,
);
assert!(diags.is_empty());
}
#[test]
fn test_double_quotes_ok() {
let diags = check_double_quotes(
r#"
msgid "this is a \"test\""
msgstr "ceci est un « test »"
"#,
);
assert!(diags.is_empty());
}
#[test]
fn test_double_quotes_error_noqa() {
let diags = check_double_quotes(
r#"
#, noqa:double-quotes
msgid "this is a \"test\""
msgstr "ceci est un test"
"#,
);
assert!(diags.is_empty());
}
#[test]
fn test_double_quotes_error() {
let diags = check_double_quotes(
r#"
msgid "this is a \"test\""
msgstr "ceci est un test"
msgid "this is a test"
msgstr "ceci est un \"test\""
"#,
);
assert_eq!(diags.len(), 2);
let diag = &diags[0];
assert_eq!(diag.severity, Severity::Info);
assert_eq!(diag.message, "missing double quotes (2 / 0)");
let diag = &diags[1];
assert_eq!(diag.severity, Severity::Info);
assert_eq!(diag.message, "extra double quotes (0 / 2)");
}
}