Skip to content

Commit 0447829

Browse files
authored
Rollup merge of #137103 - yotamofek:pr/jsonhtmldocck-deprecated-syntax, r=aDotInTheVoid
{json|html}docck: catch and error on deprecated syntax #137099 (review)
2 parents daf5985 + 797ef64 commit 0447829

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

src/etc/htmldocck.py

+14
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,24 @@ def filter_line(line):
297297
re.X | re.UNICODE,
298298
)
299299

300+
DEPRECATED_LINE_PATTERN = re.compile(
301+
r"""
302+
//\s+@
303+
""",
304+
re.X | re.UNICODE,
305+
)
306+
300307

301308
def get_commands(template):
302309
with io.open(template, encoding="utf-8") as f:
303310
for lineno, line in concat_multi_lines(f):
311+
if DEPRECATED_LINE_PATTERN.search(line):
312+
print_err(
313+
lineno,
314+
line,
315+
"Deprecated command syntax, replace `// @` with `//@ `",
316+
)
317+
continue
304318
m = LINE_PATTERN.search(line)
305319
if not m:
306320
continue

src/tools/jsondocck/src/main.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22
use std::process::ExitCode;
3-
use std::sync::OnceLock;
3+
use std::sync::LazyLock;
44
use std::{env, fs};
55

66
use regex::{Regex, RegexBuilder};
@@ -151,8 +151,7 @@ impl CommandKind {
151151
}
152152
}
153153

154-
static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
155-
fn line_pattern() -> Regex {
154+
static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
156155
RegexBuilder::new(
157156
r#"
158157
//@\s+
@@ -165,7 +164,19 @@ fn line_pattern() -> Regex {
165164
.unicode(true)
166165
.build()
167166
.unwrap()
168-
}
167+
});
168+
169+
static DEPRECATED_LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
170+
RegexBuilder::new(
171+
r#"
172+
//\s+@
173+
"#,
174+
)
175+
.ignore_whitespace(true)
176+
.unicode(true)
177+
.build()
178+
.unwrap()
179+
});
169180

170181
fn print_err(msg: &str, lineno: usize) {
171182
eprintln!("Invalid command: {} on line {}", msg, lineno)
@@ -184,21 +195,23 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
184195
for (lineno, line) in file.split('\n').enumerate() {
185196
let lineno = lineno + 1;
186197

187-
let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) {
188-
Some(c) => c,
189-
None => continue,
198+
if DEPRECATED_LINE_PATTERN.is_match(line) {
199+
print_err("Deprecated command syntax, replace `// @` with `//@ `", lineno);
200+
errors = true;
201+
continue;
202+
}
203+
204+
let Some(cap) = LINE_PATTERN.captures(line) else {
205+
continue;
190206
};
191207

192-
let negated = cap.name("negated").unwrap().as_str() == "!";
208+
let negated = &cap["negated"] == "!";
193209

194210
let args_str = &cap["args"];
195-
let args = match shlex::split(args_str) {
196-
Some(args) => args,
197-
None => {
198-
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
199-
errors = true;
200-
continue;
201-
}
211+
let Some(args) = shlex::split(args_str) else {
212+
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
213+
errors = true;
214+
continue;
202215
};
203216

204217
if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) {

0 commit comments

Comments
 (0)