Rollup merge of #137103 - yotamofek:pr/jsonhtmldocck-deprecated-syntax, r=aDotInTheVoid
{json|html}docck: catch and error on deprecated syntax https://github.com/rust-lang/rust/pull/137099#pullrequestreview-2619498733
This commit is contained in:
commit
0447829803
2 changed files with 42 additions and 15 deletions
|
@ -297,10 +297,24 @@ LINE_PATTERN = re.compile(
|
||||||
re.X | re.UNICODE,
|
re.X | re.UNICODE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DEPRECATED_LINE_PATTERN = re.compile(
|
||||||
|
r"""
|
||||||
|
//\s+@
|
||||||
|
""",
|
||||||
|
re.X | re.UNICODE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_commands(template):
|
def get_commands(template):
|
||||||
with io.open(template, encoding="utf-8") as f:
|
with io.open(template, encoding="utf-8") as f:
|
||||||
for lineno, line in concat_multi_lines(f):
|
for lineno, line in concat_multi_lines(f):
|
||||||
|
if DEPRECATED_LINE_PATTERN.search(line):
|
||||||
|
print_err(
|
||||||
|
lineno,
|
||||||
|
line,
|
||||||
|
"Deprecated command syntax, replace `// @` with `//@ `",
|
||||||
|
)
|
||||||
|
continue
|
||||||
m = LINE_PATTERN.search(line)
|
m = LINE_PATTERN.search(line)
|
||||||
if not m:
|
if not m:
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
use std::sync::OnceLock;
|
use std::sync::LazyLock;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
|
||||||
use regex::{Regex, RegexBuilder};
|
use regex::{Regex, RegexBuilder};
|
||||||
|
@ -151,8 +151,7 @@ impl CommandKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
|
static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
fn line_pattern() -> Regex {
|
|
||||||
RegexBuilder::new(
|
RegexBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
//@\s+
|
//@\s+
|
||||||
|
@ -165,7 +164,19 @@ fn line_pattern() -> Regex {
|
||||||
.unicode(true)
|
.unicode(true)
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
});
|
||||||
|
|
||||||
|
static DEPRECATED_LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
|
RegexBuilder::new(
|
||||||
|
r#"
|
||||||
|
//\s+@
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.ignore_whitespace(true)
|
||||||
|
.unicode(true)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
});
|
||||||
|
|
||||||
fn print_err(msg: &str, lineno: usize) {
|
fn print_err(msg: &str, lineno: usize) {
|
||||||
eprintln!("Invalid command: {} on line {}", msg, lineno)
|
eprintln!("Invalid command: {} on line {}", msg, lineno)
|
||||||
|
@ -184,21 +195,23 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
|
||||||
for (lineno, line) in file.split('\n').enumerate() {
|
for (lineno, line) in file.split('\n').enumerate() {
|
||||||
let lineno = lineno + 1;
|
let lineno = lineno + 1;
|
||||||
|
|
||||||
let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) {
|
if DEPRECATED_LINE_PATTERN.is_match(line) {
|
||||||
Some(c) => c,
|
print_err("Deprecated command syntax, replace `// @` with `//@ `", lineno);
|
||||||
None => continue,
|
errors = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(cap) = LINE_PATTERN.captures(line) else {
|
||||||
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let negated = cap.name("negated").unwrap().as_str() == "!";
|
let negated = &cap["negated"] == "!";
|
||||||
|
|
||||||
let args_str = &cap["args"];
|
let args_str = &cap["args"];
|
||||||
let args = match shlex::split(args_str) {
|
let Some(args) = shlex::split(args_str) else {
|
||||||
Some(args) => args,
|
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
|
||||||
None => {
|
errors = true;
|
||||||
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
|
continue;
|
||||||
errors = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) {
|
if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue