From 58922e40ea8cedc6d44970b2c2cb175dc17442e0 Mon Sep 17 00:00:00 2001 From: Fabio Batista Date: Sun, 3 Nov 2024 18:04:17 -0300 Subject: [PATCH] [`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 --- .../src/rules/eradicate/detection.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index c3ba54a199350..1798df468aa4e 100644 --- a/crates/ruff_linter/src/rules/eradicate/detection.rs +++ b/crates/ruff_linter/src/rules/eradicate/detection.rs @@ -16,7 +16,7 @@ static CODE_INDICATORS: LazyLock = LazyLock::new(|| { static ALLOWLIST_REGEX: LazyLock = LazyLock::new(|| { Regex::new( - 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]+))", + 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]+))", ).unwrap() }); @@ -297,6 +297,23 @@ mod tests { )); } + #[test] + fn comment_contains_language_injection() { + assert!(comment_contains_code("# language=123", &[])); + assert!(comment_contains_code("# language=\"pt\"", &[])); + assert!(comment_contains_code("# language='en'", &[])); + + assert!(!comment_contains_code("# language=xml", &[])); + assert!(!comment_contains_code( + "# language=HTML prefix= suffix=", + &[] + )); + assert!(!comment_contains_code( + "# language=ecma script level 4", + &[] + )); + } + #[test] fn comment_contains_todo() { let task_tags = TASK_TAGS