Skip to content

Commit 58922e4

Browse files
committed
[eradicate] ignore # language= in commented-out-code rule (ERA001)
Fixes one common case cited on #6019 Should work with the examples from Jetbrains documentation: https://www.jetbrains.com/help/pycharm/using-language-injections.html
1 parent 2b0cdd2 commit 58922e4

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

crates/ruff_linter/src/rules/eradicate/detection.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static CODE_INDICATORS: LazyLock<AhoCorasick> = LazyLock::new(|| {
1616

1717
static ALLOWLIST_REGEX: LazyLock<Regex> = LazyLock::new(|| {
1818
Regex::new(
19-
r"^(?i)(?:pylint|pyright|noqa|nosec|region|endregion|type:\s*ignore|fmt:\s*(on|off)|isort:\s*(on|off|skip|skip_file|split|dont-add-imports(:\s*\[.*?])?)|mypy:|SPDX-License-Identifier:|(?:en)?coding[:=][ \t]*([-_.a-zA-Z0-9]+))",
19+
r"^(?i)(?:pylint|pyright|noqa|nosec|region|endregion|type:\s*ignore|fmt:\s*(on|off)|isort:\s*(on|off|skip|skip_file|split|dont-add-imports(:\s*\[.*?])?)|mypy:|SPDX-License-Identifier:|language=[a-zA-Z](?: ?[-_.a-zA-Z0-9]+)+(?:\s+prefix=\S+)?(?:\s+suffix=\S+)?|(?:en)?coding[:=][ \t]*([-_.a-zA-Z0-9]+))",
2020
).unwrap()
2121
});
2222

@@ -297,6 +297,23 @@ mod tests {
297297
));
298298
}
299299

300+
#[test]
301+
fn comment_contains_language_injection() {
302+
assert!(comment_contains_code("# language=123", &[]));
303+
assert!(comment_contains_code("# language=\"pt\"", &[]));
304+
assert!(comment_contains_code("# language='en'", &[]));
305+
306+
assert!(!comment_contains_code("# language=xml", &[]));
307+
assert!(!comment_contains_code(
308+
"# language=HTML prefix=<body> suffix=</body>",
309+
&[]
310+
));
311+
assert!(!comment_contains_code(
312+
"# language=ecma script level 4",
313+
&[]
314+
));
315+
}
316+
300317
#[test]
301318
fn comment_contains_todo() {
302319
let task_tags = TASK_TAGS

0 commit comments

Comments
 (0)